@@ -13,14 +13,12 @@ import android.content.ComponentName
1313import android.content.Context
1414import android.content.Intent
1515import android.widget.RemoteViews
16+ import androidx.core.net.toUri
1617import it.niedermann.owncloud.notes.R
17- import it.niedermann.owncloud.notes.edit.BaseNoteFragment
1818import it.niedermann.owncloud.notes.edit.EditNoteActivity
1919import it.niedermann.owncloud.notes.persistence.NotesRepository
20- import it.niedermann.owncloud.notes.shared.util.WidgetUtil
2120import java.util.concurrent.ExecutorService
2221import java.util.concurrent.Executors
23- import androidx.core.net.toUri
2422
2523class SingleNoteWidget : AppWidgetProvider () {
2624 private val executor: ExecutorService = Executors .newCachedThreadPool()
@@ -52,44 +50,69 @@ class SingleNoteWidget : AppWidgetProvider() {
5250 super .onDeleted(context, appWidgetIds)
5351 }
5452
55- companion object {
56- private val TAG : String = SingleNoteWidget ::class .java.getSimpleName()
57- fun updateAppWidget (context : Context , awm : AppWidgetManager , appWidgetIds : IntArray ) {
58- val templateIntent = Intent (context, EditNoteActivity ::class .java)
59- val repo = NotesRepository .getInstance(context)
53+ private fun updateAppWidget (context : Context , awm : AppWidgetManager , appWidgetIds : IntArray ) {
54+ val repo = NotesRepository .getInstance(context)
55+ appWidgetIds.forEach { appWidgetId ->
56+ repo.getSingleNoteWidgetData(appWidgetId)?.let { data ->
57+ val pendingIntent = getPendingIntent(context, appWidgetId)
58+ val serviceIntent = getServiceIntent(context, appWidgetId)
59+ val views = getRemoteViews(context, pendingIntent, serviceIntent)
60+ awm.run {
61+ updateAppWidget(appWidgetId, views)
62+ notifyAppWidgetViewDataChanged(appWidgetId, R .id.single_note_widget_lv)
63+ }
64+ }
65+ }
66+ }
6067
61- appWidgetIds.forEach { appWidgetId ->
62- repo.getSingleNoteWidgetData(appWidgetId)?.let { data ->
63- templateIntent.putExtra(BaseNoteFragment .PARAM_ACCOUNT_ID , data.accountId)
68+ private fun getPendingIntent (
69+ context : Context ,
70+ id : Int
71+ ): PendingIntent {
72+ val intent = Intent (context, EditNoteActivity ::class .java).apply {
73+ setPackage(context.packageName)
74+ }
6475
65- val serviceIntent = Intent (context, SingleNoteWidgetService ::class .java).apply {
66- putExtra(AppWidgetManager .EXTRA_APPWIDGET_ID , appWidgetId)
67- setData(toUri(Intent .URI_INTENT_SCHEME ).toUri())
68- }
76+ val pendingIntentFlags = PendingIntent .FLAG_UPDATE_CURRENT or
77+ PendingIntent .FLAG_MUTABLE or
78+ Intent .FILL_IN_COMPONENT
6979
80+ return PendingIntent .getActivity(
81+ context,
82+ id,
83+ intent,
84+ pendingIntentFlags
85+ )
86+ }
7087
71- val views = RemoteViews (context.packageName, R .layout.widget_single_note).apply {
72- setPendingIntentTemplate(
73- R .id.single_note_widget_lv, PendingIntent .getActivity(
74- context, appWidgetId, templateIntent,
75- WidgetUtil .pendingIntentFlagCompat(PendingIntent .FLAG_UPDATE_CURRENT )
76- )
77- )
78- setRemoteAdapter(R .id.single_note_widget_lv, serviceIntent)
79- setEmptyView(
80- R .id.single_note_widget_lv,
81- R .id.widget_single_note_placeholder_tv
82- )
83- }
88+ private fun getServiceIntent (context : Context , id : Int ): Intent {
89+ return Intent (context, SingleNoteWidgetService ::class .java).apply {
90+ putExtra(AppWidgetManager .EXTRA_APPWIDGET_ID , id)
91+ val dataFlag = toUri(Intent .URI_INTENT_SCHEME ) + id
92+ val dataUri = dataFlag.toUri()
93+ setData(dataUri)
94+ }
95+ }
8496
85- awm.run {
86- updateAppWidget(appWidgetId, views)
87- notifyAppWidgetViewDataChanged(appWidgetId, R .id.single_note_widget_lv)
88- }
89- }
90- }
97+ private fun getRemoteViews (
98+ context : Context ,
99+ pendingIntent : PendingIntent ,
100+ serviceIntent : Intent
101+ ): RemoteViews {
102+ return RemoteViews (
103+ context.packageName,
104+ R .layout.widget_single_note
105+ ).apply {
106+ setPendingIntentTemplate(R .id.single_note_widget_lv, pendingIntent)
107+ setEmptyView(
108+ R .id.single_note_widget_lv,
109+ R .id.widget_single_note_placeholder_tv
110+ )
111+ setRemoteAdapter(R .id.single_note_widget_lv, serviceIntent)
91112 }
113+ }
92114
115+ companion object {
93116 @JvmStatic
94117 fun updateSingleNoteWidgets (context : Context ) {
95118 val intent = Intent (context, SingleNoteWidget ::class .java).apply {
0 commit comments