diff --git a/app/src/main/java/eu/faircode/email/ActivityWidgetUnified.java b/app/src/main/java/eu/faircode/email/ActivityWidgetUnified.java index dd85e9ecba..af85a5aa19 100644 --- a/app/src/main/java/eu/faircode/email/ActivityWidgetUnified.java +++ b/app/src/main/java/eu/faircode/email/ActivityWidgetUnified.java @@ -60,6 +60,7 @@ public class ActivityWidgetUnified extends ActivityBase { private ViewButtonColor btnColor; private Spinner spFontSize; private Spinner spPadding; + private CheckBox cbRefresh; private CheckBox cbCompose; private Button btnSave; private ContentLoadingProgressBar pbWait; @@ -92,6 +93,7 @@ public class ActivityWidgetUnified extends ActivityBase { int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); int font = prefs.getInt("widget." + appWidgetId + ".font", 0); int padding = prefs.getInt("widget." + appWidgetId + ".padding", 0); + boolean refresh = prefs.getBoolean("widget." + appWidgetId + ".refresh", false); boolean compose = prefs.getBoolean("widget." + appWidgetId + ".compose", false); getSupportActionBar().setSubtitle(R.string.title_widget_title_list); @@ -105,6 +107,7 @@ public class ActivityWidgetUnified extends ActivityBase { btnColor = findViewById(R.id.btnColor); spFontSize = findViewById(R.id.spFontSize); spPadding = findViewById(R.id.spPadding); + cbRefresh = findViewById(R.id.cbRefresh); cbCompose = findViewById(R.id.cbCompose); btnSave = findViewById(R.id.btnSave); pbWait = findViewById(R.id.pbWait); @@ -173,6 +176,7 @@ public class ActivityWidgetUnified extends ActivityBase { editor.putInt("widget." + appWidgetId + ".background", btnColor.getColor()); editor.putInt("widget." + appWidgetId + ".font", tinyOut(font)); editor.putInt("widget." + appWidgetId + ".padding", tinyOut(padding)); + editor.putBoolean("widget." + appWidgetId + ".refresh", cbRefresh.isChecked()); editor.putBoolean("widget." + appWidgetId + ".compose", cbCompose.isChecked()); editor.putInt("widget." + appWidgetId + ".version", BuildConfig.VERSION_CODE); @@ -291,6 +295,7 @@ public class ActivityWidgetUnified extends ActivityBase { btnColor.setColor(background); spFontSize.setSelection(tinyIn(font)); spPadding.setSelection(tinyIn(padding)); + cbRefresh.setChecked(refresh); cbCompose.setChecked(compose); grpReady.setVisibility(View.GONE); diff --git a/app/src/main/java/eu/faircode/email/ServiceUI.java b/app/src/main/java/eu/faircode/email/ServiceUI.java index 90fcfe060a..48d56a63fe 100644 --- a/app/src/main/java/eu/faircode/email/ServiceUI.java +++ b/app/src/main/java/eu/faircode/email/ServiceUI.java @@ -25,6 +25,8 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.view.Gravity; +import android.widget.Toast; import androidx.annotation.Nullable; import androidx.core.app.RemoteInput; @@ -35,6 +37,7 @@ import org.json.JSONException; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.HashMap; @@ -58,6 +61,8 @@ public class ServiceUI extends IntentService { static final int PI_IGNORED = 11; static final int PI_THREAD = 12; + private static final long WIDGET_SYNC_DURATION = 1500L; + public ServiceUI() { this(ServiceUI.class.getName()); } @@ -158,7 +163,11 @@ public class ServiceUI extends IntentService { break; case "sync": - onSync(id); + onSync(id, -1L); + break; + + case "widget": + onWidget(intent, (int) id); break; case "exists": @@ -456,14 +465,18 @@ public class ServiceUI extends IntentService { } } - private void onSync(long aid) { + private void onSync(long aid, long fid) { DB db = DB.getInstance(this); try { db.beginTransaction(); List accounts = db.account().getPollAccounts(aid < 0 ? null : aid); for (EntityAccount account : accounts) { - List folders = db.folder().getSynchronizingFolders(account.id); + List folders; + if (fid < 0) + folders = db.folder().getSynchronizingFolders(account.id); + else + folders = Arrays.asList(db.folder().getFolder(fid)); if (folders.size() > 0) Collections.sort(folders, folders.get(0).getComparator(this)); for (EntityFolder folder : folders) @@ -476,6 +489,29 @@ public class ServiceUI extends IntentService { } } + private void onWidget(Intent intent, int appWidgetId) { + long aid = intent.getLongExtra("account", -1L); + long fid = intent.getLongExtra("folder", -1L); + onSync(aid, fid); + + 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) { try { Intent sync = new Intent(context, ServiceUI.class) diff --git a/app/src/main/java/eu/faircode/email/WidgetUnified.java b/app/src/main/java/eu/faircode/email/WidgetUnified.java index 2720f69c79..ba56e22c69 100644 --- a/app/src/main/java/eu/faircode/email/WidgetUnified.java +++ b/app/src/main/java/eu/faircode/email/WidgetUnified.java @@ -35,6 +35,8 @@ import android.widget.RemoteViews; import androidx.core.graphics.ColorUtils; import androidx.preference.PreferenceManager; +import java.util.Date; + public class WidgetUnified extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { @@ -49,6 +51,7 @@ public class WidgetUnified extends AppWidgetProvider { int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); int font = prefs.getInt("widget." + appWidgetId + ".font", 0); int padding = prefs.getInt("widget." + appWidgetId + ".padding", 0); + boolean refresh = prefs.getBoolean("widget." + appWidgetId + ".refresh", false); boolean compose = prefs.getBoolean("widget." + appWidgetId + ".compose", false); int version = prefs.getInt("widget." + appWidgetId + ".version", 0); @@ -69,6 +72,13 @@ public class WidgetUnified extends AppWidgetProvider { PendingIntent pi = PendingIntentCompat.getActivity( context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT); + Intent sync = new Intent(context, ServiceUI.class); + sync.setAction("widget:" + appWidgetId); + sync.putExtra("account", account); + sync.putExtra("folder", folder); + PendingIntent piSync = PendingIntentCompat.getService( + context, appWidgetId, sync, PendingIntent.FLAG_UPDATE_CURRENT); + Intent edit = new Intent(context, ActivityCompose.class); edit.setAction("widget:" + appWidgetId); edit.putExtra("action", "new"); @@ -91,6 +101,15 @@ public class WidgetUnified extends AppWidgetProvider { views.setOnClickPendingIntent(R.id.title, pi); + views.setViewVisibility(R.id.refresh, refresh ? View.VISIBLE : View.GONE); + views.setViewPadding(R.id.refresh, px, px, px, px); + views.setOnClickPendingIntent(R.id.refresh, piSync); + + long refreshing = prefs.getLong("widget." + appWidgetId + ".sync", 0L); + views.setImageViewResource(R.id.refresh, + refreshing < new Date().getTime() + ? R.drawable.twotone_sync_24 : R.drawable.twotone_compare_arrows_24); + views.setViewVisibility(R.id.compose, compose ? View.VISIBLE : View.GONE); views.setViewPadding(R.id.compose, px, px, px, px); views.setOnClickPendingIntent(R.id.compose, piCompose); diff --git a/app/src/main/res/layout/activity_widget_unified.xml b/app/src/main/res/layout/activity_widget_unified.xml index 586da90620..55e3952383 100644 --- a/app/src/main/res/layout/activity_widget_unified.xml +++ b/app/src/main/res/layout/activity_widget_unified.xml @@ -121,6 +121,16 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tvPadding" /> + + + app:layout_constraintTop_toBottomOf="@id/cbRefresh" />