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.ComponentName;
     19 import android.os.UserHandle;
     20 
     21 import com.android.launcher3.AllAppsList;
     22 import com.android.launcher3.AppInfo;
     23 import com.android.launcher3.IconCache;
     24 import com.android.launcher3.ItemInfo;
     25 import com.android.launcher3.LauncherAppState;
     26 import com.android.launcher3.LauncherModel.CallbackTask;
     27 import com.android.launcher3.LauncherModel.Callbacks;
     28 import com.android.launcher3.LauncherSettings;
     29 import com.android.launcher3.LauncherSettings.Favorites;
     30 import com.android.launcher3.ShortcutInfo;
     31 
     32 import java.util.ArrayList;
     33 import java.util.HashSet;
     34 
     35 /**
     36  * Handles changes due to cache updates.
     37  */
     38 public class CacheDataUpdatedTask extends ExtendedModelTask {
     39 
     40     public static final int OP_CACHE_UPDATE = 1;
     41     public static final int OP_SESSION_UPDATE = 2;
     42 
     43     private final int mOp;
     44     private final UserHandle mUser;
     45     private final HashSet<String> mPackages;
     46 
     47     public CacheDataUpdatedTask(int op, UserHandle user, HashSet<String> packages) {
     48         mOp = op;
     49         mUser = user;
     50         mPackages = packages;
     51     }
     52 
     53     @Override
     54     public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
     55         IconCache iconCache = app.getIconCache();
     56 
     57         final ArrayList<AppInfo> updatedApps = new ArrayList<>();
     58 
     59         ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
     60         synchronized (dataModel) {
     61             for (ItemInfo info : dataModel.itemsIdMap) {
     62                 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
     63                     ShortcutInfo si = (ShortcutInfo) info;
     64                     ComponentName cn = si.getTargetComponent();
     65                     if (si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
     66                             && isValidShortcut(si) && cn != null
     67                             && mPackages.contains(cn.getPackageName())) {
     68                         iconCache.getTitleAndIcon(si, si.usingLowResIcon);
     69                         updatedShortcuts.add(si);
     70                     }
     71                 }
     72             }
     73             apps.updateIconsAndLabels(mPackages, mUser, updatedApps);
     74         }
     75         bindUpdatedShortcuts(updatedShortcuts, mUser);
     76 
     77         if (!updatedApps.isEmpty()) {
     78             scheduleCallbackTask(new CallbackTask() {
     79                 @Override
     80                 public void execute(Callbacks callbacks) {
     81                     callbacks.bindAppsUpdated(updatedApps);
     82                 }
     83             });
     84         }
     85     }
     86 
     87     public boolean isValidShortcut(ShortcutInfo si) {
     88         switch (mOp) {
     89             case OP_CACHE_UPDATE:
     90                 return true;
     91             case OP_SESSION_UPDATE:
     92                 return si.isPromise();
     93             default:
     94                 return false;
     95         }
     96     }
     97 }
     98