Batch update widgets

This commit is contained in:
M66B 2020-04-25 08:51:50 +02:00
parent 2a728930bf
commit 7fecf52bb9
1 changed files with 35 additions and 10 deletions

View File

@ -94,6 +94,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
private long lastLost = 0;
private int lastAccounts = 0;
private int lastOperations = 0;
private Handler handler;
private Map<Long, Core.State> coreStates = new Hashtable<>();
private MutableLiveData<ConnectionHelper.NetworkState> liveNetworkState = new MutableLiveData<>();
@ -113,6 +114,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
private static final int ACCOUNT_ERROR_AFTER = 60; // minutes
private static final int ACCOUNT_ERROR_AFTER_POLL = 3; // times
private static final int BACKOFF_ERROR_AFTER = 16; // seconds
private static final long WIDGET_UPDATE_DELAY = 2500L; // milliseconds
private static final List<String> PREF_EVAL = Collections.unmodifiableList(Arrays.asList(
"enabled", "poll_interval" // restart account(s)
@ -136,6 +138,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
super.onCreate();
startForeground(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(null, null).build());
handler = new Handler();
// Listen for network changes
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
@ -490,6 +494,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
});
db.message().liveWidgetUnseen(null).observe(cowner, new Observer<List<TupleMessageStats>>() {
private Integer lastCount = null;
private List<TupleMessageStats> last = null;
@Override
@ -512,7 +517,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
last = stats;
Widget.update(ServiceSynchronize.this);
handler.removeCallbacks(refreshWidget);
handler.postDelayed(refreshWidget, WIDGET_UPDATE_DELAY);
boolean badge = prefs.getBoolean("badge", true);
boolean unseen_ignored = prefs.getBoolean("unseen_ignored", false);
@ -524,13 +530,16 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
count += unseen;
}
try {
if (count == 0 || !badge)
ShortcutBadger.removeCount(ServiceSynchronize.this);
else
ShortcutBadger.applyCount(ServiceSynchronize.this, count);
} catch (Throwable ex) {
Log.e(ex);
if (lastCount == null || !lastCount.equals(count)) {
lastCount = count;
try {
if (count == 0 || !badge)
ShortcutBadger.removeCount(ServiceSynchronize.this);
else
ShortcutBadger.applyCount(ServiceSynchronize.this, count);
} catch (Throwable ex) {
Log.e(ex);
}
}
}
});
@ -553,8 +562,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
break;
}
if (changed)
WidgetUnified.updateData(ServiceSynchronize.this);
if (changed) {
handler.removeCallbacks(refreshWidgetUnified);
handler.postDelayed(refreshWidgetUnified, WIDGET_UPDATE_DELAY);
}
last = current;
}
@ -563,6 +574,20 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
prefs.registerOnSharedPreferenceChangeListener(this);
}
private final Runnable refreshWidget = new Runnable() {
@Override
public void run() {
Widget.update(ServiceSynchronize.this);
}
};
private final Runnable refreshWidgetUnified = new Runnable() {
@Override
public void run() {
WidgetUnified.updateData(ServiceSynchronize.this);
}
};
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (PREF_EVAL.contains(key)) {