diff --git a/src/Kp2aBusinessLogic/Io/AndroidContentStorage.cs b/src/Kp2aBusinessLogic/Io/AndroidContentStorage.cs index c9d1f867..86380a1b 100644 --- a/src/Kp2aBusinessLogic/Io/AndroidContentStorage.cs +++ b/src/Kp2aBusinessLogic/Io/AndroidContentStorage.cs @@ -165,7 +165,7 @@ namespace keepass2android.Io } catch (Exception e) { - Kp2aLog.LogUnexpectedError(e); + Kp2aLog.Log(e.ToString()); return false; } @@ -239,7 +239,7 @@ namespace keepass2android.Io } else return false; } - else throw new Exception("couldn't move to first result element"); + else throw new Exception("couldn't move to first result element: " + (cursor == null) + uri.ToString()); } catch (Exception e) { diff --git a/src/Kp2aBusinessLogic/Io/CachingFileStorage.cs b/src/Kp2aBusinessLogic/Io/CachingFileStorage.cs index e9735f83..31d1d0bc 100644 --- a/src/Kp2aBusinessLogic/Io/CachingFileStorage.cs +++ b/src/Kp2aBusinessLogic/Io/CachingFileStorage.cs @@ -103,7 +103,7 @@ namespace keepass2android.Io return _streamCacheDir + iocAsHexString; } - private bool IsCached(IOConnectionInfo ioc) + public bool IsCached(IOConnectionInfo ioc) { return File.Exists(CachedFilePath(ioc)) && File.Exists(VersionFilePath(ioc)) diff --git a/src/Kp2aBusinessLogic/SelectStorageLocationActivityBase.cs b/src/Kp2aBusinessLogic/SelectStorageLocationActivityBase.cs index 8ecc51fa..c5ce53bc 100644 --- a/src/Kp2aBusinessLogic/SelectStorageLocationActivityBase.cs +++ b/src/Kp2aBusinessLogic/SelectStorageLocationActivityBase.cs @@ -135,7 +135,7 @@ namespace keepass2android } catch (Exception e) { - Kp2aLog.LogUnexpectedError(e); + Kp2aLog.Log(e.ToString()); } } diff --git a/src/Kp2aBusinessLogic/database/Database.cs b/src/Kp2aBusinessLogic/database/Database.cs index 7714055c..c795e082 100644 --- a/src/Kp2aBusinessLogic/database/Database.cs +++ b/src/Kp2aBusinessLogic/database/Database.cs @@ -122,13 +122,14 @@ namespace keepass2android Root = pwDatabase.RootGroup; PopulateGlobals(Root); - Loaded = true; + KpDatabase = pwDatabase; SearchHelper = new SearchDbHelper(app); _databaseFormat = databaseFormat; CanWrite = databaseFormat.CanWrite && !fileStorage.IsReadOnly(iocInfo); + Loaded = true; } catch (Exception) { @@ -203,6 +204,8 @@ namespace keepass2android public PwGroup Search(SearchParameters searchParams, IDictionary> resultContexts) { + if (SearchHelper == null) + throw new Exception("SearchHelper is null"); return SearchHelper.Search(this, searchParams, resultContexts); } @@ -273,6 +276,8 @@ namespace keepass2android } public void Clear() { + _loaded = false; + Groups.Clear(); Entries.Clear(); Dirty.Clear(); @@ -280,7 +285,7 @@ namespace keepass2android Root = null; KpDatabase = null; - _loaded = false; + CanWrite = true; _reloadRequested = false; OtpAuxFileIoc = null; diff --git a/src/Kp2aBusinessLogic/database/KdbDatabaseFormat.cs b/src/Kp2aBusinessLogic/database/KdbDatabaseFormat.cs index 0f3b3b52..e2c7c2b7 100644 --- a/src/Kp2aBusinessLogic/database/KdbDatabaseFormat.cs +++ b/src/Kp2aBusinessLogic/database/KdbDatabaseFormat.cs @@ -83,6 +83,8 @@ namespace keepass2android } catch (Java.Lang.Exception e) { + if (e.Message == "Invalid key!") + throw new InvalidCompositeKeyException(); throw new Exception(e.LocalizedMessage ?? e.Message ?? e.GetType().Name, e); diff --git a/src/keepass2android/CreateDatabaseActivity.cs b/src/keepass2android/CreateDatabaseActivity.cs index 6e251443..191da39d 100644 --- a/src/keepass2android/CreateDatabaseActivity.cs +++ b/src/keepass2android/CreateDatabaseActivity.cs @@ -388,7 +388,7 @@ namespace keepass2android } catch (Exception e) { - Kp2aLog.LogUnexpectedError(e); + Kp2aLog.Log(e.ToString()); } } diff --git a/src/keepass2android/EntryActivity.cs b/src/keepass2android/EntryActivity.cs index c2e1ef85..1bb359dd 100644 --- a/src/keepass2android/EntryActivity.cs +++ b/src/keepass2android/EntryActivity.cs @@ -225,6 +225,11 @@ namespace keepass2android { try { + if (!_popupMenuItems.ContainsKey(fieldId)) + { + Kp2aLog.Log("Did not find field with key " + fieldId); + return; + } //create a new popup item for the plugin action: var newPopup = new PluginPopupMenuItem(this, pluginPackage, fieldId, popupItemId, displayText, iconId, bundleExtra); //see if we already have a popup item for this field with the same item id diff --git a/src/keepass2android/ExportDatabaseActivity.cs b/src/keepass2android/ExportDatabaseActivity.cs index b10d0e07..507598bf 100644 --- a/src/keepass2android/ExportDatabaseActivity.cs +++ b/src/keepass2android/ExportDatabaseActivity.cs @@ -97,7 +97,7 @@ namespace keepass2android } catch (Exception e) { - Kp2aLog.LogUnexpectedError(e); + Kp2aLog.Log(e.ToString()); } } diff --git a/src/keepass2android/GroupActivity.cs b/src/keepass2android/GroupActivity.cs index 5ae6be4c..d222f8bb 100644 --- a/src/keepass2android/GroupActivity.cs +++ b/src/keepass2android/GroupActivity.cs @@ -146,13 +146,12 @@ namespace keepass2android tv.SetPadding(tv.PaddingLeft,0,tv.PaddingRight,0); PwEntry templateEntry = this.GetItem(position); - + int size = (int)(Util.convertDpToPixel(Util.convertDpToPixel(20, Context), Context)); var bmp = Bitmap.CreateScaledBitmap( Util.DrawableToBitmap(App.Kp2a.GetDb() .DrawableFactory.GetIconDrawable(Context, App.Kp2a.GetDb().KpDatabase, templateEntry.IconId, PwUuid.Zero, false)), - (int)Util.convertDpToPixel(80, Context), - (int)Util.convertDpToPixel(80, Context), + size, size, true); diff --git a/src/keepass2android/PasswordActivity.cs b/src/keepass2android/PasswordActivity.cs index 487e0f4e..ffe61e68 100644 --- a/src/keepass2android/PasswordActivity.cs +++ b/src/keepass2android/PasswordActivity.cs @@ -18,6 +18,7 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; @@ -322,13 +323,17 @@ namespace keepass2android { if (KeyProviderType == KeyProviders.KeyFile) { - //TODO: if the user has not yet selected a keyfile, _keyFileOrProvider is empty which - //gives an (unhandled) exception here - var iocKeyfile = IOConnectionInfo.UnserializeFromString(_keyFileOrProvider); + + //if the user has not yet selected a keyfile, _keyFileOrProvider is empty + if (string.IsNullOrEmpty(_keyFileOrProvider) == false) + { + var iocKeyfile = IOConnectionInfo.UnserializeFromString(_keyFileOrProvider); - App.Kp2a.GetFileStorage(iocKeyfile) - .PrepareFileUsage(new FileStorageSetupInitiatorActivity(this, OnActivityResult, null), iocKeyfile, - RequestCodePrepareKeyFile, false); + App.Kp2a.GetFileStorage(iocKeyfile) + .PrepareFileUsage(new FileStorageSetupInitiatorActivity(this, OnActivityResult, null), iocKeyfile, + RequestCodePrepareKeyFile, false); + } + } else PerformLoadDatabase(); @@ -384,10 +389,7 @@ namespace keepass2android try { ChallengeInfo temp = _challengeProv.Encrypt(_challengeSecret); - IFileStorage fileStorage = App.Kp2a.GetOtpAuxFileStorage(_ioConnection); - IOConnectionInfo iocAux = fileStorage.GetFilePath(fileStorage.GetParentPath(_ioConnection), - fileStorage.GetFilenameWithoutPathAndExt(_ioConnection) + ".xml"); - if (!temp.Save(iocAux)) + if (!temp.Save(_otpAuxIoc)) { Toast.MakeText(this, Resource.String.ErrorUpdatingChalAuxFile, ToastLength.Long).Show(); return false; @@ -482,7 +484,8 @@ namespace keepass2android } catch (Exception e) { - Kp2aLog.LogUnexpectedError(e); + //this can happen e.g. if the file storage does not support GetParentPath + Kp2aLog.Log(e.ToString()); //retry with saved ioc try { @@ -643,6 +646,7 @@ namespace keepass2android protected override void LoadFile(IOConnectionInfo iocAux) { Activity._chalInfo = ChallengeInfo.Load(iocAux); + Activity._otpAuxIoc = iocAux; } @@ -925,7 +929,7 @@ namespace keepass2android public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) { - if (requestCode == FingerprintPermissionRequestCode && grantResults[0] == Permission.Granted) + if ((requestCode == FingerprintPermissionRequestCode) && (grantResults.Any()) && (grantResults[0] == Permission.Granted)) { var btn = FindViewById(Resource.Id.fingerprintbtn); btn.Click += (sender, args) => @@ -1390,20 +1394,29 @@ namespace keepass2android //avoid password being visible while loading: _showPassword = false; - MakePasswordMaskedOrVisible(); + try + { + MakePasswordMaskedOrVisible(); - Handler handler = new Handler(); - OnFinish onFinish = new AfterLoad(handler, this); - _performingLoad = true; - LoadDb task = (KeyProviderType == KeyProviders.Otp) - ? new SaveOtpAuxFileAndLoadDb(App.Kp2a, _ioConnection, _loadDbTask, compositeKey, _keyFileOrProvider, - onFinish, this) - : new LoadDb(App.Kp2a, _ioConnection, _loadDbTask, compositeKey, _keyFileOrProvider, onFinish); - _loadDbTask = null; // prevent accidental re-use + Handler handler = new Handler(); + OnFinish onFinish = new AfterLoad(handler, this); + _performingLoad = true; + LoadDb task = (KeyProviderType == KeyProviders.Otp) + ? new SaveOtpAuxFileAndLoadDb(App.Kp2a, _ioConnection, _loadDbTask, compositeKey, _keyFileOrProvider, + onFinish, this) + : new LoadDb(App.Kp2a, _ioConnection, _loadDbTask, compositeKey, _keyFileOrProvider, onFinish); + _loadDbTask = null; // prevent accidental re-use - SetNewDefaultFile(); + SetNewDefaultFile(); - new ProgressTask(App.Kp2a, this, task).Run(); + new ProgressTask(App.Kp2a, this, task).Run(); + } + catch (Exception e) + { + Kp2aLog.LogUnexpectedError(new Exception("cannot load database: "+e + ", c: " + (compositeKey != null) + (_ioConnection != null) + (_keyFileOrProvider != null), e)); + throw; + } + } private bool CreateCompositeKey(out CompositeKey compositeKey, out string errorMessage) @@ -1703,8 +1716,9 @@ namespace keepass2android CheckBox cbOfflineMode = (CheckBox)FindViewById(Resource.Id.work_offline); App.Kp2a.OfflineMode = cbOfflineMode.Checked = App.Kp2a.OfflineModePreference; //this won't overwrite new user settings because every change is directly saved in settings LinearLayout offlineModeContainer = FindViewById(Resource.Id.work_offline_container); - if (App.Kp2a.GetFileStorage(_ioConnection) is IOfflineSwitchable) - { + var cachingFileStorage = App.Kp2a.GetFileStorage(_ioConnection) as CachingFileStorage; + if ((cachingFileStorage != null) && cachingFileStorage.IsCached(_ioConnection)) + { offlineModeContainer.Visibility = ViewStates.Visible; } else diff --git a/src/keepass2android/app/App.cs b/src/keepass2android/app/App.cs index b43533b6..8c9fd14f 100644 --- a/src/keepass2android/app/App.cs +++ b/src/keepass2android/app/App.cs @@ -136,6 +136,14 @@ namespace keepass2android public void BroadcastDatabaseAction(Context ctx, string action) { Intent i = new Intent(action); + + //seems like this can happen. This code is for debugging. + if (App.Kp2a.GetDb().Ioc == null) + { + Kp2aLog.LogUnexpectedError(new Exception("App.Kp2a.GetDb().Ioc is null")); + return; + } + i.PutExtra(Strings.ExtraDatabaseFileDisplayname, App.Kp2a.GetFileStorage(App.Kp2a.GetDb().Ioc).GetDisplayName(App.Kp2a.GetDb().Ioc)); i.PutExtra(Strings.ExtraDatabaseFilepath, App.Kp2a.GetDb().Ioc.Path); foreach (var plugin in new PluginDatabase(ctx).GetPluginsWithAcceptedScope(Strings.ScopeDatabaseActions)) diff --git a/src/keepass2android/fileselect/FileChooserFileProvider.cs b/src/keepass2android/fileselect/FileChooserFileProvider.cs index 20db0ca1..a3ea294e 100644 --- a/src/keepass2android/fileselect/FileChooserFileProvider.cs +++ b/src/keepass2android/fileselect/FileChooserFileProvider.cs @@ -71,7 +71,7 @@ namespace keepass2android } catch (Exception e) { - Kp2aLog.LogUnexpectedError(e); + Kp2aLog.Log(e.ToString()); return null; } }