Trigger on preference changed

This commit is contained in:
M66B 2020-10-25 18:21:20 +01:00
parent efe2fec79d
commit e3ebecaf48
6 changed files with 41 additions and 37 deletions

View File

@ -34,24 +34,14 @@ import android.webkit.CookieManager;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
public class ApplicationEx extends Application implements SharedPreferences.OnSharedPreferenceChangeListener { public class ApplicationEx extends Application implements SharedPreferences.OnSharedPreferenceChangeListener {
private Thread.UncaughtExceptionHandler prev = null; private Thread.UncaughtExceptionHandler prev = null;
private static final List<String> OPTIONS_RESTART = Collections.unmodifiableList(Arrays.asList(
"secure", // privacy
"shortcuts", // misc
"language", // misc
"query_threads" // misc
));
@Override @Override
protected void attachBaseContext(Context base) { protected void attachBaseContext(Context base) {
super.attachBaseContext(getLocalizedContext(base)); super.attachBaseContext(getLocalizedContext(base));
@ -157,8 +147,35 @@ public class ApplicationEx extends Application implements SharedPreferences.OnSh
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (OPTIONS_RESTART.contains(key)) switch (key) {
case "enabled":
ServiceSynchronize.reschedule(this);
WorkerCleanup.init(this);
WorkerWatchdog.init(this);
break;
case "poll_interval":
case "schedule":
case "schedule_start":
case "schedule_end":
case "schedule_day0":
case "schedule_day1":
case "schedule_day2":
case "schedule_day3":
case "schedule_day4":
case "schedule_day5":
case "schedule_day6":
ServiceSynchronize.reschedule(this);
break;
case "watchdog":
WorkerWatchdog.init(this);
break;
case "secure": // privacy
case "shortcuts": // misc
case "language": // misc
case "query_threads": // misc
restart(); restart();
break;
}
} }
void restart() { void restart() {

View File

@ -257,7 +257,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("watchdog", checked).apply(); prefs.edit().putBoolean("watchdog", checked).apply();
WorkerWatchdog.init(getContext());
} }
}); });
@ -371,7 +370,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("auth_plain", checked).apply(); prefs.edit().putBoolean("auth_plain", checked).apply();
ServiceSynchronize.reload(getContext(), -1L, false, "auth_plain=" + checked);
} }
}); });
@ -379,7 +377,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("auth_login", checked).apply(); prefs.edit().putBoolean("auth_login", checked).apply();
ServiceSynchronize.reload(getContext(), -1L, false, "auth_login=" + checked);
} }
}); });
@ -387,7 +384,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("auth_sasl", checked).apply(); prefs.edit().putBoolean("auth_sasl", checked).apply();
ServiceSynchronize.reload(getContext(), -1L, false, "auth_sasl=" + checked);
} }
}); });

View File

@ -146,9 +146,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("enabled", checked).apply(); prefs.edit().putBoolean("enabled", checked).apply();
ServiceSynchronize.reschedule(getContext());
WorkerCleanup.init(getContext());
WorkerWatchdog.init(getContext());
} }
}); });
@ -178,7 +175,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
adapterView.setTag(value); adapterView.setTag(value);
prefs.edit().putInt("poll_interval", value).apply(); prefs.edit().putInt("poll_interval", value).apply();
grpExempted.setVisibility(value == 0 ? View.GONE : View.VISIBLE); grpExempted.setVisibility(value == 0 ? View.GONE : View.VISIBLE);
ServiceSynchronize.reschedule(getContext());
} }
} }
@ -187,7 +183,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
adapterView.setTag(null); adapterView.setTag(null);
prefs.edit().remove("poll_interval").apply(); prefs.edit().remove("poll_interval").apply();
grpExempted.setVisibility(View.GONE); grpExempted.setVisibility(View.GONE);
ServiceSynchronize.reschedule(getContext());
} }
}); });
@ -202,7 +197,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("schedule", checked).apply(); prefs.edit().putBoolean("schedule", checked).apply();
ServiceSynchronize.reschedule(getContext());
} }
}); });
@ -238,7 +232,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
prefs.edit().putBoolean("schedule_day" + day, isChecked).apply(); prefs.edit().putBoolean("schedule_day" + day, isChecked).apply();
ServiceSynchronize.reschedule(getContext());
} }
}); });
} }
@ -247,7 +240,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_nodate", checked).apply(); prefs.edit().putBoolean("sync_nodate", checked).apply();
ServiceSynchronize.reload(getContext(), null, false, "sync_nodate=" + checked);
} }
}); });
@ -255,7 +247,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_unseen", checked).apply(); prefs.edit().putBoolean("sync_unseen", checked).apply();
ServiceSynchronize.reload(getContext(), null, false, "sync_unseen=" + checked);
} }
}); });
@ -263,7 +254,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_flagged", checked).apply(); prefs.edit().putBoolean("sync_flagged", checked).apply();
ServiceSynchronize.reload(getContext(), null, false, "sync_flagged=" + checked);
} }
}); });
@ -271,7 +261,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("delete_unseen", checked).apply(); prefs.edit().putBoolean("delete_unseen", checked).apply();
ServiceSynchronize.reload(getContext(), null, false, "delete_unseen=" + checked);
} }
}); });
@ -279,7 +268,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_kept", checked).apply(); prefs.edit().putBoolean("sync_kept", checked).apply();
ServiceSynchronize.reload(getContext(), null, false, "sync_kept=" + checked);
} }
}); });
@ -295,7 +283,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_folders", checked).apply(); prefs.edit().putBoolean("sync_folders", checked).apply();
swSyncSharedFolders.setEnabled(checked); swSyncSharedFolders.setEnabled(checked);
ServiceSynchronize.reload(getContext(), null, false, "sync_folders=" + checked);
} }
}); });
@ -303,7 +290,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("sync_shared_folders", checked).apply(); prefs.edit().putBoolean("sync_shared_folders", checked).apply();
ServiceSynchronize.reload(getContext(), null, false, "sync_shared_folders=" + checked);
} }
}); });
@ -468,8 +454,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
editor.putInt("schedule_" + (start ? "start" : "end"), hour * 60 + minute); editor.putInt("schedule_" + (start ? "start" : "end"), hour * 60 + minute);
editor.putBoolean("schedule", true); editor.putBoolean("schedule", true);
editor.apply(); editor.apply();
ServiceSynchronize.reschedule(getContext());
} }
} }

View File

@ -173,7 +173,6 @@ public class ServiceExternal extends Service {
if (accountName == null) { if (accountName == null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putBoolean("enabled", enabled).apply(); prefs.edit().putBoolean("enabled", enabled).apply();
ServiceSynchronize.eval(context, "external enabled=" + enabled);
} else { } else {
EntityAccount account = db.account().getAccount(accountName); EntityAccount account = db.account().getAccount(accountName);
if (account == null) if (account == null)

View File

@ -127,9 +127,19 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
)); ));
private static final List<String> PREF_RELOAD = Collections.unmodifiableList(Arrays.asList( private static final List<String> PREF_RELOAD = Collections.unmodifiableList(Arrays.asList(
"sync_nodate",
"sync_unseen",
"sync_flagged",
"delete_unseen",
"sync_kept",
"sync_folders",
"sync_shared_folders",
"ssl_harden", // force reconnect "ssl_harden", // force reconnect
"badge", "unseen_ignored", // force update badge/widget "badge", "unseen_ignored", // force update badge/widget
"protocol", "debug" // force reconnect "protocol", "debug", // force reconnect
"auth_plain",
"auth_login",
"auth_sasl"
)); ));
static final int PI_ALARM = 1; static final int PI_ALARM = 1;
@ -1797,7 +1807,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
int pollInterval = prefs.getInt("poll_interval", DEFAULT_POLL_INTERVAL); int pollInterval = prefs.getInt("poll_interval", DEFAULT_POLL_INTERVAL);
EntityLog.log(context, "Auto optimize account=" + account.name + " poll interval=" + pollInterval); EntityLog.log(context, "Auto optimize account=" + account.name + " poll interval=" + pollInterval);
if (pollInterval == 0) { if (pollInterval == 0) {
prefs.edit().putInt("poll_interval", OPTIMIZE_POLL_INTERVAL).apply();
try { try {
db.beginTransaction(); db.beginTransaction();
for (EntityAccount a : db.account().getAccounts()) for (EntityAccount a : db.account().getAccounts())
@ -1806,7 +1815,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} finally { } finally {
db.endTransaction(); db.endTransaction();
} }
ServiceSynchronize.reschedule(ServiceSynchronize.this); prefs.edit().putInt("poll_interval", OPTIMIZE_POLL_INTERVAL).apply();
} else if (pollInterval <= 60 && account.poll_exempted) { } else if (pollInterval <= 60 && account.poll_exempted) {
db.account().setAccountPollExempted(account.id, false); db.account().setAccountPollExempted(account.id, false);
ServiceSynchronize.eval(ServiceSynchronize.this, "Optimize=" + reason); ServiceSynchronize.eval(ServiceSynchronize.this, "Optimize=" + reason);

View File

@ -83,6 +83,5 @@ public class ServiceTileSynchronize extends TileService implements SharedPrefere
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean enabled = !prefs.getBoolean("enabled", true); boolean enabled = !prefs.getBoolean("enabled", true);
prefs.edit().putBoolean("enabled", enabled).apply(); prefs.edit().putBoolean("enabled", enabled).apply();
ServiceSynchronize.eval(this, "tile=" + enabled);
} }
} }