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