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 static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH; 20 21 import android.bluetooth.BluetoothAdapter; 22 import android.bluetooth.BluetoothDevice; 23 import android.bluetooth.BluetoothDevicePicker; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.os.Bundle; 27 import android.os.UserManager; 28 import android.support.annotation.VisibleForTesting; 29 import android.view.Menu; 30 import android.view.MenuInflater; 31 32 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 33 import com.android.settings.R; 34 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 35 import com.android.settingslib.core.AbstractPreferenceController; 36 37 import java.util.List; 38 39 /** 40 * BluetoothSettings is the Settings screen for Bluetooth configuration and 41 * connection management. 42 */ 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"; 46 47 @VisibleForTesting 48 BluetoothProgressCategory mAvailableDevicesCategory; 49 50 private boolean mNeedAuth; 51 private String mLaunchPackage; 52 private String mLaunchClass; 53 private boolean mScanAllowed; 54 55 public DevicePickerFragment() { 56 super(null /* Not tied to any user restrictions. */); 57 } 58 59 @Override 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); 68 } 69 70 @Override 71 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 72 super.onCreateOptionsMenu(menu, inflater); 73 } 74 75 @Override 76 public int getMetricsCategory() { 77 return MetricsEvent.BLUETOOTH_DEVICE_PICKER; 78 } 79 80 @Override 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); 87 } 88 89 @Override 90 public void onStart() { 91 super.onStart(); 92 addCachedDevices(); 93 mSelectedDevice = null; 94 if (mScanAllowed) { 95 enableScanning(); 96 mAvailableDevicesCategory.setProgress(mLocalAdapter.isDiscovering()); 97 } 98 } 99 100 @Override 101 public void onStop() { 102 // Try disable scanning no matter what, no effect if enableScanning has not been called 103 disableScanning(); 104 super.onStop(); 105 } 106 107 @Override 108 public void onDestroy() { 109 super.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); 115 } 116 } 117 118 @Override 119 void onDevicePreferenceClick(BluetoothDevicePreference btPreference) { 120 disableScanning(); 121 LocalBluetoothPreferences.persistSelectedDeviceInPicker( 122 getActivity(), mSelectedDevice.getAddress()); 123 if ((btPreference.getCachedDevice().getBondState() == 124 BluetoothDevice.BOND_BONDED) || !mNeedAuth) { 125 sendDevicePickedIntent(mSelectedDevice); 126 finish(); 127 } else { 128 super.onDevicePreferenceClick(btPreference); 129 } 130 } 131 132 @Override 133 public void onScanningStateChanged(boolean started) { 134 super.onScanningStateChanged(started); 135 started |= mScanEnabled; 136 mAvailableDevicesCategory.setProgress(started); 137 } 138 139 public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, 140 int bondState) { 141 BluetoothDevice device = cachedDevice.getDevice(); 142 if (!device.equals(mSelectedDevice)) { 143 return; 144 } 145 if (bondState == BluetoothDevice.BOND_BONDED) { 146 sendDevicePickedIntent(device); 147 finish(); 148 } else if (bondState == BluetoothDevice.BOND_NONE) { 149 enableScanning(); 150 } 151 } 152 153 @Override 154 public void onBluetoothStateChanged(int bluetoothState) { 155 super.onBluetoothStateChanged(bluetoothState); 156 157 if (bluetoothState == BluetoothAdapter.STATE_ON) { 158 enableScanning(); 159 } 160 } 161 162 @Override 163 protected String getLogTag() { 164 return TAG; 165 } 166 167 @Override 168 protected int getPreferenceScreenResId() { 169 return R.xml.device_picker; 170 } 171 172 @Override 173 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 174 return null; 175 } 176 177 @Override 178 public String getDeviceListKey() { 179 return KEY_BT_DEVICE_LIST; 180 } 181 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); 187 } 188 getActivity().sendBroadcast(intent); 189 } 190 } 191