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