• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.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 BluetoothDiscoverableEnabler mDiscoverableEnabler;
40 
41     private final LocalBluetoothAdapter mLocalAdapter;
42 
43     private final CachedBluetoothDeviceManager mCachedDeviceManager;
44 
45     /** The Bluetooth profile manager. */
46     private final LocalBluetoothProfileManager mProfileManager;
47 
48     /** The broadcast receiver event manager. */
49     private final BluetoothEventManager mEventManager;
50 
getInstance(Context context)51     public static synchronized LocalBluetoothManager getInstance(Context context) {
52         if (sInstance == null) {
53             LocalBluetoothAdapter adapter = LocalBluetoothAdapter.getInstance();
54             if (adapter == null) {
55                 return null;
56             }
57             // This will be around as long as this process is
58             Context appContext = context.getApplicationContext();
59             sInstance = new LocalBluetoothManager(adapter, appContext);
60         }
61 
62         return sInstance;
63     }
64 
setDiscoverableEnabler(BluetoothDiscoverableEnabler discoverableEnabler)65     public void setDiscoverableEnabler(BluetoothDiscoverableEnabler discoverableEnabler) {
66         mDiscoverableEnabler = discoverableEnabler;
67     }
68 
getDiscoverableEnabler()69     public BluetoothDiscoverableEnabler getDiscoverableEnabler() {
70         return mDiscoverableEnabler;
71     }
72 
LocalBluetoothManager(LocalBluetoothAdapter adapter, Context context)73     private LocalBluetoothManager(LocalBluetoothAdapter adapter, Context context) {
74         mContext = context;
75         mLocalAdapter = adapter;
76 
77         mCachedDeviceManager = new CachedBluetoothDeviceManager(context);
78         mEventManager = new BluetoothEventManager(mLocalAdapter,
79                 mCachedDeviceManager, context);
80         mProfileManager = new LocalBluetoothProfileManager(context,
81                 mLocalAdapter, mCachedDeviceManager, mEventManager);
82     }
83 
getBluetoothAdapter()84     public LocalBluetoothAdapter getBluetoothAdapter() {
85         return mLocalAdapter;
86     }
87 
getContext()88     public Context getContext() {
89         return mContext;
90     }
91 
getForegroundActivity()92     public Context getForegroundActivity() {
93         return mForegroundActivity;
94     }
95 
isForegroundActivity()96     boolean isForegroundActivity() {
97         return mForegroundActivity != null;
98     }
99 
setForegroundActivity(Context context)100     synchronized void setForegroundActivity(Context context) {
101         if (context != null) {
102             Log.d(TAG, "setting foreground activity to non-null context");
103             mForegroundActivity = context;
104         } else {
105             if (mForegroundActivity != null) {
106                 Log.d(TAG, "setting foreground activity to null");
107                 mForegroundActivity = null;
108             }
109         }
110     }
111 
getCachedDeviceManager()112     CachedBluetoothDeviceManager getCachedDeviceManager() {
113         return mCachedDeviceManager;
114     }
115 
getEventManager()116     BluetoothEventManager getEventManager() {
117         return mEventManager;
118     }
119 
getProfileManager()120     LocalBluetoothProfileManager getProfileManager() {
121         return mProfileManager;
122     }
123 }
124