Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 17 | package android.content.pm; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.pm.ILauncherApps; |
| 23 | import android.content.pm.IOnAppsChangedListener; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 24 | import android.content.pm.PackageManager.ApplicationInfoFlags; |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager.NameNotFoundException; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 26 | import android.graphics.Rect; |
| 27 | import android.os.Bundle; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 28 | import android.os.Handler; |
| 29 | import android.os.Looper; |
| 30 | import android.os.Message; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 31 | import android.os.RemoteException; |
| 32 | import android.os.UserHandle; |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 33 | import android.os.UserManager; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 34 | import android.util.Log; |
| 35 | |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.Collections; |
| 38 | import java.util.List; |
| 39 | |
| 40 | /** |
| 41 | * Class for retrieving a list of launchable activities for the current user and any associated |
| 42 | * managed profiles. This is mainly for use by launchers. Apps can be queried for each user profile. |
| 43 | * Since the PackageManager will not deliver package broadcasts for other profiles, you can register |
| 44 | * for package changes here. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 45 | * <p> |
| 46 | * To watch for managed profiles being added or removed, register for the following broadcasts: |
| 47 | * {@link Intent#ACTION_MANAGED_PROFILE_ADDED} and {@link Intent#ACTION_MANAGED_PROFILE_REMOVED}. |
| 48 | * <p> |
| 49 | * You can retrieve the list of profiles associated with this user with |
| 50 | * {@link UserManager#getUserProfiles()}. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 51 | */ |
| 52 | public class LauncherApps { |
| 53 | |
| 54 | static final String TAG = "LauncherApps"; |
| 55 | static final boolean DEBUG = false; |
| 56 | |
| 57 | private Context mContext; |
| 58 | private ILauncherApps mService; |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 59 | private PackageManager mPm; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 60 | |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 61 | private List<CallbackMessageHandler> mCallbacks |
| 62 | = new ArrayList<CallbackMessageHandler>(); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Callbacks for package changes to this and related managed profiles. |
| 66 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 67 | public static abstract class Callback { |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 68 | /** |
| 69 | * Indicates that a package was removed from the specified profile. |
| 70 | * |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 71 | * If a package is removed while being updated onPackageChanged will be |
| 72 | * called instead. |
| 73 | * |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 74 | * @param packageName The name of the package that was removed. |
| 75 | * @param user The UserHandle of the profile that generated the change. |
| 76 | */ |
| 77 | abstract public void onPackageRemoved(String packageName, UserHandle user); |
| 78 | |
| 79 | /** |
| 80 | * Indicates that a package was added to the specified profile. |
| 81 | * |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 82 | * If a package is added while being updated then onPackageChanged will be |
| 83 | * called instead. |
| 84 | * |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 85 | * @param packageName The name of the package that was added. |
| 86 | * @param user The UserHandle of the profile that generated the change. |
| 87 | */ |
| 88 | abstract public void onPackageAdded(String packageName, UserHandle user); |
| 89 | |
| 90 | /** |
| 91 | * Indicates that a package was modified in the specified profile. |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 92 | * This can happen, for example, when the package is updated or when |
| 93 | * one or more components are enabled or disabled. |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 94 | * |
| 95 | * @param packageName The name of the package that has changed. |
| 96 | * @param user The UserHandle of the profile that generated the change. |
| 97 | */ |
| 98 | abstract public void onPackageChanged(String packageName, UserHandle user); |
| 99 | |
| 100 | /** |
| 101 | * Indicates that one or more packages have become available. For |
| 102 | * example, this can happen when a removable storage card has |
| 103 | * reappeared. |
| 104 | * |
| 105 | * @param packageNames The names of the packages that have become |
| 106 | * available. |
| 107 | * @param user The UserHandle of the profile that generated the change. |
| 108 | * @param replacing Indicates whether these packages are replacing |
| 109 | * existing ones. |
| 110 | */ |
| 111 | abstract public void onPackagesAvailable(String[] packageNames, UserHandle user, |
| 112 | boolean replacing); |
| 113 | |
| 114 | /** |
| 115 | * Indicates that one or more packages have become unavailable. For |
| 116 | * example, this can happen when a removable storage card has been |
| 117 | * removed. |
| 118 | * |
| 119 | * @param packageNames The names of the packages that have become |
| 120 | * unavailable. |
| 121 | * @param user The UserHandle of the profile that generated the change. |
| 122 | * @param replacing Indicates whether the packages are about to be |
| 123 | * replaced with new versions. |
| 124 | */ |
| 125 | abstract public void onPackagesUnavailable(String[] packageNames, UserHandle user, |
| 126 | boolean replacing); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * Indicates that one or more packages have been suspended. For |
| 130 | * example, this can happen when a Device Administrator suspends |
| 131 | * an applicaton. |
| 132 | * |
| 133 | * @param packageNames The names of the packages that have just been |
| 134 | * suspended. |
| 135 | * @param user The UserHandle of the profile that generated the change. |
| 136 | */ |
| 137 | public void onPackagesSuspended(String[] packageNames, UserHandle user) { |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Indicates that one or more packages have been unsuspended. For |
| 142 | * example, this can happen when a Device Administrator unsuspends |
| 143 | * an applicaton. |
| 144 | * |
| 145 | * @param packageNames The names of the packages that have just been |
| 146 | * unsuspended. |
| 147 | * @param user The UserHandle of the profile that generated the change. |
| 148 | */ |
| 149 | public void onPackagesUnsuspended(String[] packageNames, UserHandle user) { |
| 150 | } |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 151 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 152 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 153 | /** @hide */ |
| 154 | public LauncherApps(Context context, ILauncherApps service) { |
| 155 | mContext = context; |
| 156 | mService = service; |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 157 | mPm = context.getPackageManager(); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Retrieves a list of launchable activities that match {@link Intent#ACTION_MAIN} and |
| 162 | * {@link Intent#CATEGORY_LAUNCHER}, for a specified user. |
| 163 | * |
| 164 | * @param packageName The specific package to query. If null, it checks all installed packages |
| 165 | * in the profile. |
| 166 | * @param user The UserHandle of the profile. |
| 167 | * @return List of launchable activities. Can be an empty list but will not be null. |
| 168 | */ |
| 169 | public List<LauncherActivityInfo> getActivityList(String packageName, UserHandle user) { |
Sunny Goyal | 6cbc2fe | 2015-11-24 09:34:20 -0800 | [diff] [blame] | 170 | ParceledListSlice<ResolveInfo> activities = null; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 171 | try { |
| 172 | activities = mService.getLauncherActivities(packageName, user); |
| 173 | } catch (RemoteException re) { |
Sunny Goyal | 6cbc2fe | 2015-11-24 09:34:20 -0800 | [diff] [blame] | 174 | throw new RuntimeException("Failed to call LauncherAppsService", re); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 175 | } |
| 176 | if (activities == null) { |
| 177 | return Collections.EMPTY_LIST; |
| 178 | } |
| 179 | ArrayList<LauncherActivityInfo> lais = new ArrayList<LauncherActivityInfo>(); |
Sunny Goyal | 6cbc2fe | 2015-11-24 09:34:20 -0800 | [diff] [blame] | 180 | for (ResolveInfo ri : activities.getList()) { |
Sunny Goyal | 0736e20 | 2015-11-24 10:42:11 -0800 | [diff] [blame] | 181 | LauncherActivityInfo lai = new LauncherActivityInfo(mContext, ri, user); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 182 | if (DEBUG) { |
| 183 | Log.v(TAG, "Returning activity for profile " + user + " : " |
| 184 | + lai.getComponentName()); |
| 185 | } |
| 186 | lais.add(lai); |
| 187 | } |
| 188 | return lais; |
| 189 | } |
| 190 | |
| 191 | static ComponentName getComponentName(ResolveInfo ri) { |
| 192 | return new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Returns the activity info for a given intent and user handle, if it resolves. Otherwise it |
| 197 | * returns null. |
| 198 | * |
| 199 | * @param intent The intent to find a match for. |
| 200 | * @param user The profile to look in for a match. |
| 201 | * @return An activity info object if there is a match. |
| 202 | */ |
| 203 | public LauncherActivityInfo resolveActivity(Intent intent, UserHandle user) { |
| 204 | try { |
| 205 | ResolveInfo ri = mService.resolveActivity(intent, user); |
| 206 | if (ri != null) { |
Sunny Goyal | 0736e20 | 2015-11-24 10:42:11 -0800 | [diff] [blame] | 207 | LauncherActivityInfo info = new LauncherActivityInfo(mContext, ri, user); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 208 | return info; |
| 209 | } |
| 210 | } catch (RemoteException re) { |
Sunny Goyal | 6cbc2fe | 2015-11-24 09:34:20 -0800 | [diff] [blame] | 211 | throw new RuntimeException("Failed to call LauncherAppsService", re); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 212 | } |
| 213 | return null; |
| 214 | } |
| 215 | |
| 216 | /** |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 217 | * Starts a Main activity in the specified profile. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 218 | * |
| 219 | * @param component The ComponentName of the activity to launch |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 220 | * @param user The UserHandle of the profile |
| 221 | * @param sourceBounds The Rect containing the source bounds of the clicked icon |
| 222 | * @param opts Options to pass to startActivity |
| 223 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 224 | public void startMainActivity(ComponentName component, UserHandle user, Rect sourceBounds, |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 225 | Bundle opts) { |
Amith Yamasani | 5abdbb6 | 2014-04-08 17:23:46 -0700 | [diff] [blame] | 226 | if (DEBUG) { |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 227 | Log.i(TAG, "StartMainActivity " + component + " " + user.getIdentifier()); |
Amith Yamasani | 5abdbb6 | 2014-04-08 17:23:46 -0700 | [diff] [blame] | 228 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 229 | try { |
| 230 | mService.startActivityAsUser(component, sourceBounds, opts, user); |
| 231 | } catch (RemoteException re) { |
| 232 | // Oops! |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 237 | * Starts the settings activity to show the application details for a |
| 238 | * package in the specified profile. |
| 239 | * |
| 240 | * @param component The ComponentName of the package to launch settings for. |
| 241 | * @param user The UserHandle of the profile |
| 242 | * @param sourceBounds The Rect containing the source bounds of the clicked icon |
| 243 | * @param opts Options to pass to startActivity |
| 244 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 245 | public void startAppDetailsActivity(ComponentName component, UserHandle user, |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 246 | Rect sourceBounds, Bundle opts) { |
| 247 | try { |
| 248 | mService.showAppDetailsAsUser(component, sourceBounds, opts, user); |
| 249 | } catch (RemoteException re) { |
| 250 | // Oops! |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /** |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 255 | * Checks if the package is installed and enabled for a profile. |
| 256 | * |
| 257 | * @param packageName The package to check. |
| 258 | * @param user The UserHandle of the profile. |
| 259 | * |
| 260 | * @return true if the package exists and is enabled. |
| 261 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 262 | public boolean isPackageEnabled(String packageName, UserHandle user) { |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 263 | try { |
| 264 | return mService.isPackageEnabled(packageName, user); |
| 265 | } catch (RemoteException re) { |
Sunny Goyal | 6cbc2fe | 2015-11-24 09:34:20 -0800 | [diff] [blame] | 266 | throw new RuntimeException("Failed to call LauncherAppsService", re); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 271 | * Retrieve all of the information we know about a particular package / application. |
| 272 | * |
| 273 | * @param packageName The package of the application |
| 274 | * @param flags Additional option flags {@link PackageManager#getApplicationInfo} |
| 275 | * @param user The UserHandle of the profile. |
| 276 | * |
| 277 | * @return An {@link ApplicationInfo} containing information about the package or |
| 278 | * null of the package isn't found. |
| 279 | */ |
| 280 | public ApplicationInfo getApplicationInfo(String packageName, @ApplicationInfoFlags int flags, |
| 281 | UserHandle user) { |
| 282 | try { |
| 283 | return mService.getApplicationInfo(packageName, flags, user); |
| 284 | } catch (RemoteException re) { |
| 285 | throw new RuntimeException("Failed to call LauncherAppsService", re); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 290 | * Checks if the activity exists and it enabled for a profile. |
| 291 | * |
| 292 | * @param component The activity to check. |
| 293 | * @param user The UserHandle of the profile. |
| 294 | * |
| 295 | * @return true if the activity exists and is enabled. |
| 296 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 297 | public boolean isActivityEnabled(ComponentName component, UserHandle user) { |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 298 | try { |
| 299 | return mService.isActivityEnabled(component, user); |
| 300 | } catch (RemoteException re) { |
Sunny Goyal | 6cbc2fe | 2015-11-24 09:34:20 -0800 | [diff] [blame] | 301 | throw new RuntimeException("Failed to call LauncherAppsService", re); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | |
| 306 | /** |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 307 | * Registers a callback for changes to packages in current and managed profiles. |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 308 | * |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 309 | * @param callback The callback to register. |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 310 | */ |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 311 | public void registerCallback(Callback callback) { |
| 312 | registerCallback(callback, null); |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 316 | * Registers a callback for changes to packages in current and managed profiles. |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 317 | * |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 318 | * @param callback The callback to register. |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 319 | * @param handler that should be used to post callbacks on, may be null. |
| 320 | */ |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 321 | public void registerCallback(Callback callback, Handler handler) { |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 322 | synchronized (this) { |
Kenny Guy | 172a216 | 2015-06-19 17:21:28 +0100 | [diff] [blame] | 323 | if (callback != null && findCallbackLocked(callback) < 0) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 324 | boolean addedFirstCallback = mCallbacks.size() == 0; |
| 325 | addCallbackLocked(callback, handler); |
| 326 | if (addedFirstCallback) { |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 327 | try { |
| 328 | mService.addOnAppsChangedListener(mAppsChangedListener); |
| 329 | } catch (RemoteException re) { |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /** |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 337 | * Unregisters a callback that was previously registered. |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 338 | * |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 339 | * @param callback The callback to unregister. |
| 340 | * @see #registerCallback(Callback) |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 341 | */ |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 342 | public void unregisterCallback(Callback callback) { |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 343 | synchronized (this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 344 | removeCallbackLocked(callback); |
Kenny Guy | 44b6dee | 2014-07-10 18:10:14 +0100 | [diff] [blame] | 345 | if (mCallbacks.size() == 0) { |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 346 | try { |
| 347 | mService.removeOnAppsChangedListener(mAppsChangedListener); |
| 348 | } catch (RemoteException re) { |
| 349 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
Kenny Guy | 172a216 | 2015-06-19 17:21:28 +0100 | [diff] [blame] | 354 | /** @return position in mCallbacks for callback or -1 if not present. */ |
| 355 | private int findCallbackLocked(Callback callback) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 356 | if (callback == null) { |
| 357 | throw new IllegalArgumentException("Callback cannot be null"); |
| 358 | } |
| 359 | final int size = mCallbacks.size(); |
| 360 | for (int i = 0; i < size; ++i) { |
| 361 | if (mCallbacks.get(i).mCallback == callback) { |
Kenny Guy | 172a216 | 2015-06-19 17:21:28 +0100 | [diff] [blame] | 362 | return i; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 363 | } |
| 364 | } |
Kenny Guy | 172a216 | 2015-06-19 17:21:28 +0100 | [diff] [blame] | 365 | return -1; |
| 366 | } |
| 367 | |
| 368 | private void removeCallbackLocked(Callback callback) { |
| 369 | int pos = findCallbackLocked(callback); |
| 370 | if (pos >= 0) { |
| 371 | mCallbacks.remove(pos); |
| 372 | } |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 373 | } |
| 374 | |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 375 | private void addCallbackLocked(Callback callback, Handler handler) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 376 | // Remove if already present. |
| 377 | removeCallbackLocked(callback); |
| 378 | if (handler == null) { |
| 379 | handler = new Handler(); |
| 380 | } |
| 381 | CallbackMessageHandler toAdd = new CallbackMessageHandler(handler.getLooper(), callback); |
| 382 | mCallbacks.add(toAdd); |
| 383 | } |
| 384 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 385 | private IOnAppsChangedListener.Stub mAppsChangedListener = new IOnAppsChangedListener.Stub() { |
| 386 | |
| 387 | @Override |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 388 | public void onPackageRemoved(UserHandle user, String packageName) |
| 389 | throws RemoteException { |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 390 | if (DEBUG) { |
| 391 | Log.d(TAG, "onPackageRemoved " + user.getIdentifier() + "," + packageName); |
| 392 | } |
| 393 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 394 | for (CallbackMessageHandler callback : mCallbacks) { |
| 395 | callback.postOnPackageRemoved(packageName, user); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 396 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public void onPackageChanged(UserHandle user, String packageName) throws RemoteException { |
| 402 | if (DEBUG) { |
| 403 | Log.d(TAG, "onPackageChanged " + user.getIdentifier() + "," + packageName); |
| 404 | } |
| 405 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 406 | for (CallbackMessageHandler callback : mCallbacks) { |
| 407 | callback.postOnPackageChanged(packageName, user); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 408 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public void onPackageAdded(UserHandle user, String packageName) throws RemoteException { |
| 414 | if (DEBUG) { |
| 415 | Log.d(TAG, "onPackageAdded " + user.getIdentifier() + "," + packageName); |
| 416 | } |
| 417 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 418 | for (CallbackMessageHandler callback : mCallbacks) { |
| 419 | callback.postOnPackageAdded(packageName, user); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 420 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void onPackagesAvailable(UserHandle user, String[] packageNames, boolean replacing) |
| 426 | throws RemoteException { |
| 427 | if (DEBUG) { |
| 428 | Log.d(TAG, "onPackagesAvailable " + user.getIdentifier() + "," + packageNames); |
| 429 | } |
| 430 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 431 | for (CallbackMessageHandler callback : mCallbacks) { |
| 432 | callback.postOnPackagesAvailable(packageNames, user, replacing); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 433 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | @Override |
| 438 | public void onPackagesUnavailable(UserHandle user, String[] packageNames, boolean replacing) |
| 439 | throws RemoteException { |
| 440 | if (DEBUG) { |
| 441 | Log.d(TAG, "onPackagesUnavailable " + user.getIdentifier() + "," + packageNames); |
| 442 | } |
| 443 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 444 | for (CallbackMessageHandler callback : mCallbacks) { |
| 445 | callback.postOnPackagesUnavailable(packageNames, user, replacing); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 446 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public void onPackagesSuspended(UserHandle user, String[] packageNames) |
| 452 | throws RemoteException { |
| 453 | if (DEBUG) { |
| 454 | Log.d(TAG, "onPackagesSuspended " + user.getIdentifier() + "," + packageNames); |
| 455 | } |
| 456 | synchronized (LauncherApps.this) { |
| 457 | for (CallbackMessageHandler callback : mCallbacks) { |
| 458 | callback.postOnPackagesSuspended(packageNames, user); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public void onPackagesUnsuspended(UserHandle user, String[] packageNames) |
| 465 | throws RemoteException { |
| 466 | if (DEBUG) { |
| 467 | Log.d(TAG, "onPackagesUnsuspended " + user.getIdentifier() + "," + packageNames); |
| 468 | } |
| 469 | synchronized (LauncherApps.this) { |
| 470 | for (CallbackMessageHandler callback : mCallbacks) { |
| 471 | callback.postOnPackagesUnsuspended(packageNames, user); |
| 472 | } |
| 473 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 474 | } |
| 475 | }; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 476 | |
| 477 | private static class CallbackMessageHandler extends Handler { |
| 478 | private static final int MSG_ADDED = 1; |
| 479 | private static final int MSG_REMOVED = 2; |
| 480 | private static final int MSG_CHANGED = 3; |
| 481 | private static final int MSG_AVAILABLE = 4; |
| 482 | private static final int MSG_UNAVAILABLE = 5; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 483 | private static final int MSG_SUSPENDED = 6; |
| 484 | private static final int MSG_UNSUSPENDED = 7; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 485 | |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 486 | private LauncherApps.Callback mCallback; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 487 | |
| 488 | private static class CallbackInfo { |
| 489 | String[] packageNames; |
| 490 | String packageName; |
| 491 | boolean replacing; |
| 492 | UserHandle user; |
| 493 | } |
| 494 | |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 495 | public CallbackMessageHandler(Looper looper, LauncherApps.Callback callback) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 496 | super(looper, null, true); |
| 497 | mCallback = callback; |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public void handleMessage(Message msg) { |
| 502 | if (mCallback == null || !(msg.obj instanceof CallbackInfo)) { |
| 503 | return; |
| 504 | } |
| 505 | CallbackInfo info = (CallbackInfo) msg.obj; |
| 506 | switch (msg.what) { |
| 507 | case MSG_ADDED: |
| 508 | mCallback.onPackageAdded(info.packageName, info.user); |
| 509 | break; |
| 510 | case MSG_REMOVED: |
| 511 | mCallback.onPackageRemoved(info.packageName, info.user); |
| 512 | break; |
| 513 | case MSG_CHANGED: |
| 514 | mCallback.onPackageChanged(info.packageName, info.user); |
| 515 | break; |
| 516 | case MSG_AVAILABLE: |
| 517 | mCallback.onPackagesAvailable(info.packageNames, info.user, info.replacing); |
| 518 | break; |
| 519 | case MSG_UNAVAILABLE: |
| 520 | mCallback.onPackagesUnavailable(info.packageNames, info.user, info.replacing); |
| 521 | break; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 522 | case MSG_SUSPENDED: |
| 523 | mCallback.onPackagesSuspended(info.packageNames, info.user); |
| 524 | break; |
| 525 | case MSG_UNSUSPENDED: |
| 526 | mCallback.onPackagesUnsuspended(info.packageNames, info.user); |
| 527 | break; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | public void postOnPackageAdded(String packageName, UserHandle user) { |
| 532 | CallbackInfo info = new CallbackInfo(); |
| 533 | info.packageName = packageName; |
| 534 | info.user = user; |
| 535 | obtainMessage(MSG_ADDED, info).sendToTarget(); |
| 536 | } |
| 537 | |
| 538 | public void postOnPackageRemoved(String packageName, UserHandle user) { |
| 539 | CallbackInfo info = new CallbackInfo(); |
| 540 | info.packageName = packageName; |
| 541 | info.user = user; |
| 542 | obtainMessage(MSG_REMOVED, info).sendToTarget(); |
| 543 | } |
| 544 | |
| 545 | public void postOnPackageChanged(String packageName, UserHandle user) { |
| 546 | CallbackInfo info = new CallbackInfo(); |
| 547 | info.packageName = packageName; |
| 548 | info.user = user; |
| 549 | obtainMessage(MSG_CHANGED, info).sendToTarget(); |
| 550 | } |
| 551 | |
| 552 | public void postOnPackagesAvailable(String[] packageNames, UserHandle user, |
| 553 | boolean replacing) { |
| 554 | CallbackInfo info = new CallbackInfo(); |
| 555 | info.packageNames = packageNames; |
| 556 | info.replacing = replacing; |
| 557 | info.user = user; |
| 558 | obtainMessage(MSG_AVAILABLE, info).sendToTarget(); |
| 559 | } |
| 560 | |
| 561 | public void postOnPackagesUnavailable(String[] packageNames, UserHandle user, |
| 562 | boolean replacing) { |
| 563 | CallbackInfo info = new CallbackInfo(); |
| 564 | info.packageNames = packageNames; |
| 565 | info.replacing = replacing; |
| 566 | info.user = user; |
| 567 | obtainMessage(MSG_UNAVAILABLE, info).sendToTarget(); |
| 568 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 569 | |
| 570 | public void postOnPackagesSuspended(String[] packageNames, UserHandle user) { |
| 571 | CallbackInfo info = new CallbackInfo(); |
| 572 | info.packageNames = packageNames; |
| 573 | info.user = user; |
| 574 | obtainMessage(MSG_SUSPENDED, info).sendToTarget(); |
| 575 | } |
| 576 | |
| 577 | public void postOnPackagesUnsuspended(String[] packageNames, UserHandle user) { |
| 578 | CallbackInfo info = new CallbackInfo(); |
| 579 | info.packageNames = packageNames; |
| 580 | info.user = user; |
| 581 | obtainMessage(MSG_UNSUSPENDED, info).sendToTarget(); |
| 582 | } |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 583 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 584 | } |