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