1 /*
2  * Copyright (C) 2020 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 static com.android.launcher3.util.PackageManagerHelper.hasShortcutsPermission;
19 
20 import android.content.Context;
21 import android.content.pm.ShortcutInfo;
22 
23 import androidx.annotation.WorkerThread;
24 
25 import com.android.launcher3.LauncherAppState;
26 import com.android.launcher3.R;
27 import com.android.launcher3.shortcuts.ShortcutKey;
28 import com.android.launcher3.util.ResourceBasedOverride;
29 
30 import java.io.FileDescriptor;
31 import java.io.PrintWriter;
32 import java.util.Map;
33 
34 /**
35  * Class to extend LauncherModel functionality to provide extra data
36  */
37 public class ModelDelegate implements ResourceBasedOverride {
38 
39     /**
40      * Creates and initializes a new instance of the delegate
41      */
newInstance( Context context, LauncherAppState app, AllAppsList appsList, BgDataModel dataModel, boolean isPrimaryInstance)42     public static ModelDelegate newInstance(
43             Context context, LauncherAppState app, AllAppsList appsList, BgDataModel dataModel,
44             boolean isPrimaryInstance) {
45         ModelDelegate delegate = Overrides.getObject(
46                 ModelDelegate.class, context, R.string.model_delegate_class);
47         delegate.mApp = app;
48         delegate.mAppsList = appsList;
49         delegate.mDataModel = dataModel;
50         delegate.mIsPrimaryInstance = isPrimaryInstance;
51         return delegate;
52     }
53 
54     protected LauncherAppState mApp;
55     protected AllAppsList mAppsList;
56     protected BgDataModel mDataModel;
57     protected boolean mIsPrimaryInstance;
58 
ModelDelegate()59     public ModelDelegate() { }
60 
61     /**
62      * Called periodically to validate and update any data
63      */
64     @WorkerThread
validateData()65     public void validateData() {
66         if (hasShortcutsPermission(mApp.getContext())
67                 != mAppsList.hasShortcutHostPermission()) {
68             mApp.getModel().forceReload();
69         }
70     }
71 
72     /**
73      * Load delegate items if any in the data model
74      */
75     @WorkerThread
loadItems(UserManagerState ums, Map<ShortcutKey, ShortcutInfo> pinnedShortcuts)76     public void loadItems(UserManagerState ums, Map<ShortcutKey, ShortcutInfo> pinnedShortcuts) { }
77 
78     /**
79      * Called during loader after workspace loading is complete
80      */
81     @WorkerThread
workspaceLoadComplete()82     public void workspaceLoadComplete() { }
83 
84     /**
85      * Called at the end of model load task
86      */
87     @WorkerThread
modelLoadComplete()88     public void modelLoadComplete() { }
89 
90     /**
91      * Called when the delegate is no loner needed
92      */
93     @WorkerThread
destroy()94     public void destroy() { }
95 
96     /**
97      * Add data to a dumpsys request for Launcher (e.g. for bug reports).
98      *
99      * @see com.android.launcher3.Launcher#dump(java.lang.String, java.io.FileDescriptor,
100      *                                          java.io.PrintWriter, java.lang.String[])
101      **/
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)102     public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { }
103 }
104