TelecomService.java revision 731369c3983628e700f161138fe2ea3230033a1a
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(), "CallerInfoAsyncQuery.startQuery number=%s cookie=%s", number, cookie);
75                                    return CallerInfoAsyncQuery.startQuery(
76                                            token, context, number, listener, cookie);
77                                }
78                            },
79                            new HeadsetMediaButtonFactory() {
80                                @Override
81                                public HeadsetMediaButton create(
82                                        Context context,
83                                        CallsManager callsManager,
84                                        TelecomSystem.SyncRoot lock) {
85                                    return new HeadsetMediaButton(context, callsManager, lock);
86                                }
87                            },
88                            new ProximitySensorManagerFactory() {
89                                @Override
90                                public ProximitySensorManager create(
91                                        Context context,
92                                        CallsManager callsManager) {
93                                    return new ProximitySensorManager(context, callsManager);
94                                }
95                            },
96                            new InCallWakeLockControllerFactory() {
97                                @Override
98                                public InCallWakeLockController create(Context context,
99                                        CallsManager callsManager) {
100                                    return new InCallWakeLockController(context, callsManager);
101                                }
102                            }));
103        }
104        if (BluetoothAdapter.getDefaultAdapter() != null) {
105            context.startService(new Intent(context, BluetoothPhoneService.class));
106        }
107    }
108
109    @Override
110    public TelecomSystem getTelecomSystem() {
111        return TelecomSystem.getInstance();
112    }
113}
114