@@ -12,50 +12,40 @@ import com.simplemobiletools.commons.extensions.setBackgroundColor
1212import com.simplemobiletools.notes.R
1313import com.simplemobiletools.notes.activities.SplashActivity
1414import com.simplemobiletools.notes.extensions.config
15+ import com.simplemobiletools.notes.extensions.dbHelper
1516import com.simplemobiletools.notes.services.WidgetService
1617
1718class MyWidgetProvider : AppWidgetProvider () {
18- private fun performUpdate (context : Context , intent : Intent ) {
19- val noteId = intent.getIntExtra(NOTE_ID , - 1 )
20- val widgetId = intent.getIntExtra(WIDGET_ID , - 1 )
21- if (noteId == - 1 || widgetId == - 1 ) {
22- return
23- }
24-
25- val appWidgetManager = AppWidgetManager .getInstance(context)
26- val views = RemoteViews (context.packageName, R .layout.widget)
27- views.setBackgroundColor(R .id.notes_widget_holder, context.config.widgetBgColor)
28- setupAppOpenIntent(context, views, R .id.notes_widget_holder)
29-
30- Intent (context, WidgetService ::class .java).apply {
31- putExtra(NOTE_ID , noteId)
32- data = Uri .parse(this .toUri(Intent .URI_INTENT_SCHEME ))
33- views.setRemoteAdapter(R .id.notes_widget_listview, this )
34- }
35-
36- val startActivityIntent = context.getLaunchIntent() ? : Intent (context, SplashActivity ::class .java)
37- startActivityIntent.putExtra(OPEN_NOTE_ID , widgetId)
38- val startActivityPendingIntent = PendingIntent .getActivity(context, widgetId, startActivityIntent, PendingIntent .FLAG_UPDATE_CURRENT )
39- views.setPendingIntentTemplate(R .id.notes_widget_listview, startActivityPendingIntent)
40-
41- appWidgetManager.updateAppWidget(widgetId, views)
42- appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R .id.notes_widget_listview)
43- }
44-
45- private fun setupAppOpenIntent (context : Context , views : RemoteViews , id : Int ) {
46- val widgetId = context.config.widgetNoteId
19+ private fun setupAppOpenIntent (context : Context , views : RemoteViews , id : Int , noteId : Int ) {
4720 val intent = context.getLaunchIntent() ? : Intent (context, SplashActivity ::class .java)
48- intent.putExtra(OPEN_NOTE_ID , widgetId )
49- val pendingIntent = PendingIntent .getActivity(context, widgetId , intent, 0 )
21+ intent.putExtra(OPEN_NOTE_ID , noteId )
22+ val pendingIntent = PendingIntent .getActivity(context, noteId , intent, PendingIntent . FLAG_UPDATE_CURRENT )
5023 views.setOnClickPendingIntent(id, pendingIntent)
5124 }
5225
53- // use only this way of updating widgets instead of onUpdate, so that we can pass a widget ID too
54- override fun onReceive (context : Context , intent : Intent ) {
55- val action = intent.action
56- when (action) {
57- UPDATE_WIDGET -> performUpdate(context, intent)
58- else -> super .onReceive(context, intent)
26+ override fun onUpdate (context : Context , appWidgetManager : AppWidgetManager , appWidgetIds : IntArray ) {
27+ super .onUpdate(context, appWidgetManager, appWidgetIds)
28+ val widgets = context.dbHelper.getWidgets()
29+ widgets.forEach {
30+ val widgetId = it.widgetId
31+ val noteId = it.noteId
32+ val views = RemoteViews (context.packageName, R .layout.widget)
33+ views.setBackgroundColor(R .id.notes_widget_holder, context.config.widgetBgColor)
34+ setupAppOpenIntent(context, views, R .id.notes_widget_holder, noteId)
35+
36+ Intent (context, WidgetService ::class .java).apply {
37+ putExtra(NOTE_ID , noteId)
38+ data = Uri .parse(this .toUri(Intent .URI_INTENT_SCHEME ))
39+ views.setRemoteAdapter(R .id.notes_widget_listview, this )
40+ }
41+
42+ val startActivityIntent = context.getLaunchIntent() ? : Intent (context, SplashActivity ::class .java)
43+ startActivityIntent.putExtra(OPEN_NOTE_ID , widgetId)
44+ val startActivityPendingIntent = PendingIntent .getActivity(context, widgetId, startActivityIntent, PendingIntent .FLAG_UPDATE_CURRENT )
45+ views.setPendingIntentTemplate(R .id.notes_widget_listview, startActivityPendingIntent)
46+
47+ appWidgetManager.updateAppWidget(widgetId, views)
48+ appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R .id.notes_widget_listview)
5949 }
6050 }
6151}
0 commit comments