1 /*
2  * Copyright (C) 2011 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.settingslib.bluetooth;
18 
19 import android.content.Context;
20 import android.util.Log;
21 
22 /**
23  * LocalBluetoothManager provides a simplified interface on top of a subset of
24  * the Bluetooth API. Note that {@link #getInstance} will return null
25  * if there is no Bluetooth adapter on this device, and callers must be
26  * prepared to handle this case.
27  */
28 public class LocalBluetoothManager {
29     private static final String TAG = "LocalBluetoothManager";
30 
31     /** Singleton instance. */
32     private static LocalBluetoothManager sInstance;
33 
34     private final Context mContext;
35 
36     /** If a BT-related activity is in the foreground, this will be it. */
37     private Context mForegroundActivity;
38 
39     private final LocalBluetoothAdapter mLocalAdapter;
40 
41     private final CachedBluetoothDeviceManager mCachedDeviceManager;
42 
43     /** The Bluetooth profile manager. */
44     private final LocalBluetoothProfileManager mProfileManager;
45 
46     /** The broadcast receiver event manager. */
47     private final BluetoothEventManager mEventManager;
48 
getInstance(Context context, BluetoothManagerCallback onInitCallback)49     public static synchronized LocalBluetoothManager getInstance(Context context,
50             BluetoothManagerCallback onInitCallback) {
51         if (sInstance == null) {
52             LocalBluetoothAdapter adapter = LocalBluetoothAdapter.getInstance();
53             if (adapter == null) {
54                 return null;
55             }
56             // This will be around as long as this process is
57             Context appContext = context.getApplicationContext();
58             sInstance = new LocalBluetoothManager(adapter, appContext);
59             if (onInitCallback != null) {
60                 onInitCallback.onBluetoothManagerInitialized(appContext, sInstance);
61             }
62         }
63 
64         return sInstance;
65     }
66 
LocalBluetoothManager(LocalBluetoothAdapter adapter, Context context)67     private LocalBluetoothManager(LocalBluetoothAdapter adapter, Context context) {
68         mContext = context;
69         mLocalAdapter = adapter;
70 
71         mCachedDeviceManager = new CachedBluetoothDeviceManager(context, this);
72         mEventManager = new BluetoothEventManager(mLocalAdapter,
73                 mCachedDeviceManager, context);
74         mProfileManager = new LocalBluetoothProfileManager(context,
75                 mLocalAdapter, mCachedDeviceManager, mEventManager);
76         mEventManager.readPairedDevices();
77     }
78 
getBluetoothAdapter()79     public LocalBluetoothAdapter getBluetoothAdapter() {
80         return mLocalAdapter;
81     }
82 
getContext()83     public Context getContext() {
84         return mContext;
85     }
86 
getForegroundActivity()87     public Context getForegroundActivity() {
88         return mForegroundActivity;
89     }
90 
isForegroundActivity()91     public boolean isForegroundActivity() {
92         return mForegroundActivity != null;
93     }
94 
setForegroundActivity(Context context)95     public synchronized void setForegroundActivity(Context context) {
96         if (context != null) {
97             Log.d(TAG, "setting foreground activity to non-null context");
98             mForegroundActivity = context;
99         } else {
100             if (mForegroundActivity != null) {
101                 Log.d(TAG, "setting foreground activity to null");
102                 mForegroundActivity = null;
103             }
104         }
105     }
106 
getCachedDeviceManager()107     public CachedBluetoothDeviceManager getCachedDeviceManager() {
108         return mCachedDeviceManager;
109     }
110 
getEventManager()111     public BluetoothEventManager getEventManager() {
112         return mEventManager;
113     }
114 
getProfileManager()115     public LocalBluetoothProfileManager getProfileManager() {
116         return mProfileManager;
117     }
118 
119     public interface BluetoothManagerCallback {
onBluetoothManagerInitialized(Context appContext, LocalBluetoothManager bluetoothManager)120         void onBluetoothManagerInitialized(Context appContext,
121                 LocalBluetoothManager bluetoothManager);
122     }
123 }
124