LocalBluetoothManager.java revision 436b29e68e6608bed9e8e7d54385b8f62d89208e
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
17package com.android.settings.bluetooth;
18
19import android.content.Context;
20import 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 */
28public 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
49    public static synchronized LocalBluetoothManager getInstance(Context context) {
50        if (sInstance == null) {
51            LocalBluetoothAdapter adapter = LocalBluetoothAdapter.getInstance();
52            if (adapter == null) {
53                return null;
54            }
55            // This will be around as long as this process is
56            Context appContext = context.getApplicationContext();
57            sInstance = new LocalBluetoothManager(adapter, appContext);
58        }
59
60        return sInstance;
61    }
62
63    private LocalBluetoothManager(LocalBluetoothAdapter adapter, Context context) {
64        mContext = context;
65        mLocalAdapter = adapter;
66
67        mCachedDeviceManager = new CachedBluetoothDeviceManager();
68        mEventManager = new BluetoothEventManager(mLocalAdapter,
69                mCachedDeviceManager);
70        mProfileManager = new LocalBluetoothProfileManager(context,
71                mLocalAdapter, mCachedDeviceManager, mEventManager);
72    }
73
74    public LocalBluetoothAdapter getBluetoothAdapter() {
75        return mLocalAdapter;
76    }
77
78    public Context getContext() {
79        return mContext;
80    }
81
82    boolean isForegroundActivity() {
83        return mForegroundActivity != null;
84    }
85
86    synchronized void setForegroundActivity(Context context) {
87        if (context != null) {
88            Log.d(TAG, "setting foreground activity to non-null context");
89            mForegroundActivity = context;
90            mEventManager.resume(context);
91        } else {
92            if (mForegroundActivity != null) {
93                Log.d(TAG, "setting foreground activity to null");
94                mEventManager.pause(mForegroundActivity);
95                mForegroundActivity = null;
96            }
97        }
98    }
99
100    CachedBluetoothDeviceManager getCachedDeviceManager() {
101        return mCachedDeviceManager;
102    }
103
104    BluetoothEventManager getEventManager() {
105        return mEventManager;
106    }
107
108    LocalBluetoothProfileManager getProfileManager() {
109        return mProfileManager;
110    }
111}
112