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 final 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     }
77 
getBluetoothAdapter()78     public LocalBluetoothAdapter getBluetoothAdapter() {
79         return mLocalAdapter;
80     }
81 
getContext()82     public Context getContext() {
83         return mContext;
84     }
85 
getForegroundActivity()86     public Context getForegroundActivity() {
87         return mForegroundActivity;
88     }
89 
isForegroundActivity()90     public boolean isForegroundActivity() {
91         return mForegroundActivity != null;
92     }
93 
setForegroundActivity(Context context)94     public synchronized void setForegroundActivity(Context context) {
95         if (context != null) {
96             Log.d(TAG, "setting foreground activity to non-null context");
97             mForegroundActivity = context;
98         } else {
99             if (mForegroundActivity != null) {
100                 Log.d(TAG, "setting foreground activity to null");
101                 mForegroundActivity = null;
102             }
103         }
104     }
105 
getCachedDeviceManager()106     public CachedBluetoothDeviceManager getCachedDeviceManager() {
107         return mCachedDeviceManager;
108     }
109 
getEventManager()110     public BluetoothEventManager getEventManager() {
111         return mEventManager;
112     }
113 
getProfileManager()114     public LocalBluetoothProfileManager getProfileManager() {
115         return mProfileManager;
116     }
117 
118     public interface BluetoothManagerCallback {
onBluetoothManagerInitialized(Context appContext, LocalBluetoothManager bluetoothManager)119         void onBluetoothManagerInitialized(Context appContext,
120                 LocalBluetoothManager bluetoothManager);
121     }
122 }
123