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 com.android.systemui.recents.model;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 
22 import com.android.internal.content.PackageMonitor;
23 import com.android.internal.os.BackgroundThread;
24 import com.android.systemui.recents.events.EventBus;
25 import com.android.systemui.recents.events.activity.PackagesChangedEvent;
26 
27 /**
28  * The package monitor listens for changes from PackageManager to update the contents of the
29  * Recents list.
30  */
31 public class RecentsPackageMonitor extends PackageMonitor {
32 
33     /** Registers the broadcast receivers with the specified callbacks. */
register(Context context)34     public void register(Context context) {
35         try {
36             // We register for events from all users, but will cross-reference them with
37             // packages for the current user and any profiles they have.  Ensure that events are
38             // handled in a background thread.
39             register(context, BackgroundThread.get().getLooper(), UserHandle.ALL, true);
40         } catch (IllegalStateException e) {
41             e.printStackTrace();
42         }
43     }
44 
45     /** Unregisters the broadcast receivers. */
46     @Override
unregister()47     public void unregister() {
48         try {
49             super.unregister();
50         } catch (IllegalStateException e) {
51             e.printStackTrace();
52         }
53     }
54 
55     @Override
onPackageRemoved(String packageName, int uid)56     public void onPackageRemoved(String packageName, int uid) {
57         // Notify callbacks on the main thread that a package has changed
58         final int eventUserId = getChangingUserId();
59         EventBus.getDefault().post(new PackagesChangedEvent(this, packageName, eventUserId));
60     }
61 
62     @Override
onPackageChanged(String packageName, int uid, String[] components)63     public boolean onPackageChanged(String packageName, int uid, String[] components) {
64         onPackageModified(packageName);
65         return true;
66     }
67 
68     @Override
onPackageModified(String packageName)69     public void onPackageModified(String packageName) {
70         // Notify callbacks on the main thread that a package has changed
71         final int eventUserId = getChangingUserId();
72         EventBus.getDefault().post(new PackagesChangedEvent(this, packageName, eventUserId));
73     }
74 }
75