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