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.AppInfo;
21 import com.android.launcher3.LauncherAppState;
22 import com.android.launcher3.LauncherModel;
23 import com.android.launcher3.LauncherModel.ModelUpdateTask;
24 import com.android.launcher3.LauncherModel.CallbackTask;
25 import com.android.launcher3.model.BgDataModel.Callbacks;
26 import com.android.launcher3.WorkspaceItemInfo;
27 import com.android.launcher3.util.ComponentKey;
28 import com.android.launcher3.util.ItemInfoMatcher;
29 import com.android.launcher3.widget.WidgetListRowEntry;
30 
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.concurrent.Executor;
35 
36 /**
37  * Extension of {@link ModelUpdateTask} with some utility methods
38  */
39 public abstract class BaseModelUpdateTask implements ModelUpdateTask {
40 
41     private static final boolean DEBUG_TASKS = false;
42     private static final String TAG = "BaseModelUpdateTask";
43 
44     private LauncherAppState mApp;
45     private LauncherModel mModel;
46     private BgDataModel mDataModel;
47     private AllAppsList mAllAppsList;
48     private Executor mUiExecutor;
49 
init(LauncherAppState app, LauncherModel model, BgDataModel dataModel, AllAppsList allAppsList, Executor uiExecutor)50     public void init(LauncherAppState app, LauncherModel model,
51             BgDataModel dataModel, AllAppsList allAppsList, Executor uiExecutor) {
52         mApp = app;
53         mModel = model;
54         mDataModel = dataModel;
55         mAllAppsList = allAppsList;
56         mUiExecutor = uiExecutor;
57     }
58 
59     @Override
run()60     public final void run() {
61         if (!mModel.isModelLoaded()) {
62             if (DEBUG_TASKS) {
63                 Log.d(TAG, "Ignoring model task since loader is pending=" + this);
64             }
65             // Loader has not yet run.
66             return;
67         }
68         execute(mApp, mDataModel, mAllAppsList);
69     }
70 
71     /**
72      * Execute the actual task. Called on the worker thread.
73      */
execute( LauncherAppState app, BgDataModel dataModel, AllAppsList apps)74     public abstract void execute(
75             LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
76 
77     /**
78      * Schedules a {@param task} to be executed on the current callbacks.
79      */
scheduleCallbackTask(final CallbackTask task)80     public final void scheduleCallbackTask(final CallbackTask task) {
81         final Callbacks callbacks = mModel.getCallback();
82         mUiExecutor.execute(() -> {
83             Callbacks cb = mModel.getCallback();
84             if (callbacks == cb && cb != null) {
85                 task.execute(callbacks);
86             }
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 
96 
bindUpdatedWorkspaceItems(final ArrayList<WorkspaceItemInfo> updatedShortcuts)97     public void bindUpdatedWorkspaceItems(final ArrayList<WorkspaceItemInfo> updatedShortcuts) {
98         if (!updatedShortcuts.isEmpty()) {
99             scheduleCallbackTask(c -> c.bindWorkspaceItemsChanged(updatedShortcuts));
100         }
101     }
102 
bindDeepShortcuts(BgDataModel dataModel)103     public void bindDeepShortcuts(BgDataModel dataModel) {
104         final HashMap<ComponentKey, Integer> shortcutMapCopy =
105                 new HashMap<>(dataModel.deepShortcutMap);
106         scheduleCallbackTask(callbacks -> callbacks.bindDeepShortcutMap(shortcutMapCopy));
107     }
108 
bindUpdatedWidgets(BgDataModel dataModel)109     public void bindUpdatedWidgets(BgDataModel dataModel) {
110         final ArrayList<WidgetListRowEntry> widgets =
111                 dataModel.widgetsModel.getWidgetsList(mApp.getContext());
112         scheduleCallbackTask(c -> c.bindAllWidgets(widgets));
113     }
114 
deleteAndBindComponentsRemoved(final ItemInfoMatcher matcher)115     public void deleteAndBindComponentsRemoved(final ItemInfoMatcher matcher) {
116         getModelWriter().deleteItemsFromDatabase(matcher);
117 
118         // Call the components-removed callback
119         scheduleCallbackTask(c -> c.bindWorkspaceComponentsRemoved(matcher));
120     }
121 
bindApplicationsIfNeeded()122     public void bindApplicationsIfNeeded() {
123         if (mAllAppsList.getAndResetChangeFlag()) {
124             AppInfo[] apps = mAllAppsList.copyData();
125             scheduleCallbackTask(c -> c.bindAllApplications(apps));
126         }
127     }
128 }
129