summaryrefslogtreecommitdiff
path: root/telephony/common/com/android/internal/telephony/PackageChangeReceiver.java
blob: e9b7d95b170a4c0d6ec827d16e9dc04d7c63fd11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.telephony;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.UserHandle;

/**
 * Helper class for monitoring the state of packages: adding, removing,
 * updating, and disappearing and reappearing on the SD card.
 */
public abstract class PackageChangeReceiver extends BroadcastReceiver {
    static final IntentFilter sPackageIntentFilter = new IntentFilter();
    private static HandlerThread sHandlerThread;
    static {
        sPackageIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
        sPackageIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        sPackageIntentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
        sPackageIntentFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
        sPackageIntentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
        sPackageIntentFilter.addDataScheme("package");
    }
    // Keep an instance of Context around as long as we still want the receiver:
    // if the instance of Context gets garbage-collected, it'll unregister the receiver, so only
    // unset when we want to unregister.
    Context mRegisteredContext;

    /**
     * To register the intents that needed for monitoring the state of packages. Once this method
     * has been called on an instance of {@link PackageChangeReceiver}, all subsequent calls must
     * have the same {@code user} argument.
     */
    public void register(@NonNull Context context, @Nullable Looper thread,
            @Nullable UserHandle user) {
        if (mRegisteredContext != null) {
            throw new IllegalStateException("Already registered");
        }
        Handler handler = new Handler(thread == null ? getStaticLooper() : thread);
        mRegisteredContext = user == null ? context : context.createContextAsUser(user, 0);
        mRegisteredContext.registerReceiver(this, sPackageIntentFilter, null, handler);
    }

    /**
     * To unregister the intents for monitoring the state of packages
     */
    public void unregister() {
        if (mRegisteredContext == null) {
            throw new IllegalStateException("Not registered");
        }
        mRegisteredContext.unregisterReceiver(this);
        mRegisteredContext = null;
    }

    private static synchronized Looper getStaticLooper() {
        if (sHandlerThread == null) {
            sHandlerThread = new HandlerThread(PackageChangeReceiver.class.getSimpleName());
            sHandlerThread.start();
        }
        return sHandlerThread.getLooper();
    }

    /**
     * This method is invoked when receive the Intent.ACTION_PACKAGE_ADDED
     */
    public void onPackageAdded(@Nullable String packageName) {
    }

    /**
     * This method is invoked when receive the Intent.ACTION_PACKAGE_REMOVED
     */
    public void onPackageRemoved(@Nullable String packageName) {
    }

    /**
     * This method is invoked when Intent.EXTRA_REPLACING as extra field is true
     */
    public void onPackageUpdateFinished(@Nullable String packageName) {
    }

    /**
     * This method is invoked when receive the Intent.ACTION_PACKAGE_CHANGED or
     * Intent.EXTRA_REPLACING as extra field is true
     */
    public void onPackageModified(@Nullable String packageName) {
    }

    /**
     * This method is invoked when receive the Intent.ACTION_QUERY_PACKAGE_RESTART and
     * Intent.ACTION_PACKAGE_RESTARTED
     */
    public void onHandleForceStop(@Nullable String[] packages, boolean doit) {
    }

    /**
     * This method is invoked when receive the Intent.ACTION_PACKAGE_REMOVED
     */
    public void onPackageDisappeared() {
    }

    /**
     * This method is invoked when receive the Intent.ACTION_PACKAGE_ADDED
     */
    public void onPackageAppeared() {
    }

    @Override
    public void onReceive(@Nullable Context context, @Nullable Intent intent) {
        String action = intent.getAction();

        if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
            String pkg = getPackageName(intent);
            if (pkg != null) {
                if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
                    onPackageUpdateFinished(pkg);
                    onPackageModified(pkg);
                } else {
                    onPackageAdded(pkg);
                }
                onPackageAppeared();
            }
        } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
            String pkg = getPackageName(intent);
            if (pkg != null) {
                if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
                    onPackageRemoved(pkg);
                }
                onPackageDisappeared();
            }
        } else if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
            String pkg = getPackageName(intent);
            if (pkg != null) {
                onPackageModified(pkg);
            }
        } else if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) {
            String[] disappearingPackages = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
            onHandleForceStop(disappearingPackages, false);
        } else if (Intent.ACTION_PACKAGE_RESTARTED.equals(action)) {
            String[] disappearingPackages = new String[] {getPackageName(intent)};
            onHandleForceStop(disappearingPackages, true);
        }
    }

    String getPackageName(Intent intent) {
        Uri uri = intent.getData();
        String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
        return pkg;
    }
}