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