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
17package com.android.systemui.recents.model;
18
19import android.content.Context;
20import android.os.UserHandle;
21
22import com.android.internal.content.PackageMonitor;
23import com.android.systemui.recents.events.EventBus;
24import com.android.systemui.recents.events.activity.PackagesChangedEvent;
25import com.android.systemui.recents.misc.ForegroundThread;
26
27/**
28 * The package monitor listens for changes from PackageManager to update the contents of the
29 * Recents list.
30 */
31public class RecentsPackageMonitor extends PackageMonitor {
32
33    /** Registers the broadcast receivers with the specified callbacks. */
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, ForegroundThread.get().getLooper(), UserHandle.ALL, true);
40        } catch (IllegalStateException e) {
41            e.printStackTrace();
42        }
43    }
44
45    /** Unregisters the broadcast receivers. */
46    @Override
47    public void unregister() {
48        try {
49            super.unregister();
50        } catch (IllegalStateException e) {
51            e.printStackTrace();
52        }
53    }
54
55    @Override
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
63    public boolean onPackageChanged(String packageName, int uid, String[] components) {
64        onPackageModified(packageName);
65        return true;
66    }
67
68    @Override
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