Merge pull request #2183 from nextcloud/bugfix/restrictions-to-implicit-and-pending-intents

Fix / Restrictions to Implicit and Pending intents (Widget Bugfix)
This commit is contained in:
Andy Scherzinger 2024-05-16 10:47:31 +02:00 committed by GitHub
commit 7ccd7c3d39
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 66 additions and 42 deletions

View file

@ -28,9 +28,7 @@ public class WidgetUtil {
* @return {@param flags} | {@link PendingIntent#FLAG_MUTABLE}
*/
public static int pendingIntentFlagCompat(int flags) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
return flags | PendingIntent.FLAG_IMMUTABLE;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
return flags | PendingIntent.FLAG_MUTABLE;
}
return flags;

View file

@ -23,8 +23,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.edit.EditNoteActivity;
import it.niedermann.owncloud.notes.persistence.NotesRepository;
import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData;
public class NoteListWidget extends AppWidgetProvider {
private static final String TAG = NoteListWidget.class.getSimpleName();
@ -45,9 +45,15 @@ public class NoteListWidget extends AppWidgetProvider {
Log.v(TAG, "-- data - " + data);
Intent editNoteIntent = new Intent(context, EditNoteActivity.class);
editNoteIntent.setPackage(context.getPackageName());
int pendingIntentFlags = pendingIntentFlagCompat(PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_COMPONENT);
PendingIntent editNotePendingIntent = PendingIntent.getActivity(context, 0, editNoteIntent, pendingIntentFlags);
views = new RemoteViews(context.getPackageName(), R.layout.widget_note_list);
views.setRemoteAdapter(R.id.note_list_widget_lv, serviceIntent);
views.setPendingIntentTemplate(R.id.note_list_widget_lv, PendingIntent.getActivity(context, 0, new Intent(), pendingIntentFlagCompat(PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_COMPONENT)));
views.setPendingIntentTemplate(R.id.note_list_widget_lv, editNotePendingIntent);
views.setEmptyView(R.id.note_list_widget_lv, R.id.widget_note_list_placeholder_tv);
awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.note_list_widget_lv);
@ -69,21 +75,28 @@ public class NoteListWidget extends AppWidgetProvider {
super.onReceive(context, intent);
final var awm = AppWidgetManager.getInstance(context);
if (intent.getAction() != null) {
if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
if (intent.getExtras() != null) {
updateAppWidget(context, awm, new int[]{intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)});
} else {
Log.w(TAG, "intent.getExtras() is null");
}
} else {
updateAppWidget(context, awm, awm.getAppWidgetIds(new ComponentName(context, NoteListWidget.class)));
}
}
} else {
Log.w(TAG, "intent.getAction() is null");
if (intent.getAction() == null) {
Log.w(TAG, "Intent action is null");
return;
}
if (!intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
Log.w(TAG, "Intent action is not ACTION_APPWIDGET_UPDATE");
return;
}
if (!intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
Log.w(TAG,"Update widget via default appWidgetIds");
updateAppWidget(context, awm, awm.getAppWidgetIds(new ComponentName(context, NoteListWidget.class)));
}
if (intent.getExtras() == null) {
Log.w(TAG, "Intent doesn't have bundle");
return;
}
Log.w(TAG,"Update widget via given appWidgetIds");
updateAppWidget(context, awm, new int[]{intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)});
}
@Override

View file

@ -6,6 +6,11 @@
*/
package it.niedermann.owncloud.notes.widget.notelist;
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.PARAM_CATEGORY;
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_ALL;
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_CATEGORY;
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_STARRED;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
@ -33,11 +38,6 @@ import it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType;
import it.niedermann.owncloud.notes.shared.model.NavigationCategory;
import it.niedermann.owncloud.notes.shared.util.NotesColorUtil;
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.PARAM_CATEGORY;
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_ALL;
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_CATEGORY;
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_STARRED;
public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFactory {
private static final String TAG = NoteListWidgetFactory.class.getSimpleName();
@ -79,7 +79,7 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
Log.w(TAG, "Error caught at onDataSetChanged: " + e);
}
}
@ -93,25 +93,44 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact
return dbNotes.size() + 1;
}
private Intent getEditNoteIntent(Bundle bundle) {
final Intent intent = new Intent(context, EditNoteActivity.class);
intent.setPackage(context.getPackageName());
intent.putExtras(bundle);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
return intent;
}
private Intent getCreateNoteIntent(Account localAccount ) {
final Bundle bundle = new Bundle();
bundle.putSerializable(PARAM_CATEGORY, data.getMode() == MODE_DISPLAY_STARRED ? new NavigationCategory(ENavigationCategoryType.FAVORITES) : new NavigationCategory(localAccount.getId(), data.getCategory()));
bundle.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, data.getAccountId());
return getEditNoteIntent(bundle);
}
private Intent getOpenNoteIntent(Note note) {
final Bundle bundle = new Bundle();
bundle.putLong(EditNoteActivity.PARAM_NOTE_ID, note.getId());
bundle.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());
return getEditNoteIntent(bundle);
}
@Override
public RemoteViews getViewAt(int position) {
final RemoteViews note_content;
if (position == 0) {
final Account localAccount = repo.getAccountById(data.getAccountId());
final Intent createNoteIntent = getCreateNoteIntent(localAccount);
final Intent openIntent = new Intent(Intent.ACTION_MAIN).setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName()));
final Intent createIntent = new Intent(context, EditNoteActivity.class);
final Bundle extras = new Bundle();
extras.putSerializable(PARAM_CATEGORY, data.getMode() == MODE_DISPLAY_STARRED ? new NavigationCategory(ENavigationCategoryType.FAVORITES) : new NavigationCategory(localAccount.getId(), data.getCategory()));
extras.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, data.getAccountId());
createIntent.putExtras(extras);
createIntent.setData(Uri.parse(createIntent.toUri(Intent.URI_INTENT_SCHEME)));
note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry_add);
note_content.setOnClickFillInIntent(R.id.widget_entry_content_tv, openIntent);
note_content.setOnClickFillInIntent(R.id.widget_entry_fav_icon, createIntent);
note_content.setOnClickFillInIntent(R.id.widget_entry_fav_icon, createNoteIntent);
note_content.setTextViewText(R.id.widget_entry_content_tv, getCategoryTitle(context, data.getMode(), data.getCategory()));
note_content.setImageViewResource(R.id.widget_entry_fav_icon, R.drawable.ic_add_blue_24dp);
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 class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact
}
final Note note = dbNotes.get(position);
final Intent fillInIntent = new Intent(context, EditNoteActivity.class);
final Bundle extras = new Bundle();
extras.putLong(EditNoteActivity.PARAM_NOTE_ID, note.getId());
extras.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());
fillInIntent.putExtras(extras);
fillInIntent.setData(Uri.parse(fillInIntent.toUri(Intent.URI_INTENT_SCHEME)));
final Intent openNoteIntent = getOpenNoteIntent(note);
note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry);
note_content.setOnClickFillInIntent(R.id.widget_note_list_entry, fillInIntent);
note_content.setOnClickFillInIntent(R.id.widget_note_list_entry, openNoteIntent);
note_content.setTextViewText(R.id.widget_entry_content_tv, note.getTitle());
note_content.setImageViewResource(R.id.widget_entry_fav_icon, note.getFavorite()
? R.drawable.ic_star_yellow_24dp