blob: 3fd0e21017e9b964d3746ce3d3b5d2adaef4b8bc [file] [log] [blame]
Ihab Awad78a5e6b2015-02-06 10:13:05 -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
Ihab Awad8d5d9dd2015-03-12 11:11:06 -070019import com.android.internal.annotations.VisibleForTesting;
Hall Liu486cb192016-10-21 18:23:33 -070020import com.android.server.telecom.bluetooth.BluetoothDeviceManager;
21import com.android.server.telecom.bluetooth.BluetoothRouteManager;
Hall Liu784a4962018-03-06 11:03:17 -080022import com.android.server.telecom.bluetooth.BluetoothStateReceiver;
Hall Liuecda5542015-12-04 11:31:31 -080023import com.android.server.telecom.components.UserCallIntentProcessor;
24import com.android.server.telecom.components.UserCallIntentProcessorFactory;
Tyler Gunn2b17f232017-03-08 08:51:00 -080025import com.android.server.telecom.ui.IncomingCallNotifier;
Tony Maka9930942016-01-15 10:57:14 +000026import com.android.server.telecom.ui.MissedCallNotifierImpl.MissedCallNotifierImplFactory;
27import com.android.server.telecom.BluetoothPhoneServiceImpl.BluetoothPhoneServiceImplFactory;
28import com.android.server.telecom.CallAudioManager.AudioServiceFactory;
Hall Liu7c928322016-12-06 18:15:39 -080029import com.android.server.telecom.DefaultDialerCache.DefaultDialerManagerAdapter;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -070030
Tyler Gunnb5e7d802015-10-21 08:17:38 -070031import android.Manifest;
Ihab Awad78a5e6b2015-02-06 10:13:05 -080032import android.content.BroadcastReceiver;
33import android.content.Context;
34import android.content.Intent;
35import android.content.IntentFilter;
Tyler Gunn2b17f232017-03-08 08:51:00 -080036import android.content.pm.ApplicationInfo;
37import android.content.pm.PackageManager;
Hall Liu9ccc43d2015-12-15 14:18:33 -080038import android.net.Uri;
Ihab Awad78a5e6b2015-02-06 10:13:05 -080039import android.os.UserHandle;
Brad Ebingera3eccfe2016-10-05 15:45:22 -070040import android.telecom.Log;
Tyler Gunn2b17f232017-03-08 08:51:00 -080041import android.telecom.PhoneAccountHandle;
Ihab Awad78a5e6b2015-02-06 10:13:05 -080042
Hall Liu9ccc43d2015-12-15 14:18:33 -080043import java.io.FileNotFoundException;
44import java.io.InputStream;
45
Ihab Awad78a5e6b2015-02-06 10:13:05 -080046/**
47 * Top-level Application class for Telecom.
48 */
Hall Liu3037ac62016-09-22 14:40:03 -070049public class TelecomSystem {
Ihab Awad78a5e6b2015-02-06 10:13:05 -080050
51 /**
52 * This interface is implemented by system-instantiated components (e.g., Services and
53 * Activity-s) that wish to use the TelecomSystem but would like to be testable. Such a
54 * component should implement the getTelecomSystem() method to return the global singleton,
55 * and use its own method. Tests can subclass the component to return a non-singleton.
56 *
57 * A refactoring goal for Telecom is to limit use of the TelecomSystem singleton to those
58 * system-instantiated components, and have all other parts of the system just take all their
59 * dependencies as explicit arguments to their constructor or other methods.
60 */
61 public interface Component {
62 TelecomSystem getTelecomSystem();
63 }
64
Ihab Awad8d5d9dd2015-03-12 11:11:06 -070065
66 /**
67 * Tagging interface for the object used for synchronizing multi-threaded operations in
68 * the Telecom system.
69 */
70 public interface SyncRoot {
71 }
72
Ihab Awad78a5e6b2015-02-06 10:13:05 -080073 private static final IntentFilter USER_SWITCHED_FILTER =
74 new IntentFilter(Intent.ACTION_USER_SWITCHED);
75
Tony Maka9930942016-01-15 10:57:14 +000076 private static final IntentFilter USER_STARTING_FILTER =
77 new IntentFilter(Intent.ACTION_USER_STARTING);
78
Hall Liu3037ac62016-09-22 14:40:03 -070079 private static final IntentFilter BOOT_COMPLETE_FILTER =
80 new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
81
Tyler Gunnb5e7d802015-10-21 08:17:38 -070082 /** Intent filter for dialer secret codes. */
83 private static final IntentFilter DIALER_SECRET_CODE_FILTER;
84
85 /**
86 * Initializes the dialer secret code intent filter. Setup to handle the various secret codes
87 * which can be dialed (e.g. in format *#*#code#*#*) to trigger various behavior in Telecom.
88 */
89 static {
90 DIALER_SECRET_CODE_FILTER = new IntentFilter(
91 "android.provider.Telephony.SECRET_CODE");
92 DIALER_SECRET_CODE_FILTER.addDataScheme("android_secret_code");
93 DIALER_SECRET_CODE_FILTER
94 .addDataAuthority(DialerCodeReceiver.TELECOM_SECRET_CODE_DEBUG_ON, null);
95 DIALER_SECRET_CODE_FILTER
96 .addDataAuthority(DialerCodeReceiver.TELECOM_SECRET_CODE_DEBUG_OFF, null);
97 DIALER_SECRET_CODE_FILTER
98 .addDataAuthority(DialerCodeReceiver.TELECOM_SECRET_CODE_MARK, null);
99 }
100
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800101 private static TelecomSystem INSTANCE = null;
102
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700103 private final SyncRoot mLock = new SyncRoot() { };
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800104 private final MissedCallNotifier mMissedCallNotifier;
Tyler Gunn2b17f232017-03-08 08:51:00 -0800105 private final IncomingCallNotifier mIncomingCallNotifier;
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800106 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
107 private final CallsManager mCallsManager;
108 private final RespondViaSmsManager mRespondViaSmsManager;
109 private final Context mContext;
110 private final BluetoothPhoneServiceImpl mBluetoothPhoneServiceImpl;
111 private final CallIntentProcessor mCallIntentProcessor;
112 private final TelecomBroadcastIntentProcessor mTelecomBroadcastIntentProcessor;
113 private final TelecomServiceImpl mTelecomServiceImpl;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700114 private final ContactsAsyncHelper mContactsAsyncHelper;
Tyler Gunnb5e7d802015-10-21 08:17:38 -0700115 private final DialerCodeReceiver mDialerCodeReceiver;
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800116
Hall Liu3037ac62016-09-22 14:40:03 -0700117 private boolean mIsBootComplete = false;
118
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800119 private final BroadcastReceiver mUserSwitchedReceiver = new BroadcastReceiver() {
120 @Override
121 public void onReceive(Context context, Intent intent) {
Brad Ebinger58f36582016-01-21 14:20:07 -0800122 Log.startSession("TSSwR.oR");
123 try {
Hall Liu3037ac62016-09-22 14:40:03 -0700124 synchronized (mLock) {
125 int userHandleId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
126 UserHandle currentUserHandle = new UserHandle(userHandleId);
127 mPhoneAccountRegistrar.setCurrentUserHandle(currentUserHandle);
128 mCallsManager.onUserSwitch(currentUserHandle);
129 }
Brad Ebinger58f36582016-01-21 14:20:07 -0800130 } finally {
131 Log.endSession();
132 }
Tony Maka9930942016-01-15 10:57:14 +0000133 }
134 };
135
136 private final BroadcastReceiver mUserStartingReceiver = new BroadcastReceiver() {
137 @Override
138 public void onReceive(Context context, Intent intent) {
Brad Ebinger58f36582016-01-21 14:20:07 -0800139 Log.startSession("TSStR.oR");
140 try {
Hall Liu3037ac62016-09-22 14:40:03 -0700141 synchronized (mLock) {
142 int userHandleId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
143 UserHandle addingUserHandle = new UserHandle(userHandleId);
144 mCallsManager.onUserStarting(addingUserHandle);
145 }
146 } finally {
147 Log.endSession();
148 }
149 }
150 };
151
152 private final BroadcastReceiver mBootCompletedReceiver = new BroadcastReceiver() {
153 @Override
154 public void onReceive(Context context, Intent intent) {
155 Log.startSession("TSBCR.oR");
156 try {
157 synchronized (mLock) {
158 mIsBootComplete = true;
159 mCallsManager.onBootCompleted();
160 }
Brad Ebinger58f36582016-01-21 14:20:07 -0800161 } finally {
162 Log.endSession();
163 }
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800164 }
165 };
166
167 public static TelecomSystem getInstance() {
168 return INSTANCE;
169 }
170
171 public static void setInstance(TelecomSystem instance) {
172 if (INSTANCE != null) {
Hall Liu3037ac62016-09-22 14:40:03 -0700173 Log.w("TelecomSystem", "Attempt to set TelecomSystem.INSTANCE twice");
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800174 }
175 Log.i(TelecomSystem.class, "TelecomSystem.INSTANCE being set");
176 INSTANCE = instance;
177 }
178
Ihab Awad8de76912015-02-17 12:25:52 -0800179 public TelecomSystem(
180 Context context,
Tony Maka9930942016-01-15 10:57:14 +0000181 MissedCallNotifierImplFactory missedCallNotifierImplFactory,
Ihab Awadabcbce42015-04-07 14:04:01 -0700182 CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
Ihab Awad8de76912015-02-17 12:25:52 -0800183 HeadsetMediaButtonFactory headsetMediaButtonFactory,
184 ProximitySensorManagerFactory proximitySensorManagerFactory,
Hall Liu8fb1fb72015-10-22 15:24:40 -0700185 InCallWakeLockControllerFactory inCallWakeLockControllerFactory,
Tony Maka9930942016-01-15 10:57:14 +0000186 AudioServiceFactory audioServiceFactory,
187 BluetoothPhoneServiceImplFactory
Brad Ebinger7ade5e22016-04-05 18:43:50 -0700188 bluetoothPhoneServiceImplFactory,
Pengquan Meng606ff7f2017-12-20 16:13:04 -0800189 ConnectionServiceFocusManager.ConnectionServiceFocusManagerFactory
190 connectionServiceFocusManagerFactory,
Hall Liu6d4b66d2016-04-01 16:31:13 -0700191 Timeouts.Adapter timeoutsAdapter,
Brad Ebinger6e8f3d72016-06-20 11:35:42 -0700192 AsyncRingtonePlayer asyncRingtonePlayer,
Tyler Gunneaaf0742016-09-15 15:36:38 -0700193 PhoneNumberUtilsAdapter phoneNumberUtilsAdapter,
Brad Ebinger7bba1112017-06-08 13:57:28 -0700194 IncomingCallNotifier incomingCallNotifier,
Tyler Gunn02e00dd2017-08-24 15:44:02 -0700195 InCallTonePlayer.ToneGeneratorFactory toneGeneratorFactory,
Tyler Gunn8bb2b012017-08-04 09:28:59 -0700196 ClockProxy clockProxy) {
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800197 mContext = context.getApplicationContext();
Brad Ebingera3eccfe2016-10-05 15:45:22 -0700198 LogUtils.initLogging(mContext);
Hall Liu7c928322016-12-06 18:15:39 -0800199 DefaultDialerManagerAdapter defaultDialerAdapter =
200 new DefaultDialerCache.DefaultDialerManagerAdapterImpl();
201
202 DefaultDialerCache defaultDialerCache = new DefaultDialerCache(mContext,
203 defaultDialerAdapter, mLock);
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800204
Brad Ebinger83bc4112016-04-01 15:30:14 -0700205 Log.startSession("TS.init");
Tyler Gunn2b17f232017-03-08 08:51:00 -0800206 mPhoneAccountRegistrar = new PhoneAccountRegistrar(mContext, defaultDialerCache,
207 new PhoneAccountRegistrar.AppLabelProxy() {
208 @Override
209 public CharSequence getAppLabel(String packageName) {
210 PackageManager pm = mContext.getPackageManager();
211 try {
212 ApplicationInfo info = pm.getApplicationInfo(packageName, 0);
213 return pm.getApplicationLabel(info);
214 } catch (PackageManager.NameNotFoundException nnfe) {
215 Log.w(this, "Could not determine package name.");
216 }
217
218 return null;
219 }
220 });
Hall Liu9ccc43d2015-12-15 14:18:33 -0800221 mContactsAsyncHelper = new ContactsAsyncHelper(
222 new ContactsAsyncHelper.ContentResolverAdapter() {
223 @Override
224 public InputStream openInputStream(Context context, Uri uri)
225 throws FileNotFoundException {
226 return context.getContentResolver().openInputStream(uri);
227 }
228 });
Hall Liu486cb192016-10-21 18:23:33 -0700229 BluetoothDeviceManager bluetoothDeviceManager = new BluetoothDeviceManager(mContext,
230 new BluetoothAdapterProxy(), mLock);
231 BluetoothRouteManager bluetoothRouteManager = new BluetoothRouteManager(mContext, mLock,
232 bluetoothDeviceManager, new Timeouts.Adapter());
Hall Liu784a4962018-03-06 11:03:17 -0800233 BluetoothStateReceiver bluetoothStateReceiver = new BluetoothStateReceiver(
234 bluetoothDeviceManager, bluetoothRouteManager);
235 mContext.registerReceiver(bluetoothStateReceiver, BluetoothStateReceiver.INTENT_FILTER);
236
Hall Liuf62630a2015-10-27 14:53:39 -0700237 WiredHeadsetManager wiredHeadsetManager = new WiredHeadsetManager(mContext);
Santos Cordonf78a72f2016-01-21 22:10:32 +0000238 SystemStateProvider systemStateProvider = new SystemStateProvider(mContext);
Hall Liuf62630a2015-10-27 14:53:39 -0700239
Tony Maka9930942016-01-15 10:57:14 +0000240 mMissedCallNotifier = missedCallNotifierImplFactory
Hall Liu7c928322016-12-06 18:15:39 -0800241 .makeMissedCallNotifierImpl(mContext, mPhoneAccountRegistrar, defaultDialerCache);
Santos Cordon501b9b32016-03-07 14:40:07 -0800242
mike dooley66f26d12016-12-19 13:25:47 -0800243 EmergencyCallHelper emergencyCallHelper = new EmergencyCallHelper(mContext,
mike dooley21fb1af2016-12-05 14:25:53 -0800244 mContext.getResources().getString(R.string.ui_default_package), timeoutsAdapter);
245
Tyler Gunnc74b3e22017-11-07 15:03:24 -0800246 InCallControllerFactory inCallControllerFactory = new InCallControllerFactory() {
247 @Override
248 public InCallController create(Context context, SyncRoot lock,
249 CallsManager callsManager, SystemStateProvider systemStateProvider,
250 DefaultDialerCache defaultDialerCache, Timeouts.Adapter timeoutsAdapter,
251 EmergencyCallHelper emergencyCallHelper) {
252 return new InCallController(context, lock, callsManager, systemStateProvider,
253 defaultDialerCache, timeoutsAdapter, emergencyCallHelper);
254 }
255 };
256
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800257 mCallsManager = new CallsManager(
Ihab Awad8de76912015-02-17 12:25:52 -0800258 mContext,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700259 mLock,
260 mContactsAsyncHelper,
Ihab Awadabcbce42015-04-07 14:04:01 -0700261 callerInfoAsyncQueryFactory,
Ihab Awad8de76912015-02-17 12:25:52 -0800262 mMissedCallNotifier,
263 mPhoneAccountRegistrar,
264 headsetMediaButtonFactory,
265 proximitySensorManagerFactory,
Hall Liu8fb1fb72015-10-22 15:24:40 -0700266 inCallWakeLockControllerFactory,
Pengquan Meng606ff7f2017-12-20 16:13:04 -0800267 connectionServiceFocusManagerFactory,
Hall Liuf62630a2015-10-27 14:53:39 -0700268 audioServiceFactory,
Hall Liu486cb192016-10-21 18:23:33 -0700269 bluetoothRouteManager,
Santos Cordonf78a72f2016-01-21 22:10:32 +0000270 wiredHeadsetManager,
Santos Cordon501b9b32016-03-07 14:40:07 -0800271 systemStateProvider,
Hall Liu7c928322016-12-06 18:15:39 -0800272 defaultDialerCache,
Hall Liu6d4b66d2016-04-01 16:31:13 -0700273 timeoutsAdapter,
Brad Ebinger6e8f3d72016-06-20 11:35:42 -0700274 asyncRingtonePlayer,
Tyler Gunneaaf0742016-09-15 15:36:38 -0700275 phoneNumberUtilsAdapter,
Brad Ebinger7bba1112017-06-08 13:57:28 -0700276 emergencyCallHelper,
Tyler Gunn02e00dd2017-08-24 15:44:02 -0700277 toneGeneratorFactory,
Tyler Gunnc74b3e22017-11-07 15:03:24 -0800278 clockProxy,
Hall Liu784a4962018-03-06 11:03:17 -0800279 bluetoothStateReceiver,
Tyler Gunnc74b3e22017-11-07 15:03:24 -0800280 inCallControllerFactory);
Ihab Awad8de76912015-02-17 12:25:52 -0800281
Tyler Gunn2b17f232017-03-08 08:51:00 -0800282 mIncomingCallNotifier = incomingCallNotifier;
283 incomingCallNotifier.setCallsManagerProxy(new IncomingCallNotifier.CallsManagerProxy() {
284 @Override
285 public boolean hasCallsForOtherPhoneAccount(PhoneAccountHandle phoneAccountHandle) {
286 return mCallsManager.hasCallsForOtherPhoneAccount(phoneAccountHandle);
287 }
288
289 @Override
290 public int getNumCallsForOtherPhoneAccount(PhoneAccountHandle phoneAccountHandle) {
291 return mCallsManager.getNumCallsForOtherPhoneAccount(phoneAccountHandle);
292 }
293
294 @Override
295 public Call getActiveCall() {
296 return mCallsManager.getActiveCall();
297 }
298 });
299 mCallsManager.setIncomingCallNotifier(mIncomingCallNotifier);
300
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700301 mRespondViaSmsManager = new RespondViaSmsManager(mCallsManager, mLock);
Ihab Awad8de76912015-02-17 12:25:52 -0800302 mCallsManager.setRespondViaSmsManager(mRespondViaSmsManager);
303
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800304 mContext.registerReceiver(mUserSwitchedReceiver, USER_SWITCHED_FILTER);
Tony Maka9930942016-01-15 10:57:14 +0000305 mContext.registerReceiver(mUserStartingReceiver, USER_STARTING_FILTER);
Hall Liu3037ac62016-09-22 14:40:03 -0700306 mContext.registerReceiver(mBootCompletedReceiver, BOOT_COMPLETE_FILTER);
Tony Maka9930942016-01-15 10:57:14 +0000307
Hall Liub3979ee2015-11-11 16:21:25 -0800308 mBluetoothPhoneServiceImpl = bluetoothPhoneServiceImplFactory.makeBluetoothPhoneServiceImpl(
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700309 mContext, mLock, mCallsManager, mPhoneAccountRegistrar);
Ihab Awad8de76912015-02-17 12:25:52 -0800310 mCallIntentProcessor = new CallIntentProcessor(mContext, mCallsManager);
311 mTelecomBroadcastIntentProcessor = new TelecomBroadcastIntentProcessor(
312 mContext, mCallsManager);
Tyler Gunnb5e7d802015-10-21 08:17:38 -0700313
314 // Register the receiver for the dialer secret codes, used to enable extended logging.
315 mDialerCodeReceiver = new DialerCodeReceiver(mCallsManager);
316 mContext.registerReceiver(mDialerCodeReceiver, DIALER_SECRET_CODE_FILTER,
317 Manifest.permission.CONTROL_INCALL_EXPERIENCE, null);
318
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700319 mTelecomServiceImpl = new TelecomServiceImpl(
Hall Liuecda5542015-12-04 11:31:31 -0800320 mContext, mCallsManager, mPhoneAccountRegistrar,
321 new CallIntentProcessor.AdapterImpl(),
322 new UserCallIntentProcessorFactory() {
323 @Override
324 public UserCallIntentProcessor create(Context context, UserHandle userHandle) {
325 return new UserCallIntentProcessor(context, userHandle);
326 }
327 },
Hall Liu7c928322016-12-06 18:15:39 -0800328 defaultDialerCache,
Hall Liu0a6dd302015-12-16 15:06:49 -0800329 new TelecomServiceImpl.SubscriptionManagerAdapterImpl(),
Hall Liuecda5542015-12-04 11:31:31 -0800330 mLock);
Brad Ebinger83bc4112016-04-01 15:30:14 -0700331 Log.endSession();
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800332 }
333
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700334 @VisibleForTesting
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800335 public PhoneAccountRegistrar getPhoneAccountRegistrar() {
336 return mPhoneAccountRegistrar;
337 }
338
Hall Liu9ecbb1c2016-04-14 14:35:48 -0700339 @VisibleForTesting
340 public CallsManager getCallsManager() {
341 return mCallsManager;
342 }
343
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800344 public BluetoothPhoneServiceImpl getBluetoothPhoneServiceImpl() {
345 return mBluetoothPhoneServiceImpl;
346 }
347
348 public CallIntentProcessor getCallIntentProcessor() {
349 return mCallIntentProcessor;
350 }
351
352 public TelecomBroadcastIntentProcessor getTelecomBroadcastIntentProcessor() {
353 return mTelecomBroadcastIntentProcessor;
354 }
355
356 public TelecomServiceImpl getTelecomServiceImpl() {
357 return mTelecomServiceImpl;
358 }
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700359
360 public Object getLock() {
361 return mLock;
362 }
Hall Liu3037ac62016-09-22 14:40:03 -0700363
364 public boolean isBootComplete() {
365 return mIsBootComplete;
366 }
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800367}