Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 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.server.telecom; |
| 18 | |
Hai Zhang | c707b0d | 2019-05-23 16:46:33 -0700 | [diff] [blame] | 19 | import android.app.role.RoleManager; |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 24 | import android.content.IntentFilter; |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 25 | import android.content.ServiceConnection; |
| 26 | import android.os.IBinder; |
| 27 | import android.os.RemoteException; |
| 28 | import android.os.ServiceManager; |
| 29 | import android.os.UserHandle; |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 30 | import android.telecom.DefaultDialerManager; |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 31 | import android.telecom.PhoneAccountHandle; |
| 32 | import android.telecom.TelecomManager; |
| 33 | import android.telephony.CarrierConfigManager; |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 34 | import android.util.IntArray; |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 35 | import android.util.Slog; |
| 36 | |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 37 | import com.android.internal.annotations.GuardedBy; |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 38 | import com.android.internal.telephony.SmsApplication; |
| 39 | import com.android.server.LocalServices; |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 40 | import com.android.server.SystemService; |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 41 | import com.android.server.pm.UserManagerService; |
Philip P. Moltmann | 4845667 | 2019-01-20 13:14:03 -0800 | [diff] [blame] | 42 | import com.android.server.pm.permission.PermissionManagerServiceInternal; |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Starts the telecom component by binding to its ITelecomService implementation. Telecom is setup |
| 46 | * to run in the system-server process so once it is loaded into memory it will stay running. |
| 47 | * @hide |
| 48 | */ |
| 49 | public class TelecomLoaderService extends SystemService { |
| 50 | private static final String TAG = "TelecomLoaderService"; |
| 51 | |
| 52 | private class TelecomServiceConnection implements ServiceConnection { |
| 53 | @Override |
| 54 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 55 | // Normally, we would listen for death here, but since telecom runs in the same process |
| 56 | // as this loader (process="system") thats redundant here. |
| 57 | try { |
| 58 | service.linkToDeath(new IBinder.DeathRecipient() { |
| 59 | @Override |
| 60 | public void binderDied() { |
| 61 | connectToTelecom(); |
| 62 | } |
| 63 | }, 0); |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 64 | SmsApplication.getDefaultMmsApplication(mContext, false); |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 65 | ServiceManager.addService(Context.TELECOM_SERVICE, service); |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 66 | |
| 67 | synchronized (mLock) { |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 68 | final PermissionManagerServiceInternal permissionManager = |
| 69 | LocalServices.getService(PermissionManagerServiceInternal.class); |
Hai Zhang | c139927 | 2019-05-07 15:50:58 -0700 | [diff] [blame] | 70 | if (mDefaultSimCallManagerRequests != null) { |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 71 | if (mDefaultSimCallManagerRequests != null) { |
| 72 | TelecomManager telecomManager = |
| 73 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
| 74 | PhoneAccountHandle phoneAccount = telecomManager.getSimCallManager(); |
| 75 | if (phoneAccount != null) { |
| 76 | final int requestCount = mDefaultSimCallManagerRequests.size(); |
| 77 | final String packageName = |
| 78 | phoneAccount.getComponentName().getPackageName(); |
| 79 | for (int i = requestCount - 1; i >= 0; i--) { |
| 80 | final int userId = mDefaultSimCallManagerRequests.get(i); |
| 81 | mDefaultSimCallManagerRequests.remove(i); |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 82 | permissionManager |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 83 | .grantDefaultPermissionsToDefaultSimCallManager( |
| 84 | packageName, userId); |
| 85 | } |
| 86 | } |
| 87 | } |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 90 | } catch (RemoteException e) { |
| 91 | Slog.w(TAG, "Failed linking to death."); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void onServiceDisconnected(ComponentName name) { |
| 97 | connectToTelecom(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private static final ComponentName SERVICE_COMPONENT = new ComponentName( |
| 102 | "com.android.server.telecom", |
Ihab Awad | c4959ab | 2015-02-06 17:01:49 -0800 | [diff] [blame] | 103 | "com.android.server.telecom.components.TelecomService"); |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 104 | |
| 105 | private static final String SERVICE_ACTION = "com.android.ITelecomService"; |
| 106 | |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 107 | private final Object mLock = new Object(); |
| 108 | |
| 109 | @GuardedBy("mLock") |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 110 | private IntArray mDefaultSimCallManagerRequests; |
| 111 | |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 112 | private final Context mContext; |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 113 | |
| 114 | @GuardedBy("mLock") |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 115 | private TelecomServiceConnection mServiceConnection; |
| 116 | |
| 117 | public TelecomLoaderService(Context context) { |
| 118 | super(context); |
| 119 | mContext = context; |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 120 | registerDefaultAppProviders(); |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void onStart() { |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onBootPhase(int phase) { |
| 129 | if (phase == PHASE_ACTIVITY_MANAGER_READY) { |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 130 | registerDefaultAppNotifier(); |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 131 | registerCarrierConfigChangedReceiver(); |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 132 | connectToTelecom(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private void connectToTelecom() { |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 137 | synchronized (mLock) { |
| 138 | if (mServiceConnection != null) { |
| 139 | // TODO: Is unbinding worth doing or wait for system to rebind? |
| 140 | mContext.unbindService(mServiceConnection); |
| 141 | mServiceConnection = null; |
| 142 | } |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 143 | |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 144 | TelecomServiceConnection serviceConnection = new TelecomServiceConnection(); |
| 145 | Intent intent = new Intent(SERVICE_ACTION); |
| 146 | intent.setComponent(SERVICE_COMPONENT); |
| 147 | int flags = Context.BIND_IMPORTANT | Context.BIND_FOREGROUND_SERVICE |
| 148 | | Context.BIND_AUTO_CREATE; |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 149 | |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 150 | // Bind to Telecom and register the service |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 151 | if (mContext.bindServiceAsUser(intent, serviceConnection, flags, UserHandle.SYSTEM)) { |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 152 | mServiceConnection = serviceConnection; |
| 153 | } |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 156 | |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 157 | |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 158 | private void registerDefaultAppProviders() { |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 159 | final PermissionManagerServiceInternal permissionManager = |
| 160 | LocalServices.getService(PermissionManagerServiceInternal.class); |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 161 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 162 | // Set a callback for the permission grant policy to query the default sms app. |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 163 | permissionManager.setSmsAppPackagesProvider(userId -> { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 164 | synchronized (mLock) { |
| 165 | if (mServiceConnection == null) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 166 | return null; |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 167 | } |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 168 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 169 | ComponentName smsComponent = SmsApplication.getDefaultSmsApplication( |
| 170 | mContext, true); |
| 171 | if (smsComponent != null) { |
| 172 | return new String[]{smsComponent.getPackageName()}; |
| 173 | } |
| 174 | return null; |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 175 | }); |
| 176 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 177 | // Set a callback for the permission grant policy to query the default dialer app. |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 178 | permissionManager.setDialerAppPackagesProvider(userId -> { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 179 | synchronized (mLock) { |
| 180 | if (mServiceConnection == null) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 181 | return null; |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 182 | } |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 183 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 184 | String packageName = DefaultDialerManager.getDefaultDialerApplication(mContext); |
| 185 | if (packageName != null) { |
| 186 | return new String[]{packageName}; |
| 187 | } |
| 188 | return null; |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 189 | }); |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 190 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 191 | // Set a callback for the permission grant policy to query the default sim call manager. |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 192 | permissionManager.setSimCallManagerPackagesProvider(userId -> { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 193 | synchronized (mLock) { |
| 194 | if (mServiceConnection == null) { |
| 195 | if (mDefaultSimCallManagerRequests == null) { |
| 196 | mDefaultSimCallManagerRequests = new IntArray(); |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 197 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 198 | mDefaultSimCallManagerRequests.add(userId); |
| 199 | return null; |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 200 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 201 | } |
| 202 | TelecomManager telecomManager = |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 203 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 204 | PhoneAccountHandle phoneAccount = telecomManager.getSimCallManager(userId); |
| 205 | if (phoneAccount != null) { |
| 206 | return new String[]{phoneAccount.getComponentName().getPackageName()}; |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 207 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 208 | return null; |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 209 | }); |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | private void registerDefaultAppNotifier() { |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 213 | // Notify the package manager on default app changes |
Hai Zhang | c707b0d | 2019-05-23 16:46:33 -0700 | [diff] [blame] | 214 | final RoleManager roleManager = mContext.getSystemService(RoleManager.class); |
| 215 | roleManager.addOnRoleHoldersChangedListenerAsUser(mContext.getMainExecutor(), |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 216 | (roleName, user) -> updateSimCallManagerPermissions(user.getIdentifier()), |
| 217 | UserHandle.ALL); |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 218 | } |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 219 | |
| 220 | |
| 221 | private void registerCarrierConfigChangedReceiver() { |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 222 | BroadcastReceiver receiver = new BroadcastReceiver() { |
| 223 | @Override |
| 224 | public void onReceive(Context context, Intent intent) { |
| 225 | if (intent.getAction().equals(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED)) { |
| 226 | for (int userId : UserManagerService.getInstance().getUserIds()) { |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 227 | updateSimCallManagerPermissions(userId); |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
| 231 | }; |
| 232 | |
| 233 | mContext.registerReceiverAsUser(receiver, UserHandle.ALL, |
| 234 | new IntentFilter(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED), null, null); |
| 235 | } |
| 236 | |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 237 | private void updateSimCallManagerPermissions(int userId) { |
| 238 | final PermissionManagerServiceInternal permissionManager = |
| 239 | LocalServices.getService(PermissionManagerServiceInternal.class); |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 240 | TelecomManager telecomManager = |
| 241 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
| 242 | PhoneAccountHandle phoneAccount = telecomManager.getSimCallManager(userId); |
| 243 | if (phoneAccount != null) { |
| 244 | Slog.i(TAG, "updating sim call manager permissions for userId:" + userId); |
| 245 | String packageName = phoneAccount.getComponentName().getPackageName(); |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 246 | permissionManager.grantDefaultPermissionsToDefaultSimCallManager(packageName, userId); |
Sailesh Nepal | cf85562 | 2015-07-28 19:22:14 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
Santos Cordon | 5d2c1e6 | 2014-11-21 15:20:15 -0800 | [diff] [blame] | 249 | } |