Async widget sync state update

pull/205/head
M66B 3 years ago
parent b88ef51be8
commit 766b92438d

@ -658,29 +658,21 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
prefs.getBoolean(_key, false)) { prefs.getBoolean(_key, false)) {
int appWidgetId = Integer.parseInt(_key.split("\\.")[1]); int appWidgetId = Integer.parseInt(_key.split("\\.")[1]);
String key = "widget." + appWidgetId + ".sync";
boolean sync = prefs.contains(key);
if (!sync)
continue;
long account = prefs.getLong("widget." + appWidgetId + ".account", -1L); long account = prefs.getLong("widget." + appWidgetId + ".account", -1L);
long folder = prefs.getLong("widget." + appWidgetId + ".folder", -1L); long folder = prefs.getLong("widget." + appWidgetId + ".folder", -1L);
if (folder > 0) { boolean state;
if (!folders.contains(folder)) { if (folder > 0)
prefs.edit().remove(key).apply(); state = folders.contains(folder);
WidgetUnified.init(ServiceSynchronize.this, appWidgetId); else if (account > 0)
} state = accounts.contains(account);
} else if (account > 0) { else
if (!accounts.contains(account)) { state = (accounts.size() > 0);
prefs.edit().remove(key).apply();
WidgetUnified.init(ServiceSynchronize.this, appWidgetId); String key = "widget." + appWidgetId + ".syncing";
} if (state != prefs.getBoolean(key, false)) {
} else { prefs.edit().putBoolean(key, state).apply();
if (accounts.size() == 0) { WidgetUnified.init(ServiceSynchronize.this, appWidgetId);
prefs.edit().remove(key).apply();
WidgetUnified.init(ServiceSynchronize.this, appWidgetId);
}
} }
} }
} }

@ -58,8 +58,6 @@ public class ServiceUI extends IntentService {
static final int PI_SNOOZE = 10; static final int PI_SNOOZE = 10;
static final int PI_IGNORED = 11; static final int PI_IGNORED = 11;
private static final long WIDGET_SYNC_DURATION = 90 * 1000L;
public ServiceUI() { public ServiceUI() {
this(ServiceUI.class.getName()); this(ServiceUI.class.getName());
} }
@ -491,23 +489,6 @@ public class ServiceUI extends IntentService {
long aid = intent.getLongExtra("account", -1L); long aid = intent.getLongExtra("account", -1L);
long fid = intent.getLongExtra("folder", -1L); long fid = intent.getLongExtra("folder", -1L);
onSync(aid, fid, fid < 0); onSync(aid, fid, fid < 0);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String key = "widget." + appWidgetId + ".sync";
prefs.edit().putLong(key, new Date().getTime() + WIDGET_SYNC_DURATION).apply();
WidgetUnified.init(this, appWidgetId);
ApplicationEx.getMainHandler().postDelayed(new Runnable() {
@Override
public void run() {
try {
prefs.edit().remove(key).apply();
WidgetUnified.init(ServiceUI.this, appWidgetId);
} catch (Throwable ex) {
Log.e(ex);
}
}
}, WIDGET_SYNC_DURATION);
} }
static void sync(Context context, Long account) { static void sync(Context context, Long account) {

@ -105,9 +105,8 @@ public class WidgetUnified extends AppWidgetProvider {
views.setViewPadding(R.id.refresh, px, px, px, px); views.setViewPadding(R.id.refresh, px, px, px, px);
views.setOnClickPendingIntent(R.id.refresh, piSync); views.setOnClickPendingIntent(R.id.refresh, piSync);
long now = new Date().getTime(); boolean syncing = prefs.getBoolean("widget." + appWidgetId + ".syncing", false);
long refreshing = prefs.getLong("widget." + appWidgetId + ".sync", 0L); views.setImageViewResource(R.id.refresh, syncing ? R.drawable.twotone_compare_arrows_24 : R.drawable.twotone_sync_24);
views.setImageViewResource(R.id.refresh, refreshing < now ? R.drawable.twotone_sync_24 : R.drawable.twotone_compare_arrows_24);
views.setViewVisibility(R.id.refresh, refresh ? View.VISIBLE : View.INVISIBLE); views.setViewVisibility(R.id.refresh, refresh ? View.VISIBLE : View.INVISIBLE);
views.setViewVisibility(R.id.compose, compose ? View.VISIBLE : View.GONE); views.setViewVisibility(R.id.compose, compose ? View.VISIBLE : View.GONE);

Loading…
Cancel
Save