2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.bluetooth;
19 import android.bluetooth.BluetoothAdapter;
20 import android.bluetooth.BluetoothDevice;
21 import android.bluetooth.BluetoothDevicePicker;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.os.UserManager;
26 import android.support.annotation.VisibleForTesting;
27 import android.view.Menu;
28 import android.view.MenuInflater;
30 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
31 import com.android.settings.R;
32 import com.android.settings.core.PreferenceController;
33 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
35 import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
37 import java.util.List;
40 * BluetoothSettings is the Settings screen for Bluetooth configuration and
41 * connection management.
43 public final class DevicePickerFragment extends DeviceListPreferenceFragment {
44 private static final String KEY_BT_DEVICE_LIST = "bt_device_list";
45 private static final String TAG = "DevicePickerFragment";
48 BluetoothProgressCategory mAvailableDevicesCategory;
50 private boolean mNeedAuth;
51 private String mLaunchPackage;
52 private String mLaunchClass;
53 private boolean mScanAllowed;
55 public DevicePickerFragment() {
56 super(null /* Not tied to any user restrictions. */);
60 void initPreferencesFromPreferenceScreen() {
61 Intent intent = getActivity().getIntent();
62 mNeedAuth = intent.getBooleanExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
63 setFilter(intent.getIntExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
64 BluetoothDevicePicker.FILTER_TYPE_ALL));
65 mLaunchPackage = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE);
66 mLaunchClass = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS);
67 mAvailableDevicesCategory = (BluetoothProgressCategory) findPreference(KEY_BT_DEVICE_LIST);
71 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
72 super.onCreateOptionsMenu(menu, inflater);
76 public int getMetricsCategory() {
77 return MetricsEvent.BLUETOOTH_DEVICE_PICKER;
81 public void onCreate(Bundle savedInstanceState) {
82 super.onCreate(savedInstanceState);
83 getActivity().setTitle(getString(R.string.device_picker));
84 UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
85 mScanAllowed = !um.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH);
86 setHasOptionsMenu(true);
90 public void onStart() {
93 mSelectedDevice = null;
96 mAvailableDevicesCategory.setProgress(mLocalAdapter.isDiscovering());
101 public void onStop() {
102 // Try disable scanning no matter what, no effect if enableScanning has not been called
108 public void onDestroy() {
110 /* Check if any device was selected, if no device selected
111 * send ACTION_DEVICE_SELECTED with a null device, otherwise
112 * don't do anything */
113 if (mSelectedDevice == null) {
114 sendDevicePickedIntent(null);
119 void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
121 LocalBluetoothPreferences.persistSelectedDeviceInPicker(
122 getActivity(), mSelectedDevice.getAddress());
123 if ((btPreference.getCachedDevice().getBondState() ==
124 BluetoothDevice.BOND_BONDED) || !mNeedAuth) {
125 sendDevicePickedIntent(mSelectedDevice);
128 super.onDevicePreferenceClick(btPreference);
133 public void onScanningStateChanged(boolean started) {
134 super.onScanningStateChanged(started);
135 started |= mScanEnabled;
136 mAvailableDevicesCategory.setProgress(started);
139 public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice,
141 BluetoothDevice device = cachedDevice.getDevice();
142 if (!device.equals(mSelectedDevice)) {
145 if (bondState == BluetoothDevice.BOND_BONDED) {
146 sendDevicePickedIntent(device);
148 } else if (bondState == BluetoothDevice.BOND_NONE) {
154 public void onBluetoothStateChanged(int bluetoothState) {
155 super.onBluetoothStateChanged(bluetoothState);
157 if (bluetoothState == BluetoothAdapter.STATE_ON) {
163 protected String getLogTag() {
168 protected int getPreferenceScreenResId() {
169 return R.xml.device_picker;
173 protected List<PreferenceController> getPreferenceControllers(Context context) {
178 public String getDeviceListKey() {
179 return KEY_BT_DEVICE_LIST;
182 private void sendDevicePickedIntent(BluetoothDevice device) {
183 Intent intent = new Intent(BluetoothDevicePicker.ACTION_DEVICE_SELECTED);
184 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
185 if (mLaunchPackage != null && mLaunchClass != null) {
186 intent.setClassName(mLaunchPackage, mLaunchClass);
188 getActivity().sendBroadcast(intent);