Skip to content

Commit

Permalink
Dynamic Widget Update Notification
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek-singh-3212 committed Aug 27, 2021
1 parent b9531d5 commit b165394
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
4 changes: 2 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/ModelBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ protected void onStop() {
super.onStop();

if (!isFinishing() && colIsOpen() && mSched != null) {
WidgetStatus.update(this);
WidgetStatus.update(this, false);
}
UIUtils.saveCollectionInBackground();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void onBackPressed() {
public void onStop() {
super.onStop();
if (colIsOpen()) {
WidgetStatus.update(this);
WidgetStatus.update(this, false);
UIUtils.saveCollectionInBackground();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down Expand Up @@ -123,27 +123,26 @@ 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
@Deprecated
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);
manager.updateAppWidget(thisWidget, updateViews);
}


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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/widget/WidgetAlarm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
15 changes: 8 additions & 7 deletions AnkiDroid/src/main/java/com/ichi2/widget/WidgetStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class WidgetStatus {

private static boolean sSmallWidgetEnabled = false;
@SuppressWarnings("deprecation") // #7108: AsyncTask
private static android.os.AsyncTask<Context, Void, Context> sUpdateDeckStatusAsyncTask;
private static android.os.AsyncTask<Object, Void, Context> sUpdateDeckStatusAsyncTask;


/** This class should not be instantiated. */
Expand All @@ -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");
}
Expand All @@ -67,17 +67,18 @@ public static int[] fetchSmall(Context context) {
return new int[] {pair.first, pair.second};
}

private static class UpdateDeckStatusAsyncTask extends BaseAsyncTask<Context, Void, Context> {
private static class UpdateDeckStatusAsyncTask extends BaseAsyncTask<Object, Void, Context> {

@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;
}
Expand Down

0 comments on commit b165394

Please sign in to comment.