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.NonNull; 24 import androidx.annotation.WorkerThread; 25 26 import com.android.launcher3.LauncherAppState; 27 import com.android.launcher3.R; 28 import com.android.launcher3.shortcuts.ShortcutKey; 29 import com.android.launcher3.util.PackageManagerHelper; 30 import com.android.launcher3.util.ResourceBasedOverride; 31 32 import java.io.FileDescriptor; 33 import java.io.PrintWriter; 34 import java.util.Map; 35 36 /** 37 * Class to extend LauncherModel functionality to provide extra data 38 */ 39 public class ModelDelegate implements ResourceBasedOverride { 40 41 /** 42 * Creates and initializes a new instance of the delegate 43 */ newInstance( Context context, LauncherAppState app, PackageManagerHelper pmHelper, AllAppsList appsList, BgDataModel dataModel, boolean isPrimaryInstance)44 public static ModelDelegate newInstance( 45 Context context, LauncherAppState app, PackageManagerHelper pmHelper, 46 AllAppsList appsList, BgDataModel dataModel, boolean isPrimaryInstance) { 47 ModelDelegate delegate = Overrides.getObject( 48 ModelDelegate.class, context, R.string.model_delegate_class); 49 delegate.init(app, pmHelper, appsList, dataModel, isPrimaryInstance); 50 return delegate; 51 } 52 53 protected final Context mContext; 54 protected PackageManagerHelper mPmHelper; 55 protected LauncherAppState mApp; 56 protected AllAppsList mAppsList; 57 protected BgDataModel mDataModel; 58 protected boolean mIsPrimaryInstance; 59 ModelDelegate(Context context)60 public ModelDelegate(Context context) { 61 mContext = context; 62 } 63 64 /** 65 * Initializes the object with the given params. 66 */ init(LauncherAppState app, PackageManagerHelper pmHelper, AllAppsList appsList, BgDataModel dataModel, boolean isPrimaryInstance)67 private void init(LauncherAppState app, PackageManagerHelper pmHelper, AllAppsList appsList, 68 BgDataModel dataModel, boolean isPrimaryInstance) { 69 this.mApp = app; 70 this.mPmHelper = pmHelper; 71 this.mAppsList = appsList; 72 this.mDataModel = dataModel; 73 this.mIsPrimaryInstance = isPrimaryInstance; 74 } 75 76 /** Called periodically to validate and update any data */ 77 @WorkerThread validateData()78 public void validateData() { 79 if (hasShortcutsPermission(mApp.getContext()) 80 != mAppsList.hasShortcutHostPermission()) { 81 mApp.getModel().forceReload(); 82 } 83 } 84 85 /** Load workspace items (for example, those in the hot seat) if any in the data model */ 86 @WorkerThread loadAndBindWorkspaceItems(@onNull UserManagerState ums, @NonNull BgDataModel.Callbacks[] callbacks, @NonNull Map<ShortcutKey, ShortcutInfo> pinnedShortcuts)87 public void loadAndBindWorkspaceItems(@NonNull UserManagerState ums, 88 @NonNull BgDataModel.Callbacks[] callbacks, 89 @NonNull Map<ShortcutKey, ShortcutInfo> pinnedShortcuts) { } 90 91 /** Load all apps items if any in the data model */ 92 @WorkerThread loadAndBindAllAppsItems(@onNull UserManagerState ums, @NonNull BgDataModel.Callbacks[] callbacks, @NonNull Map<ShortcutKey, ShortcutInfo> pinnedShortcuts)93 public void loadAndBindAllAppsItems(@NonNull UserManagerState ums, 94 @NonNull BgDataModel.Callbacks[] callbacks, 95 @NonNull Map<ShortcutKey, ShortcutInfo> pinnedShortcuts) { } 96 97 /** Load other items like widget recommendations if any in the data model */ 98 @WorkerThread loadAndBindOtherItems(@onNull BgDataModel.Callbacks[] callbacks)99 public void loadAndBindOtherItems(@NonNull BgDataModel.Callbacks[] callbacks) { } 100 101 /** binds everything not bound by launcherBinder */ 102 @WorkerThread bindAllModelExtras(@onNull BgDataModel.Callbacks[] callbacks)103 public void bindAllModelExtras(@NonNull BgDataModel.Callbacks[] callbacks) { } 104 105 /** Marks the ModelDelegate as active */ markActive()106 public void markActive() { } 107 108 /** Load String cache */ 109 @WorkerThread loadStringCache(@onNull StringCache cache)110 public void loadStringCache(@NonNull StringCache cache) { 111 cache.loadStrings(mContext); 112 } 113 114 /** 115 * Called during loader after workspace loading is complete 116 */ 117 @WorkerThread workspaceLoadComplete()118 public void workspaceLoadComplete() { } 119 120 /** 121 * Called at the end of model load task 122 */ 123 @WorkerThread modelLoadComplete()124 public void modelLoadComplete() { } 125 126 /** 127 * Called when the delegate is no loner needed 128 */ 129 @WorkerThread destroy()130 public void destroy() { } 131 132 /** 133 * Add data to a dumpsys request for Launcher (e.g. for bug reports). 134 * 135 * @see com.android.launcher3.Launcher#dump(java.lang.String, java.io.FileDescriptor, 136 * java.io.PrintWriter, java.lang.String[]) 137 **/ dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)138 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { } 139 } 140