• 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 
32 import java.util.ArrayList;
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(new Runnable() {
82             public void run() {
83                 Callbacks cb = mModel.getCallback();
84                 if (callbacks == cb && cb != null) {
85                     task.execute(callbacks);
86                 }
87             }
88         });
89     }
90 
getModelWriter()91     public ModelWriter getModelWriter() {
92         // Updates from model task, do not deal with icon position in hotseat.
93         return mModel.getWriter(false /* hasVerticalHotseat */);
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 MultiHashMap<PackageItemInfo, WidgetItem> widgets
121                 = dataModel.widgetsModel.getWidgetsMap();
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