2929import com .nononsenseapps .notepad .widget .list .ListWidgetProvider ;
3030import com .nononsenseapps .notepad .widget .list .WidgetPrefs ;
3131
32+ import java .util .concurrent .ExecutorService ;
33+ import java .util .concurrent .Executors ;
34+
3235/**
3336 * The purpose here is to make it easy for other classes to notify that
3437 * something has changed in the database. Will also call update on the widgets
@@ -84,6 +87,11 @@ private static void notifyChange(Context context, Uri uri) {
8487 context .getContentResolver ().notifyChange (Notification .URI , null );
8588 }
8689
90+ /**
91+ * Runs code in a separate thread
92+ */
93+ private static final ExecutorService mExecutor = Executors .newSingleThreadExecutor ();
94+
8795 /**
8896 * Instead of doing this in a service which might be killed, simply call
8997 * this whenever something is changed in here
@@ -95,16 +103,21 @@ public static void updateWidgets(Context context) {
95103 int [] appWidgetIds = appWidgetManager .getAppWidgetIds (
96104 new ComponentName (context , ListWidgetProvider .class ));
97105
98- // Tell the widgets that the list items should be invalidated and refreshed!
99- // Will call onDatasetChanged in ListWidgetService, doing a new requery
100-
101- // Only update widgets that exist
102- for (int widgetId : appWidgetIds ) {
103- final WidgetPrefs prefs = new WidgetPrefs (context , widgetId );
104- if (prefs .isPresent ()) {
105- appWidgetManager .notifyAppWidgetViewDataChanged (widgetId , R .id .notesList );
106+ // .notifyAppWidgetViewDataChanged() is a very slow function that takes 40 seconds for
107+ // each list widget present in the launcher. We run it in a background thread. This way,
108+ // we ensure that, when the user taps a checkbox in the app, the note is recognized as
109+ // completed right away, without waiting for every list-widget to update. This fixes #574.
110+ // See onDataSetChanged() in ListWidgetService.java, where the slow query is located.
111+ mExecutor .execute (() -> {
112+ for (int widgetId : appWidgetIds ) { // Only update widgets that exist
113+ final WidgetPrefs prefs = new WidgetPrefs (context , widgetId );
114+ if (prefs .isPresent ()) {
115+ // Tell the widgets that the list items should be invalidated and refreshed!
116+ // Will call onDatasetChanged in ListWidgetService, doing a new requery
117+ appWidgetManager .notifyAppWidgetViewDataChanged (widgetId , R .id .notesList );
118+ }
106119 }
107- }
120+ });
108121
109122 }
110123}
0 commit comments