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.init(context, app, appsList, dataModel, isPrimaryInstance); 48 return delegate; 49 } 50 51 protected Context mContext; 52 protected LauncherAppState mApp; 53 protected AllAppsList mAppsList; 54 protected BgDataModel mDataModel; 55 protected boolean mIsPrimaryInstance; 56 ModelDelegate()57 public ModelDelegate() { } 58 59 /** 60 * Initializes the object with the given params. 61 */ init(Context context, LauncherAppState app, AllAppsList appsList, BgDataModel dataModel, boolean isPrimaryInstance)62 private void init(Context context, LauncherAppState app, AllAppsList appsList, 63 BgDataModel dataModel, boolean isPrimaryInstance) { 64 this.mApp = app; 65 this.mAppsList = appsList; 66 this.mDataModel = dataModel; 67 this.mIsPrimaryInstance = isPrimaryInstance; 68 this.mContext = context; 69 } 70 71 /** 72 * Called periodically to validate and update any data 73 */ 74 @WorkerThread validateData()75 public void validateData() { 76 if (hasShortcutsPermission(mApp.getContext()) 77 != mAppsList.hasShortcutHostPermission()) { 78 mApp.getModel().forceReload(); 79 } 80 } 81 82 /** 83 * Load hot seat items if any in the data model 84 */ 85 @WorkerThread loadHotseatItems(UserManagerState ums, Map<ShortcutKey, ShortcutInfo> pinnedShortcuts)86 public void loadHotseatItems(UserManagerState ums, 87 Map<ShortcutKey, ShortcutInfo> pinnedShortcuts) { } 88 89 /** 90 * Load all apps items if any in the data model 91 */ 92 @WorkerThread loadAllAppsItems(UserManagerState ums, Map<ShortcutKey, ShortcutInfo> pinnedShortcuts)93 public void loadAllAppsItems(UserManagerState ums, 94 Map<ShortcutKey, ShortcutInfo> pinnedShortcuts) { } 95 96 /** 97 * Load widget recommendation items if any in the data model 98 */ 99 @WorkerThread loadWidgetsRecommendationItems()100 public void loadWidgetsRecommendationItems() { } 101 102 /** 103 * Marks the ModelDelegate as active 104 */ markActive()105 public void markActive() { } 106 107 /** 108 * Load String cache 109 */ 110 @WorkerThread loadStringCache(StringCache cache)111 public void loadStringCache(StringCache cache) { 112 cache.loadStrings(mContext); 113 } 114 115 /** 116 * Called during loader after workspace loading is complete 117 */ 118 @WorkerThread workspaceLoadComplete()119 public void workspaceLoadComplete() { } 120 121 /** 122 * Called at the end of model load task 123 */ 124 @WorkerThread modelLoadComplete()125 public void modelLoadComplete() { } 126 127 /** 128 * Called when the delegate is no loner needed 129 */ 130 @WorkerThread destroy()131 public void destroy() { } 132 133 /** 134 * Add data to a dumpsys request for Launcher (e.g. for bug reports). 135 * 136 * @see com.android.launcher3.Launcher#dump(java.lang.String, java.io.FileDescriptor, 137 * java.io.PrintWriter, java.lang.String[]) 138 **/ dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)139 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { } 140 } 141