Loading app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.kt +198 −156 Original line number Diff line number Diff line /* * Nextcloud Notes - Android Client * * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2017-2025 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: GPL-3.0-or-later */ 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; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.RemoteViews; import android.widget.RemoteViewsService; import androidx.annotation.NonNull; import androidx.core.content.ContextCompat; import java.util.ArrayList; import java.util.List; import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.edit.EditNoteActivity; import it.niedermann.owncloud.notes.main.MainActivity; import it.niedermann.owncloud.notes.persistence.NotesRepository; import it.niedermann.owncloud.notes.persistence.entity.Account; import it.niedermann.owncloud.notes.persistence.entity.Note; import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData; import it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType; import it.niedermann.owncloud.notes.shared.model.NavigationCategory; import it.niedermann.owncloud.notes.shared.util.NotesColorUtil; public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFactory { private static final String TAG = NoteListWidgetFactory.class.getSimpleName(); private final Context context; private final int appWidgetId; private final NotesRepository repo; @NonNull private final List<Note> dbNotes = new ArrayList<>(); private NotesListWidgetData data; NoteListWidgetFactory(Context context, Intent intent) { this.context = context; this.appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); repo = NotesRepository.getInstance(context); } @Override public void onCreate() { package it.niedermann.owncloud.notes.widget.notelist import android.appwidget.AppWidgetManager import android.content.ComponentName import android.content.Context import android.content.Intent import android.os.Bundle import android.util.Log import android.widget.RemoteViews import android.widget.RemoteViewsService.RemoteViewsFactory import androidx.core.content.ContextCompat import it.niedermann.owncloud.notes.R import it.niedermann.owncloud.notes.edit.EditNoteActivity import it.niedermann.owncloud.notes.main.MainActivity import it.niedermann.owncloud.notes.persistence.NotesRepository import it.niedermann.owncloud.notes.persistence.entity.Account import it.niedermann.owncloud.notes.persistence.entity.Note import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData 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 androidx.core.net.toUri class NoteListWidgetFactory internal constructor(private val context: Context, intent: Intent) : RemoteViewsFactory { private val appWidgetId: Int = intent.getIntExtra( AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID ) private val repo: NotesRepository = NotesRepository.getInstance(context) private val dbNotes: MutableList<Note> = ArrayList() private var data: NotesListWidgetData? = null override fun onCreate() { // Nothing to do here… } @Override public void onDataSetChanged() { dbNotes.clear(); override fun onDataSetChanged() { dbNotes.clear() try { data = repo.getNoteListWidgetData(appWidgetId); data = repo.getNoteListWidgetData(appWidgetId) if (data == null) { Log.w(TAG, "Widget data is null"); return; } Log.v(TAG, "--- data - " + data); switch (data.getMode()) { case MODE_DISPLAY_ALL -> dbNotes.addAll(repo.searchRecentByModified(data.getAccountId(), "%")); case MODE_DISPLAY_STARRED -> dbNotes.addAll(repo.searchFavoritesByModified(data.getAccountId(), "%")); default -> { if (data.getCategory() != null) { dbNotes.addAll(repo.searchCategoryByModified(data.getAccountId(), "%", data.getCategory())); Log.w(TAG, "Widget data is null") return } val widgetData = data ?: return Log.v(TAG, "--- data - $widgetData") when (widgetData.mode) { NotesListWidgetData.MODE_DISPLAY_ALL -> dbNotes.addAll( repo.searchRecentByModified( widgetData.accountId, "%" ) ) NotesListWidgetData.MODE_DISPLAY_STARRED -> dbNotes.addAll( repo.searchFavoritesByModified( widgetData.accountId, "%" ) ) else -> { if (widgetData.category != null) { dbNotes.addAll( repo.searchCategoryByModified( widgetData.accountId, "%", widgetData.category ) ) } else { dbNotes.addAll(repo.searchUncategorizedByModified(data.getAccountId(), "%")); dbNotes.addAll( repo.searchUncategorizedByModified( widgetData.accountId, "%" ) ) } } } } catch (Exception e) { Log.w(TAG, "Error caught at onDataSetChanged: " + e); } catch (e: Exception) { Log.w(TAG, "Error caught at onDataSetChanged: $e") } } @Override public void onDestroy() { override fun onDestroy() { //NoOp } @Override public int getCount() { return dbNotes.size() + 1; override fun getCount(): Int { 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 fun getEditNoteIntent(bundle: Bundle): Intent { return Intent(context, EditNoteActivity::class.java).apply { setPackage(context.packageName) putExtras(bundle) setData(toUri(Intent.URI_INTENT_SCHEME).toUri()) } } 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()); private fun getCreateNoteIntent(localAccount: Account): Intent { val bundle = Bundle() return getEditNoteIntent(bundle); } data?.let { val navigationCategory = if (it.mode == NotesListWidgetData.MODE_DISPLAY_STARRED) NavigationCategory( ENavigationCategoryType.FAVORITES ) else NavigationCategory(localAccount.id, it.category) 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()); bundle.putSerializable(EditNoteActivity.PARAM_CATEGORY, navigationCategory) bundle.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, it.accountId) } return getEditNoteIntent(bundle); return getEditNoteIntent(bundle) } @Override public RemoteViews getViewAt(int position) { final RemoteViews note_content; private fun getOpenNoteIntent(note: Note): Intent { val bundle = Bundle().apply { putLong(EditNoteActivity.PARAM_NOTE_ID, note.id) putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.accountId) } 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())); 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, 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()) ? localAccount.getColor() : ContextCompat.getColor(context, R.color.widget_foreground)); } else { position--; if (position > dbNotes.size() - 1 || dbNotes.get(position) == null) { Log.e(TAG, "Could not find position \"" + position + "\" in dbNotes list."); return null; return getEditNoteIntent(bundle) } final Note note = dbNotes.get(position); final Intent openNoteIntent = getOpenNoteIntent(note); private fun getRemoteViewFromData(): RemoteViews? { val widgetData = data ?: return null val localAccount = repo.getAccountById(widgetData.accountId) val createNoteIntent = getCreateNoteIntent(localAccount) val openIntent = Intent(Intent.ACTION_MAIN).setComponent( ComponentName( context.packageName, MainActivity::class.java.getName() ) ) note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry); 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 : R.drawable.ic_star_grey_ccc_24dp); return RemoteViews(context.packageName, R.layout.widget_entry_add).apply { setOnClickFillInIntent(R.id.widget_entry_content_tv, openIntent) setOnClickFillInIntent(R.id.widget_entry_fav_icon, createNoteIntent) setTextViewText( R.id.widget_entry_content_tv, getCategoryTitle(context, widgetData.mode, widgetData.category) ) setImageViewResource( R.id.widget_entry_fav_icon, R.drawable.ic_add_blue_24dp ) setInt( R.id.widget_entry_fav_icon, "setColorFilter", if (NotesColorUtil.contrastRatioIsSufficient( ContextCompat.getColor( context, R.color.widget_background ), localAccount.color ) ) localAccount.color else ContextCompat.getColor(context, R.color.widget_foreground) ) } } return note_content; private fun getRemoteViewFromPosition(position: Int): RemoteViews? { var position = position position-- if (position < 0 || position >= dbNotes.size) { Log.e(TAG, "Could not find position \"$position\" in dbNotes list.") return null } val note = dbNotes[position] val openNoteIntent = getOpenNoteIntent(note) return RemoteViews(context.packageName, R.layout.widget_entry).apply { setOnClickFillInIntent(R.id.widget_note_list_entry, openNoteIntent) setTextViewText(R.id.widget_entry_content_tv, note.title) setImageViewResource( R.id.widget_entry_fav_icon, if (note.favorite) R.drawable.ic_star_yellow_24dp else R.drawable.ic_star_grey_ccc_24dp ) } } @NonNull private static String getCategoryTitle(@NonNull Context context, int displayMode, String category) { return switch (displayMode) { case MODE_DISPLAY_STARRED -> context.getString(R.string.label_favorites); case MODE_DISPLAY_CATEGORY -> "".equals(category) ? context.getString(R.string.action_uncategorized) : category; default -> context.getString(R.string.app_name); }; override fun getViewAt(position: Int): RemoteViews? { return if (position == 0 && data != null) { getRemoteViewFromData() } else { getRemoteViewFromPosition(position) } } @Override public RemoteViews getLoadingView() { return null; override fun getLoadingView(): RemoteViews? { return null } @Override public int getViewTypeCount() { return 2; override fun getViewTypeCount(): Int { return 2 } @Override public long getItemId(int position) { override fun getItemId(position: Int): Long { var position = position if (position == 0) { return -1; 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; position-- if (position > dbNotes.size - 1 || dbNotes.get(position) == null) { Log.e(TAG, "Could not find position \"" + position + "\" in dbNotes list.") return -2 } return dbNotes.get(position).getId(); return dbNotes[position].id } } @Override public boolean hasStableIds() { return true; override fun hasStableIds(): Boolean { return true } companion object { private val TAG: String = NoteListWidgetFactory::class.java.getSimpleName() private fun getCategoryTitle( context: Context, displayMode: Int, category: String? ): String { return when (displayMode) { NotesListWidgetData.MODE_DISPLAY_STARRED -> context.getString(R.string.label_favorites) NotesListWidgetData.MODE_DISPLAY_CATEGORY -> if ("" == category) context.getString(R.string.action_uncategorized) else category else -> context.getString(R.string.app_name) }!! } } } Loading
app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.kt +198 −156 Original line number Diff line number Diff line /* * Nextcloud Notes - Android Client * * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2017-2025 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: GPL-3.0-or-later */ 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; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.RemoteViews; import android.widget.RemoteViewsService; import androidx.annotation.NonNull; import androidx.core.content.ContextCompat; import java.util.ArrayList; import java.util.List; import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.edit.EditNoteActivity; import it.niedermann.owncloud.notes.main.MainActivity; import it.niedermann.owncloud.notes.persistence.NotesRepository; import it.niedermann.owncloud.notes.persistence.entity.Account; import it.niedermann.owncloud.notes.persistence.entity.Note; import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData; import it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType; import it.niedermann.owncloud.notes.shared.model.NavigationCategory; import it.niedermann.owncloud.notes.shared.util.NotesColorUtil; public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFactory { private static final String TAG = NoteListWidgetFactory.class.getSimpleName(); private final Context context; private final int appWidgetId; private final NotesRepository repo; @NonNull private final List<Note> dbNotes = new ArrayList<>(); private NotesListWidgetData data; NoteListWidgetFactory(Context context, Intent intent) { this.context = context; this.appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); repo = NotesRepository.getInstance(context); } @Override public void onCreate() { package it.niedermann.owncloud.notes.widget.notelist import android.appwidget.AppWidgetManager import android.content.ComponentName import android.content.Context import android.content.Intent import android.os.Bundle import android.util.Log import android.widget.RemoteViews import android.widget.RemoteViewsService.RemoteViewsFactory import androidx.core.content.ContextCompat import it.niedermann.owncloud.notes.R import it.niedermann.owncloud.notes.edit.EditNoteActivity import it.niedermann.owncloud.notes.main.MainActivity import it.niedermann.owncloud.notes.persistence.NotesRepository import it.niedermann.owncloud.notes.persistence.entity.Account import it.niedermann.owncloud.notes.persistence.entity.Note import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData 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 androidx.core.net.toUri class NoteListWidgetFactory internal constructor(private val context: Context, intent: Intent) : RemoteViewsFactory { private val appWidgetId: Int = intent.getIntExtra( AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID ) private val repo: NotesRepository = NotesRepository.getInstance(context) private val dbNotes: MutableList<Note> = ArrayList() private var data: NotesListWidgetData? = null override fun onCreate() { // Nothing to do here… } @Override public void onDataSetChanged() { dbNotes.clear(); override fun onDataSetChanged() { dbNotes.clear() try { data = repo.getNoteListWidgetData(appWidgetId); data = repo.getNoteListWidgetData(appWidgetId) if (data == null) { Log.w(TAG, "Widget data is null"); return; } Log.v(TAG, "--- data - " + data); switch (data.getMode()) { case MODE_DISPLAY_ALL -> dbNotes.addAll(repo.searchRecentByModified(data.getAccountId(), "%")); case MODE_DISPLAY_STARRED -> dbNotes.addAll(repo.searchFavoritesByModified(data.getAccountId(), "%")); default -> { if (data.getCategory() != null) { dbNotes.addAll(repo.searchCategoryByModified(data.getAccountId(), "%", data.getCategory())); Log.w(TAG, "Widget data is null") return } val widgetData = data ?: return Log.v(TAG, "--- data - $widgetData") when (widgetData.mode) { NotesListWidgetData.MODE_DISPLAY_ALL -> dbNotes.addAll( repo.searchRecentByModified( widgetData.accountId, "%" ) ) NotesListWidgetData.MODE_DISPLAY_STARRED -> dbNotes.addAll( repo.searchFavoritesByModified( widgetData.accountId, "%" ) ) else -> { if (widgetData.category != null) { dbNotes.addAll( repo.searchCategoryByModified( widgetData.accountId, "%", widgetData.category ) ) } else { dbNotes.addAll(repo.searchUncategorizedByModified(data.getAccountId(), "%")); dbNotes.addAll( repo.searchUncategorizedByModified( widgetData.accountId, "%" ) ) } } } } catch (Exception e) { Log.w(TAG, "Error caught at onDataSetChanged: " + e); } catch (e: Exception) { Log.w(TAG, "Error caught at onDataSetChanged: $e") } } @Override public void onDestroy() { override fun onDestroy() { //NoOp } @Override public int getCount() { return dbNotes.size() + 1; override fun getCount(): Int { 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 fun getEditNoteIntent(bundle: Bundle): Intent { return Intent(context, EditNoteActivity::class.java).apply { setPackage(context.packageName) putExtras(bundle) setData(toUri(Intent.URI_INTENT_SCHEME).toUri()) } } 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()); private fun getCreateNoteIntent(localAccount: Account): Intent { val bundle = Bundle() return getEditNoteIntent(bundle); } data?.let { val navigationCategory = if (it.mode == NotesListWidgetData.MODE_DISPLAY_STARRED) NavigationCategory( ENavigationCategoryType.FAVORITES ) else NavigationCategory(localAccount.id, it.category) 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()); bundle.putSerializable(EditNoteActivity.PARAM_CATEGORY, navigationCategory) bundle.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, it.accountId) } return getEditNoteIntent(bundle); return getEditNoteIntent(bundle) } @Override public RemoteViews getViewAt(int position) { final RemoteViews note_content; private fun getOpenNoteIntent(note: Note): Intent { val bundle = Bundle().apply { putLong(EditNoteActivity.PARAM_NOTE_ID, note.id) putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.accountId) } 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())); 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, 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()) ? localAccount.getColor() : ContextCompat.getColor(context, R.color.widget_foreground)); } else { position--; if (position > dbNotes.size() - 1 || dbNotes.get(position) == null) { Log.e(TAG, "Could not find position \"" + position + "\" in dbNotes list."); return null; return getEditNoteIntent(bundle) } final Note note = dbNotes.get(position); final Intent openNoteIntent = getOpenNoteIntent(note); private fun getRemoteViewFromData(): RemoteViews? { val widgetData = data ?: return null val localAccount = repo.getAccountById(widgetData.accountId) val createNoteIntent = getCreateNoteIntent(localAccount) val openIntent = Intent(Intent.ACTION_MAIN).setComponent( ComponentName( context.packageName, MainActivity::class.java.getName() ) ) note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry); 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 : R.drawable.ic_star_grey_ccc_24dp); return RemoteViews(context.packageName, R.layout.widget_entry_add).apply { setOnClickFillInIntent(R.id.widget_entry_content_tv, openIntent) setOnClickFillInIntent(R.id.widget_entry_fav_icon, createNoteIntent) setTextViewText( R.id.widget_entry_content_tv, getCategoryTitle(context, widgetData.mode, widgetData.category) ) setImageViewResource( R.id.widget_entry_fav_icon, R.drawable.ic_add_blue_24dp ) setInt( R.id.widget_entry_fav_icon, "setColorFilter", if (NotesColorUtil.contrastRatioIsSufficient( ContextCompat.getColor( context, R.color.widget_background ), localAccount.color ) ) localAccount.color else ContextCompat.getColor(context, R.color.widget_foreground) ) } } return note_content; private fun getRemoteViewFromPosition(position: Int): RemoteViews? { var position = position position-- if (position < 0 || position >= dbNotes.size) { Log.e(TAG, "Could not find position \"$position\" in dbNotes list.") return null } val note = dbNotes[position] val openNoteIntent = getOpenNoteIntent(note) return RemoteViews(context.packageName, R.layout.widget_entry).apply { setOnClickFillInIntent(R.id.widget_note_list_entry, openNoteIntent) setTextViewText(R.id.widget_entry_content_tv, note.title) setImageViewResource( R.id.widget_entry_fav_icon, if (note.favorite) R.drawable.ic_star_yellow_24dp else R.drawable.ic_star_grey_ccc_24dp ) } } @NonNull private static String getCategoryTitle(@NonNull Context context, int displayMode, String category) { return switch (displayMode) { case MODE_DISPLAY_STARRED -> context.getString(R.string.label_favorites); case MODE_DISPLAY_CATEGORY -> "".equals(category) ? context.getString(R.string.action_uncategorized) : category; default -> context.getString(R.string.app_name); }; override fun getViewAt(position: Int): RemoteViews? { return if (position == 0 && data != null) { getRemoteViewFromData() } else { getRemoteViewFromPosition(position) } } @Override public RemoteViews getLoadingView() { return null; override fun getLoadingView(): RemoteViews? { return null } @Override public int getViewTypeCount() { return 2; override fun getViewTypeCount(): Int { return 2 } @Override public long getItemId(int position) { override fun getItemId(position: Int): Long { var position = position if (position == 0) { return -1; 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; position-- if (position > dbNotes.size - 1 || dbNotes.get(position) == null) { Log.e(TAG, "Could not find position \"" + position + "\" in dbNotes list.") return -2 } return dbNotes.get(position).getId(); return dbNotes[position].id } } @Override public boolean hasStableIds() { return true; override fun hasStableIds(): Boolean { return true } companion object { private val TAG: String = NoteListWidgetFactory::class.java.getSimpleName() private fun getCategoryTitle( context: Context, displayMode: Int, category: String? ): String { return when (displayMode) { NotesListWidgetData.MODE_DISPLAY_STARRED -> context.getString(R.string.label_favorites) NotesListWidgetData.MODE_DISPLAY_CATEGORY -> if ("" == category) context.getString(R.string.action_uncategorized) else category else -> context.getString(R.string.app_name) }!! } } }