Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.controls.management |
| 18 | |
Fabian Kozynski | a43c4b2 | 2020-02-24 15:43:42 -0500 | [diff] [blame] | 19 | import android.app.ActivityManager |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 20 | import android.content.ComponentName |
| 21 | import android.content.Context |
| 22 | import android.content.pm.ServiceInfo |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 23 | import android.os.UserHandle |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 24 | import android.service.controls.ControlsProviderService |
| 25 | import android.util.Log |
| 26 | import com.android.internal.annotations.VisibleForTesting |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 27 | import com.android.settingslib.applications.ServiceListing |
| 28 | import com.android.settingslib.widget.CandidateInfo |
| 29 | import com.android.systemui.controls.ControlsServiceInfo |
| 30 | import com.android.systemui.dagger.qualifiers.Background |
| 31 | import java.util.concurrent.Executor |
| 32 | import javax.inject.Inject |
| 33 | import javax.inject.Singleton |
| 34 | |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 35 | private fun createServiceListing(context: Context): ServiceListing { |
| 36 | return ServiceListing.Builder(context).apply { |
| 37 | setIntentAction(ControlsProviderService.SERVICE_CONTROLS) |
| 38 | setPermission("android.permission.BIND_CONTROLS") |
| 39 | setNoun("Controls Provider") |
| 40 | setSetting("controls_providers") |
| 41 | setTag("controls_providers") |
Matt Pietal | b0a9eef | 2020-04-29 09:33:29 -0400 | [diff] [blame] | 42 | setAddDeviceLockedFlags(true) |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 43 | }.build() |
| 44 | } |
| 45 | |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 46 | /** |
| 47 | * Provides a listing of components to be used as ControlsServiceProvider. |
| 48 | * |
| 49 | * This controller keeps track of components that satisfy: |
| 50 | * |
| 51 | * * Has an intent-filter responding to [ControlsProviderService.CONTROLS_ACTION] |
| 52 | * * Has the bind permission `android.permission.BIND_CONTROLS` |
| 53 | */ |
| 54 | @Singleton |
| 55 | class ControlsListingControllerImpl @VisibleForTesting constructor( |
| 56 | private val context: Context, |
| 57 | @Background private val backgroundExecutor: Executor, |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 58 | private val serviceListingBuilder: (Context) -> ServiceListing |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 59 | ) : ControlsListingController { |
| 60 | |
| 61 | @Inject |
| 62 | constructor(context: Context, executor: Executor): this( |
| 63 | context, |
| 64 | executor, |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 65 | ::createServiceListing |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 66 | ) |
| 67 | |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 68 | private var serviceListing = serviceListingBuilder(context) |
Fabian Kozynski | 1a47862 | 2020-05-04 14:21:31 -0400 | [diff] [blame] | 69 | // All operations in background thread |
| 70 | private val callbacks = mutableSetOf<ControlsListingController.ControlsListingCallback>() |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 71 | |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 72 | companion object { |
| 73 | private const val TAG = "ControlsListingControllerImpl" |
| 74 | } |
| 75 | |
Matt Pietal | b0a9eef | 2020-04-29 09:33:29 -0400 | [diff] [blame] | 76 | private var availableComponents = emptySet<ComponentName>() |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 77 | private var availableServices = emptyList<ServiceInfo>() |
| 78 | |
Fabian Kozynski | a43c4b2 | 2020-02-24 15:43:42 -0500 | [diff] [blame] | 79 | override var currentUserId = ActivityManager.getCurrentUser() |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 80 | private set |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 81 | |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 82 | private val serviceListingCallback = ServiceListing.Callback { |
Matt Pietal | b0a9eef | 2020-04-29 09:33:29 -0400 | [diff] [blame] | 83 | val newServices = it.toList() |
| 84 | val newComponents = |
| 85 | newServices.mapTo(mutableSetOf<ComponentName>(), { s -> s.getComponentName() }) |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 86 | |
| 87 | backgroundExecutor.execute { |
Matt Pietal | b0a9eef | 2020-04-29 09:33:29 -0400 | [diff] [blame] | 88 | if (!newComponents.equals(availableComponents)) { |
| 89 | Log.d(TAG, "ServiceConfig reloaded, count: ${newComponents.size}") |
| 90 | availableComponents = newComponents |
| 91 | availableServices = newServices |
| 92 | val currentServices = getCurrentServices() |
| 93 | callbacks.forEach { |
| 94 | it.onServicesUpdated(currentServices) |
| 95 | } |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 100 | init { |
Matt Pietal | b0a9eef | 2020-04-29 09:33:29 -0400 | [diff] [blame] | 101 | Log.d(TAG, "Initializing") |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 102 | serviceListing.addCallback(serviceListingCallback) |
Fabian Kozynski | 3508673 | 2020-03-10 14:25:32 -0400 | [diff] [blame] | 103 | serviceListing.setListening(true) |
| 104 | serviceListing.reload() |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | override fun changeUser(newUser: UserHandle) { |
| 108 | backgroundExecutor.execute { |
| 109 | callbacks.clear() |
| 110 | availableServices = emptyList() |
| 111 | serviceListing.setListening(false) |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 112 | currentUserId = newUser.identifier |
| 113 | val contextForUser = context.createContextAsUser(newUser, 0) |
| 114 | serviceListing = serviceListingBuilder(contextForUser) |
| 115 | serviceListing.addCallback(serviceListingCallback) |
Fabian Kozynski | 3508673 | 2020-03-10 14:25:32 -0400 | [diff] [blame] | 116 | serviceListing.setListening(true) |
| 117 | serviceListing.reload() |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 121 | /** |
| 122 | * Adds a callback to this controller. |
| 123 | * |
| 124 | * The callback will be notified after it is added as well as any time that the valid |
| 125 | * components change. |
| 126 | * |
| 127 | * @param listener a callback to be notified |
| 128 | */ |
| 129 | override fun addCallback(listener: ControlsListingController.ControlsListingCallback) { |
| 130 | backgroundExecutor.execute { |
Matt Pietal | b0a9eef | 2020-04-29 09:33:29 -0400 | [diff] [blame] | 131 | val services = getCurrentServices() |
| 132 | Log.d(TAG, "Subscribing callback, service count: ${services.size}") |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 133 | callbacks.add(listener) |
Matt Pietal | b0a9eef | 2020-04-29 09:33:29 -0400 | [diff] [blame] | 134 | listener.onServicesUpdated(services) |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Removes a callback from this controller. |
| 140 | * |
| 141 | * @param listener the callback to be removed. |
| 142 | */ |
| 143 | override fun removeCallback(listener: ControlsListingController.ControlsListingCallback) { |
| 144 | backgroundExecutor.execute { |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 145 | Log.d(TAG, "Unsubscribing callback") |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 146 | callbacks.remove(listener) |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @return a list of components that satisfy the requirements to be a |
| 152 | * [ControlsProviderService] |
| 153 | */ |
Fabian Kozynski | 84371de | 2020-02-27 10:58:25 -0500 | [diff] [blame] | 154 | override fun getCurrentServices(): List<ControlsServiceInfo> = |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 155 | availableServices.map { ControlsServiceInfo(context, it) } |
| 156 | |
| 157 | /** |
| 158 | * Get the localized label for the component. |
| 159 | * |
| 160 | * @param name the name of the component |
| 161 | * @return a label as returned by [CandidateInfo.loadLabel] or `null`. |
| 162 | */ |
| 163 | override fun getAppLabel(name: ComponentName): CharSequence? { |
Matt Pietal | 638253a | 2020-03-02 09:10:43 -0500 | [diff] [blame] | 164 | return getCurrentServices().firstOrNull { it.componentName == name } |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 165 | ?.loadLabel() |
| 166 | } |
Matt Pietal | 638253a | 2020-03-02 09:10:43 -0500 | [diff] [blame] | 167 | } |