17ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk/*
27ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * Copyright (C) 2011 The Android Open Source Project
37ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk *
47ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * Licensed under the Apache License, Version 2.0 (the "License");
57ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * you may not use this file except in compliance with the License.
67ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * You may obtain a copy of the License at
77ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk *
87ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk *      http://www.apache.org/licenses/LICENSE-2.0
97ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk *
107ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * Unless required by applicable law or agreed to in writing, software
117ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * distributed under the License is distributed on an "AS IS" BASIS,
127ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * See the License for the specific language governing permissions and
147ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * limitations under the License.
157ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk */
167ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
177ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkpackage com.android.settingslib.bluetooth;
187ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
197ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.content.Context;
207ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.util.Log;
217ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
227ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk/**
237ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * LocalBluetoothManager provides a simplified interface on top of a subset of
247ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * the Bluetooth API. Note that {@link #getInstance} will return null
257ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * if there is no Bluetooth adapter on this device, and callers must be
267ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * prepared to handle this case.
277ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk */
2882dd3b0bf6716ccee0f82b2b2db234cdefd34ff6Fan Zhangpublic class LocalBluetoothManager {
297ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private static final String TAG = "LocalBluetoothManager";
307ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
317ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    /** Singleton instance. */
327ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private static LocalBluetoothManager sInstance;
337ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
347ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private final Context mContext;
357ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
367ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    /** If a BT-related activity is in the foreground, this will be it. */
377ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private Context mForegroundActivity;
387ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
397ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private final LocalBluetoothAdapter mLocalAdapter;
407ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
417ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private final CachedBluetoothDeviceManager mCachedDeviceManager;
427ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
437ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    /** The Bluetooth profile manager. */
447ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private final LocalBluetoothProfileManager mProfileManager;
457ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
467ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    /** The broadcast receiver event manager. */
477ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private final BluetoothEventManager mEventManager;
487ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
497ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public static synchronized LocalBluetoothManager getInstance(Context context,
507ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            BluetoothManagerCallback onInitCallback) {
517ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        if (sInstance == null) {
527ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            LocalBluetoothAdapter adapter = LocalBluetoothAdapter.getInstance();
537ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            if (adapter == null) {
547ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk                return null;
557ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            }
567ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            // This will be around as long as this process is
577ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            Context appContext = context.getApplicationContext();
587ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            sInstance = new LocalBluetoothManager(adapter, appContext);
597ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            if (onInitCallback != null) {
607ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk                onInitCallback.onBluetoothManagerInitialized(appContext, sInstance);
617ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            }
627ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        }
637ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
647ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return sInstance;
657ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
667ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
677ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    private LocalBluetoothManager(LocalBluetoothAdapter adapter, Context context) {
687ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        mContext = context;
697ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        mLocalAdapter = adapter;
707ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
71be3c5dbee66758517a8198f98ed2e20c80af326bJason Monk        mCachedDeviceManager = new CachedBluetoothDeviceManager(context, this);
727ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        mEventManager = new BluetoothEventManager(mLocalAdapter,
737ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk                mCachedDeviceManager, context);
747ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        mProfileManager = new LocalBluetoothProfileManager(context,
757ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk                mLocalAdapter, mCachedDeviceManager, mEventManager);
767ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
777ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
787ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public LocalBluetoothAdapter getBluetoothAdapter() {
797ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return mLocalAdapter;
807ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
817ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
827ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public Context getContext() {
837ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return mContext;
847ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
857ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
867ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public Context getForegroundActivity() {
877ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return mForegroundActivity;
887ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
897ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
907ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public boolean isForegroundActivity() {
917ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return mForegroundActivity != null;
927ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
937ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
947ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public synchronized void setForegroundActivity(Context context) {
957ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        if (context != null) {
967ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            Log.d(TAG, "setting foreground activity to non-null context");
977ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            mForegroundActivity = context;
987ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        } else {
997ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            if (mForegroundActivity != null) {
1007ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk                Log.d(TAG, "setting foreground activity to null");
1017ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk                mForegroundActivity = null;
1027ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk            }
1037ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        }
1047ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1057ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
1067ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public CachedBluetoothDeviceManager getCachedDeviceManager() {
1077ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return mCachedDeviceManager;
1087ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1097ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
1107ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public BluetoothEventManager getEventManager() {
1117ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return mEventManager;
1127ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1137ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
1147ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public LocalBluetoothProfileManager getProfileManager() {
1157ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        return mProfileManager;
1167ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1177ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk
1187ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    public interface BluetoothManagerCallback {
1197ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk        void onBluetoothManagerInitialized(Context appContext,
1207ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk                LocalBluetoothManager bluetoothManager);
1217ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk    }
1227ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk}
123