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