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