summaryrefslogtreecommitdiff
path: root/PermissionController/src/com/android/permissioncontroller/permission/data/PackageBroadcastReceiver.kt
blob: b2e0236fa490e383b9409ffe8282f0d51f8f905d (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * 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.permissioncontroller.permission.data

import android.app.Application
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import com.android.modules.utils.build.SdkLevel
import com.android.permissioncontroller.PermissionControllerApplication
import com.android.permissioncontroller.permission.data.v34.LightInstallSourceInfoLiveData
import com.android.permissioncontroller.permission.data.v34.SafetyLabelInfoLiveData
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * Listens for package additions, replacements, and removals, and notifies listeners.
 */
object PackageBroadcastReceiver : BroadcastReceiver() {

    private val app: Application = PermissionControllerApplication.get()
    private val intentFilter = IntentFilter(Intent.ACTION_PACKAGE_ADDED).apply {
        addAction(Intent.ACTION_PACKAGE_REMOVED)
        addAction(Intent.ACTION_PACKAGE_REPLACED)
        addAction(Intent.ACTION_PACKAGE_CHANGED)
        addDataScheme("package")
    }

    /**
     * Map<packageName, callbacks listenening to package>
     */
    private val changeCallbacks = mutableMapOf<String, MutableSet<PackageBroadcastListener>>()
    /**
     * A list of listener IDs, which listen to all package additions, changes, and removals.
     */
    private val allCallbacks = mutableSetOf<PackageBroadcastListener>()

    /**
     * Add a callback which will be notified when the specified packaged is changed or removed.
     */
    fun addChangeCallback(packageName: String, listener: PackageBroadcastListener) {
        GlobalScope.launch(Main.immediate) {
            val wasEmpty = hasNoListeners()

            changeCallbacks.getOrPut(packageName, { mutableSetOf() }).add(listener)

            if (wasEmpty) {
                app.applicationContext.registerReceiverForAllUsers(this@PackageBroadcastReceiver,
                        intentFilter, null, null)
            }
        }
    }

    /**
     * Add a callback which will be notified any time a package is added, removed, or changed.
     *
     * @param listener the listener to be added
     * @return returns the integer ID assigned to the
     */
    fun addAllCallback(listener: PackageBroadcastListener) {
        GlobalScope.launch(Main.immediate) {
            val wasEmpty = hasNoListeners()

            allCallbacks.add(listener)

            if (wasEmpty) {
                app.applicationContext.registerReceiverForAllUsers(this@PackageBroadcastReceiver,
                        intentFilter, null, null)
            }
        }
    }

    /**
     * Removes a package add/remove/change callback.
     *
     * @param listener the listener we wish to remove
     */
    fun removeAllCallback(listener: PackageBroadcastListener) {
        GlobalScope.launch(Main.immediate) {
            val wasEmpty = hasNoListeners()

            if (allCallbacks.remove(listener) && hasNoListeners() && !wasEmpty) {
                app.applicationContext.unregisterReceiver(this@PackageBroadcastReceiver)
            }
        }
    }

    /**
     * Removes a change callback.
     *
     * @param packageName the package the listener is listening for
     * @param listener the listener we wish to remove
     */
    fun removeChangeCallback(packageName: String?, listener: PackageBroadcastListener) {
        GlobalScope.launch(Main.immediate) {
            val wasEmpty = hasNoListeners()

            changeCallbacks[packageName]?.let { callbackSet ->
                callbackSet.remove(listener)
                if (callbackSet.isEmpty()) {
                    changeCallbacks.remove(packageName)
                }
                if (hasNoListeners() && !wasEmpty) {
                    app.applicationContext.unregisterReceiver(this@PackageBroadcastReceiver)
                }
            }
        }
    }

    private fun getNumListeners(): Int {
        var numListeners = allCallbacks.size
        for ((_, changeCallbackSet) in changeCallbacks) {
            numListeners += changeCallbackSet.size
        }
        return numListeners
    }

    private fun hasNoListeners(): Boolean {
        return getNumListeners() == 0
    }

    /**
     * Upon receiving a broadcast, rout it to the proper callbacks.
     *
     * @param context the context of the broadcast
     * @param intent data about the broadcast which was sent
     */
    override fun onReceive(context: Context, intent: Intent) {
        val packageName = intent.data?.schemeSpecificPart ?: return

        for (callback in allCallbacks.toList()) {
            callback.onPackageUpdate(packageName)
        }

        if (intent.action != Intent.ACTION_PACKAGE_ADDED) {
            changeCallbacks[packageName]?.toList()?.let { callbacks ->
                for (callback in callbacks) {
                    callback.onPackageUpdate(packageName)
                }
            }
        }

        if (intent.action == Intent.ACTION_PACKAGE_REMOVED) {
            // Invalidate all livedatas associated with this package
            LightPackageInfoLiveData.invalidateAllForPackage(packageName)
            PermStateLiveData.invalidateAllForPackage(packageName)
            PackagePermissionsLiveData.invalidateAllForPackage(packageName)
            HibernationSettingStateLiveData.invalidateAllForPackage(packageName)
            LightAppPermGroupLiveData.invalidateAllForPackage(packageName)
            AppPermGroupUiInfoLiveData.invalidateAllForPackage(packageName)
            if (SdkLevel.isAtLeastU()) {
                SafetyLabelInfoLiveData.invalidateAllForPackage(packageName)
                LightInstallSourceInfoLiveData.invalidateAllForPackage(packageName)
            }
        }
    }

    /**
     * A listener interface for objects desiring to be notified of package broadcasts.
     */
    interface PackageBroadcastListener {
        /**
         * To be called when a specific package has been changed, or when any package has been
         * installed.
         *
         * @param packageName the name of the package which was updated
         */
        fun onPackageUpdate(packageName: String)
    }
}