Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix / Restrictions to Implicit and Pending intents (Widget Bugfix) #2183

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ private 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
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();
Expand All @@ -45,9 +45,15 @@ static void updateAppWidget(Context context, AppWidgetManager awm, int[] appWidg

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);
Expand All @@ -69,21 +75,28 @@ public void onReceive(Context context, Intent intent) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,11 +38,6 @@
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();

Expand Down Expand Up @@ -79,7 +79,7 @@ public void onDataSetChanged() {
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
Log.w(TAG, "Error caught at onDataSetChanged: " + e);
}
}

Expand All @@ -93,25 +93,44 @@ public int getCount() {
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 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)));
final Intent createNoteIntent = getCreateNoteIntent(localAccount);
final Intent openIntent = new Intent(Intent.ACTION_MAIN).setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName()));

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())
Expand All @@ -125,16 +144,10 @@ public RemoteViews getViewAt(int position) {
}

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
Expand Down
Loading