Synchronizing model data access

Test: Presubmit
Bug: 173328873
Change-Id: Ifc1724e17ebfa2ee5f21bb3f272986b8a14fdbc1
This commit is contained in:
Sunny Goyal
2020-12-10 16:22:41 -08:00
parent cbf8fe08da
commit cfcefb0c47
4 changed files with 25 additions and 17 deletions

View File

@@ -52,8 +52,7 @@ public class HotseatPredictionModel {
public static Bundle convertDataModelToAppTargetBundle(Context context, BgDataModel dataModel) {
Bundle bundle = new Bundle();
ArrayList<AppTargetEvent> events = new ArrayList<>();
ArrayList<ItemInfo> workspaceItems = new ArrayList<>(dataModel.workspaceItems);
workspaceItems.addAll(dataModel.appWidgets);
ArrayList<ItemInfo> workspaceItems = dataModel.getAllWorkspaceItems();
for (ItemInfo item : workspaceItems) {
AppTarget target = getAppTargetFromInfo(context, item);
if (target != null && !isTrackedForPrediction(item)) continue;

View File

@@ -629,11 +629,13 @@ public class LauncherPreviewRenderer extends ContextThemeWrapper
private WorkspaceResult(BgDataModel dataModel,
WidgetsModel widgetsModel,
Map<ComponentKey, AppWidgetProviderInfo> widgetProviderInfoMap) {
mWorkspaceItems = dataModel.workspaceItems;
mAppWidgets = dataModel.appWidgets;
mHotseatPredictions = dataModel.extraItems.get(CONTAINER_HOTSEAT_PREDICTION);
mWidgetsModel = widgetsModel;
mWidgetProvidersMap = widgetProviderInfoMap;
synchronized (dataModel) {
mWorkspaceItems = dataModel.workspaceItems;
mAppWidgets = dataModel.appWidgets;
mHotseatPredictions = dataModel.extraItems.get(CONTAINER_HOTSEAT_PREDICTION);
mWidgetsModel = widgetsModel;
mWidgetProvidersMap = widgetProviderInfoMap;
}
}
}
}

View File

@@ -375,6 +375,16 @@ public class BgDataModel {
}
}
/**
* Returns a list containing all workspace items including widgets.
*/
public synchronized ArrayList<ItemInfo> getAllWorkspaceItems() {
ArrayList<ItemInfo> items = new ArrayList<>(workspaceItems.size() + appWidgets.size());
items.addAll(workspaceItems);
items.addAll(appWidgets);
return items;
}
/**
* Calls the provided {@code op} for all workspaceItems in the in-memory model (both persisted
* items and dynamic/predicted items for the provided {@code userHandle}.

View File

@@ -166,12 +166,7 @@ public class LoaderTask implements Runnable {
private void sendFirstScreenActiveInstallsBroadcast() {
ArrayList<ItemInfo> firstScreenItems = new ArrayList<>();
ArrayList<ItemInfo> allItems = new ArrayList<>();
synchronized (mBgDataModel) {
allItems.addAll(mBgDataModel.workspaceItems);
allItems.addAll(mBgDataModel.appWidgets);
}
ArrayList<ItemInfo> allItems = mBgDataModel.getAllWorkspaceItems();
// Screen set is never empty
final int firstScreen = mBgDataModel.collectWorkspaceScreens().get(0);
@@ -858,10 +853,12 @@ public class LoaderTask implements Runnable {
.call(contentResolver,
LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
.getIntArray(LauncherSettings.Settings.EXTRA_VALUE);
for (int folderId : deletedFolderIds) {
mBgDataModel.workspaceItems.remove(mBgDataModel.folders.get(folderId));
mBgDataModel.folders.remove(folderId);
mBgDataModel.itemsIdMap.remove(folderId);
synchronized (mBgDataModel) {
for (int folderId : deletedFolderIds) {
mBgDataModel.workspaceItems.remove(mBgDataModel.folders.get(folderId));
mBgDataModel.folders.remove(folderId);
mBgDataModel.itemsIdMap.remove(folderId);
}
}
// Remove any ghost widgets