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.os.IBinder;
24
25import com.android.internal.telephony.CallerInfoAsyncQuery;
26import com.android.server.telecom.CallerInfoAsyncQueryFactory;
27import com.android.server.telecom.CallsManager;
28import com.android.server.telecom.HeadsetMediaButton;
29import com.android.server.telecom.HeadsetMediaButtonFactory;
30import com.android.server.telecom.InCallWakeLockControllerFactory;
31import com.android.server.telecom.ProximitySensorManagerFactory;
32import com.android.server.telecom.InCallWakeLockController;
33import com.android.server.telecom.Log;
34import com.android.server.telecom.ProximitySensorManager;
35import com.android.server.telecom.TelecomSystem;
36import com.android.server.telecom.ui.MissedCallNotifierImpl;
37
38/**
39 * Implementation of the ITelecom interface.
40 */
41public class TelecomService extends Service implements TelecomSystem.Component {
42
43    @Override
44    public IBinder onBind(Intent intent) {
45        Log.d(this, "onBind");
46        initializeTelecomSystem(this);
47        synchronized (getTelecomSystem().getLock()) {
48            return getTelecomSystem().getTelecomServiceImpl().getBinder();
49        }
50    }
51
52    /**
53     * This method is to be called by components (Activitys, Services, ...) to initialize the
54     * Telecom singleton. It should only be called on the main thread. As such, it is atomic
55     * and needs no synchronization -- it will either perform its initialization, after which
56     * the {@link TelecomSystem#getInstance()} will be initialized, or some other invocation of
57     * this method on the main thread will have happened strictly prior to it, and this method
58     * will be a benign no-op.
59     *
60     * @param context
61     */
62    static void initializeTelecomSystem(Context context) {
63        if (TelecomSystem.getInstance() == null) {
64            TelecomSystem.setInstance(
65                    new TelecomSystem(
66                            context,
67                            new MissedCallNotifierImpl(context.getApplicationContext()),
68                            new CallerInfoAsyncQueryFactory() {
69                                @Override
70                                public CallerInfoAsyncQuery startQuery(int token, Context context,
71                                        String number,
72                                        CallerInfoAsyncQuery.OnQueryCompleteListener listener,
73                                        Object cookie) {
74                                    Log.i(TelecomSystem.getInstance(),
75                                            "CallerInfoAsyncQuery.startQuery number=%s cookie=%s",
76                                            Log.pii(number), cookie);
77                                    return CallerInfoAsyncQuery.startQuery(
78                                            token, context, number, listener, cookie);
79                                }
80                            },
81                            new HeadsetMediaButtonFactory() {
82                                @Override
83                                public HeadsetMediaButton create(
84                                        Context context,
85                                        CallsManager callsManager,
86                                        TelecomSystem.SyncRoot lock) {
87                                    return new HeadsetMediaButton(context, callsManager, lock);
88                                }
89                            },
90                            new ProximitySensorManagerFactory() {
91                                @Override
92                                public ProximitySensorManager create(
93                                        Context context,
94                                        CallsManager callsManager) {
95                                    return new ProximitySensorManager(context, callsManager);
96                                }
97                            },
98                            new InCallWakeLockControllerFactory() {
99                                @Override
100                                public InCallWakeLockController create(Context context,
101                                        CallsManager callsManager) {
102                                    return new InCallWakeLockController(context, callsManager);
103                                }
104                            }));
105        }
106        if (BluetoothAdapter.getDefaultAdapter() != null) {
107            context.startService(new Intent(context, BluetoothPhoneService.class));
108        }
109    }
110
111    @Override
112    public TelecomSystem getTelecomSystem() {
113        return TelecomSystem.getInstance();
114    }
115}
116