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