blob: a853529f49e439dff17f818c960c8372d64cd513 [file] [log] [blame]
Santos Cordon5d2c1e62014-11-21 15:20:15 -08001/*
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.server.telecom;
18
Hai Zhangc707b0d2019-05-23 16:46:33 -070019import android.app.role.RoleManager;
Sailesh Nepalcf855622015-07-28 19:22:14 -070020import android.content.BroadcastReceiver;
Santos Cordon5d2c1e62014-11-21 15:20:15 -080021import android.content.ComponentName;
22import android.content.Context;
23import android.content.Intent;
Sailesh Nepalcf855622015-07-28 19:22:14 -070024import android.content.IntentFilter;
Santos Cordon5d2c1e62014-11-21 15:20:15 -080025import android.content.ServiceConnection;
26import android.os.IBinder;
27import android.os.RemoteException;
28import android.os.ServiceManager;
29import android.os.UserHandle;
Svetoslavcdfd2302015-06-25 19:07:31 -070030import android.telecom.DefaultDialerManager;
Sailesh Nepalcf855622015-07-28 19:22:14 -070031import android.telecom.PhoneAccountHandle;
32import android.telecom.TelecomManager;
33import android.telephony.CarrierConfigManager;
Svetoslava5a0d942015-07-01 19:49:58 -070034import android.util.IntArray;
Santos Cordon5d2c1e62014-11-21 15:20:15 -080035import android.util.Slog;
36
Svetoslava5a0d942015-07-01 19:49:58 -070037import com.android.internal.annotations.GuardedBy;
Svetoslavcdfd2302015-06-25 19:07:31 -070038import com.android.internal.telephony.SmsApplication;
39import com.android.server.LocalServices;
Santos Cordon5d2c1e62014-11-21 15:20:15 -080040import com.android.server.SystemService;
Sailesh Nepalcf855622015-07-28 19:22:14 -070041import com.android.server.pm.UserManagerService;
Philip P. Moltmann48456672019-01-20 13:14:03 -080042import com.android.server.pm.permission.PermissionManagerServiceInternal;
Santos Cordon5d2c1e62014-11-21 15:20:15 -080043
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 */
49public 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);
Svetoslavcdfd2302015-06-25 19:07:31 -070064 SmsApplication.getDefaultMmsApplication(mContext, false);
Santos Cordon5d2c1e62014-11-21 15:20:15 -080065 ServiceManager.addService(Context.TELECOM_SERVICE, service);
Svetoslava5a0d942015-07-01 19:49:58 -070066
67 synchronized (mLock) {
Todd Kennedy583378d2019-07-12 06:50:30 -070068 final PermissionManagerServiceInternal permissionManager =
69 LocalServices.getService(PermissionManagerServiceInternal.class);
Hai Zhangc1399272019-05-07 15:50:58 -070070 if (mDefaultSimCallManagerRequests != null) {
Sailesh Nepalcf855622015-07-28 19:22:14 -070071 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 Kennedy583378d2019-07-12 06:50:30 -070082 permissionManager
Sailesh Nepalcf855622015-07-28 19:22:14 -070083 .grantDefaultPermissionsToDefaultSimCallManager(
84 packageName, userId);
85 }
86 }
87 }
Svetoslava5a0d942015-07-01 19:49:58 -070088 }
89 }
Santos Cordon5d2c1e62014-11-21 15:20:15 -080090 } 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 Awadc4959ab2015-02-06 17:01:49 -0800103 "com.android.server.telecom.components.TelecomService");
Santos Cordon5d2c1e62014-11-21 15:20:15 -0800104
105 private static final String SERVICE_ACTION = "com.android.ITelecomService";
106
Svetoslava5a0d942015-07-01 19:49:58 -0700107 private final Object mLock = new Object();
108
109 @GuardedBy("mLock")
Sailesh Nepalcf855622015-07-28 19:22:14 -0700110 private IntArray mDefaultSimCallManagerRequests;
111
Santos Cordon5d2c1e62014-11-21 15:20:15 -0800112 private final Context mContext;
Svetoslava5a0d942015-07-01 19:49:58 -0700113
114 @GuardedBy("mLock")
Santos Cordon5d2c1e62014-11-21 15:20:15 -0800115 private TelecomServiceConnection mServiceConnection;
116
117 public TelecomLoaderService(Context context) {
118 super(context);
119 mContext = context;
Svetoslavcdfd2302015-06-25 19:07:31 -0700120 registerDefaultAppProviders();
Santos Cordon5d2c1e62014-11-21 15:20:15 -0800121 }
122
123 @Override
124 public void onStart() {
125 }
126
127 @Override
128 public void onBootPhase(int phase) {
129 if (phase == PHASE_ACTIVITY_MANAGER_READY) {
Svetoslavcdfd2302015-06-25 19:07:31 -0700130 registerDefaultAppNotifier();
Sailesh Nepalcf855622015-07-28 19:22:14 -0700131 registerCarrierConfigChangedReceiver();
Santos Cordon5d2c1e62014-11-21 15:20:15 -0800132 connectToTelecom();
133 }
134 }
135
136 private void connectToTelecom() {
Svetoslava5a0d942015-07-01 19:49:58 -0700137 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 Cordon5d2c1e62014-11-21 15:20:15 -0800143
Svetoslava5a0d942015-07-01 19:49:58 -0700144 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 Cordon5d2c1e62014-11-21 15:20:15 -0800149
Svetoslava5a0d942015-07-01 19:49:58 -0700150 // Bind to Telecom and register the service
Xiaohui Chene4de5a02015-09-22 15:33:31 -0700151 if (mContext.bindServiceAsUser(intent, serviceConnection, flags, UserHandle.SYSTEM)) {
Svetoslava5a0d942015-07-01 19:49:58 -0700152 mServiceConnection = serviceConnection;
153 }
Santos Cordon5d2c1e62014-11-21 15:20:15 -0800154 }
155 }
Svetoslavcdfd2302015-06-25 19:07:31 -0700156
Svetoslava5a0d942015-07-01 19:49:58 -0700157
Svetoslavcdfd2302015-06-25 19:07:31 -0700158 private void registerDefaultAppProviders() {
Todd Kennedy583378d2019-07-12 06:50:30 -0700159 final PermissionManagerServiceInternal permissionManager =
160 LocalServices.getService(PermissionManagerServiceInternal.class);
Svetoslavcdfd2302015-06-25 19:07:31 -0700161
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000162 // Set a callback for the permission grant policy to query the default sms app.
Todd Kennedy583378d2019-07-12 06:50:30 -0700163 permissionManager.setSmsAppPackagesProvider(userId -> {
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000164 synchronized (mLock) {
165 if (mServiceConnection == null) {
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000166 return null;
Svetoslava5a0d942015-07-01 19:49:58 -0700167 }
Svetoslavcdfd2302015-06-25 19:07:31 -0700168 }
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000169 ComponentName smsComponent = SmsApplication.getDefaultSmsApplication(
170 mContext, true);
171 if (smsComponent != null) {
172 return new String[]{smsComponent.getPackageName()};
173 }
174 return null;
Svetoslavcdfd2302015-06-25 19:07:31 -0700175 });
176
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000177 // Set a callback for the permission grant policy to query the default dialer app.
Todd Kennedy583378d2019-07-12 06:50:30 -0700178 permissionManager.setDialerAppPackagesProvider(userId -> {
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000179 synchronized (mLock) {
180 if (mServiceConnection == null) {
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000181 return null;
Svetoslava5a0d942015-07-01 19:49:58 -0700182 }
Svetoslavcdfd2302015-06-25 19:07:31 -0700183 }
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000184 String packageName = DefaultDialerManager.getDefaultDialerApplication(mContext);
185 if (packageName != null) {
186 return new String[]{packageName};
187 }
188 return null;
Svetoslavcdfd2302015-06-25 19:07:31 -0700189 });
Sailesh Nepalcf855622015-07-28 19:22:14 -0700190
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000191 // Set a callback for the permission grant policy to query the default sim call manager.
Todd Kennedy583378d2019-07-12 06:50:30 -0700192 permissionManager.setSimCallManagerPackagesProvider(userId -> {
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000193 synchronized (mLock) {
194 if (mServiceConnection == null) {
195 if (mDefaultSimCallManagerRequests == null) {
196 mDefaultSimCallManagerRequests = new IntArray();
Sailesh Nepalcf855622015-07-28 19:22:14 -0700197 }
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000198 mDefaultSimCallManagerRequests.add(userId);
199 return null;
Sailesh Nepalcf855622015-07-28 19:22:14 -0700200 }
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000201 }
202 TelecomManager telecomManager =
Sailesh Nepalcf855622015-07-28 19:22:14 -0700203 (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000204 PhoneAccountHandle phoneAccount = telecomManager.getSimCallManager(userId);
205 if (phoneAccount != null) {
206 return new String[]{phoneAccount.getComponentName().getPackageName()};
Sailesh Nepalcf855622015-07-28 19:22:14 -0700207 }
Philip P. Moltmannb0be05c2018-09-19 02:46:56 +0000208 return null;
Sailesh Nepalcf855622015-07-28 19:22:14 -0700209 });
Svetoslavcdfd2302015-06-25 19:07:31 -0700210 }
211
212 private void registerDefaultAppNotifier() {
Svetoslavcdfd2302015-06-25 19:07:31 -0700213 // Notify the package manager on default app changes
Hai Zhangc707b0d2019-05-23 16:46:33 -0700214 final RoleManager roleManager = mContext.getSystemService(RoleManager.class);
215 roleManager.addOnRoleHoldersChangedListenerAsUser(mContext.getMainExecutor(),
Todd Kennedy583378d2019-07-12 06:50:30 -0700216 (roleName, user) -> updateSimCallManagerPermissions(user.getIdentifier()),
217 UserHandle.ALL);
Svetoslavcdfd2302015-06-25 19:07:31 -0700218 }
Sailesh Nepalcf855622015-07-28 19:22:14 -0700219
220
221 private void registerCarrierConfigChangedReceiver() {
Sailesh Nepalcf855622015-07-28 19:22:14 -0700222 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 Kennedy583378d2019-07-12 06:50:30 -0700227 updateSimCallManagerPermissions(userId);
Sailesh Nepalcf855622015-07-28 19:22:14 -0700228 }
229 }
230 }
231 };
232
233 mContext.registerReceiverAsUser(receiver, UserHandle.ALL,
234 new IntentFilter(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED), null, null);
235 }
236
Todd Kennedy583378d2019-07-12 06:50:30 -0700237 private void updateSimCallManagerPermissions(int userId) {
238 final PermissionManagerServiceInternal permissionManager =
239 LocalServices.getService(PermissionManagerServiceInternal.class);
Sailesh Nepalcf855622015-07-28 19:22:14 -0700240 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 Kennedy583378d2019-07-12 06:50:30 -0700246 permissionManager.grantDefaultPermissionsToDefaultSimCallManager(packageName, userId);
Sailesh Nepalcf855622015-07-28 19:22:14 -0700247 }
248 }
Santos Cordon5d2c1e62014-11-21 15:20:15 -0800249}