77package it.niedermann.owncloud.notes.widget.notelist
88
99import android.appwidget.AppWidgetManager
10+ import android.content.ComponentName
1011import android.content.Context
1112import android.content.Intent
1213import android.os.Bundle
@@ -19,12 +20,14 @@ import androidx.core.net.toUri
1920import com.nextcloud.android.common.ui.util.PlatformThemeUtil
2021import it.niedermann.owncloud.notes.R
2122import it.niedermann.owncloud.notes.edit.EditNoteActivity
23+ import it.niedermann.owncloud.notes.main.MainActivity
2224import it.niedermann.owncloud.notes.persistence.NotesRepository
2325import it.niedermann.owncloud.notes.persistence.entity.Account
2426import it.niedermann.owncloud.notes.persistence.entity.Note
2527import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData
2628import it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType
2729import it.niedermann.owncloud.notes.shared.model.NavigationCategory
30+ import it.niedermann.owncloud.notes.shared.util.NotesColorUtil
2831
2932class NoteListWidgetFactory internal constructor(private val context : Context , intent : Intent ) :
3033 RemoteViewsFactory {
@@ -90,7 +93,7 @@ class NoteListWidgetFactory internal constructor(private val context: Context, i
9093 override fun onDestroy () = Unit
9194
9295 override fun getCount (): Int {
93- return dbNotes.size
96+ return dbNotes.size + 1
9497 }
9598
9699 private fun getEditNoteIntent (bundle : Bundle ): Intent {
@@ -125,24 +128,50 @@ class NoteListWidgetFactory internal constructor(private val context: Context, i
125128 return getEditNoteIntent(bundle)
126129 }
127130
128- override fun getViewAt (position : Int ): RemoteViews ? {
129- val note = dbNotes.getOrNull(position) ? : return null
131+ private fun getOpenAppIntent (): Intent {
132+ return Intent (Intent .ACTION_MAIN ).setComponent(
133+ ComponentName (context.packageName, MainActivity ::class .java.name)
134+ )
135+ }
130136
131- val openNoteIntent = getOpenNoteIntent(note)
137+ private fun getHeaderRemoteView (widgetData : NotesListWidgetData ): RemoteViews {
138+ val localAccount = repo.getAccountById(widgetData.accountId)
139+ val createNoteIntent = getCreateNoteIntent(localAccount)
140+
141+ return RemoteViews (context.packageName, R .layout.widget_entry_add).apply {
142+ setOnClickFillInIntent(R .id.widget_entry_content_tv, getOpenAppIntent())
143+ setOnClickFillInIntent(R .id.widget_entry_fav_icon, createNoteIntent)
144+ setTextViewText(
145+ R .id.widget_entry_content_tv,
146+ getCategoryTitle(context, widgetData.mode, widgetData.category)
147+ )
148+ setImageViewResource(R .id.widget_entry_fav_icon, R .drawable.ic_add_blue_24dp)
149+ setInt(
150+ R .id.widget_entry_fav_icon,
151+ " setColorFilter" ,
152+ if (NotesColorUtil .contrastRatioIsSufficient(
153+ ContextCompat .getColor(context, R .color.widget_background),
154+ localAccount.color
155+ )
156+ ) localAccount.color
157+ else ContextCompat .getColor(context, R .color.widget_foreground)
158+ )
159+ }
160+ }
132161
133- var createNoteIntent : Intent ? = null
134- data?. let {
135- val localAccount = repo.getAccountById(it.accountId)
136- createNoteIntent = getCreateNoteIntent(localAccount )
162+ override fun getViewAt ( position : Int ): RemoteViews ? {
163+ if (position == 0 ) {
164+ val widgetData = data ? : return null
165+ return getHeaderRemoteView(widgetData )
137166 }
138167
168+ val note = dbNotes.getOrNull(position - 1 ) ? : return null
169+
170+ val openNoteIntent = getOpenNoteIntent(note)
171+
139172 return RemoteViews (context.packageName, R .layout.widget_entry).apply {
140173 setOnClickFillInIntent(R .id.widget_note_list_entry, openNoteIntent)
141174
142- createNoteIntent?.let {
143- setOnClickFillInIntent(R .id.widget_entry_fav_icon, createNoteIntent)
144- }
145-
146175 setTextViewText(R .id.widget_entry_title, note.title)
147176
148177 if (note.category.isEmpty()) {
@@ -178,7 +207,11 @@ class NoteListWidgetFactory internal constructor(private val context: Context, i
178207 }
179208
180209 override fun getItemId (position : Int ): Long {
181- return dbNotes[position].id
210+ if (position == 0 ) {
211+ return - 1
212+ }
213+ val note = dbNotes.getOrNull(position - 1 ) ? : return - 2
214+ return note.id
182215 }
183216
184217 override fun hasStableIds (): Boolean {
@@ -187,5 +220,17 @@ class NoteListWidgetFactory internal constructor(private val context: Context, i
187220
188221 companion object {
189222 private val TAG : String = NoteListWidgetFactory ::class .java.getSimpleName()
223+
224+ private fun getCategoryTitle (context : Context , displayMode : Int , category : String? ): String {
225+ return when (displayMode) {
226+ NotesListWidgetData .MODE_DISPLAY_STARRED -> context.getString(R .string.label_favorites)
227+ NotesListWidgetData .MODE_DISPLAY_CATEGORY -> if (" " == category)
228+ context.getString(R .string.action_uncategorized)
229+ else
230+ category
231+
232+ else -> context.getString(R .string.app_name)
233+ } ? : context.getString(R .string.app_name)
234+ }
190235 }
191236}
0 commit comments