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