From 40a6fdc667675b6f5138d7f069d7cc3dd2605b2f Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Fri, 3 Mar 2023 19:12:40 +0600 Subject: [PATCH] Fix widgets are not clearing on account remove --- .../notes/persistence/NotesRepository.java | 1 + .../notelist/NoteListWidgetFactory.java | 13 ++++++++++ .../widget/singlenote/SingleNoteWidget.java | 26 +++++++++---------- .../singlenote/SingleNoteWidgetFactory.java | 1 + 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java index 9770197b9..65b32d3f1 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java @@ -227,6 +227,7 @@ public class NotesRepository { } db.getAccountDao().deleteAccount(account); + notifyWidgets(); } public Account getAccountByName(String accountName) { diff --git a/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.java b/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.java index a17124a8a..9f30e3589 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.java @@ -58,6 +58,11 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact dbNotes.clear(); try { data = repo.getNoteListWidgetData(appWidgetId); + + if (data == null) { + return; + } + Log.v(TAG, "--- data - " + data); switch (data.getMode()) { case MODE_DISPLAY_ALL: @@ -87,11 +92,19 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact @Override public int getCount() { + if (data == null) { + return 0; + } + return dbNotes.size() + 1; } @Override public RemoteViews getViewAt(int position) { + if (data == null) { + return null; + } + final RemoteViews note_content; if (position == 0) { diff --git a/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidget.java b/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidget.java index 52131ced8..20df222bb 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidget.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidget.java @@ -34,22 +34,22 @@ public class SingleNoteWidget extends AppWidgetProvider { final var data = repo.getSingleNoteWidgetData(appWidgetId); if (data != null) { templateIntent.putExtra(BaseNoteFragment.PARAM_ACCOUNT_ID, data.getAccountId()); - - final var serviceIntent = new Intent(context, SingleNoteWidgetService.class); - serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); - serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME))); - - final var views = new RemoteViews(context.getPackageName(), R.layout.widget_single_note); - views.setPendingIntentTemplate(R.id.single_note_widget_lv, PendingIntent.getActivity(context, appWidgetId, templateIntent, - pendingIntentFlagCompat(PendingIntent.FLAG_UPDATE_CURRENT))); - views.setRemoteAdapter(R.id.single_note_widget_lv, serviceIntent); - views.setEmptyView(R.id.single_note_widget_lv, R.id.widget_single_note_placeholder_tv); - - awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.single_note_widget_lv); - awm.updateAppWidget(appWidgetId, views); } else { Log.i(TAG, "onUpdate has been triggered before the user finished configuring the widget"); } + + final var serviceIntent = new Intent(context, SingleNoteWidgetService.class); + serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); + serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME))); + + final var views = new RemoteViews(context.getPackageName(), R.layout.widget_single_note); + views.setPendingIntentTemplate(R.id.single_note_widget_lv, PendingIntent.getActivity(context, appWidgetId, templateIntent, + pendingIntentFlagCompat(PendingIntent.FLAG_UPDATE_CURRENT))); + views.setRemoteAdapter(R.id.single_note_widget_lv, serviceIntent); + views.setEmptyView(R.id.single_note_widget_lv, R.id.widget_single_note_placeholder_tv); + + awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.single_note_widget_lv); + awm.updateAppWidget(appWidgetId, views); } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java b/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java index 7a4d11a7d..e700c8825 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java @@ -51,6 +51,7 @@ public class SingleNoteWidgetFactory implements RemoteViewsService.RemoteViewsFa Log.e(TAG, "Error: note not found"); } } else { + note = null; Log.w(TAG, "Widget with ID " + appWidgetId + " seems to be not configured yet."); } } -- GitLab