1 /*
2  * Copyright (C) 2010 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 com.android.internal.content;
18 
19 import android.app.Activity;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.os.Looper;
28 import android.os.UserHandle;
29 import android.util.Slog;
30 
31 import com.android.internal.os.BackgroundThread;
32 import com.android.internal.util.Preconditions;
33 
34 import java.util.HashSet;
35 
36 /**
37  * Helper class for monitoring the state of packages: adding, removing,
38  * updating, and disappearing and reappearing on the SD card.
39  */
40 public abstract class PackageMonitor extends android.content.BroadcastReceiver {
41     static final IntentFilter sPackageFilt = new IntentFilter();
42     static final IntentFilter sNonDataFilt = new IntentFilter();
43     static final IntentFilter sExternalFilt = new IntentFilter();
44 
45     static {
46         sPackageFilt.addAction(Intent.ACTION_PACKAGE_ADDED);
47         sPackageFilt.addAction(Intent.ACTION_PACKAGE_REMOVED);
48         sPackageFilt.addAction(Intent.ACTION_PACKAGE_CHANGED);
49         sPackageFilt.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
50         sPackageFilt.addAction(Intent.ACTION_PACKAGE_RESTARTED);
51         sPackageFilt.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
52         sPackageFilt.addDataScheme("package");
53         sNonDataFilt.addAction(Intent.ACTION_UID_REMOVED);
54         sNonDataFilt.addAction(Intent.ACTION_USER_STOPPED);
55         sNonDataFilt.addAction(Intent.ACTION_PACKAGES_SUSPENDED);
56         sNonDataFilt.addAction(Intent.ACTION_PACKAGES_UNSUSPENDED);
57         sExternalFilt.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
58         sExternalFilt.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
59     }
60 
61     final HashSet<String> mUpdatingPackages = new HashSet<String>();
62 
63     Context mRegisteredContext;
64     Handler mRegisteredHandler;
65     String[] mDisappearingPackages;
66     String[] mAppearingPackages;
67     String[] mModifiedPackages;
68     int mChangeType;
69     int mChangeUserId = UserHandle.USER_NULL;
70     boolean mSomePackagesChanged;
71     String[] mModifiedComponents;
72 
73     String[] mTempArray = new String[1];
74 
75     @UnsupportedAppUsage
PackageMonitor()76     public PackageMonitor() {
77     }
78 
79     @UnsupportedAppUsage
register(Context context, Looper thread, boolean externalStorage)80     public void register(Context context, Looper thread, boolean externalStorage) {
81         register(context, thread, null, externalStorage);
82     }
83 
84     @UnsupportedAppUsage
register(Context context, Looper thread, UserHandle user, boolean externalStorage)85     public void register(Context context, Looper thread, UserHandle user,
86             boolean externalStorage) {
87         register(context, user, externalStorage,
88                 (thread == null) ? BackgroundThread.getHandler() : new Handler(thread));
89     }
90 
register(Context context, UserHandle user, boolean externalStorage, Handler handler)91     public void register(Context context, UserHandle user,
92         boolean externalStorage, Handler handler) {
93         if (mRegisteredContext != null) {
94             throw new IllegalStateException("Already registered");
95         }
96         mRegisteredContext = context;
97         mRegisteredHandler = Preconditions.checkNotNull(handler);
98         if (user != null) {
99             context.registerReceiverAsUser(this, user, sPackageFilt, null, mRegisteredHandler);
100             context.registerReceiverAsUser(this, user, sNonDataFilt, null, mRegisteredHandler);
101             if (externalStorage) {
102                 context.registerReceiverAsUser(this, user, sExternalFilt, null,
103                         mRegisteredHandler);
104             }
105         } else {
106             context.registerReceiver(this, sPackageFilt, null, mRegisteredHandler);
107             context.registerReceiver(this, sNonDataFilt, null, mRegisteredHandler);
108             if (externalStorage) {
109                 context.registerReceiver(this, sExternalFilt, null, mRegisteredHandler);
110             }
111         }
112     }
113 
getRegisteredHandler()114     public Handler getRegisteredHandler() {
115         return mRegisteredHandler;
116     }
117 
118     @UnsupportedAppUsage
unregister()119     public void unregister() {
120         if (mRegisteredContext == null) {
121             throw new IllegalStateException("Not registered");
122         }
123         mRegisteredContext.unregisterReceiver(this);
124         mRegisteredContext = null;
125     }
126 
127     //not yet implemented
isPackageUpdating(String packageName)128     boolean isPackageUpdating(String packageName) {
129         synchronized (mUpdatingPackages) {
130             return mUpdatingPackages.contains(packageName);
131         }
132     }
133 
onBeginPackageChanges()134     public void onBeginPackageChanges() {
135     }
136 
137     /**
138      * Called when a package is really added (and not replaced).
139      */
onPackageAdded(String packageName, int uid)140     public void onPackageAdded(String packageName, int uid) {
141     }
142 
143     /**
144      * Called when a package is really removed (and not replaced).
145      */
146     @UnsupportedAppUsage
onPackageRemoved(String packageName, int uid)147     public void onPackageRemoved(String packageName, int uid) {
148     }
149 
150     /**
151      * Called when a package is really removed (and not replaced) for
152      * all users on the device.
153      */
onPackageRemovedAllUsers(String packageName, int uid)154     public void onPackageRemovedAllUsers(String packageName, int uid) {
155     }
156 
onPackageUpdateStarted(String packageName, int uid)157     public void onPackageUpdateStarted(String packageName, int uid) {
158     }
159 
onPackageUpdateFinished(String packageName, int uid)160     public void onPackageUpdateFinished(String packageName, int uid) {
161     }
162 
163     /**
164      * Direct reflection of {@link Intent#ACTION_PACKAGE_CHANGED
165      * Intent.ACTION_PACKAGE_CHANGED} being received, informing you of
166      * changes to the enabled/disabled state of components in a package
167      * and/or of the overall package.
168      *
169      * @param packageName The name of the package that is changing.
170      * @param uid The user ID the package runs under.
171      * @param components Any components in the package that are changing.  If
172      * the overall package is changing, this will contain an entry of the
173      * package name itself.
174      * @return Return true to indicate you care about this change, which will
175      * result in {@link #onSomePackagesChanged()} being called later.  If you
176      * return false, no further callbacks will happen about this change.  The
177      * default implementation returns true if this is a change to the entire
178      * package.
179      */
180     @UnsupportedAppUsage
onPackageChanged(String packageName, int uid, String[] components)181     public boolean onPackageChanged(String packageName, int uid, String[] components) {
182         if (components != null) {
183             for (String name : components) {
184                 if (packageName.equals(name)) {
185                     return true;
186                 }
187             }
188         }
189         return false;
190     }
191 
onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit)192     public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
193         return false;
194     }
195 
onHandleUserStop(Intent intent, int userHandle)196     public void onHandleUserStop(Intent intent, int userHandle) {
197     }
198 
onUidRemoved(int uid)199     public void onUidRemoved(int uid) {
200     }
201 
onPackagesAvailable(String[] packages)202     public void onPackagesAvailable(String[] packages) {
203     }
204 
onPackagesUnavailable(String[] packages)205     public void onPackagesUnavailable(String[] packages) {
206     }
207 
onPackagesSuspended(String[] packages)208     public void onPackagesSuspended(String[] packages) {
209     }
210 
onPackagesSuspended(String[] packages, Bundle launcherExtras)211     public void onPackagesSuspended(String[] packages, Bundle launcherExtras) {
212         onPackagesSuspended(packages);
213     }
214 
onPackagesUnsuspended(String[] packages)215     public void onPackagesUnsuspended(String[] packages) {
216     }
217 
218     public static final int PACKAGE_UNCHANGED = 0;
219     public static final int PACKAGE_UPDATING = 1;
220     public static final int PACKAGE_TEMPORARY_CHANGE = 2;
221     public static final int PACKAGE_PERMANENT_CHANGE = 3;
222 
223     /**
224      * Called when a package disappears for any reason.
225      */
onPackageDisappeared(String packageName, int reason)226     public void onPackageDisappeared(String packageName, int reason) {
227     }
228 
229     /**
230      * Called when a package appears for any reason.
231      */
onPackageAppeared(String packageName, int reason)232     public void onPackageAppeared(String packageName, int reason) {
233     }
234 
235     /**
236      * Called when an existing package is updated or its disabled state changes.
237      */
onPackageModified(String packageName)238     public void onPackageModified(String packageName) {
239     }
240 
didSomePackagesChange()241     public boolean didSomePackagesChange() {
242         return mSomePackagesChanged;
243     }
244 
isPackageAppearing(String packageName)245     public int isPackageAppearing(String packageName) {
246         if (mAppearingPackages != null) {
247             for (int i=mAppearingPackages.length-1; i>=0; i--) {
248                 if (packageName.equals(mAppearingPackages[i])) {
249                     return mChangeType;
250                 }
251             }
252         }
253         return PACKAGE_UNCHANGED;
254     }
255 
anyPackagesAppearing()256     public boolean anyPackagesAppearing() {
257         return mAppearingPackages != null;
258     }
259 
260     @UnsupportedAppUsage
isPackageDisappearing(String packageName)261     public int isPackageDisappearing(String packageName) {
262         if (mDisappearingPackages != null) {
263             for (int i=mDisappearingPackages.length-1; i>=0; i--) {
264                 if (packageName.equals(mDisappearingPackages[i])) {
265                     return mChangeType;
266                 }
267             }
268         }
269         return PACKAGE_UNCHANGED;
270     }
271 
anyPackagesDisappearing()272     public boolean anyPackagesDisappearing() {
273         return mDisappearingPackages != null;
274     }
275 
isReplacing()276     public boolean isReplacing() {
277         return mChangeType == PACKAGE_UPDATING;
278     }
279 
280     @UnsupportedAppUsage
isPackageModified(String packageName)281     public boolean isPackageModified(String packageName) {
282         if (mModifiedPackages != null) {
283             for (int i=mModifiedPackages.length-1; i>=0; i--) {
284                 if (packageName.equals(mModifiedPackages[i])) {
285                     return true;
286                 }
287             }
288         }
289         return false;
290     }
291 
isComponentModified(String className)292     public boolean isComponentModified(String className) {
293         if (className == null || mModifiedComponents == null) {
294             return false;
295         }
296         for (int i = mModifiedComponents.length - 1; i >= 0; i--) {
297             if (className.equals(mModifiedComponents[i])) {
298                 return true;
299             }
300         }
301         return false;
302     }
303 
onSomePackagesChanged()304     public void onSomePackagesChanged() {
305     }
306 
onFinishPackageChanges()307     public void onFinishPackageChanges() {
308     }
309 
onPackageDataCleared(String packageName, int uid)310     public void onPackageDataCleared(String packageName, int uid) {
311     }
312 
getChangingUserId()313     public int getChangingUserId() {
314         return mChangeUserId;
315     }
316 
getPackageName(Intent intent)317     String getPackageName(Intent intent) {
318         Uri uri = intent.getData();
319         String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
320         return pkg;
321     }
322 
323     @Override
onReceive(Context context, Intent intent)324     public void onReceive(Context context, Intent intent) {
325         mChangeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
326                 UserHandle.USER_NULL);
327         if (mChangeUserId == UserHandle.USER_NULL) {
328             Slog.w("PackageMonitor", "Intent broadcast does not contain user handle: " + intent);
329             return;
330         }
331         onBeginPackageChanges();
332 
333         mDisappearingPackages = mAppearingPackages = null;
334         mSomePackagesChanged = false;
335         mModifiedComponents = null;
336 
337         String action = intent.getAction();
338         if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
339             String pkg = getPackageName(intent);
340             int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
341             // We consider something to have changed regardless of whether
342             // this is just an update, because the update is now finished
343             // and the contents of the package may have changed.
344             mSomePackagesChanged = true;
345             if (pkg != null) {
346                 mAppearingPackages = mTempArray;
347                 mTempArray[0] = pkg;
348                 if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
349                     mModifiedPackages = mTempArray;
350                     mChangeType = PACKAGE_UPDATING;
351                     onPackageUpdateFinished(pkg, uid);
352                     onPackageModified(pkg);
353                 } else {
354                     mChangeType = PACKAGE_PERMANENT_CHANGE;
355                     onPackageAdded(pkg, uid);
356                 }
357                 onPackageAppeared(pkg, mChangeType);
358                 if (mChangeType == PACKAGE_UPDATING) {
359                     synchronized (mUpdatingPackages) {
360                         mUpdatingPackages.remove(pkg);
361                     }
362                 }
363             }
364         } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
365             String pkg = getPackageName(intent);
366             int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
367             if (pkg != null) {
368                 mDisappearingPackages = mTempArray;
369                 mTempArray[0] = pkg;
370                 if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
371                     mChangeType = PACKAGE_UPDATING;
372                     synchronized (mUpdatingPackages) {
373                         //not used for now
374                         //mUpdatingPackages.add(pkg);
375                     }
376                     onPackageUpdateStarted(pkg, uid);
377                 } else {
378                     mChangeType = PACKAGE_PERMANENT_CHANGE;
379                     // We only consider something to have changed if this is
380                     // not a replace; for a replace, we just need to consider
381                     // it when it is re-added.
382                     mSomePackagesChanged = true;
383                     onPackageRemoved(pkg, uid);
384                     if (intent.getBooleanExtra(Intent.EXTRA_REMOVED_FOR_ALL_USERS, false)) {
385                         onPackageRemovedAllUsers(pkg, uid);
386                     }
387                 }
388                 onPackageDisappeared(pkg, mChangeType);
389             }
390         } else if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
391             String pkg = getPackageName(intent);
392             int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
393             mModifiedComponents = intent.getStringArrayExtra(
394                     Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST);
395             if (pkg != null) {
396                 mModifiedPackages = mTempArray;
397                 mTempArray[0] = pkg;
398                 mChangeType = PACKAGE_PERMANENT_CHANGE;
399                 if (onPackageChanged(pkg, uid, mModifiedComponents)) {
400                     mSomePackagesChanged = true;
401                 }
402                 onPackageModified(pkg);
403             }
404         } else if (Intent.ACTION_PACKAGE_DATA_CLEARED.equals(action)) {
405             String pkg = getPackageName(intent);
406             int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
407             if (pkg != null) {
408                 onPackageDataCleared(pkg, uid);
409             }
410         } else if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) {
411             mDisappearingPackages = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
412             mChangeType = PACKAGE_TEMPORARY_CHANGE;
413             boolean canRestart = onHandleForceStop(intent,
414                     mDisappearingPackages,
415                     intent.getIntExtra(Intent.EXTRA_UID, 0), false);
416             if (canRestart) setResultCode(Activity.RESULT_OK);
417         } else if (Intent.ACTION_PACKAGE_RESTARTED.equals(action)) {
418             mDisappearingPackages = new String[] {getPackageName(intent)};
419             mChangeType = PACKAGE_TEMPORARY_CHANGE;
420             onHandleForceStop(intent, mDisappearingPackages,
421                     intent.getIntExtra(Intent.EXTRA_UID, 0), true);
422         } else if (Intent.ACTION_UID_REMOVED.equals(action)) {
423             onUidRemoved(intent.getIntExtra(Intent.EXTRA_UID, 0));
424         } else if (Intent.ACTION_USER_STOPPED.equals(action)) {
425             if (intent.hasExtra(Intent.EXTRA_USER_HANDLE)) {
426                 onHandleUserStop(intent, intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
427             }
428         } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
429             String[] pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
430             mAppearingPackages = pkgList;
431             mChangeType = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)
432                     ? PACKAGE_UPDATING : PACKAGE_TEMPORARY_CHANGE;
433             mSomePackagesChanged = true;
434             if (pkgList != null) {
435                 onPackagesAvailable(pkgList);
436                 for (int i=0; i<pkgList.length; i++) {
437                     onPackageAppeared(pkgList[i], mChangeType);
438                 }
439             }
440         } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
441             String[] pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
442             mDisappearingPackages = pkgList;
443             mChangeType = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)
444                     ? PACKAGE_UPDATING : PACKAGE_TEMPORARY_CHANGE;
445             mSomePackagesChanged = true;
446             if (pkgList != null) {
447                 onPackagesUnavailable(pkgList);
448                 for (int i=0; i<pkgList.length; i++) {
449                     onPackageDisappeared(pkgList[i], mChangeType);
450                 }
451             }
452         } else if (Intent.ACTION_PACKAGES_SUSPENDED.equals(action)) {
453             String[] pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
454             final Bundle launcherExtras = intent.getBundleExtra(Intent.EXTRA_LAUNCHER_EXTRAS);
455             mSomePackagesChanged = true;
456             onPackagesSuspended(pkgList, launcherExtras);
457         } else if (Intent.ACTION_PACKAGES_UNSUSPENDED.equals(action)) {
458             String[] pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
459             mSomePackagesChanged = true;
460             onPackagesUnsuspended(pkgList);
461         }
462 
463         if (mSomePackagesChanged) {
464             onSomePackagesChanged();
465         }
466 
467         onFinishPackageChanges();
468         mChangeUserId = UserHandle.USER_NULL;
469     }
470 }
471