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); 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 ?? e.Message ??
_app.GetResourceString(UiStringKey.ErrorOcurred)+e.GetType().Name, e); _app.GetResourceString(UiStringKey.ErrorOcurred) + exception.GetType().Name, e);
return ex; return ex;
} }
return e;
}
public IWriteTransaction OpenWriteTransaction(IOConnectionInfo ioc, bool useFileTransaction) public IWriteTransaction OpenWriteTransaction(IOConnectionInfo ioc, bool useFileTransaction)
{ {
return new JavaFileStorageWriteTransaction(IocToPath(ioc), useFileTransaction, this); 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) 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) public void PrepareFileUsage(Context ctx, IOConnectionInfo ioc)
{
try
{ {
_jfs.PrepareFileUsage(ctx, IocToPath(ioc)); _jfs.PrepareFileUsage(ctx, IocToPath(ioc));
} }
catch (Exception e)
{
throw LogAndConvertJavaException(e);
}
}
public void OnCreate(IFileStorageSetupActivity activity, Bundle savedInstanceState) 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)"); Log.d(TAG, "LoggedIn=false (due to unlink exception)");
setLoggedIn(false); setLoggedIn(false);
clearKeys(); 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 @Override
public InputStream openFileForRead(String path) throws Exception { public InputStream openFileForRead(String path) throws Exception {
logDebug("openFileForRead...");
GDrivePath gdrivePath = new GDrivePath(path); GDrivePath gdrivePath = new GDrivePath(path);
Drive driveService = getDriveService(gdrivePath.getAccount()); Drive driveService = getDriveService(gdrivePath.getAccount());
try try
{ {
File file = getFileForPath(gdrivePath, driveService); File file = getFileForPath(gdrivePath, driveService);
return getFileContent(file, driveService); InputStream res = getFileContent(file, driveService);
logDebug("openFileForRead ok.");
return res;
} }
catch (Exception e) catch (Exception e)
{ {
@ -340,15 +343,17 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
@Override @Override
public void uploadFile(String path, byte[] data, boolean writeTransactional) public void uploadFile(String path, byte[] data, boolean writeTransactional)
throws Exception { throws Exception {
logDebug("upload file...");
try
{
ByteArrayContent content = new ByteArrayContent(null, data); ByteArrayContent content = new ByteArrayContent(null, data);
GDrivePath gdrivePath = new GDrivePath(path); GDrivePath gdrivePath = new GDrivePath(path);
Drive driveService = getDriveService(gdrivePath.getAccount()); Drive driveService = getDriveService(gdrivePath.getAccount());
try
{
File driveFile = getFileForPath(gdrivePath, driveService); File driveFile = getFileForPath(gdrivePath, driveService);
getDriveService(gdrivePath.getAccount()).files() getDriveService(gdrivePath.getAccount()).files()
.update(driveFile.getId(), driveFile, content).execute(); .update(driveFile.getId(), driveFile, content).execute();
logDebug("upload file ok.");
} }
catch (Exception e) catch (Exception e)
{ {
@ -451,8 +456,11 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
} }
private Exception convertException(Exception e) { private Exception convertException(Exception e) {
logDebug("Exception: " + e.toString());
e.printStackTrace();
if (UserRecoverableAuthIOException.class.isAssignableFrom(e.getClass())) 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... //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(); mAccountData.clear();
} }
@ -682,29 +690,37 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
private void initializeAccount(final Context appContext, private void initializeAccount(final Context appContext,
final String accountName) throws IOException { final String accountName) throws IOException {
logDebug("Init account for " + accountName);
if (!mAccountData.containsKey(accountName)) if (!mAccountData.containsKey(accountName))
{ {
AccountData newAccountData = new AccountData(); AccountData newAccountData = new AccountData();
newAccountData.drive = createDriveService(accountName, appContext); newAccountData.drive = createDriveService(accountName, appContext);
mAccountData.put(accountName, newAccountData); mAccountData.put(accountName, newAccountData);
logDebug("Added account data for " + accountName); logDebug("Added account data for " + accountName);
}
AccountData accountData = mAccountData.get(accountName);
//try to finish the initialization. If this fails, we throw. //try to finish the initialization. If this fails, we throw.
//in case of "Always return true" (inside CachingFileStorage) this means //in case of "Always return true" (inside CachingFileStorage) this means
//we have a partially uninitialized AccountData object. //we have a partially uninitialized AccountData object.
//We'll try to initialize later in verify() if (e.g.) network is available again. //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 private void finishInitialization(AccountData newAccountData, String accountName) throws IOException
{ {
if (TextUtils.isEmpty(newAccountData.mRootFolderId)) if (TextUtils.isEmpty(newAccountData.mRootFolderId))
{ {
logDebug("Finish init account for " + accountName);
About about = newAccountData.drive.about().get().execute(); About about = newAccountData.drive.about().get().execute();
newAccountData.mRootFolderId = about.getRootFolderId(); newAccountData.mRootFolderId = about.getRootFolderId();
} }
else
{
logDebug("Account for " + accountName + " already fully initialized.");
}
} }
@Override @Override
@ -726,6 +742,7 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
{ {
try try
{ {
logDebug("prepareFileUsage " + path + "...");
String accountName; String accountName;
GDrivePath gdrivePath = null; GDrivePath gdrivePath = null;
if (path.startsWith(getProtocolPrefix())) if (path.startsWith(getProtocolPrefix()))
@ -740,9 +757,11 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
accountName = path; accountName = path;
initializeAccount(appContext, accountName); initializeAccount(appContext, accountName);
logDebug("prepareFileUsage ok");
} }
catch (UserRecoverableAuthIOException e) catch (UserRecoverableAuthIOException e)
{ {
logDebug("prepareFileUsage: UserInteractionRequiredException");
throw new UserInteractionRequiredException(e); throw new UserInteractionRequiredException(e);
} }
@ -764,6 +783,7 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
@Override @Override
public void onStart(final JavaFileStorage.FileStorageSetupActivity setupAct) { public void onStart(final JavaFileStorage.FileStorageSetupActivity setupAct) {
logDebug("onStart");
Activity activity = (Activity)setupAct; Activity activity = (Activity)setupAct;
if (PROCESS_NAME_SELECTFILE.equals(setupAct.getProcessName())) if (PROCESS_NAME_SELECTFILE.equals(setupAct.getProcessName()))

View File

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