JavaFileStorage: restore UserInteractionRequiredException

DropboxFileStorage: correctly throw UserInteractionRequiredException
GoogleDriveFileStorage:
 * extended logging
 * fix: extended try block in upload (constructing path might fail)
 * fix: correctly finish initialize process in initializeAccount if partially initialized
This commit is contained in:
Philipp Crocoll 2014-08-01 04:21:21 +02:00
parent 4c4ba93c89
commit e77c8f9ce5
5 changed files with 77 additions and 21 deletions

View File

@ -93,15 +93,33 @@ namespace keepass2android.Io
}
private Exception LogAndConvertJavaException(Java.Lang.Exception e)
private Exception LogAndConvertJavaException(Exception e)
{
Kp2aLog.Log(e.Message);
var ex = new Exception(e.LocalizedMessage ??
if (e is UserInteractionRequiredException)
return e;
//seems like UserInteractionRequiredException is not propagated correctly into the C# world, we can't catch it
// -> rethrow correctly here
// Note: the Contains-check looks a bit broad, but it should be safe
if (e.ToString().Contains("keepass2android.javafilestorage.UserInteractionRequiredException"))
{
throw new UserInteractionRequiredException();
}
Java.Lang.Exception exception = e as Java.Lang.Exception;
if (exception != null)
{
var ex = new Exception(exception.LocalizedMessage ??
e.Message ??
_app.GetResourceString(UiStringKey.ErrorOcurred)+e.GetType().Name, e);
_app.GetResourceString(UiStringKey.ErrorOcurred) + exception.GetType().Name, e);
return ex;
}
return e;
}
public IWriteTransaction OpenWriteTransaction(IOConnectionInfo ioc, bool useFileTransaction)
{
return new JavaFileStorageWriteTransaction(IocToPath(ioc), useFileTransaction, this);
@ -241,13 +259,29 @@ namespace keepass2android.Io
public void PrepareFileUsage(IFileStorageSetupInitiatorActivity activity, IOConnectionInfo ioc, int requestCode, Boolean alwaysReturnSuccess)
{
_jfs.PrepareFileUsage((IJavaFileStorageFileStorageSetupInitiatorActivity)activity, IocToPath(ioc), requestCode, alwaysReturnSuccess);
try
{
_jfs.PrepareFileUsage((IJavaFileStorageFileStorageSetupInitiatorActivity) activity, IocToPath(ioc), requestCode,
alwaysReturnSuccess);
}
catch (Exception e)
{
throw LogAndConvertJavaException(e);
}
}
public void PrepareFileUsage(Context ctx, IOConnectionInfo ioc)
{
try
{
_jfs.PrepareFileUsage(ctx, IocToPath(ioc));
}
catch (Exception e)
{
throw LogAndConvertJavaException(e);
}
}
public void OnCreate(IFileStorageSetupActivity activity, Bundle savedInstanceState)
{

View File

@ -175,7 +175,7 @@ public class DropboxFileStorage extends JavaFileStorageBase {
Log.d(TAG, "LoggedIn=false (due to unlink exception)");
setLoggedIn(false);
clearKeys();
return new Exception("Unlinked from Dropbox!", e);
return new UserInteractionRequiredException("Unlinked from Dropbox! User must re-link.", e);
}

View File

@ -281,13 +281,16 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
@Override
public InputStream openFileForRead(String path) throws Exception {
logDebug("openFileForRead...");
GDrivePath gdrivePath = new GDrivePath(path);
Drive driveService = getDriveService(gdrivePath.getAccount());
try
{
File file = getFileForPath(gdrivePath, driveService);
return getFileContent(file, driveService);
InputStream res = getFileContent(file, driveService);
logDebug("openFileForRead ok.");
return res;
}
catch (Exception e)
{
@ -340,15 +343,17 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
@Override
public void uploadFile(String path, byte[] data, boolean writeTransactional)
throws Exception {
logDebug("upload file...");
try
{
ByteArrayContent content = new ByteArrayContent(null, data);
GDrivePath gdrivePath = new GDrivePath(path);
Drive driveService = getDriveService(gdrivePath.getAccount());
try
{
File driveFile = getFileForPath(gdrivePath, driveService);
getDriveService(gdrivePath.getAccount()).files()
.update(driveFile.getId(), driveFile, content).execute();
logDebug("upload file ok.");
}
catch (Exception e)
{
@ -451,8 +456,11 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
}
private Exception convertException(Exception e) {
logDebug("Exception: " + e.toString());
e.printStackTrace();
if (UserRecoverableAuthIOException.class.isAssignableFrom(e.getClass()))
{
logDebug("clearing account data.");
//this is not really nice because it removes data from the cache which might still be valid but we don't have the account name here...
mAccountData.clear();
}
@ -682,29 +690,37 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
private void initializeAccount(final Context appContext,
final String accountName) throws IOException {
logDebug("Init account for " + accountName);
if (!mAccountData.containsKey(accountName))
{
AccountData newAccountData = new AccountData();
newAccountData.drive = createDriveService(accountName, appContext);
mAccountData.put(accountName, newAccountData);
logDebug("Added account data for " + accountName);
}
AccountData accountData = mAccountData.get(accountName);
//try to finish the initialization. If this fails, we throw.
//in case of "Always return true" (inside CachingFileStorage) this means
//we have a partially uninitialized AccountData object.
//We'll try to initialize later in verify() if (e.g.) network is available again.
finishInitialization(newAccountData, accountName);
}
finishInitialization(accountData, accountName);
}
private void finishInitialization(AccountData newAccountData, String accountName) throws IOException
{
if (TextUtils.isEmpty(newAccountData.mRootFolderId))
{
logDebug("Finish init account for " + accountName);
About about = newAccountData.drive.about().get().execute();
newAccountData.mRootFolderId = about.getRootFolderId();
}
else
{
logDebug("Account for " + accountName + " already fully initialized.");
}
}
@Override
@ -726,6 +742,7 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
{
try
{
logDebug("prepareFileUsage " + path + "...");
String accountName;
GDrivePath gdrivePath = null;
if (path.startsWith(getProtocolPrefix()))
@ -740,9 +757,11 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
accountName = path;
initializeAccount(appContext, accountName);
logDebug("prepareFileUsage ok");
}
catch (UserRecoverableAuthIOException e)
{
logDebug("prepareFileUsage: UserInteractionRequiredException");
throw new UserInteractionRequiredException(e);
}
@ -764,6 +783,7 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
@Override
public void onStart(final JavaFileStorage.FileStorageSetupActivity setupAct) {
logDebug("onStart");
Activity activity = (Activity)setupAct;
if (PROCESS_NAME_SELECTFILE.equals(setupAct.getProcessName()))

View File

@ -1,10 +1,8 @@
package keepass2android.javafilestorage;
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
public class UserInteractionRequiredException extends Exception {
public UserInteractionRequiredException(UserRecoverableAuthIOException e) {
public UserInteractionRequiredException(Throwable e) {
super(e);
}
@ -12,6 +10,10 @@ public class UserInteractionRequiredException extends Exception {
}
public UserInteractionRequiredException(String string, Throwable e) {
super(string, e);
}
/**
*
*/