LoadDb and SaveDb respect the SyncInBackground preference

This commit is contained in:
Philipp Crocoll
2025-05-13 11:10:26 +02:00
parent c3b6612591
commit cfb185b53d
11 changed files with 204 additions and 59 deletions

View File

@@ -1753,17 +1753,10 @@ namespace keepass2android
cbOfflineMode.Checked =
App.Kp2a
.OfflineModePreference; //this won't overwrite new user settings because every change is directly saved in settings
LinearLayout offlineModeContainer = FindViewById<LinearLayout>(Resource.Id.work_offline_container);
var cachingFileStorage = App.Kp2a.GetFileStorage(_ioConnection) as CachingFileStorage;
if ((cachingFileStorage != null) && cachingFileStorage.IsCached(_ioConnection))
{
offlineModeContainer.Visibility = ViewStates.Visible;
}
else
{
offlineModeContainer.Visibility = ViewStates.Gone;
App.Kp2a.OfflineMode = false;
}
CheckBox cbSyncInBackground = (CheckBox)FindViewById(Resource.Id.sync_in_background)!;
cbSyncInBackground.Checked = App.Kp2a.SyncInBackgroundPreference;
UpdateInternalCacheCheckboxesVisibility();
@@ -2039,10 +2032,38 @@ namespace keepass2android
{
App.Kp2a.OfflineModePreference = App.Kp2a.OfflineMode = args.IsChecked;
};
}
private String LoadKeyProviderStringForIoc(String filename) {
CheckBox cbSyncInBackground = (CheckBox)FindViewById(Resource.Id.sync_in_background);
cbSyncInBackground.CheckedChange += (sender, args) =>
{
App.Kp2a.SyncInBackgroundPreference = args.IsChecked;
UpdateInternalCacheCheckboxesVisibility();
};
}
private void UpdateInternalCacheCheckboxesVisibility()
{
LinearLayout syncInBackgroundContainer = FindViewById<LinearLayout>(Resource.Id.sync_in_background_container)!;
LinearLayout offlineModeContainer = FindViewById<LinearLayout>(Resource.Id.work_offline_container)!;
var cachingFileStorage = App.Kp2a.GetFileStorage(_ioConnection) as CachingFileStorage;
if ((cachingFileStorage != null) && cachingFileStorage.IsCached(_ioConnection))
{
syncInBackgroundContainer.Visibility = ViewStates.Visible;
offlineModeContainer.Visibility =
App.Kp2a.SyncInBackgroundPreference ? ViewStates.Gone : ViewStates.Visible;
}
else
{
syncInBackgroundContainer.Visibility = offlineModeContainer.Visibility = ViewStates.Gone;
App.Kp2a.OfflineMode = false;
}
}
private String LoadKeyProviderStringForIoc(String filename) {
if ( _rememberKeyfile ) {
string keyfile = App.Kp2a.FileDbHelper.GetKeyFileForFile(filename);
if (String.IsNullOrEmpty(keyfile))

View File

@@ -318,6 +318,30 @@
android:text="@string/help_quickunlock"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/sync_in_background_container"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/sync_in_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/SyncOfflineCacheInBackground_title" />
<keepass2android.views.Kp2aShortHelpView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance_Help_Dense"
app:help_text="@string/SyncOfflineCacheInBackground_summary"
app:title_text="@string/SyncOfflineCacheInBackground_title"
android:text="@string/SyncOfflineCacheInBackground_summary"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/work_offline_container"
android:orientation="horizontal"
@@ -328,7 +352,7 @@
android:id="@+id/work_offline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/UseOfflineMode" />
<keepass2android.views.Kp2aShortHelpView
android:layout_width="wrap_content"
@@ -341,6 +365,7 @@
/>
</LinearLayout>
<Button
android:id="@+id/kill_app"
android:text="@string/kill_app_label"

View File

@@ -56,12 +56,16 @@ namespace keepass2android
OperationWithFinishHandler task;
OnOperationFinishedHandler onOperationFinishedHandler = new ActionOnOperationFinished(_activity, (success, message, activity) =>
{
if (!String.IsNullOrEmpty(message))
App.Kp2a.ShowMessage(activity, message, success ? MessageSeverity.Info : MessageSeverity.Error);
new Handler(Looper.MainLooper).Post(() =>
{
if (!String.IsNullOrEmpty(message))
App.Kp2a.ShowMessage(activity, message, success ? MessageSeverity.Info : MessageSeverity.Error);
// Tell the adapter to refresh it's list
BaseAdapter adapter = (activity as GroupBaseActivity)?.ListAdapter;
new Handler(Looper.MainLooper).Post(() => adapter?.NotifyDataSetChanged());
// Tell the adapter to refresh it's list
BaseAdapter adapter = (activity as GroupBaseActivity)?.ListAdapter;
adapter?.NotifyDataSetChanged();
});
if (App.Kp2a.CurrentDb?.OtpAuxFileIoc != null)
{

View File

@@ -46,7 +46,10 @@ namespace keepass2android.Utils
{
public void ShowMessage(Message message)
{
Toast.MakeText(App.Context, message.Text, ToastLength.Long).Show();
new Handler(Looper.MainLooper).Post(() =>
{
Toast.MakeText(App.Context, message.Text, ToastLength.Long).Show();
});
}
public List<Message> PendingMessages => new();

View File

@@ -798,9 +798,14 @@ namespace keepass2android
fileStorage = innerFileStorage;
}
}
if (fileStorage is IOfflineSwitchable)
if (fileStorage is IOfflineSwitchable switchable)
{
((IOfflineSwitchable)fileStorage).IsOffline = App.Kp2a.OfflineMode;
switchable.IsOffline = App.Kp2a.OfflineMode;
if (switchable.IsOffline)
{
//users of the file storage can set this to false, but the default is to show a warning:
switchable.TriggerWarningWhenFallingBackToCache = true;
}
}
return fileStorage;
}
@@ -1151,10 +1156,28 @@ namespace keepass2android
}
}
/// <summary>
/// true if the app is used in offline mode
/// </summary>
public bool OfflineMode
public bool SyncInBackgroundPreference
{
get
{
var prefs = PreferenceManager.GetDefaultSharedPreferences(LocaleManager.LocalizedAppContext);
return prefs.GetBoolean(LocaleManager.LocalizedAppContext.GetString(Resource.String.SyncOfflineCacheInBackground_key), false);
}
set
{
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(LocaleManager.LocalizedAppContext);
ISharedPreferencesEditor edit = prefs.Edit();
edit.PutBoolean(LocaleManager.LocalizedAppContext.GetString(Resource.String.SyncOfflineCacheInBackground_key), value);
edit.Commit();
}
}
/// <summary>
/// true if the app is used in offline mode
/// </summary>
public bool OfflineMode
{
get; set;
}