1 /* 2 * Copyright (C) 2016 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 android.content.Context; 19 import android.os.UserHandle; 20 21 import com.android.launcher3.AllAppsList; 22 import com.android.launcher3.ItemInfo; 23 import com.android.launcher3.LauncherAppState; 24 import com.android.launcher3.LauncherModel; 25 import com.android.launcher3.LauncherSettings; 26 import com.android.launcher3.ShortcutInfo; 27 import com.android.launcher3.compat.UserManagerCompat; 28 import com.android.launcher3.graphics.LauncherIcons; 29 import com.android.launcher3.shortcuts.DeepShortcutManager; 30 import com.android.launcher3.shortcuts.ShortcutInfoCompat; 31 import com.android.launcher3.shortcuts.ShortcutKey; 32 import com.android.launcher3.util.ComponentKey; 33 34 import java.util.ArrayList; 35 import java.util.HashMap; 36 import java.util.Iterator; 37 import java.util.List; 38 39 /** 40 * Task to handle changing of lock state of the user 41 */ 42 public class UserLockStateChangedTask extends ExtendedModelTask { 43 44 private final UserHandle mUser; 45 46 public UserLockStateChangedTask(UserHandle user) { 47 mUser = user; 48 } 49 50 @Override 51 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) { 52 Context context = app.getContext(); 53 boolean isUserUnlocked = UserManagerCompat.getInstance(context).isUserUnlocked(mUser); 54 DeepShortcutManager deepShortcutManager = DeepShortcutManager.getInstance(context); 55 56 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>(); 57 if (isUserUnlocked) { 58 List<ShortcutInfoCompat> shortcuts = 59 deepShortcutManager.queryForPinnedShortcuts(null, mUser); 60 if (deepShortcutManager.wasLastCallSuccess()) { 61 for (ShortcutInfoCompat shortcut : shortcuts) { 62 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut); 63 } 64 } else { 65 // Shortcut manager can fail due to some race condition when the lock state 66 // changes too frequently. For the purpose of the update, 67 // consider it as still locked. 68 isUserUnlocked = false; 69 } 70 } 71 72 // Update the workspace to reflect the changes to updated shortcuts residing on it. 73 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>(); 74 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>(); 75 for (ItemInfo itemInfo : dataModel.itemsIdMap) { 76 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT 77 && mUser.equals(itemInfo.user)) { 78 ShortcutInfo si = (ShortcutInfo) itemInfo; 79 if (isUserUnlocked) { 80 ShortcutInfoCompat shortcut = pinnedShortcuts.get(ShortcutKey.fromItemInfo(si)); 81 // We couldn't verify the shortcut during loader. If its no longer available 82 // (probably due to clear data), delete the workspace item as well 83 if (shortcut == null) { 84 deletedShortcutInfos.add(si); 85 continue; 86 } 87 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER; 88 si.updateFromDeepShortcutInfo(shortcut, context); 89 si.iconBitmap = LauncherIcons.createShortcutIcon(shortcut, context); 90 } else { 91 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER; 92 } 93 updatedShortcutInfos.add(si); 94 } 95 } 96 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser); 97 if (!deletedShortcutInfos.isEmpty()) { 98 getModelWriter().deleteItemsFromDatabase(deletedShortcutInfos); 99 } 100 101 // Remove shortcut id map for that user 102 Iterator<ComponentKey> keysIter = dataModel.deepShortcutMap.keySet().iterator(); 103 while (keysIter.hasNext()) { 104 if (keysIter.next().user.equals(mUser)) { 105 keysIter.remove(); 106 } 107 } 108 109 if (isUserUnlocked) { 110 dataModel.updateDeepShortcutMap( 111 null, mUser, deepShortcutManager.queryForAllShortcuts(mUser)); 112 } 113 bindDeepShortcuts(dataModel); 114 } 115 } 116