Made pre-loading optional
Refactoring of PasswordActivity/QuickUnlock lifecycle
This commit is contained in:
		| @@ -109,7 +109,7 @@ namespace keepass2android | ||||
| 			var filename = fileStorage.GetFilenameWithoutPathAndExt(iocInfo); | ||||
| 			try | ||||
| 			{ | ||||
| 				pwDatabase.Open(databaseData, filename, iocInfo, compositeKey, status); | ||||
| 				pwDatabase.Open(databaseData ?? fileStorage.OpenFileForRead(iocInfo), filename, iocInfo, compositeKey, status); | ||||
| 			} | ||||
| 			catch (Exception) | ||||
| 			{ | ||||
| @@ -118,8 +118,11 @@ namespace keepass2android | ||||
| 					//if we don't get a password, we don't know whether this means "empty password" or "no password" | ||||
| 					//retry without password: | ||||
| 					compositeKey.RemoveUserKey(compositeKey.GetUserKey(typeof (KcpPassword))); | ||||
| 					if (databaseData != null) | ||||
| 					{ | ||||
| 						databaseData.Seek(0, SeekOrigin.Begin); | ||||
| 					pwDatabase.Open(databaseData, filename, iocInfo, compositeKey, status); | ||||
| 					} | ||||
| 					pwDatabase.Open(databaseData ?? fileStorage.OpenFileForRead(iocInfo), filename, iocInfo, compositeKey, status); | ||||
| 				} | ||||
| 				else throw; | ||||
| 			} | ||||
|   | ||||
| @@ -48,7 +48,7 @@ namespace keepass2android | ||||
| 			try | ||||
| 			{ | ||||
| 				StatusLogger.UpdateMessage(UiStringKey.loading_database); | ||||
| 				_app.LoadDatabase(_ioc, _databaseData.Result, _pass, _key, StatusLogger); | ||||
| 				_app.LoadDatabase(_ioc, _databaseData == null ? null : _databaseData.Result, _pass, _key, StatusLogger); | ||||
| 				SaveFileData (_ioc, _key); | ||||
| 				 | ||||
| 			} catch (KeyFileException) { | ||||
|   | ||||
| @@ -37,12 +37,9 @@ namespace keepass2android | ||||
| 		public const Result ExitLock = Result.FirstUser+1; | ||||
| 		public const Result ExitRefresh = Result.FirstUser+2; | ||||
| 		public const Result ExitRefreshTitle = Result.FirstUser+3; | ||||
| 		public const Result ExitForceLock = Result.FirstUser+4; | ||||
| 		public const Result ExitQuickUnlock = Result.FirstUser+5; | ||||
| 		public const Result ExitCloseAfterTaskComplete = Result.FirstUser+6; | ||||
| 		public const Result ExitChangeDb = Result.FirstUser+7; | ||||
| 		public const Result ExitForceLockAndChangeDb = Result.FirstUser+8; | ||||
| 		public const Result ExitReloadDb = Result.FirstUser+9; | ||||
| 		public const Result ExitCloseAfterTaskComplete = Result.FirstUser+4; | ||||
| 		public const Result ExitChangeDb = Result.FirstUser+5; // NOTE: Nothing is currently using this, but in the future a "Change Database" menu option might. | ||||
| 		public const Result ExitReloadDb = Result.FirstUser+6; | ||||
|  | ||||
| 		AppTask _appTask; | ||||
|  | ||||
|   | ||||
| @@ -59,6 +59,8 @@ namespace keepass2android | ||||
| 		private bool _rememberKeyfile; | ||||
| 		ISharedPreferences _prefs; | ||||
|  | ||||
| 		private bool _started; | ||||
|  | ||||
| 		public PasswordActivity (IntPtr javaReference, JniHandleOwnership transfer) | ||||
| 			: base(javaReference, transfer) | ||||
| 		{ | ||||
| @@ -139,26 +141,21 @@ namespace keepass2android | ||||
| 			//NOTE: original code from k eepassdroid used switch ((Android.App.Result)requestCode) { (but doesn't work here, although k eepassdroid works) | ||||
| 			switch(resultCode) { | ||||
|  | ||||
| 				case KeePass.ExitNormal: // Returned to this screen using the Back key, treat as locking the database | ||||
| 					App.Kp2a.LockDatabase(); | ||||
| 					break; | ||||
| 				case KeePass.ExitLock: | ||||
| 					// The database has already been locked, and the quick unlock screen will be shown if appropriate | ||||
| 					break; | ||||
| 				case KeePass.ExitForceLock: | ||||
| 					App.Kp2a.LockDatabase(false); | ||||
| 					break; | ||||
| 				case KeePass.ExitForceLockAndChangeDb: | ||||
| 				case KeePass.ExitChangeDb: // What's the difference between this and ExitForceLockAndChangeDb? | ||||
| 				case KeePass.ExitNormal: // Returned to this screen using the Back key, treat as exiting the database | ||||
| 				case KeePass.ExitChangeDb: | ||||
| 					App.Kp2a.LockDatabase(false); | ||||
| 					Finish(); | ||||
| 					break; | ||||
| 				case KeePass.ExitCloseAfterTaskComplete: | ||||
| 					// Do not lock the database | ||||
| 					SetResult(KeePass.ExitCloseAfterTaskComplete); | ||||
| 					Finish(); | ||||
| 					break; | ||||
| 				case KeePass.ExitQuickUnlock: | ||||
| 					App.Kp2a.UnlockDatabase(); | ||||
| 					LaunchNextActivity(); | ||||
| 					break; | ||||
| 				case KeePass.ExitReloadDb: | ||||
| 					//if the activity was killed, fill password/keyfile so the user can directly hit load again | ||||
| 					if (App.Kp2a.GetDb().Loaded) | ||||
| @@ -180,8 +177,9 @@ namespace keepass2android | ||||
| 							SetEditText(Resource.Id.pass_keyfile, kcpKeyfile.Path); | ||||
| 						} | ||||
| 					} | ||||
| 					App.Kp2a.LockDatabase(false); | ||||
| 					break; | ||||
| 				case Result.Ok: | ||||
| 				case Result.Ok: // Key file browse dialog OK'ed. | ||||
| 					if (requestCode == Intents.RequestCodeFileBrowseForKeyfile) { | ||||
| 						string filename = Util.IntentToFilename(data); | ||||
| 						if (filename != null) { | ||||
| @@ -258,6 +256,13 @@ namespace keepass2android | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
| 			if (App.Kp2a.GetDb().Loaded && App.Kp2a.GetDb().Ioc != null && | ||||
| 				App.Kp2a.GetDb().Ioc.GetDisplayName() != _ioConnection.GetDisplayName()) | ||||
| 			{ | ||||
| 				// A different database is currently loaded, unload it before loading the new one requested | ||||
| 				App.Kp2a.LockDatabase(false); | ||||
| 			} | ||||
|  | ||||
| 			AppTask = AppTask.GetTaskInOnCreate(savedInstanceState, Intent); | ||||
| 			 | ||||
| 			SetContentView(Resource.Layout.password); | ||||
| @@ -363,19 +368,7 @@ namespace keepass2android | ||||
| 		protected override void OnStart() | ||||
| 		{ | ||||
| 			base.OnStart(); | ||||
|  | ||||
| 			if (App.Kp2a.QuickUnlockEnabled && App.Kp2a.QuickLocked) | ||||
| 			{ | ||||
| 				Intent i = new Intent(this, typeof(QuickUnlock)); | ||||
| 				PutIoConnectionToIntent(_ioConnection, i); | ||||
| 				Kp2aLog.Log("Starting QuickUnlock"); | ||||
| 				StartActivityForResult(i, 0); | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				// Create task to kick off file loading while the user enters the password | ||||
| 				_loadDbTask = Task.Factory.StartNew<MemoryStream>(LoadDbFile); | ||||
| 			} | ||||
| 			_started = true; | ||||
| 		} | ||||
|  | ||||
| 		private MemoryStream LoadDbFile() | ||||
| @@ -410,27 +403,33 @@ namespace keepass2android | ||||
| 			AppTask.ToBundle(outState); | ||||
| 		} | ||||
| 		 | ||||
| 		/* | ||||
| 		protected override void OnResume() { | ||||
| 		protected override void OnResume() | ||||
| 		{ | ||||
| 			base.OnResume(); | ||||
|  | ||||
| 			if (_startedWithActivityResult) | ||||
| 				return; | ||||
|  | ||||
| 			if (App.Kp2a.GetDb().Loaded && (App.Kp2a.GetDb().Ioc != null) | ||||
| 			    && (_ioConnection != null) && (App.Kp2a.GetDb().Ioc.GetDisplayName() == _ioConnection.GetDisplayName())) | ||||
| 			// OnResume is run every time the activity comes to the foreground. This code should only run when the activity is started (OnStart), but must | ||||
| 			// be run in OnResume rather than OnStart so that it always occurrs after OnActivityResult (when re-creating a killed activity, OnStart occurs before OnActivityResult) | ||||
| 			if (_started)  | ||||
| 			{ | ||||
| 				if (App.Kp2a.QuickLocked == false) | ||||
| 				_started = false; | ||||
| 				if (App.Kp2a.DatabaseIsUnlocked) | ||||
| 				{ | ||||
| 					LaunchNextActivity(); | ||||
| 				} | ||||
| 				else  | ||||
| 				else if (App.Kp2a.QuickUnlockEnabled && App.Kp2a.QuickLocked) | ||||
| 				{ | ||||
| 					TryStartQuickUnlock(); | ||||
| 					var i = new Intent(this, typeof(QuickUnlock)); | ||||
| 					PutIoConnectionToIntent(_ioConnection, i); | ||||
| 					Kp2aLog.Log("Starting QuickUnlock"); | ||||
| 					StartActivityForResult(i, 0); | ||||
| 				} | ||||
| 				else if (_loadDbTask == null && _prefs.GetBoolean(GetString(Resource.String.PreloadDatabaseEnabled_key), true)) | ||||
| 				{ | ||||
| 					// Create task to kick off file loading while the user enters the password | ||||
| 					_loadDbTask = Task.Factory.StartNew<MemoryStream>(LoadDbFile); | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 		 * */ | ||||
| 		 | ||||
| 		private void RetrieveSettings() { | ||||
| 			String defaultFilename = _prefs.GetString(KeyDefaultFilename, ""); | ||||
|   | ||||
| @@ -72,8 +72,6 @@ namespace keepass2android | ||||
| 				keyboard.ShowSoftInput(pwd, 0); | ||||
| 			}, 50); | ||||
|  | ||||
| 			SetResult(KeePass.ExitChangeDb); | ||||
|  | ||||
| 			Button btnUnlock = (Button)FindViewById(Resource.Id.QuickUnlock_button); | ||||
| 			btnUnlock.Click += (object sender, EventArgs e) =>  | ||||
| 			{ | ||||
| @@ -82,11 +80,11 @@ namespace keepass2android | ||||
| 				String expectedPasswordPart = password.Substring(Math.Max(0,password.Length-quickUnlockLength),Math.Min(password.Length, quickUnlockLength)); | ||||
| 				if (pwd.Text == expectedPasswordPart) | ||||
| 				{ | ||||
| 					SetResult(KeePass.ExitQuickUnlock); | ||||
| 					App.Kp2a.UnlockDatabase(); | ||||
| 				} | ||||
| 				else | ||||
| 				{ | ||||
| 					SetResult(KeePass.ExitForceLock); | ||||
| 					App.Kp2a.LockDatabase(false); | ||||
| 					Toast.MakeText(this, GetString(Resource.String.QuickUnlock_fail), ToastLength.Long).Show(); | ||||
| 				} | ||||
| 				Finish(); | ||||
| @@ -95,22 +93,10 @@ namespace keepass2android | ||||
| 			Button btnLock = (Button)FindViewById(Resource.Id.QuickUnlock_buttonLock); | ||||
| 			btnLock.Click += (object sender, EventArgs e) =>  | ||||
| 			{ | ||||
| 				SetResult(KeePass.ExitForceLockAndChangeDb); | ||||
| 				App.Kp2a.LockDatabase(false); | ||||
| 				Finish(); | ||||
| 			}; | ||||
| 		} | ||||
|  | ||||
|  | ||||
| 		protected override void OnResume() | ||||
| 		{ | ||||
| 			base.OnResume(); | ||||
|  | ||||
| 			if ( ! App.Kp2a.GetDb().Loaded ) { | ||||
| 				SetResult(KeePass.ExitChangeDb); | ||||
| 				Finish(); | ||||
| 				return; | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
|   | ||||
							
								
								
									
										1052
									
								
								src/keepass2android/Resources/Resource.designer.cs
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1052
									
								
								src/keepass2android/Resources/Resource.designer.cs
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -92,4 +92,7 @@ | ||||
| 	<string name="ShowUnlockedNotification_key">ShowUnlockedNotification</string> | ||||
| 	<bool name="ShowUnlockedNotification_default">true</bool> | ||||
|  | ||||
| 	<string name="PreloadDatabaseEnabled_key">PreloadDatabaseEnabled</string> | ||||
| 	<bool name="PreloadDatabaseEnabled_default">true</bool> | ||||
|  | ||||
| </resources> | ||||
| @@ -233,6 +233,9 @@ | ||||
| 	<string name="ShowUnlockedNotification_title">Notification while unlocked</string> | ||||
| 	<string name="ShowUnlockedNotification_summary">Show an ongoing notification while the database is unlocked.</string> | ||||
|  | ||||
| 	<string name="PreloadDatabaseEnabled_title">Pre-load database file</string> | ||||
| 	<string name="PreloadDatabaseEnabled_summary">Start background loading or downloading of the database file during password entry.</string> | ||||
|  | ||||
| 	<string name="AskOverwriteBinary">Do you want to overwrite the existing binary with the same name?</string> | ||||
|   <string name="AskOverwriteBinary_title">Overwrite existing binary?</string> | ||||
|   <string name="AskOverwriteBinary_yes">Overwrite</string> | ||||
|   | ||||
| @@ -163,6 +163,12 @@ | ||||
| 			  android:defaultValue="true" | ||||
| 			  android:title="@string/CheckForFileChangesOnSave_title" | ||||
| 			  android:key="@string/CheckForFileChangesOnSave_key" /> | ||||
|  | ||||
| 		<CheckBoxPreference | ||||
| 			android:enabled="true" | ||||
| 				 android:persistent="true" | ||||
| 				 android:summary="@string/PreloadDatabaseEnabled_summary" | ||||
| 				 android:defaultValue="@bool/PreloadDatabaseEnabled_default" | ||||
| 				 android:title="@string/PreloadDatabaseEnabled_title" | ||||
| 				 android:key="@string/PreloadDatabaseEnabled_key" /> | ||||
| 	</PreferenceScreen> | ||||
| </PreferenceScreen> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 AlexVallat
					AlexVallat