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