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