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 java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class ApplicationEx extends Application implements SharedPreferences.OnSharedPreferenceChangeListener {
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
protected void attachBaseContext(Context base) {
super.attachBaseContext(getLocalizedContext(base));
@ -157,8 +147,35 @@ public class ApplicationEx extends Application implements SharedPreferences.OnSh
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (OPTIONS_RESTART.contains(key))
restart();
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();
break;
}
}
void restart() {

View File

@ -257,7 +257,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("watchdog", checked).apply();
WorkerWatchdog.init(getContext());
}
});
@ -371,7 +370,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
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
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
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
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
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
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
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);
prefs.edit().putInt("poll_interval", value).apply();
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);
prefs.edit().remove("poll_interval").apply();
grpExempted.setVisibility(View.GONE);
ServiceSynchronize.reschedule(getContext());
}
});
@ -202,7 +197,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("schedule", checked).apply();
ServiceSynchronize.reschedule(getContext());
}
});
@ -238,7 +232,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
prefs.edit().putBoolean("schedule_day" + day, isChecked).apply();
ServiceSynchronize.reschedule(getContext());
}
});
}
@ -247,7 +240,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
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
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
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
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
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
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
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
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
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) {
prefs.edit().putBoolean("sync_folders", checked).apply();
swSyncSharedFolders.setEnabled(checked);
ServiceSynchronize.reload(getContext(), null, false, "sync_folders=" + checked);
}
});
@ -303,7 +290,6 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
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.putBoolean("schedule", true);
editor.apply();
ServiceSynchronize.reschedule(getContext());
}
}

View File

@ -173,7 +173,6 @@ public class ServiceExternal extends Service {
if (accountName == null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putBoolean("enabled", enabled).apply();
ServiceSynchronize.eval(context, "external enabled=" + enabled);
} else {
EntityAccount account = db.account().getAccount(accountName);
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(
"sync_nodate",
"sync_unseen",
"sync_flagged",
"delete_unseen",
"sync_kept",
"sync_folders",
"sync_shared_folders",
"ssl_harden", // force reconnect
"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;
@ -1797,7 +1807,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
int pollInterval = prefs.getInt("poll_interval", DEFAULT_POLL_INTERVAL);
EntityLog.log(context, "Auto optimize account=" + account.name + " poll interval=" + pollInterval);
if (pollInterval == 0) {
prefs.edit().putInt("poll_interval", OPTIMIZE_POLL_INTERVAL).apply();
try {
db.beginTransaction();
for (EntityAccount a : db.account().getAccounts())
@ -1806,7 +1815,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} finally {
db.endTransaction();
}
ServiceSynchronize.reschedule(ServiceSynchronize.this);
prefs.edit().putInt("poll_interval", OPTIMIZE_POLL_INTERVAL).apply();
} else if (pollInterval <= 60 && account.poll_exempted) {
db.account().setAccountPollExempted(account.id, false);
ServiceSynchronize.eval(ServiceSynchronize.this, "Optimize=" + reason);

View File

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