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