• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.AllAppsList;
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.LauncherModel.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.concurrent.Executor;
34 
35 /**
36  * Extension of {@link ModelUpdateTask} with some utility methods
37  */
38 public abstract class BaseModelUpdateTask implements ModelUpdateTask {
39 
40     private static final boolean DEBUG_TASKS = false;
41     private static final String TAG = "BaseModelUpdateTask";
42 
43     private LauncherAppState mApp;
44     private LauncherModel mModel;
45     private BgDataModel mDataModel;
46     private AllAppsList mAllAppsList;
47     private Executor mUiExecutor;
48 
init(LauncherAppState app, LauncherModel model, BgDataModel dataModel, AllAppsList allAppsList, Executor uiExecutor)49     public void init(LauncherAppState app, LauncherModel model,
50             BgDataModel dataModel, AllAppsList allAppsList, Executor uiExecutor) {
51         mApp = app;
52         mModel = model;
53         mDataModel = dataModel;
54         mAllAppsList = allAppsList;
55         mUiExecutor = uiExecutor;
56     }
57 
58     @Override
run()59     public final void run() {
60         if (!mModel.isModelLoaded()) {
61             if (DEBUG_TASKS) {
62                 Log.d(TAG, "Ignoring model task since loader is pending=" + this);
63             }
64             // Loader has not yet run.
65             return;
66         }
67         execute(mApp, mDataModel, mAllAppsList);
68     }
69 
70     /**
71      * Execute the actual task. Called on the worker thread.
72      */
execute( LauncherAppState app, BgDataModel dataModel, AllAppsList apps)73     public abstract void execute(
74             LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
75 
76     /**
77      * Schedules a {@param task} to be executed on the current callbacks.
78      */
scheduleCallbackTask(final CallbackTask task)79     public final void scheduleCallbackTask(final CallbackTask task) {
80         final Callbacks callbacks = mModel.getCallback();
81         mUiExecutor.execute(() -> {
82             Callbacks cb = mModel.getCallback();
83             if (callbacks == cb && cb != null) {
84                 task.execute(callbacks);
85             }
86         });
87     }
88 
getModelWriter()89     public ModelWriter getModelWriter() {
90         // Updates from model task, do not deal with icon position in hotseat. Also no need to
91         // verify changes as the ModelTasks always push the changes to callbacks
92         return mModel.getWriter(false /* hasVerticalHotseat */, false /* verifyChanges */);
93     }
94 
95 
bindUpdatedWorkspaceItems(final ArrayList<WorkspaceItemInfo> updatedShortcuts)96     public void bindUpdatedWorkspaceItems(final ArrayList<WorkspaceItemInfo> updatedShortcuts) {
97         if (!updatedShortcuts.isEmpty()) {
98             scheduleCallbackTask(new CallbackTask() {
99                 @Override
100                 public void execute(Callbacks callbacks) {
101                     callbacks.bindWorkspaceItemsChanged(updatedShortcuts);
102                 }
103             });
104         }
105     }
106 
bindDeepShortcuts(BgDataModel dataModel)107     public void bindDeepShortcuts(BgDataModel dataModel) {
108         final HashMap<ComponentKey, Integer> shortcutMapCopy =
109                 new HashMap<>(dataModel.deepShortcutMap);
110         scheduleCallbackTask(callbacks -> callbacks.bindDeepShortcutMap(shortcutMapCopy));
111     }
112 
bindUpdatedWidgets(BgDataModel dataModel)113     public void bindUpdatedWidgets(BgDataModel dataModel) {
114         final ArrayList<WidgetListRowEntry> widgets =
115                 dataModel.widgetsModel.getWidgetsList(mApp.getContext());
116         scheduleCallbackTask(new CallbackTask() {
117             @Override
118             public void execute(Callbacks callbacks) {
119                 callbacks.bindAllWidgets(widgets);
120             }
121         });
122     }
123 
deleteAndBindComponentsRemoved(final ItemInfoMatcher matcher)124     public void deleteAndBindComponentsRemoved(final ItemInfoMatcher matcher) {
125         getModelWriter().deleteItemsFromDatabase(matcher);
126 
127         // Call the components-removed callback
128         scheduleCallbackTask(new CallbackTask() {
129             @Override
130             public void execute(Callbacks callbacks) {
131                 callbacks.bindWorkspaceComponentsRemoved(matcher);
132             }
133         });
134     }
135 }
136