Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 18 | |
| 19 | import android.content.ComponentName; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 20 | import android.content.Context; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 21 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 22 | import com.android.launcher3.compat.LauncherActivityInfoCompat; |
| 23 | import com.android.launcher3.compat.LauncherAppsCompat; |
| 24 | import com.android.launcher3.compat.UserHandleCompat; |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 25 | import com.android.launcher3.util.FlagOp; |
Sunny Goyal | da891c1 | 2016-03-18 18:29:24 -0700 | [diff] [blame] | 26 | import com.android.launcher3.util.StringFilter; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 27 | |
Michael Jurka | 34c2e6c | 2013-12-13 16:07:45 +0100 | [diff] [blame] | 28 | import java.util.ArrayList; |
Sunny Goyal | 77919b9 | 2015-05-06 16:53:21 -0700 | [diff] [blame] | 29 | import java.util.HashSet; |
Michael Jurka | 34c2e6c | 2013-12-13 16:07:45 +0100 | [diff] [blame] | 30 | import java.util.List; |
| 31 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Stores the list of all applications for the all apps view. |
| 35 | */ |
| 36 | class AllAppsList { |
| 37 | public static final int DEFAULT_APPLICATIONS_NUMBER = 42; |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 38 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 39 | /** The list off all apps. */ |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 40 | public ArrayList<AppInfo> data = |
| 41 | new ArrayList<AppInfo>(DEFAULT_APPLICATIONS_NUMBER); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 42 | /** The list of apps that have been added since the last notify() call. */ |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 43 | public ArrayList<AppInfo> added = |
| 44 | new ArrayList<AppInfo>(DEFAULT_APPLICATIONS_NUMBER); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 45 | /** The list of apps that have been removed since the last notify() call. */ |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 46 | public ArrayList<AppInfo> removed = new ArrayList<AppInfo>(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 47 | /** The list of apps that have been modified since the last notify() call. */ |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 48 | public ArrayList<AppInfo> modified = new ArrayList<AppInfo>(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 49 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 50 | private IconCache mIconCache; |
| 51 | |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 52 | private AppFilter mAppFilter; |
| 53 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 54 | /** |
| 55 | * Boring constructor. |
| 56 | */ |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 57 | public AllAppsList(IconCache iconCache, AppFilter appFilter) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 58 | mIconCache = iconCache; |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 59 | mAppFilter = appFilter; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Add the supplied ApplicationInfo objects to the list, and enqueue it into the |
| 64 | * list to broadcast when notify() is called. |
Joe Onorato | d65d08e | 2010-04-20 15:43:37 -0400 | [diff] [blame] | 65 | * |
| 66 | * If the app is already in the list, doesn't add it. |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 67 | */ |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 68 | public void add(AppInfo info) { |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 69 | if (mAppFilter != null && !mAppFilter.shouldShowApp(info.componentName)) { |
| 70 | return; |
| 71 | } |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 72 | if (findActivity(data, info.componentName, info.user)) { |
Joe Onorato | d65d08e | 2010-04-20 15:43:37 -0400 | [diff] [blame] | 73 | return; |
| 74 | } |
Daniel Sandler | 054019d | 2010-04-15 16:28:40 -0400 | [diff] [blame] | 75 | data.add(info); |
| 76 | added.add(info); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 77 | } |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 78 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 79 | public void clear() { |
| 80 | data.clear(); |
| 81 | // TODO: do we clear these too? |
| 82 | added.clear(); |
| 83 | removed.clear(); |
| 84 | modified.clear(); |
| 85 | } |
| 86 | |
| 87 | public int size() { |
| 88 | return data.size(); |
| 89 | } |
| 90 | |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 91 | public AppInfo get(int index) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 92 | return data.get(index); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Add the icons for the supplied apk called packageName. |
| 97 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 98 | public void addPackage(Context context, String packageName, UserHandleCompat user) { |
| 99 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context); |
| 100 | final List<LauncherActivityInfoCompat> matches = launcherApps.getActivityList(packageName, |
| 101 | user); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 102 | |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 103 | for (LauncherActivityInfoCompat info : matches) { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 104 | add(new AppInfo(context, info, user, mIconCache)); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Remove the apps for the given apk identified by packageName. |
| 110 | */ |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 111 | public void removePackage(String packageName, UserHandleCompat user) { |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 112 | final List<AppInfo> data = this.data; |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 113 | for (int i = data.size() - 1; i >= 0; i--) { |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 114 | AppInfo info = data.get(i); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 115 | final ComponentName component = info.intent.getComponent(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 116 | if (info.user.equals(user) && packageName.equals(component.getPackageName())) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 117 | removed.add(info); |
| 118 | data.remove(i); |
| 119 | } |
| 120 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 121 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 122 | |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 123 | /** |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 124 | * Updates the apps for the given packageName and user based on {@param op}. |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 125 | */ |
Sunny Goyal | da891c1 | 2016-03-18 18:29:24 -0700 | [diff] [blame] | 126 | public void updatePackageFlags(StringFilter pkgFilter, UserHandleCompat user, FlagOp op) { |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 127 | final List<AppInfo> data = this.data; |
| 128 | for (int i = data.size() - 1; i >= 0; i--) { |
| 129 | AppInfo info = data.get(i); |
| 130 | final ComponentName component = info.intent.getComponent(); |
Sunny Goyal | da891c1 | 2016-03-18 18:29:24 -0700 | [diff] [blame] | 131 | if (info.user.equals(user) && pkgFilter.matches(component.getPackageName())) { |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 132 | info.isDisabled = op.apply(info.isDisabled); |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 133 | modified.add(info); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
Sunny Goyal | 77919b9 | 2015-05-06 16:53:21 -0700 | [diff] [blame] | 138 | public void updateIconsAndLabels(HashSet<String> packages, UserHandleCompat user, |
| 139 | ArrayList<AppInfo> outUpdates) { |
| 140 | for (AppInfo info : data) { |
| 141 | if (info.user.equals(user) && packages.contains(info.componentName.getPackageName())) { |
| 142 | mIconCache.updateTitleAndIcon(info); |
| 143 | outUpdates.add(info); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 148 | /** |
| 149 | * Add and remove icons for this package which has been updated. |
| 150 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 151 | public void updatePackage(Context context, String packageName, UserHandleCompat user) { |
| 152 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context); |
| 153 | final List<LauncherActivityInfoCompat> matches = launcherApps.getActivityList(packageName, |
| 154 | user); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 155 | if (matches.size() > 0) { |
| 156 | // Find disabled/removed activities and remove them from data and add them |
| 157 | // to the removed list. |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 158 | for (int i = data.size() - 1; i >= 0; i--) { |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 159 | final AppInfo applicationInfo = data.get(i); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 160 | final ComponentName component = applicationInfo.intent.getComponent(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 161 | if (user.equals(applicationInfo.user) |
| 162 | && packageName.equals(component.getPackageName())) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 163 | if (!findActivity(matches, component)) { |
| 164 | removed.add(applicationInfo); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 165 | data.remove(i); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // Find enabled activities and add them to the adapter |
| 171 | // Also updates existing activities with new labels/icons |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 172 | for (final LauncherActivityInfoCompat info : matches) { |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 173 | AppInfo applicationInfo = findApplicationInfoLocked( |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 174 | info.getComponentName().getPackageName(), user, |
Adam Cohen | 52a2841 | 2014-07-09 11:30:12 -0700 | [diff] [blame] | 175 | info.getComponentName().getClassName()); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 176 | if (applicationInfo == null) { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 177 | add(new AppInfo(context, info, user, mIconCache)); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 178 | } else { |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 179 | mIconCache.getTitleAndIcon(applicationInfo, info, true /* useLowResIcon */); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 180 | modified.add(applicationInfo); |
| 181 | } |
| 182 | } |
Dianne Hackborn | 2d86dfe | 2010-07-24 16:48:01 -0700 | [diff] [blame] | 183 | } else { |
| 184 | // Remove all data for this package. |
| 185 | for (int i = data.size() - 1; i >= 0; i--) { |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 186 | final AppInfo applicationInfo = data.get(i); |
Dianne Hackborn | 2d86dfe | 2010-07-24 16:48:01 -0700 | [diff] [blame] | 187 | final ComponentName component = applicationInfo.intent.getComponent(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 188 | if (user.equals(applicationInfo.user) |
| 189 | && packageName.equals(component.getPackageName())) { |
Dianne Hackborn | 2d86dfe | 2010-07-24 16:48:01 -0700 | [diff] [blame] | 190 | removed.add(applicationInfo); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 191 | mIconCache.remove(component, user); |
Dianne Hackborn | 2d86dfe | 2010-07-24 16:48:01 -0700 | [diff] [blame] | 192 | data.remove(i); |
| 193 | } |
| 194 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * Returns whether <em>apps</em> contains <em>component</em>. |
| 201 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 202 | private static boolean findActivity(List<LauncherActivityInfoCompat> apps, |
| 203 | ComponentName component) { |
| 204 | for (LauncherActivityInfoCompat info : apps) { |
| 205 | if (info.getComponentName().equals(component)) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 206 | return true; |
| 207 | } |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | /** |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 213 | * Query the launcher apps service for whether the supplied package has |
| 214 | * MAIN/LAUNCHER activities in the supplied package. |
| 215 | */ |
| 216 | static boolean packageHasActivities(Context context, String packageName, |
| 217 | UserHandleCompat user) { |
| 218 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context); |
| 219 | return launcherApps.getActivityList(packageName, user).size() > 0; |
| 220 | } |
| 221 | |
| 222 | /** |
Joe Onorato | d65d08e | 2010-04-20 15:43:37 -0400 | [diff] [blame] | 223 | * Returns whether <em>apps</em> contains <em>component</em>. |
| 224 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 225 | private static boolean findActivity(ArrayList<AppInfo> apps, ComponentName component, |
| 226 | UserHandleCompat user) { |
Joe Onorato | d65d08e | 2010-04-20 15:43:37 -0400 | [diff] [blame] | 227 | final int N = apps.size(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 228 | for (int i = 0; i < N; i++) { |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 229 | final AppInfo info = apps.get(i); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 230 | if (info.user.equals(user) && info.componentName.equals(component)) { |
Joe Onorato | d65d08e | 2010-04-20 15:43:37 -0400 | [diff] [blame] | 231 | return true; |
| 232 | } |
| 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 238 | * Find an ApplicationInfo object for the given packageName and className. |
| 239 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 240 | private AppInfo findApplicationInfoLocked(String packageName, UserHandleCompat user, |
| 241 | String className) { |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 242 | for (AppInfo info: data) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 243 | final ComponentName component = info.intent.getComponent(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 244 | if (user.equals(info.user) && packageName.equals(component.getPackageName()) |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 245 | && className.equals(component.getClassName())) { |
| 246 | return info; |
| 247 | } |
| 248 | } |
| 249 | return null; |
| 250 | } |
| 251 | } |