From b1653948eb86ddb76f48a94985d1883a00331185 Mon Sep 17 00:00:00 2001 From: Prateek Singh Date: Sat, 28 Aug 2021 02:46:47 +0530 Subject: [PATCH] Dynamic Widget Update Notification --- .../src/main/java/com/ichi2/anki/CardBrowser.java | 2 +- .../src/main/java/com/ichi2/anki/DeckPicker.java | 4 ++-- .../main/java/com/ichi2/anki/ModelBrowser.java | 2 +- .../java/com/ichi2/anki/ModelFieldEditor.java | 2 +- .../src/main/java/com/ichi2/anki/NoteEditor.java | 2 +- .../src/main/java/com/ichi2/anki/Reviewer.java | 2 +- .../java/com/ichi2/anki/StudyOptionsActivity.java | 2 +- .../com/ichi2/widget/AnkiDroidWidgetSmall.java | 15 +++++++-------- .../src/main/java/com/ichi2/widget/WidgetAlarm.kt | 4 ++-- .../main/java/com/ichi2/widget/WidgetStatus.java | 15 ++++++++------- 10 files changed, 25 insertions(+), 25 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.java b/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.java index 9481b3a29ee5..f1def20bf536 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.java +++ b/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.java @@ -899,7 +899,7 @@ protected void onStop() { // cancel rendering the question and answer, which has shared access to mCards super.onStop(); if (!isFinishing()) { - WidgetStatus.update(this); + WidgetStatus.update(this, false); UIUtils.saveCollectionInBackground(); } } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.java b/AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.java index 2757708a4d87..bf11b80b98ca 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.java +++ b/AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.java @@ -998,7 +998,7 @@ protected void onStop() { Timber.d("onStop()"); super.onStop(); if (colIsOpen()) { - WidgetStatus.update(this); + WidgetStatus.update(this, false); // Ignore the modification - a change in deck shouldn't trigger the icon for "pending changes". UIUtils.saveCollectionInBackground(true); } @@ -1972,7 +1972,7 @@ public void onPostExecute(Payload data) { supportInvalidateOptionsMenu(); updateDeckList(); - WidgetStatus.update(DeckPicker.this); + WidgetStatus.update(DeckPicker.this, false); if (mFragmented) { try { loadStudyOptionsFragment(false); diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/ModelBrowser.java b/AnkiDroid/src/main/java/com/ichi2/anki/ModelBrowser.java index 9acb691f4e61..a410dcf30018 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/ModelBrowser.java +++ b/AnkiDroid/src/main/java/com/ichi2/anki/ModelBrowser.java @@ -221,7 +221,7 @@ public boolean onOptionsItemSelected(MenuItem item) { public void onStop() { super.onStop(); if (!isFinishing()) { - WidgetStatus.update(this); + WidgetStatus.update(this, false); UIUtils.saveCollectionInBackground(); } } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.java b/AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.java index a2425af8d7a6..2899cefe14a7 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.java +++ b/AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.java @@ -105,7 +105,7 @@ protected void onCreate(Bundle savedInstanceState) { protected void onStop() { super.onStop(); if (!isFinishing()) { - WidgetStatus.update(this); + WidgetStatus.update(this, false); UIUtils.saveCollectionInBackground(); } } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.java b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.java index 7b4fb34c481a..4553ec2a6dc1 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.java +++ b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.java @@ -716,7 +716,7 @@ private void modifyCurrentSelection(Toolbar.TextFormatter formatter, FieldEditTe protected void onStop() { super.onStop(); if (!isFinishing()) { - WidgetStatus.update(this); + WidgetStatus.update(this, false); UIUtils.saveCollectionInBackground(); } } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.java b/AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.java index 1d62199e6150..70a837d0198c 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.java +++ b/AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.java @@ -1095,7 +1095,7 @@ protected void onStop() { super.onStop(); if (!isFinishing() && colIsOpen() && mSched != null) { - WidgetStatus.update(this); + WidgetStatus.update(this, false); } UIUtils.saveCollectionInBackground(); } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/StudyOptionsActivity.java b/AnkiDroid/src/main/java/com/ichi2/anki/StudyOptionsActivity.java index 0836b3955875..228b8828cc27 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/StudyOptionsActivity.java +++ b/AnkiDroid/src/main/java/com/ichi2/anki/StudyOptionsActivity.java @@ -112,7 +112,7 @@ public void onBackPressed() { public void onStop() { super.onStop(); if (colIsOpen()) { - WidgetStatus.update(this); + WidgetStatus.update(this, false); UIUtils.saveCollectionInBackground(); } } diff --git a/AnkiDroid/src/main/java/com/ichi2/widget/AnkiDroidWidgetSmall.java b/AnkiDroid/src/main/java/com/ichi2/widget/AnkiDroidWidgetSmall.java index 6ce867e86a7b..62d1f6d1e0b2 100644 --- a/AnkiDroid/src/main/java/com/ichi2/widget/AnkiDroidWidgetSmall.java +++ b/AnkiDroid/src/main/java/com/ichi2/widget/AnkiDroidWidgetSmall.java @@ -51,7 +51,7 @@ public class AnkiDroidWidgetSmall extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Timber.d("SmallWidget: onUpdate"); - WidgetStatus.update(context); + WidgetStatus.update(context, true); } @@ -123,11 +123,10 @@ public static class UpdateService extends Service { /** The cached number of total due cards. */ private int mDueCardsCount; - - public void doUpdate(Context context) { + public void doUpdate(Context context, boolean showUpdateNotification) { Timber.d("Updating widget"); AppWidgetManager.getInstance(context) - .updateAppWidget(new ComponentName(context, AnkiDroidWidgetSmall.class), buildUpdate(context, true)); + .updateAppWidget(new ComponentName(context, AnkiDroidWidgetSmall.class), buildUpdate(context, true, showUpdateNotification)); } @Override @@ -135,7 +134,7 @@ public void doUpdate(Context context) { public void onStart(Intent intent, int startId) { Timber.i("SmallWidget: OnStart"); - RemoteViews updateViews = buildUpdate(this, true); + RemoteViews updateViews = buildUpdate(this, true, true); ComponentName thisWidget = new ComponentName(this, AnkiDroidWidgetSmall.class); AppWidgetManager manager = AppWidgetManager.getInstance(this); @@ -143,7 +142,7 @@ public void onStart(Intent intent, int startId) { } - private RemoteViews buildUpdate(Context context, boolean updateDueDecksNow) { + private RemoteViews buildUpdate(Context context, boolean updateDueDecksNow, boolean showUpdateNotification) { Timber.d("buildUpdate"); RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_small); @@ -162,7 +161,7 @@ public void onReceive(Context context, Intent intent) { if (action != null && action.equals(Intent.ACTION_MEDIA_MOUNTED)) { Timber.d("mMountReceiver - Action = Media Mounted"); if (remounted) { - WidgetStatus.update(getBaseContext()); + WidgetStatus.update(getBaseContext(), true); remounted = false; if (mMountReceiver != null) { AnkiDroidApp.getInstance().unregisterReceiver(mMountReceiver); @@ -198,7 +197,7 @@ public void onReceive(Context context, Intent intent) { SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(context); int lastUpdatedCard = preferences.getInt("widgetCard", 0); // If their is any change in card the show the notification. - if (lastUpdatedCard != mDueCardsCount) { + if (showUpdateNotification && lastUpdatedCard != mDueCardsCount) { Intent intent = new Intent(NotificationService.INTENT_ACTION); Context appContext = context.getApplicationContext(); intent.putExtra(NotificationService.CARD_NOTIFICATION_TYPE, NotificationService.CARD_NOTIFICATION_WIDGET); diff --git a/AnkiDroid/src/main/java/com/ichi2/widget/WidgetAlarm.kt b/AnkiDroid/src/main/java/com/ichi2/widget/WidgetAlarm.kt index 662d3866dea3..61cf18dccac6 100644 --- a/AnkiDroid/src/main/java/com/ichi2/widget/WidgetAlarm.kt +++ b/AnkiDroid/src/main/java/com/ichi2/widget/WidgetAlarm.kt @@ -34,8 +34,8 @@ class WidgetAlarm() : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { Timber.d("Widget Alarm BRODCAST RECIVED") - // Update the widget. - AnkiDroidWidgetSmall.UpdateService().doUpdate(context) + // Update the widget and show the notification if their is any change. + AnkiDroidWidgetSmall.UpdateService().doUpdate(context, true) } /** diff --git a/AnkiDroid/src/main/java/com/ichi2/widget/WidgetStatus.java b/AnkiDroid/src/main/java/com/ichi2/widget/WidgetStatus.java index 4b057a204cb0..501112118732 100644 --- a/AnkiDroid/src/main/java/com/ichi2/widget/WidgetStatus.java +++ b/AnkiDroid/src/main/java/com/ichi2/widget/WidgetStatus.java @@ -34,7 +34,7 @@ public final class WidgetStatus { private static boolean sSmallWidgetEnabled = false; @SuppressWarnings("deprecation") // #7108: AsyncTask - private static android.os.AsyncTask sUpdateDeckStatusAsyncTask; + private static android.os.AsyncTask sUpdateDeckStatusAsyncTask; /** This class should not be instantiated. */ @@ -46,14 +46,14 @@ private WidgetStatus() { * Request the widget to update its status. */ @SuppressWarnings("deprecation") // #7108: AsyncTask - public static void update(Context context) { + public static void update(Context context, boolean showUpdateNotification) { SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(context); sSmallWidgetEnabled = preferences.getBoolean("widgetSmallEnabled", false); boolean canExecuteTask = ((sUpdateDeckStatusAsyncTask == null) || (sUpdateDeckStatusAsyncTask.getStatus() == android.os.AsyncTask.Status.FINISHED)); if (sSmallWidgetEnabled && canExecuteTask) { Timber.d("WidgetStatus.update(): updating"); sUpdateDeckStatusAsyncTask = new UpdateDeckStatusAsyncTask(); - sUpdateDeckStatusAsyncTask.execute(context); + sUpdateDeckStatusAsyncTask.execute(context, showUpdateNotification); } else { Timber.d("WidgetStatus.update(): already running or not enabled"); } @@ -67,17 +67,18 @@ public static int[] fetchSmall(Context context) { return new int[] {pair.first, pair.second}; } - private static class UpdateDeckStatusAsyncTask extends BaseAsyncTask { + private static class UpdateDeckStatusAsyncTask extends BaseAsyncTask { @Override - protected Context doInBackground(Context... params) { + protected Context doInBackground(Object... params) { super.doInBackground(params); Timber.d("WidgetStatus.UpdateDeckStatusAsyncTask.doInBackground()"); - Context context = params[0]; + Context context = (Context) params[0]; + Boolean showUpdateNotification = (Boolean) params[1]; if (!AnkiDroidApp.isSdCardMounted() && sSmallWidgetEnabled) { return context; } - new AnkiDroidWidgetSmall.UpdateService().doUpdate(context); + new AnkiDroidWidgetSmall.UpdateService().doUpdate(context, showUpdateNotification); return context; }