1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.launcher3.model; 17 18 import android.util.Log; 19 20 import com.android.launcher3.LauncherAppState; 21 import com.android.launcher3.LauncherModel; 22 import com.android.launcher3.LauncherModel.CallbackTask; 23 import com.android.launcher3.LauncherModel.ModelUpdateTask; 24 import com.android.launcher3.model.BgDataModel.Callbacks; 25 import com.android.launcher3.model.BgDataModel.FixedContainerItems; 26 import com.android.launcher3.model.data.AppInfo; 27 import com.android.launcher3.model.data.ItemInfo; 28 import com.android.launcher3.model.data.WorkspaceItemInfo; 29 import com.android.launcher3.util.ComponentKey; 30 import com.android.launcher3.util.ItemInfoMatcher; 31 import com.android.launcher3.widget.model.WidgetsListBaseEntry; 32 33 import java.util.ArrayList; 34 import java.util.HashMap; 35 import java.util.List; 36 import java.util.Objects; 37 import java.util.concurrent.Executor; 38 import java.util.stream.Collectors; 39 40 /** 41 * Extension of {@link ModelUpdateTask} with some utility methods 42 */ 43 public abstract class BaseModelUpdateTask implements ModelUpdateTask { 44 45 private static final boolean DEBUG_TASKS = false; 46 private static final String TAG = "BaseModelUpdateTask"; 47 48 private LauncherAppState mApp; 49 private LauncherModel mModel; 50 private BgDataModel mDataModel; 51 private AllAppsList mAllAppsList; 52 private Executor mUiExecutor; 53 init(LauncherAppState app, LauncherModel model, BgDataModel dataModel, AllAppsList allAppsList, Executor uiExecutor)54 public void init(LauncherAppState app, LauncherModel model, 55 BgDataModel dataModel, AllAppsList allAppsList, Executor uiExecutor) { 56 mApp = app; 57 mModel = model; 58 mDataModel = dataModel; 59 mAllAppsList = allAppsList; 60 mUiExecutor = uiExecutor; 61 } 62 63 @Override run()64 public final void run() { 65 if (!mModel.isModelLoaded()) { 66 if (DEBUG_TASKS) { 67 Log.d(TAG, "Ignoring model task since loader is pending=" + this); 68 } 69 // Loader has not yet run. 70 return; 71 } 72 execute(mApp, mDataModel, mAllAppsList); 73 } 74 75 /** 76 * Execute the actual task. Called on the worker thread. 77 */ execute( LauncherAppState app, BgDataModel dataModel, AllAppsList apps)78 public abstract void execute( 79 LauncherAppState app, BgDataModel dataModel, AllAppsList apps); 80 81 /** 82 * Schedules a {@param task} to be executed on the current callbacks. 83 */ scheduleCallbackTask(final CallbackTask task)84 public final void scheduleCallbackTask(final CallbackTask task) { 85 for (final Callbacks cb : mModel.getCallbacks()) { 86 mUiExecutor.execute(() -> task.execute(cb)); 87 } 88 } 89 getModelWriter()90 public ModelWriter getModelWriter() { 91 // Updates from model task, do not deal with icon position in hotseat. Also no need to 92 // verify changes as the ModelTasks always push the changes to callbacks 93 return mModel.getWriter(false /* hasVerticalHotseat */, false /* verifyChanges */); 94 } 95 bindUpdatedWorkspaceItems(List<WorkspaceItemInfo> allUpdates)96 public void bindUpdatedWorkspaceItems(List<WorkspaceItemInfo> allUpdates) { 97 // Bind workspace items 98 List<WorkspaceItemInfo> workspaceUpdates = allUpdates.stream() 99 .filter(info -> info.id != ItemInfo.NO_ID) 100 .collect(Collectors.toList()); 101 if (!workspaceUpdates.isEmpty()) { 102 scheduleCallbackTask(c -> c.bindWorkspaceItemsChanged(workspaceUpdates)); 103 } 104 105 // Bind extra items if any 106 allUpdates.stream() 107 .mapToInt(info -> info.container) 108 .distinct() 109 .mapToObj(mDataModel.extraItems::get) 110 .filter(Objects::nonNull) 111 .forEach(this::bindExtraContainerItems); 112 } 113 bindExtraContainerItems(FixedContainerItems item)114 public void bindExtraContainerItems(FixedContainerItems item) { 115 FixedContainerItems copy = item.clone(); 116 scheduleCallbackTask(c -> c.bindExtraContainerItems(copy)); 117 } 118 bindDeepShortcuts(BgDataModel dataModel)119 public void bindDeepShortcuts(BgDataModel dataModel) { 120 final HashMap<ComponentKey, Integer> shortcutMapCopy = 121 new HashMap<>(dataModel.deepShortcutMap); 122 scheduleCallbackTask(callbacks -> callbacks.bindDeepShortcutMap(shortcutMapCopy)); 123 } 124 bindUpdatedWidgets(BgDataModel dataModel)125 public void bindUpdatedWidgets(BgDataModel dataModel) { 126 final ArrayList<WidgetsListBaseEntry> widgets = 127 dataModel.widgetsModel.getWidgetsListForPicker(mApp.getContext()); 128 scheduleCallbackTask(c -> c.bindAllWidgets(widgets)); 129 } 130 deleteAndBindComponentsRemoved(final ItemInfoMatcher matcher)131 public void deleteAndBindComponentsRemoved(final ItemInfoMatcher matcher) { 132 getModelWriter().deleteItemsFromDatabase(matcher); 133 134 // Call the components-removed callback 135 scheduleCallbackTask(c -> c.bindWorkspaceComponentsRemoved(matcher)); 136 } 137 bindApplicationsIfNeeded()138 public void bindApplicationsIfNeeded() { 139 if (mAllAppsList.getAndResetChangeFlag()) { 140 AppInfo[] apps = mAllAppsList.copyData(); 141 int flags = mAllAppsList.getFlags(); 142 scheduleCallbackTask(c -> c.bindAllApplications(apps, flags)); 143 } 144 } 145 } 146