Skip to content

Commit 7ccd7c3

Browse files
Merge pull request #2183 from nextcloud/bugfix/restrictions-to-implicit-and-pending-intents
Fix / Restrictions to Implicit and Pending intents (Widget Bugfix)
2 parents 6f81dd6 + 0d50381 commit 7ccd7c3

File tree

3 files changed

+66
-42
lines changed

3 files changed

+66
-42
lines changed

app/src/main/java/it/niedermann/owncloud/notes/shared/util/WidgetUtil.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ private WidgetUtil() {
2828
* @return {@param flags} | {@link PendingIntent#FLAG_MUTABLE}
2929
*/
3030
public static int pendingIntentFlagCompat(int flags) {
31-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
32-
return flags | PendingIntent.FLAG_IMMUTABLE;
33-
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
31+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
3432
return flags | PendingIntent.FLAG_MUTABLE;
3533
}
3634
return flags;

app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidget.java

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import java.util.concurrent.Executors;
2424

2525
import it.niedermann.owncloud.notes.R;
26+
import it.niedermann.owncloud.notes.edit.EditNoteActivity;
2627
import it.niedermann.owncloud.notes.persistence.NotesRepository;
27-
import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData;
2828

2929
public class NoteListWidget extends AppWidgetProvider {
3030
private static final String TAG = NoteListWidget.class.getSimpleName();
@@ -45,9 +45,15 @@ static void updateAppWidget(Context context, AppWidgetManager awm, int[] appWidg
4545

4646
Log.v(TAG, "-- data - " + data);
4747

48+
Intent editNoteIntent = new Intent(context, EditNoteActivity.class);
49+
editNoteIntent.setPackage(context.getPackageName());
50+
51+
int pendingIntentFlags = pendingIntentFlagCompat(PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_COMPONENT);
52+
PendingIntent editNotePendingIntent = PendingIntent.getActivity(context, 0, editNoteIntent, pendingIntentFlags);
53+
4854
views = new RemoteViews(context.getPackageName(), R.layout.widget_note_list);
4955
views.setRemoteAdapter(R.id.note_list_widget_lv, serviceIntent);
50-
views.setPendingIntentTemplate(R.id.note_list_widget_lv, PendingIntent.getActivity(context, 0, new Intent(), pendingIntentFlagCompat(PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_COMPONENT)));
56+
views.setPendingIntentTemplate(R.id.note_list_widget_lv, editNotePendingIntent);
5157
views.setEmptyView(R.id.note_list_widget_lv, R.id.widget_note_list_placeholder_tv);
5258

5359
awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.note_list_widget_lv);
@@ -69,21 +75,28 @@ public void onReceive(Context context, Intent intent) {
6975
super.onReceive(context, intent);
7076
final var awm = AppWidgetManager.getInstance(context);
7177

72-
if (intent.getAction() != null) {
73-
if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
74-
if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
75-
if (intent.getExtras() != null) {
76-
updateAppWidget(context, awm, new int[]{intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)});
77-
} else {
78-
Log.w(TAG, "intent.getExtras() is null");
79-
}
80-
} else {
81-
updateAppWidget(context, awm, awm.getAppWidgetIds(new ComponentName(context, NoteListWidget.class)));
82-
}
83-
}
84-
} else {
85-
Log.w(TAG, "intent.getAction() is null");
78+
if (intent.getAction() == null) {
79+
Log.w(TAG, "Intent action is null");
80+
return;
81+
}
82+
83+
if (!intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
84+
Log.w(TAG, "Intent action is not ACTION_APPWIDGET_UPDATE");
85+
return;
8686
}
87+
88+
if (!intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
89+
Log.w(TAG,"Update widget via default appWidgetIds");
90+
updateAppWidget(context, awm, awm.getAppWidgetIds(new ComponentName(context, NoteListWidget.class)));
91+
}
92+
93+
if (intent.getExtras() == null) {
94+
Log.w(TAG, "Intent doesn't have bundle");
95+
return;
96+
}
97+
98+
Log.w(TAG,"Update widget via given appWidgetIds");
99+
updateAppWidget(context, awm, new int[]{intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)});
87100
}
88101

89102
@Override

app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.java

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
*/
77
package it.niedermann.owncloud.notes.widget.notelist;
88

9+
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.PARAM_CATEGORY;
10+
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_ALL;
11+
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_CATEGORY;
12+
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_STARRED;
13+
914
import android.appwidget.AppWidgetManager;
1015
import android.content.ComponentName;
1116
import android.content.Context;
@@ -33,11 +38,6 @@
3338
import it.niedermann.owncloud.notes.shared.model.NavigationCategory;
3439
import it.niedermann.owncloud.notes.shared.util.NotesColorUtil;
3540

36-
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.PARAM_CATEGORY;
37-
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_ALL;
38-
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_CATEGORY;
39-
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_STARRED;
40-
4141
public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFactory {
4242
private static final String TAG = NoteListWidgetFactory.class.getSimpleName();
4343

@@ -79,7 +79,7 @@ public void onDataSetChanged() {
7979
}
8080
}
8181
} catch (IllegalArgumentException e) {
82-
e.printStackTrace();
82+
Log.w(TAG, "Error caught at onDataSetChanged: " + e);
8383
}
8484
}
8585

@@ -93,25 +93,44 @@ public int getCount() {
9393
return dbNotes.size() + 1;
9494
}
9595

96+
private Intent getEditNoteIntent(Bundle bundle) {
97+
final Intent intent = new Intent(context, EditNoteActivity.class);
98+
intent.setPackage(context.getPackageName());
99+
intent.putExtras(bundle);
100+
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
101+
102+
return intent;
103+
}
104+
105+
private Intent getCreateNoteIntent(Account localAccount ) {
106+
final Bundle bundle = new Bundle();
107+
bundle.putSerializable(PARAM_CATEGORY, data.getMode() == MODE_DISPLAY_STARRED ? new NavigationCategory(ENavigationCategoryType.FAVORITES) : new NavigationCategory(localAccount.getId(), data.getCategory()));
108+
bundle.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, data.getAccountId());
109+
110+
return getEditNoteIntent(bundle);
111+
}
112+
113+
private Intent getOpenNoteIntent(Note note) {
114+
final Bundle bundle = new Bundle();
115+
bundle.putLong(EditNoteActivity.PARAM_NOTE_ID, note.getId());
116+
bundle.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());
117+
118+
return getEditNoteIntent(bundle);
119+
}
120+
96121
@Override
97122
public RemoteViews getViewAt(int position) {
98123
final RemoteViews note_content;
99124

100125
if (position == 0) {
101126
final Account localAccount = repo.getAccountById(data.getAccountId());
102-
final Intent openIntent = new Intent(Intent.ACTION_MAIN).setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName()));
103-
final Intent createIntent = new Intent(context, EditNoteActivity.class);
104-
final Bundle extras = new Bundle();
105-
106-
extras.putSerializable(PARAM_CATEGORY, data.getMode() == MODE_DISPLAY_STARRED ? new NavigationCategory(ENavigationCategoryType.FAVORITES) : new NavigationCategory(localAccount.getId(), data.getCategory()));
107-
extras.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, data.getAccountId());
108127

109-
createIntent.putExtras(extras);
110-
createIntent.setData(Uri.parse(createIntent.toUri(Intent.URI_INTENT_SCHEME)));
128+
final Intent createNoteIntent = getCreateNoteIntent(localAccount);
129+
final Intent openIntent = new Intent(Intent.ACTION_MAIN).setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName()));
111130

112131
note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry_add);
113132
note_content.setOnClickFillInIntent(R.id.widget_entry_content_tv, openIntent);
114-
note_content.setOnClickFillInIntent(R.id.widget_entry_fav_icon, createIntent);
133+
note_content.setOnClickFillInIntent(R.id.widget_entry_fav_icon, createNoteIntent);
115134
note_content.setTextViewText(R.id.widget_entry_content_tv, getCategoryTitle(context, data.getMode(), data.getCategory()));
116135
note_content.setImageViewResource(R.id.widget_entry_fav_icon, R.drawable.ic_add_blue_24dp);
117136
note_content.setInt(R.id.widget_entry_fav_icon, "setColorFilter", NotesColorUtil.contrastRatioIsSufficient(ContextCompat.getColor(context, R.color.widget_background), localAccount.getColor())
@@ -125,16 +144,10 @@ public RemoteViews getViewAt(int position) {
125144
}
126145

127146
final Note note = dbNotes.get(position);
128-
final Intent fillInIntent = new Intent(context, EditNoteActivity.class);
129-
final Bundle extras = new Bundle();
130-
extras.putLong(EditNoteActivity.PARAM_NOTE_ID, note.getId());
131-
extras.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());
132-
133-
fillInIntent.putExtras(extras);
134-
fillInIntent.setData(Uri.parse(fillInIntent.toUri(Intent.URI_INTENT_SCHEME)));
147+
final Intent openNoteIntent = getOpenNoteIntent(note);
135148

136149
note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry);
137-
note_content.setOnClickFillInIntent(R.id.widget_note_list_entry, fillInIntent);
150+
note_content.setOnClickFillInIntent(R.id.widget_note_list_entry, openNoteIntent);
138151
note_content.setTextViewText(R.id.widget_entry_content_tv, note.getTitle());
139152
note_content.setImageViewResource(R.id.widget_entry_fav_icon, note.getFavorite()
140153
? R.drawable.ic_star_yellow_24dp

0 commit comments

Comments
 (0)