diff options
author | Stefan Niedermann <info@niedermann.it> | 2021-03-07 22:55:05 +0300 |
---|---|---|
committer | Niedermann IT-Dienstleistungen <stefan-niedermann@users.noreply.github.com> | 2021-03-08 00:11:22 +0300 |
commit | cfd0902a76aced7dd362b4fc2792c73fef526ff1 (patch) | |
tree | eadf3d06b330261dcace08a157993d03c615ca85 /app/src/main/java/it/niedermann/owncloud/notes/widget | |
parent | 6cbbab725d7f29ee50db51786c5c519bbdaf70de (diff) |
NoteListWidget: Move create button to scrollable entry, remove header
Diffstat (limited to 'app/src/main/java/it/niedermann/owncloud/notes/widget')
-rw-r--r-- | app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidget.java | 72 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.java | 86 |
2 files changed, 62 insertions, 96 deletions
diff --git a/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidget.java b/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidget.java index e3f36162..38516bb3 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidget.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidget.java @@ -6,29 +6,15 @@ import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.graphics.Color; import android.net.Uri; import android.util.Log; import android.widget.RemoteViews; -import androidx.core.content.ContextCompat; - import java.util.NoSuchElementException; import it.niedermann.owncloud.notes.R; -import it.niedermann.owncloud.notes.preferences.DarkModeSetting; import it.niedermann.owncloud.notes.edit.EditNoteActivity; -import it.niedermann.owncloud.notes.main.MainActivity; -import it.niedermann.owncloud.notes.branding.BrandingUtil; -import it.niedermann.owncloud.notes.shared.model.Category; -import it.niedermann.owncloud.notes.shared.model.LocalAccount; import it.niedermann.owncloud.notes.persistence.NotesDatabase; -import it.niedermann.owncloud.notes.NotesApplication; - -import static it.niedermann.owncloud.notes.edit.EditNoteActivity.PARAM_CATEGORY; -import static it.niedermann.owncloud.notes.widget.notelist.NoteListsWidgetData.MODE_DISPLAY_ALL; -import static it.niedermann.owncloud.notes.widget.notelist.NoteListsWidgetData.MODE_DISPLAY_CATEGORY; -import static it.niedermann.owncloud.notes.widget.notelist.NoteListsWidgetData.MODE_DISPLAY_STARRED; public class NoteListWidget extends AppWidgetProvider { private static final String TAG = NoteListWidget.class.getSimpleName(); @@ -41,39 +27,15 @@ public class NoteListWidget extends AppWidgetProvider { final NotesDatabase db = NotesDatabase.getInstance(context); RemoteViews views; - DarkModeSetting darkTheme; for (int appWidgetId : appWidgetIds) { try { final NoteListsWidgetData data = db.getNoteListWidgetData(appWidgetId); - final LocalAccount localAccount = db.getAccount(data.getAccountId()); - - String category = null; - if (data.getCategoryId() != null) { - category = db.getCategoryTitleById(data.getAccountId(), data.getCategoryId()); - } - - darkTheme = DarkModeSetting.fromModeID(data.getThemeMode()); Intent serviceIntent = new Intent(context, NoteListWidgetService.class); serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME))); - // Launch application when user taps the header icon or app title - Intent intent = new Intent(Intent.ACTION_MAIN); - intent.setComponent(new ComponentName(context.getPackageName(), - MainActivity.class.getName())); - - // Open the main app if the user taps the widget header - PendingIntent openAppI = PendingIntent.getActivity(context, PENDING_INTENT_OPEN_APP_RQ, - intent, - PendingIntent.FLAG_UPDATE_CURRENT); - - // Launch create note activity if user taps "+" icon on header - PendingIntent newNoteI = PendingIntent.getActivity(context, (PENDING_INTENT_NEW_NOTE_RQ + appWidgetId), - new Intent(context, EditNoteActivity.class).putExtra(PARAM_CATEGORY, new Category(category, data.getMode() == MODE_DISPLAY_STARRED)), - PendingIntent.FLAG_UPDATE_CURRENT); - PendingIntent templatePI = PendingIntent.getActivity(context, PENDING_INTENT_EDIT_NOTE_RQ, new Intent(context, EditNoteActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); @@ -81,26 +43,11 @@ public class NoteListWidget extends AppWidgetProvider { Log.v(TAG, "-- data - " + data); views = new RemoteViews(context.getPackageName(), R.layout.widget_note_list); - views.setTextViewText(R.id.widget_note_list_title_tv, getWidgetTitle(context, data.getMode(), category)); - views.setOnClickPendingIntent(R.id.widget_note_header_icon, openAppI); - views.setOnClickPendingIntent(R.id.widget_note_list_title_tv, openAppI); - views.setOnClickPendingIntent(R.id.widget_note_list_create_icon, newNoteI); views.setPendingIntentTemplate(R.id.note_list_widget_lv, templatePI); views.setRemoteAdapter(R.id.note_list_widget_lv, serviceIntent); views.setEmptyView(R.id.note_list_widget_lv, R.id.widget_note_list_placeholder_tv); - awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.note_list_widget_lv); - if (BrandingUtil.isBrandingEnabled(context)) { - views.setInt(R.id.widget_note_header, "setBackgroundColor", localAccount.getColor()); - views.setInt(R.id.widget_note_header_icon, "setColorFilter", localAccount.getTextColor()); - views.setInt(R.id.widget_note_list_create_icon, "setColorFilter", localAccount.getTextColor()); - views.setTextColor(R.id.widget_note_list_title_tv, localAccount.getTextColor()); - } else { - views.setInt(R.id.widget_note_header, "setBackgroundColor", ContextCompat.getColor(context, R.color.defaultBrand)); - views.setInt(R.id.widget_note_header_icon, "setColorFilter", Color.WHITE); - views.setInt(R.id.widget_note_list_create_icon, "setColorFilter", Color.WHITE); - views.setTextColor(R.id.widget_note_list_title_tv, Color.WHITE); - } + awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.note_list_widget_lv); awm.updateAppWidget(appWidgetId, views); } catch (NoSuchElementException e) { Log.i(TAG, "onUpdate has been triggered before the user finished configuring the widget"); @@ -146,23 +93,6 @@ public class NoteListWidget extends AppWidgetProvider { } } - private static String getWidgetTitle(Context context, int displayMode, String category) { - switch (displayMode) { - case MODE_DISPLAY_ALL: - return context.getString(R.string.app_name); - case MODE_DISPLAY_STARRED: - return context.getString(R.string.label_favorites); - case MODE_DISPLAY_CATEGORY: - if ("".equals(category)) { - return context.getString(R.string.action_uncategorized); - } else { - return category; - } - default: - return null; - } - } - /** * Update note list widgets, if the note data was changed. */ 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 901a9f3e..245a374c 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 @@ -1,6 +1,5 @@ package it.niedermann.owncloud.notes.widget.notelist; -import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.Intent; @@ -10,16 +9,17 @@ import android.util.Log; import android.widget.RemoteViews; import android.widget.RemoteViewsService; +import androidx.annotation.NonNull; + import java.util.ArrayList; import java.util.List; import it.niedermann.owncloud.notes.R; -import it.niedermann.owncloud.notes.preferences.DarkModeSetting; import it.niedermann.owncloud.notes.edit.EditNoteActivity; +import it.niedermann.owncloud.notes.persistence.NotesDatabase; import it.niedermann.owncloud.notes.shared.model.Category; import it.niedermann.owncloud.notes.shared.model.DBNote; -import it.niedermann.owncloud.notes.persistence.NotesDatabase; -import it.niedermann.owncloud.notes.NotesApplication; +import it.niedermann.owncloud.notes.shared.model.LocalAccount; import static it.niedermann.owncloud.notes.edit.EditNoteActivity.PARAM_CATEGORY; import static it.niedermann.owncloud.notes.widget.notelist.NoteListsWidgetData.MODE_DISPLAY_ALL; @@ -45,6 +45,7 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact @Override public void onCreate() { + // Nothing to do hereā¦ } @Override @@ -82,29 +83,37 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact */ @Override public int getCount() { - return dbNotes.size(); + return dbNotes.size() + 1; } @Override public RemoteViews getViewAt(int position) { final RemoteViews note_content; -// if(position == 0) { -// final Intent fillInIntent = new Intent(); -// final Bundle extras = new Bundle(); -// extras.putExtra(PARAM_CATEGORY, new Category(db.getCategoryTitleById(data.getAccountId(), data.getCategoryId()), data.getMode() == MODE_DISPLAY_STARRED)), -// extras.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId()); -// -// fillInIntent.putExtras(extras); -// fillInIntent.setData(Uri.parse(fillInIntent.toUri(Intent.URI_INTENT_SCHEME))); -// -// fillInIntent.setData(Uri.parse(fillInIntent.toUri(Intent.URI_INTENT_SCHEME))); -// -// note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry); -// note_content.setOnClickFillInIntent(R.id.widget_note_list_entry, fillInIntent); -// note_content.setTextViewText(R.id.widget_entry_content_tv, "Add new note in this category"); -// note_content.setImageViewResource(R.id.widget_entry_fav_icon, R.drawable.ic_add_blue_24dp); -// } else { + if (position == 0) { + final LocalAccount localAccount = db.getAccount(data.getAccountId()); + final Intent fillInIntent = new Intent(); + final Bundle extras = new Bundle(); + + String category = null; + Long categoryId = data.getCategoryId(); + if (categoryId != null) { + category = db.getCategoryTitleById(data.getAccountId(), categoryId); + } + + extras.putSerializable(PARAM_CATEGORY, new Category(category, data.getMode() == MODE_DISPLAY_STARRED)); + extras.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, data.getAccountId()); + + fillInIntent.putExtras(extras); + fillInIntent.setData(Uri.parse(fillInIntent.toUri(Intent.URI_INTENT_SCHEME))); + + note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry_add); + note_content.setOnClickFillInIntent(R.id.widget_note_list_entry, fillInIntent); + note_content.setTextViewText(R.id.widget_entry_content_tv, getAddButtonText(context, data.getMode(), category)); + 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", localAccount.getColor()); + } else { + position--; if (position > dbNotes.size() - 1 || dbNotes.get(position) == null) { Log.e(TAG, "Could not find position \"" + position + "\" in dbNotes list."); return null; @@ -125,12 +134,29 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact note_content.setImageViewResource(R.id.widget_entry_fav_icon, note.isFavorite() ? R.drawable.ic_star_yellow_24dp : R.drawable.ic_star_grey_ccc_24dp); -// } + } return note_content; } + @NonNull + private static String getAddButtonText(@NonNull Context context, int displayMode, String category) { + switch (displayMode) { + case MODE_DISPLAY_STARRED: + return context.getString(R.string.widget_note_list_add_favorite); + case MODE_DISPLAY_CATEGORY: + if ("".equals(category)) { + return context.getString(R.string.widget_note_list_add); + } else { + return context.getString(R.string.widget_note_list_add_to_category, category); + } + case MODE_DISPLAY_ALL: + default: + return context.getString(R.string.widget_note_list_add); + } + } + @Override public RemoteViews getLoadingView() { return null; @@ -138,12 +164,22 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact @Override public int getViewTypeCount() { - return 1; + return 2; } @Override - public long getItemId(int i) { - return i; + public long getItemId(int position) { + if (position == 0) { + return -1; + } else { + position--; + if (position > dbNotes.size() - 1 || dbNotes.get(position) == null) { + Log.e(TAG, "Could not find position \"" + position + "\" in dbNotes list."); + return -2; + } + DBNote note = dbNotes.get(position); + return note.getId(); + } } @Override |