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;
27import android.telecom.Log;
28
29import com.android.internal.telephony.CallerInfoAsyncQuery;
30import com.android.server.telecom.AsyncRingtonePlayer;
31import com.android.server.telecom.BluetoothAdapterProxy;
32import com.android.server.telecom.BluetoothPhoneServiceImpl;
33import com.android.server.telecom.CallerInfoAsyncQueryFactory;
34import com.android.server.telecom.CallsManager;
35import com.android.server.telecom.DefaultDialerCache;
36import com.android.server.telecom.HeadsetMediaButton;
37import com.android.server.telecom.HeadsetMediaButtonFactory;
38import com.android.server.telecom.InCallWakeLockControllerFactory;
39import com.android.server.telecom.CallAudioManager;
40import com.android.server.telecom.PhoneAccountRegistrar;
41import com.android.server.telecom.PhoneNumberUtilsAdapterImpl;
42import com.android.server.telecom.ProximitySensorManagerFactory;
43import com.android.server.telecom.InCallWakeLockController;
44import com.android.server.telecom.ProximitySensorManager;
45import com.android.server.telecom.TelecomSystem;
46import com.android.server.telecom.TelecomWakeLock;
47import com.android.server.telecom.Timeouts;
48import com.android.server.telecom.ui.IncomingCallNotifier;
49import com.android.server.telecom.ui.MissedCallNotifierImpl;
50import com.android.server.telecom.ui.NotificationChannelManager;
51
52/**
53 * Implementation of the ITelecom interface.
54 */
55public class TelecomService extends Service implements TelecomSystem.Component {
56
57    @Override
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     */
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
170    public TelecomSystem getTelecomSystem() {
171        return TelecomSystem.getInstance();
172    }
173}
174