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.components; 18 19 import android.app.Service; 20 import android.bluetooth.BluetoothAdapter; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.media.IAudioService; 24 import android.media.ToneGenerator; 25 import android.os.IBinder; 26 import android.os.PowerManager; 27 import android.os.ServiceManager; 28 import android.os.SystemClock; 29 import android.telecom.Log; 30 31 import com.android.internal.telephony.CallerInfoAsyncQuery; 32 import com.android.server.telecom.AsyncRingtonePlayer; 33 import com.android.server.telecom.BluetoothAdapterProxy; 34 import com.android.server.telecom.BluetoothPhoneServiceImpl; 35 import com.android.server.telecom.CallerInfoAsyncQueryFactory; 36 import com.android.server.telecom.CallsManager; 37 import com.android.server.telecom.ClockProxy; 38 import com.android.server.telecom.DefaultDialerCache; 39 import com.android.server.telecom.HeadsetMediaButton; 40 import com.android.server.telecom.HeadsetMediaButtonFactory; 41 import com.android.server.telecom.InCallTonePlayer; 42 import com.android.server.telecom.InCallWakeLockControllerFactory; 43 import com.android.server.telecom.CallAudioManager; 44 import com.android.server.telecom.PhoneAccountRegistrar; 45 import com.android.server.telecom.PhoneNumberUtilsAdapterImpl; 46 import com.android.server.telecom.ProximitySensorManagerFactory; 47 import com.android.server.telecom.InCallWakeLockController; 48 import com.android.server.telecom.ProximitySensorManager; 49 import com.android.server.telecom.TelecomSystem; 50 import com.android.server.telecom.TelecomWakeLock; 51 import com.android.server.telecom.Timeouts; 52 import com.android.server.telecom.ui.IncomingCallNotifier; 53 import com.android.server.telecom.ui.MissedCallNotifierImpl; 54 import com.android.server.telecom.ui.NotificationChannelManager; 55 56 /** 57 * Implementation of the ITelecom interface. 58 */ 59 public class TelecomService extends Service implements TelecomSystem.Component { 60 61 @Override 62 public IBinder onBind(Intent intent) { 63 Log.d(this, "onBind"); 64 initializeTelecomSystem(this); 65 synchronized (getTelecomSystem().getLock()) { 66 return getTelecomSystem().getTelecomServiceImpl().getBinder(); 67 } 68 } 69 70 /** 71 * This method is to be called by components (Activitys, Services, ...) to initialize the 72 * Telecom singleton. It should only be called on the main thread. As such, it is atomic 73 * and needs no synchronization -- it will either perform its initialization, after which 74 * the {@link TelecomSystem#getInstance()} will be initialized, or some other invocation of 75 * this method on the main thread will have happened strictly prior to it, and this method 76 * will be a benign no-op. 77 * 78 * @param context 79 */ 80 static void initializeTelecomSystem(Context context) { 81 if (TelecomSystem.getInstance() == null) { 82 NotificationChannelManager notificationChannelManager = 83 new NotificationChannelManager(); 84 notificationChannelManager.createChannels(context); 85 TelecomSystem.setInstance( 86 new TelecomSystem( 87 context, 88 new MissedCallNotifierImpl.MissedCallNotifierImplFactory() { 89 @Override 90 public MissedCallNotifierImpl makeMissedCallNotifierImpl( 91 Context context, 92 PhoneAccountRegistrar phoneAccountRegistrar, 93 DefaultDialerCache defaultDialerCache) { 94 return new MissedCallNotifierImpl(context, 95 phoneAccountRegistrar, defaultDialerCache); 96 } 97 }, 98 new CallerInfoAsyncQueryFactory() { 99 @Override 100 public CallerInfoAsyncQuery startQuery(int token, Context context, 101 String number, 102 CallerInfoAsyncQuery.OnQueryCompleteListener listener, 103 Object cookie) { 104 Log.i(TelecomSystem.getInstance(), 105 "CallerInfoAsyncQuery.startQuery number=%s cookie=%s", 106 Log.pii(number), cookie); 107 return CallerInfoAsyncQuery.startQuery( 108 token, context, number, listener, cookie); 109 } 110 }, 111 new HeadsetMediaButtonFactory() { 112 @Override 113 public HeadsetMediaButton create( 114 Context context, 115 CallsManager callsManager, 116 TelecomSystem.SyncRoot lock) { 117 return new HeadsetMediaButton(context, callsManager, lock); 118 } 119 }, 120 new ProximitySensorManagerFactory() { 121 @Override 122 public ProximitySensorManager create( 123 Context context, 124 CallsManager callsManager) { 125 return new ProximitySensorManager( 126 new TelecomWakeLock( 127 context, 128 PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, 129 ProximitySensorManager.class.getSimpleName()), 130 callsManager); 131 } 132 }, 133 new InCallWakeLockControllerFactory() { 134 @Override 135 public InCallWakeLockController create(Context context, 136 CallsManager callsManager) { 137 return new InCallWakeLockController( 138 new TelecomWakeLock(context, 139 PowerManager.FULL_WAKE_LOCK, 140 InCallWakeLockController.class.getSimpleName()), 141 callsManager); 142 } 143 }, 144 new CallAudioManager.AudioServiceFactory() { 145 @Override 146 public IAudioService getAudioService() { 147 return IAudioService.Stub.asInterface( 148 ServiceManager.getService(Context.AUDIO_SERVICE)); 149 } 150 }, 151 new BluetoothPhoneServiceImpl.BluetoothPhoneServiceImplFactory() { 152 @Override 153 public BluetoothPhoneServiceImpl makeBluetoothPhoneServiceImpl( 154 Context context, TelecomSystem.SyncRoot lock, 155 CallsManager callsManager, 156 PhoneAccountRegistrar phoneAccountRegistrar) { 157 return new BluetoothPhoneServiceImpl(context, lock, 158 callsManager, new BluetoothAdapterProxy(), 159 phoneAccountRegistrar); 160 } 161 }, 162 new Timeouts.Adapter(), 163 new AsyncRingtonePlayer(), 164 new PhoneNumberUtilsAdapterImpl(), 165 new IncomingCallNotifier(context), 166 ToneGenerator::new, 167 new ClockProxy() { 168 @Override 169 public long currentTimeMillis() { 170 return System.currentTimeMillis(); 171 } 172 173 @Override 174 public long elapsedRealtime() { 175 return SystemClock.elapsedRealtime(); 176 } 177 })); 178 } 179 if (BluetoothAdapter.getDefaultAdapter() != null) { 180 context.startService(new Intent(context, BluetoothPhoneService.class)); 181 } 182 } 183 184 @Override 185 public TelecomSystem getTelecomSystem() { 186 return TelecomSystem.getInstance(); 187 } 188 } 189