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