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