• 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.os.UserHandle;
19 import android.util.Log;
20 
21 import com.android.launcher3.AllAppsList;
22 import com.android.launcher3.LauncherAppState;
23 import com.android.launcher3.LauncherModel;
24 import com.android.launcher3.LauncherModel.ModelUpdateTask;
25 import com.android.launcher3.LauncherModel.CallbackTask;
26 import com.android.launcher3.LauncherModel.Callbacks;
27 import com.android.launcher3.ShortcutInfo;
28 import com.android.launcher3.util.ComponentKey;
29 import com.android.launcher3.util.ItemInfoMatcher;
30 import com.android.launcher3.util.MultiHashMap;
31 import com.android.launcher3.widget.WidgetListRowEntry;
32 
33 import java.util.ArrayList;
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 
bindUpdatedShortcuts( final ArrayList<ShortcutInfo> updatedShortcuts, final UserHandle user)97     public void bindUpdatedShortcuts(
98             final ArrayList<ShortcutInfo> updatedShortcuts, final UserHandle user) {
99         if (!updatedShortcuts.isEmpty()) {
100             scheduleCallbackTask(new CallbackTask() {
101                 @Override
102                 public void execute(Callbacks callbacks) {
103                     callbacks.bindShortcutsChanged(updatedShortcuts, user);
104                 }
105             });
106         }
107     }
108 
bindDeepShortcuts(BgDataModel dataModel)109     public void bindDeepShortcuts(BgDataModel dataModel) {
110         final MultiHashMap<ComponentKey, String> shortcutMapCopy = dataModel.deepShortcutMap.clone();
111         scheduleCallbackTask(new CallbackTask() {
112             @Override
113             public void execute(Callbacks callbacks) {
114                 callbacks.bindDeepShortcutMap(shortcutMapCopy);
115             }
116         });
117     }
118 
bindUpdatedWidgets(BgDataModel dataModel)119     public void bindUpdatedWidgets(BgDataModel dataModel) {
120         final ArrayList<WidgetListRowEntry> widgets =
121                 dataModel.widgetsModel.getWidgetsList(mApp.getContext());
122         scheduleCallbackTask(new CallbackTask() {
123             @Override
124             public void execute(Callbacks callbacks) {
125                 callbacks.bindAllWidgets(widgets);
126             }
127         });
128     }
129 
deleteAndBindComponentsRemoved(final ItemInfoMatcher matcher)130     public void deleteAndBindComponentsRemoved(final ItemInfoMatcher matcher) {
131         getModelWriter().deleteItemsFromDatabase(matcher);
132 
133         // Call the components-removed callback
134         scheduleCallbackTask(new CallbackTask() {
135             @Override
136             public void execute(Callbacks callbacks) {
137                 callbacks.bindWorkspaceComponentsRemoved(matcher);
138             }
139         });
140     }
141 }
142