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 19 import com.android.internal.annotations.VisibleForTesting; 20 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.os.UserHandle; 26 27 /** 28 * Top-level Application class for Telecom. 29 */ 30 public final class TelecomSystem { 31 32 /** 33 * This interface is implemented by system-instantiated components (e.g., Services and 34 * Activity-s) that wish to use the TelecomSystem but would like to be testable. Such a 35 * component should implement the getTelecomSystem() method to return the global singleton, 36 * and use its own method. Tests can subclass the component to return a non-singleton. 37 * 38 * A refactoring goal for Telecom is to limit use of the TelecomSystem singleton to those 39 * system-instantiated components, and have all other parts of the system just take all their 40 * dependencies as explicit arguments to their constructor or other methods. 41 */ 42 public interface Component { getTelecomSystem()43 TelecomSystem getTelecomSystem(); 44 } 45 46 47 /** 48 * Tagging interface for the object used for synchronizing multi-threaded operations in 49 * the Telecom system. 50 */ 51 public interface SyncRoot { 52 } 53 54 private static final IntentFilter USER_SWITCHED_FILTER = 55 new IntentFilter(Intent.ACTION_USER_SWITCHED); 56 57 private static TelecomSystem INSTANCE = null; 58 59 private final SyncRoot mLock = new SyncRoot() { }; 60 private final MissedCallNotifier mMissedCallNotifier; 61 private final PhoneAccountRegistrar mPhoneAccountRegistrar; 62 private final CallsManager mCallsManager; 63 private final RespondViaSmsManager mRespondViaSmsManager; 64 private final Context mContext; 65 private final BluetoothPhoneServiceImpl mBluetoothPhoneServiceImpl; 66 private final CallIntentProcessor mCallIntentProcessor; 67 private final TelecomBroadcastIntentProcessor mTelecomBroadcastIntentProcessor; 68 private final TelecomServiceImpl mTelecomServiceImpl; 69 private final ContactsAsyncHelper mContactsAsyncHelper; 70 71 private final BroadcastReceiver mUserSwitchedReceiver = new BroadcastReceiver() { 72 @Override 73 public void onReceive(Context context, Intent intent) { 74 int userHandleId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); 75 UserHandle currentUserHandle = new UserHandle(userHandleId); 76 mPhoneAccountRegistrar.setCurrentUserHandle(currentUserHandle); 77 } 78 }; 79 getInstance()80 public static TelecomSystem getInstance() { 81 return INSTANCE; 82 } 83 setInstance(TelecomSystem instance)84 public static void setInstance(TelecomSystem instance) { 85 if (INSTANCE != null) { 86 throw new RuntimeException("Attempt to set TelecomSystem.INSTANCE twice"); 87 } 88 Log.i(TelecomSystem.class, "TelecomSystem.INSTANCE being set"); 89 INSTANCE = instance; 90 } 91 TelecomSystem( Context context, MissedCallNotifier missedCallNotifier, CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory, HeadsetMediaButtonFactory headsetMediaButtonFactory, ProximitySensorManagerFactory proximitySensorManagerFactory, InCallWakeLockControllerFactory inCallWakeLockControllerFactory)92 public TelecomSystem( 93 Context context, 94 MissedCallNotifier missedCallNotifier, 95 CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory, 96 HeadsetMediaButtonFactory headsetMediaButtonFactory, 97 ProximitySensorManagerFactory proximitySensorManagerFactory, 98 InCallWakeLockControllerFactory inCallWakeLockControllerFactory) { 99 mContext = context.getApplicationContext(); 100 101 mMissedCallNotifier = missedCallNotifier; 102 mPhoneAccountRegistrar = new PhoneAccountRegistrar(mContext); 103 mContactsAsyncHelper = new ContactsAsyncHelper(mLock); 104 105 mCallsManager = new CallsManager( 106 mContext, 107 mLock, 108 mContactsAsyncHelper, 109 callerInfoAsyncQueryFactory, 110 mMissedCallNotifier, 111 mPhoneAccountRegistrar, 112 headsetMediaButtonFactory, 113 proximitySensorManagerFactory, 114 inCallWakeLockControllerFactory); 115 116 mRespondViaSmsManager = new RespondViaSmsManager(mCallsManager, mLock); 117 mCallsManager.setRespondViaSmsManager(mRespondViaSmsManager); 118 119 mContext.registerReceiver(mUserSwitchedReceiver, USER_SWITCHED_FILTER); 120 mBluetoothPhoneServiceImpl = new BluetoothPhoneServiceImpl( 121 mContext, mLock, mCallsManager, mPhoneAccountRegistrar); 122 mCallIntentProcessor = new CallIntentProcessor(mContext, mCallsManager); 123 mTelecomBroadcastIntentProcessor = new TelecomBroadcastIntentProcessor( 124 mContext, mCallsManager); 125 mTelecomServiceImpl = new TelecomServiceImpl( 126 mContext, mCallsManager, mPhoneAccountRegistrar, mLock); 127 } 128 129 @VisibleForTesting getPhoneAccountRegistrar()130 public PhoneAccountRegistrar getPhoneAccountRegistrar() { 131 return mPhoneAccountRegistrar; 132 } 133 getBluetoothPhoneServiceImpl()134 public BluetoothPhoneServiceImpl getBluetoothPhoneServiceImpl() { 135 return mBluetoothPhoneServiceImpl; 136 } 137 getCallIntentProcessor()138 public CallIntentProcessor getCallIntentProcessor() { 139 return mCallIntentProcessor; 140 } 141 getTelecomBroadcastIntentProcessor()142 public TelecomBroadcastIntentProcessor getTelecomBroadcastIntentProcessor() { 143 return mTelecomBroadcastIntentProcessor; 144 } 145 getTelecomServiceImpl()146 public TelecomServiceImpl getTelecomServiceImpl() { 147 return mTelecomServiceImpl; 148 } 149 getLock()150 public Object getLock() { 151 return mLock; 152 } 153 } 154