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 
17 package com.android.server.telecom;
18 
19 import android.app.Application;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.os.UserHandle;
25 
26 /**
27  * Top-level Application class for Telecom.
28  */
29 public final class TelecomGlobals {
30     private static final String TAG = TelecomGlobals.class.getSimpleName();
31 
32     private static final IntentFilter USER_SWITCHED_FILTER =
33             new IntentFilter(Intent.ACTION_USER_SWITCHED);
34 
35     private static final TelecomGlobals INSTANCE = new TelecomGlobals();
36 
37     /**
38      * The Telecom service implementation.
39      */
40     private TelecomService mTelecomService;
41 
42     /**
43      * Missed call notifier. Exists here so that the instance can be shared with
44      * {@link TelecomBroadcastReceiver}.
45      */
46     private MissedCallNotifier mMissedCallNotifier;
47 
48     /**
49      * Maintains the list of registered {@link android.telecom.PhoneAccountHandle}s.
50      */
51     private PhoneAccountRegistrar mPhoneAccountRegistrar;
52 
53     /**
54      * The calls manager for the Telecom service.
55      */
56     private CallsManager mCallsManager;
57 
58     /**
59      * The application context.
60      */
61     private Context mContext;
62 
63     private final BroadcastReceiver mUserSwitchedReceiver = new BroadcastReceiver() {
64         @Override
65         public void onReceive(Context context, Intent intent) {
66             int userHandleId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
67             UserHandle currentUserHandle = new UserHandle(userHandleId);
68             mPhoneAccountRegistrar.setCurrentUserHandle(currentUserHandle);
69         }
70     };
71 
getInstance()72     static TelecomGlobals getInstance() {
73         return INSTANCE;
74     }
75 
initialize(Context context)76     void initialize(Context context) {
77         if (mContext != null) {
78             Log.e(TAG, new Exception(), "Attempting to intialize TelecomGlobals a second time.");
79             return;
80         } else {
81             Log.i(TAG, "TelecomGlobals initializing");
82         }
83         mContext = context.getApplicationContext();
84 
85         mMissedCallNotifier = new MissedCallNotifier(mContext);
86         mPhoneAccountRegistrar = new PhoneAccountRegistrar(mContext);
87 
88         mCallsManager = new CallsManager(mContext, mMissedCallNotifier, mPhoneAccountRegistrar);
89         CallsManager.initialize(mCallsManager);
90         Log.i(this, "CallsManager initialized");
91 
92         // Start the BluetoothPhoneService
93         BluetoothPhoneService.start(mContext);
94 
95         mContext.registerReceiver(mUserSwitchedReceiver, USER_SWITCHED_FILTER);
96     }
97 
getMissedCallNotifier()98     MissedCallNotifier getMissedCallNotifier() {
99         return mMissedCallNotifier;
100     }
101 
getPhoneAccountRegistrar()102     PhoneAccountRegistrar getPhoneAccountRegistrar() {
103         return mPhoneAccountRegistrar;
104     }
105 
getCallsManager()106     CallsManager getCallsManager() {
107         return mCallsManager;
108     }
109 }
110