blob: e443d5073c549e75893930e4b306049664d39ca3 [file] [log] [blame]
Amith Yamasani4f582632014-02-19 14:31:52 -08001/*
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
17package android.content.pm;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.ILauncherApps;
23import android.content.pm.IOnAppsChangedListener;
Kenny Guy77242752016-01-15 13:29:06 +000024import android.content.pm.PackageManager.ApplicationInfoFlags;
Amith Yamasanie781c812014-05-28 15:28:18 -070025import android.content.pm.PackageManager.NameNotFoundException;
Amith Yamasani4f582632014-02-19 14:31:52 -080026import android.graphics.Rect;
27import android.os.Bundle;
Kenny Guyb42c89b2014-07-28 19:20:07 +010028import android.os.Handler;
29import android.os.Looper;
30import android.os.Message;
Amith Yamasani4f582632014-02-19 14:31:52 -080031import android.os.RemoteException;
32import android.os.UserHandle;
Amith Yamasanie781c812014-05-28 15:28:18 -070033import android.os.UserManager;
Amith Yamasani4f582632014-02-19 14:31:52 -080034import android.util.Log;
35
36import java.util.ArrayList;
37import java.util.Collections;
38import 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 Yamasanie781c812014-05-28 15:28:18 -070045 * <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 Yamasani4f582632014-02-19 14:31:52 -080051 */
52public 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 Yamasanie781c812014-05-28 15:28:18 -070059 private PackageManager mPm;
Amith Yamasani4f582632014-02-19 14:31:52 -080060
Kenny Guyb42c89b2014-07-28 19:20:07 +010061 private List<CallbackMessageHandler> mCallbacks
62 = new ArrayList<CallbackMessageHandler>();
Kenny Guyc01545372014-06-16 14:17:26 +010063
64 /**
65 * Callbacks for package changes to this and related managed profiles.
66 */
Kenny Guyf939dba2014-08-15 15:32:34 +010067 public static abstract class Callback {
Kenny Guyc01545372014-06-16 14:17:26 +010068 /**
69 * Indicates that a package was removed from the specified profile.
70 *
Kenny Guyb42c89b2014-07-28 19:20:07 +010071 * If a package is removed while being updated onPackageChanged will be
72 * called instead.
73 *
Kenny Guyc01545372014-06-16 14:17:26 +010074 * @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 Guyb42c89b2014-07-28 19:20:07 +010082 * If a package is added while being updated then onPackageChanged will be
83 * called instead.
84 *
Kenny Guyc01545372014-06-16 14:17:26 +010085 * @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 Guyb42c89b2014-07-28 19:20:07 +010092 * This can happen, for example, when the package is updated or when
93 * one or more components are enabled or disabled.
Kenny Guyc01545372014-06-16 14:17:26 +010094 *
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 Guy77242752016-01-15 13:29:06 +0000127
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 Guyc01545372014-06-16 14:17:26 +0100151 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800152
Amith Yamasani4f582632014-02-19 14:31:52 -0800153 /** @hide */
154 public LauncherApps(Context context, ILauncherApps service) {
155 mContext = context;
156 mService = service;
Amith Yamasanie781c812014-05-28 15:28:18 -0700157 mPm = context.getPackageManager();
Amith Yamasani4f582632014-02-19 14:31:52 -0800158 }
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 Goyal6cbc2fe2015-11-24 09:34:20 -0800170 ParceledListSlice<ResolveInfo> activities = null;
Amith Yamasani4f582632014-02-19 14:31:52 -0800171 try {
172 activities = mService.getLauncherActivities(packageName, user);
173 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700174 throw re.rethrowFromSystemServer();
Amith Yamasani4f582632014-02-19 14:31:52 -0800175 }
176 if (activities == null) {
177 return Collections.EMPTY_LIST;
178 }
179 ArrayList<LauncherActivityInfo> lais = new ArrayList<LauncherActivityInfo>();
Sunny Goyal6cbc2fe2015-11-24 09:34:20 -0800180 for (ResolveInfo ri : activities.getList()) {
Sunny Goyal0736e202015-11-24 10:42:11 -0800181 LauncherActivityInfo lai = new LauncherActivityInfo(mContext, ri, user);
Amith Yamasani4f582632014-02-19 14:31:52 -0800182 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 Goyal0736e202015-11-24 10:42:11 -0800207 LauncherActivityInfo info = new LauncherActivityInfo(mContext, ri, user);
Amith Yamasani4f582632014-02-19 14:31:52 -0800208 return info;
209 }
210 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700211 throw re.rethrowFromSystemServer();
Amith Yamasani4f582632014-02-19 14:31:52 -0800212 }
213 return null;
214 }
215
216 /**
Kenny Guyf939dba2014-08-15 15:32:34 +0100217 * Starts a Main activity in the specified profile.
Amith Yamasani4f582632014-02-19 14:31:52 -0800218 *
219 * @param component The ComponentName of the activity to launch
Amith Yamasanie781c812014-05-28 15:28:18 -0700220 * @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 Guyf939dba2014-08-15 15:32:34 +0100224 public void startMainActivity(ComponentName component, UserHandle user, Rect sourceBounds,
Amith Yamasanie781c812014-05-28 15:28:18 -0700225 Bundle opts) {
Amith Yamasani5abdbb62014-04-08 17:23:46 -0700226 if (DEBUG) {
Kenny Guyf939dba2014-08-15 15:32:34 +0100227 Log.i(TAG, "StartMainActivity " + component + " " + user.getIdentifier());
Amith Yamasani5abdbb62014-04-08 17:23:46 -0700228 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800229 try {
230 mService.startActivityAsUser(component, sourceBounds, opts, user);
231 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700232 throw re.rethrowFromSystemServer();
Amith Yamasani4f582632014-02-19 14:31:52 -0800233 }
234 }
235
236 /**
Kenny Guy466d2032014-07-23 12:23:35 +0100237 * 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 Guyf939dba2014-08-15 15:32:34 +0100245 public void startAppDetailsActivity(ComponentName component, UserHandle user,
Kenny Guy466d2032014-07-23 12:23:35 +0100246 Rect sourceBounds, Bundle opts) {
247 try {
248 mService.showAppDetailsAsUser(component, sourceBounds, opts, user);
249 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700250 throw re.rethrowFromSystemServer();
Kenny Guy466d2032014-07-23 12:23:35 +0100251 }
252 }
253
254 /**
Kenny Guy53fa4ec2014-04-29 14:24:18 +0100255 * 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 Guyf939dba2014-08-15 15:32:34 +0100262 public boolean isPackageEnabled(String packageName, UserHandle user) {
Kenny Guy53fa4ec2014-04-29 14:24:18 +0100263 try {
264 return mService.isPackageEnabled(packageName, user);
265 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700266 throw re.rethrowFromSystemServer();
Kenny Guy53fa4ec2014-04-29 14:24:18 +0100267 }
268 }
269
270 /**
Kenny Guy77242752016-01-15 13:29:06 +0000271 * 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) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700285 throw re.rethrowFromSystemServer();
Kenny Guy77242752016-01-15 13:29:06 +0000286 }
287 }
288
289 /**
Kenny Guy53fa4ec2014-04-29 14:24:18 +0100290 * 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 Guyf939dba2014-08-15 15:32:34 +0100297 public boolean isActivityEnabled(ComponentName component, UserHandle user) {
Kenny Guy53fa4ec2014-04-29 14:24:18 +0100298 try {
299 return mService.isActivityEnabled(component, user);
300 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700301 throw re.rethrowFromSystemServer();
Kenny Guy53fa4ec2014-04-29 14:24:18 +0100302 }
303 }
304
305
306 /**
Kenny Guy10a574f2014-08-26 16:17:58 +0100307 * Registers a callback for changes to packages in current and managed profiles.
Kenny Guyc01545372014-06-16 14:17:26 +0100308 *
Kenny Guy10a574f2014-08-26 16:17:58 +0100309 * @param callback The callback to register.
Kenny Guyc01545372014-06-16 14:17:26 +0100310 */
Kenny Guy10a574f2014-08-26 16:17:58 +0100311 public void registerCallback(Callback callback) {
312 registerCallback(callback, null);
Kenny Guyb42c89b2014-07-28 19:20:07 +0100313 }
314
315 /**
Kenny Guy10a574f2014-08-26 16:17:58 +0100316 * Registers a callback for changes to packages in current and managed profiles.
Kenny Guyb42c89b2014-07-28 19:20:07 +0100317 *
Kenny Guy10a574f2014-08-26 16:17:58 +0100318 * @param callback The callback to register.
Kenny Guyb42c89b2014-07-28 19:20:07 +0100319 * @param handler that should be used to post callbacks on, may be null.
320 */
Kenny Guy10a574f2014-08-26 16:17:58 +0100321 public void registerCallback(Callback callback, Handler handler) {
Kenny Guyc01545372014-06-16 14:17:26 +0100322 synchronized (this) {
Kenny Guy172a2162015-06-19 17:21:28 +0100323 if (callback != null && findCallbackLocked(callback) < 0) {
Kenny Guyb42c89b2014-07-28 19:20:07 +0100324 boolean addedFirstCallback = mCallbacks.size() == 0;
325 addCallbackLocked(callback, handler);
326 if (addedFirstCallback) {
Kenny Guyc01545372014-06-16 14:17:26 +0100327 try {
328 mService.addOnAppsChangedListener(mAppsChangedListener);
329 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700330 throw re.rethrowFromSystemServer();
Kenny Guyc01545372014-06-16 14:17:26 +0100331 }
332 }
333 }
334 }
335 }
336
337 /**
Kenny Guy10a574f2014-08-26 16:17:58 +0100338 * Unregisters a callback that was previously registered.
Kenny Guyc01545372014-06-16 14:17:26 +0100339 *
Kenny Guy10a574f2014-08-26 16:17:58 +0100340 * @param callback The callback to unregister.
341 * @see #registerCallback(Callback)
Kenny Guyc01545372014-06-16 14:17:26 +0100342 */
Kenny Guy10a574f2014-08-26 16:17:58 +0100343 public void unregisterCallback(Callback callback) {
Kenny Guyc01545372014-06-16 14:17:26 +0100344 synchronized (this) {
Kenny Guyb42c89b2014-07-28 19:20:07 +0100345 removeCallbackLocked(callback);
Kenny Guy44b6dee2014-07-10 18:10:14 +0100346 if (mCallbacks.size() == 0) {
Amith Yamasanie781c812014-05-28 15:28:18 -0700347 try {
348 mService.removeOnAppsChangedListener(mAppsChangedListener);
349 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700350 throw re.rethrowFromSystemServer();
Amith Yamasanie781c812014-05-28 15:28:18 -0700351 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800352 }
353 }
354 }
355
Kenny Guy172a2162015-06-19 17:21:28 +0100356 /** @return position in mCallbacks for callback or -1 if not present. */
357 private int findCallbackLocked(Callback callback) {
Kenny Guyb42c89b2014-07-28 19:20:07 +0100358 if (callback == null) {
359 throw new IllegalArgumentException("Callback cannot be null");
360 }
361 final int size = mCallbacks.size();
362 for (int i = 0; i < size; ++i) {
363 if (mCallbacks.get(i).mCallback == callback) {
Kenny Guy172a2162015-06-19 17:21:28 +0100364 return i;
Kenny Guyb42c89b2014-07-28 19:20:07 +0100365 }
366 }
Kenny Guy172a2162015-06-19 17:21:28 +0100367 return -1;
368 }
369
370 private void removeCallbackLocked(Callback callback) {
371 int pos = findCallbackLocked(callback);
372 if (pos >= 0) {
373 mCallbacks.remove(pos);
374 }
Kenny Guyb42c89b2014-07-28 19:20:07 +0100375 }
376
Kenny Guyf939dba2014-08-15 15:32:34 +0100377 private void addCallbackLocked(Callback callback, Handler handler) {
Kenny Guyb42c89b2014-07-28 19:20:07 +0100378 // Remove if already present.
379 removeCallbackLocked(callback);
380 if (handler == null) {
381 handler = new Handler();
382 }
383 CallbackMessageHandler toAdd = new CallbackMessageHandler(handler.getLooper(), callback);
384 mCallbacks.add(toAdd);
385 }
386
Amith Yamasani4f582632014-02-19 14:31:52 -0800387 private IOnAppsChangedListener.Stub mAppsChangedListener = new IOnAppsChangedListener.Stub() {
388
389 @Override
Kenny Guyb42c89b2014-07-28 19:20:07 +0100390 public void onPackageRemoved(UserHandle user, String packageName)
391 throws RemoteException {
Amith Yamasani4f582632014-02-19 14:31:52 -0800392 if (DEBUG) {
393 Log.d(TAG, "onPackageRemoved " + user.getIdentifier() + "," + packageName);
394 }
395 synchronized (LauncherApps.this) {
Kenny Guyb42c89b2014-07-28 19:20:07 +0100396 for (CallbackMessageHandler callback : mCallbacks) {
397 callback.postOnPackageRemoved(packageName, user);
Kenny Guyc01545372014-06-16 14:17:26 +0100398 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800399 }
400 }
401
402 @Override
403 public void onPackageChanged(UserHandle user, String packageName) throws RemoteException {
404 if (DEBUG) {
405 Log.d(TAG, "onPackageChanged " + user.getIdentifier() + "," + packageName);
406 }
407 synchronized (LauncherApps.this) {
Kenny Guyb42c89b2014-07-28 19:20:07 +0100408 for (CallbackMessageHandler callback : mCallbacks) {
409 callback.postOnPackageChanged(packageName, user);
Kenny Guyc01545372014-06-16 14:17:26 +0100410 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800411 }
412 }
413
414 @Override
415 public void onPackageAdded(UserHandle user, String packageName) throws RemoteException {
416 if (DEBUG) {
417 Log.d(TAG, "onPackageAdded " + user.getIdentifier() + "," + packageName);
418 }
419 synchronized (LauncherApps.this) {
Kenny Guyb42c89b2014-07-28 19:20:07 +0100420 for (CallbackMessageHandler callback : mCallbacks) {
421 callback.postOnPackageAdded(packageName, user);
Kenny Guyc01545372014-06-16 14:17:26 +0100422 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800423 }
424 }
425
426 @Override
427 public void onPackagesAvailable(UserHandle user, String[] packageNames, boolean replacing)
428 throws RemoteException {
429 if (DEBUG) {
430 Log.d(TAG, "onPackagesAvailable " + user.getIdentifier() + "," + packageNames);
431 }
432 synchronized (LauncherApps.this) {
Kenny Guyb42c89b2014-07-28 19:20:07 +0100433 for (CallbackMessageHandler callback : mCallbacks) {
434 callback.postOnPackagesAvailable(packageNames, user, replacing);
Kenny Guyc01545372014-06-16 14:17:26 +0100435 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800436 }
437 }
438
439 @Override
440 public void onPackagesUnavailable(UserHandle user, String[] packageNames, boolean replacing)
441 throws RemoteException {
442 if (DEBUG) {
443 Log.d(TAG, "onPackagesUnavailable " + user.getIdentifier() + "," + packageNames);
444 }
445 synchronized (LauncherApps.this) {
Kenny Guyb42c89b2014-07-28 19:20:07 +0100446 for (CallbackMessageHandler callback : mCallbacks) {
447 callback.postOnPackagesUnavailable(packageNames, user, replacing);
Kenny Guyc01545372014-06-16 14:17:26 +0100448 }
Kenny Guy77242752016-01-15 13:29:06 +0000449 }
450 }
451
452 @Override
453 public void onPackagesSuspended(UserHandle user, String[] packageNames)
454 throws RemoteException {
455 if (DEBUG) {
456 Log.d(TAG, "onPackagesSuspended " + user.getIdentifier() + "," + packageNames);
457 }
458 synchronized (LauncherApps.this) {
459 for (CallbackMessageHandler callback : mCallbacks) {
460 callback.postOnPackagesSuspended(packageNames, user);
461 }
462 }
463 }
464
465 @Override
466 public void onPackagesUnsuspended(UserHandle user, String[] packageNames)
467 throws RemoteException {
468 if (DEBUG) {
469 Log.d(TAG, "onPackagesUnsuspended " + user.getIdentifier() + "," + packageNames);
470 }
471 synchronized (LauncherApps.this) {
472 for (CallbackMessageHandler callback : mCallbacks) {
473 callback.postOnPackagesUnsuspended(packageNames, user);
474 }
475 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800476 }
477 };
Kenny Guyb42c89b2014-07-28 19:20:07 +0100478
479 private static class CallbackMessageHandler extends Handler {
480 private static final int MSG_ADDED = 1;
481 private static final int MSG_REMOVED = 2;
482 private static final int MSG_CHANGED = 3;
483 private static final int MSG_AVAILABLE = 4;
484 private static final int MSG_UNAVAILABLE = 5;
Kenny Guy77242752016-01-15 13:29:06 +0000485 private static final int MSG_SUSPENDED = 6;
486 private static final int MSG_UNSUSPENDED = 7;
Kenny Guyb42c89b2014-07-28 19:20:07 +0100487
Kenny Guyf939dba2014-08-15 15:32:34 +0100488 private LauncherApps.Callback mCallback;
Kenny Guyb42c89b2014-07-28 19:20:07 +0100489
490 private static class CallbackInfo {
491 String[] packageNames;
492 String packageName;
493 boolean replacing;
494 UserHandle user;
495 }
496
Kenny Guyf939dba2014-08-15 15:32:34 +0100497 public CallbackMessageHandler(Looper looper, LauncherApps.Callback callback) {
Kenny Guyb42c89b2014-07-28 19:20:07 +0100498 super(looper, null, true);
499 mCallback = callback;
500 }
501
502 @Override
503 public void handleMessage(Message msg) {
504 if (mCallback == null || !(msg.obj instanceof CallbackInfo)) {
505 return;
506 }
507 CallbackInfo info = (CallbackInfo) msg.obj;
508 switch (msg.what) {
509 case MSG_ADDED:
510 mCallback.onPackageAdded(info.packageName, info.user);
511 break;
512 case MSG_REMOVED:
513 mCallback.onPackageRemoved(info.packageName, info.user);
514 break;
515 case MSG_CHANGED:
516 mCallback.onPackageChanged(info.packageName, info.user);
517 break;
518 case MSG_AVAILABLE:
519 mCallback.onPackagesAvailable(info.packageNames, info.user, info.replacing);
520 break;
521 case MSG_UNAVAILABLE:
522 mCallback.onPackagesUnavailable(info.packageNames, info.user, info.replacing);
523 break;
Kenny Guy77242752016-01-15 13:29:06 +0000524 case MSG_SUSPENDED:
525 mCallback.onPackagesSuspended(info.packageNames, info.user);
526 break;
527 case MSG_UNSUSPENDED:
528 mCallback.onPackagesUnsuspended(info.packageNames, info.user);
529 break;
Kenny Guyb42c89b2014-07-28 19:20:07 +0100530 }
531 }
532
533 public void postOnPackageAdded(String packageName, UserHandle user) {
534 CallbackInfo info = new CallbackInfo();
535 info.packageName = packageName;
536 info.user = user;
537 obtainMessage(MSG_ADDED, info).sendToTarget();
538 }
539
540 public void postOnPackageRemoved(String packageName, UserHandle user) {
541 CallbackInfo info = new CallbackInfo();
542 info.packageName = packageName;
543 info.user = user;
544 obtainMessage(MSG_REMOVED, info).sendToTarget();
545 }
546
547 public void postOnPackageChanged(String packageName, UserHandle user) {
548 CallbackInfo info = new CallbackInfo();
549 info.packageName = packageName;
550 info.user = user;
551 obtainMessage(MSG_CHANGED, info).sendToTarget();
552 }
553
554 public void postOnPackagesAvailable(String[] packageNames, UserHandle user,
555 boolean replacing) {
556 CallbackInfo info = new CallbackInfo();
557 info.packageNames = packageNames;
558 info.replacing = replacing;
559 info.user = user;
560 obtainMessage(MSG_AVAILABLE, info).sendToTarget();
561 }
562
563 public void postOnPackagesUnavailable(String[] packageNames, UserHandle user,
564 boolean replacing) {
565 CallbackInfo info = new CallbackInfo();
566 info.packageNames = packageNames;
567 info.replacing = replacing;
568 info.user = user;
569 obtainMessage(MSG_UNAVAILABLE, info).sendToTarget();
570 }
Kenny Guy77242752016-01-15 13:29:06 +0000571
572 public void postOnPackagesSuspended(String[] packageNames, UserHandle user) {
573 CallbackInfo info = new CallbackInfo();
574 info.packageNames = packageNames;
575 info.user = user;
576 obtainMessage(MSG_SUSPENDED, info).sendToTarget();
577 }
578
579 public void postOnPackagesUnsuspended(String[] packageNames, UserHandle user) {
580 CallbackInfo info = new CallbackInfo();
581 info.packageNames = packageNames;
582 info.user = user;
583 obtainMessage(MSG_UNSUSPENDED, info).sendToTarget();
584 }
Kenny Guyb42c89b2014-07-28 19:20:07 +0100585 }
Amith Yamasani4f582632014-02-19 14:31:52 -0800586}