Invalidate stale BgDataModel when predictions are disabled
Launcher stores ItemInfos for cached predictions in list BgDataModel.cachedPredictedItems on Launcher start (LoaderTask#run). The list reused if launcher has to rebind UI. Hence, the list should be cleared if launcher receives empty predictions.
Bug: 161245294
Test: Manual
Change-Id: I275655d5f52f6a6e5297473dd2f642728c1964a6
diff --git a/src/com/android/launcher3/model/PredictionModel.java b/src/com/android/launcher3/model/PredictionModel.java
index 1429843..cb3903d 100644
--- a/src/com/android/launcher3/model/PredictionModel.java
+++ b/src/com/android/launcher3/model/PredictionModel.java
@@ -14,6 +14,7 @@
* limitations under the License.
*/
package com.android.launcher3.model;
+
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
import android.content.ComponentName;
@@ -24,6 +25,7 @@
import androidx.annotation.AnyThread;
import androidx.annotation.WorkerThread;
+import com.android.launcher3.LauncherAppState;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.pm.UserCache;
@@ -64,18 +66,28 @@
mUserCache = UserCache.INSTANCE.get(mContext);
}
+
/**
* Formats and stores a list of component key in device preferences.
*/
@AnyThread
public void cachePredictionComponentKeys(List<ComponentKey> componentKeys) {
MODEL_EXECUTOR.execute(() -> {
+ LauncherAppState appState = LauncherAppState.getInstance(mContext);
StringBuilder builder = new StringBuilder();
int count = Math.min(componentKeys.size(), MAX_CACHE_ITEMS);
for (int i = 0; i < count; i++) {
builder.append(serializeComponentKeyToString(componentKeys.get(i)));
builder.append("\n");
}
+ if (componentKeys.isEmpty() /* should invalidate loader items */) {
+ appState.getModel().enqueueModelUpdateTask(new BaseModelUpdateTask() {
+ @Override
+ public void execute(LauncherAppState app, BgDataModel model, AllAppsList apps) {
+ model.cachedPredictedItems.clear();
+ }
+ });
+ }
mDevicePrefs.edit().putString(CACHED_ITEMS_KEY, builder.toString()).apply();
});
}