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 static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH; 20 21import android.bluetooth.BluetoothAdapter; 22import android.bluetooth.BluetoothDevice; 23import android.bluetooth.BluetoothDevicePicker; 24import android.content.Context; 25import android.content.Intent; 26import android.os.Bundle; 27import android.os.UserManager; 28 29import com.android.settings.R; 30 31/** 32 * BluetoothSettings is the Settings screen for Bluetooth configuration and 33 * connection management. 34 */ 35public final class DevicePickerFragment extends DeviceListPreferenceFragment { 36 37 public DevicePickerFragment() { 38 super(null /* Not tied to any user restrictions. */); 39 } 40 41 private boolean mNeedAuth; 42 private String mLaunchPackage; 43 private String mLaunchClass; 44 private boolean mStartScanOnResume; 45 46 @Override 47 void addPreferencesForActivity() { 48 addPreferencesFromResource(R.xml.device_picker); 49 50 Intent intent = getActivity().getIntent(); 51 mNeedAuth = intent.getBooleanExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false); 52 setFilter(intent.getIntExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE, 53 BluetoothDevicePicker.FILTER_TYPE_ALL)); 54 mLaunchPackage = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE); 55 mLaunchClass = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS); 56 } 57 58 @Override 59 public void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 getActivity().setTitle(getString(R.string.device_picker)); 62 UserManager um = (UserManager) getSystemService(Context.USER_SERVICE); 63 mStartScanOnResume = !um.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH) 64 && (savedInstanceState == null); // don't start scan after rotation 65 } 66 67 @Override 68 public void onResume() { 69 super.onResume(); 70 addCachedDevices(); 71 if (mStartScanOnResume) { 72 mLocalAdapter.startScanning(true); 73 mStartScanOnResume = false; 74 } 75 } 76 77 @Override 78 void onDevicePreferenceClick(BluetoothDevicePreference btPreference) { 79 mLocalAdapter.stopScanning(); 80 LocalBluetoothPreferences.persistSelectedDeviceInPicker( 81 getActivity(), mSelectedDevice.getAddress()); 82 if ((btPreference.getCachedDevice().getBondState() == 83 BluetoothDevice.BOND_BONDED) || !mNeedAuth) { 84 sendDevicePickedIntent(mSelectedDevice); 85 finish(); 86 } else { 87 super.onDevicePreferenceClick(btPreference); 88 } 89 } 90 91 public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, 92 int bondState) { 93 if (bondState == BluetoothDevice.BOND_BONDED) { 94 BluetoothDevice device = cachedDevice.getDevice(); 95 if (device.equals(mSelectedDevice)) { 96 sendDevicePickedIntent(device); 97 finish(); 98 } 99 } 100 } 101 102 @Override 103 public void onBluetoothStateChanged(int bluetoothState) { 104 super.onBluetoothStateChanged(bluetoothState); 105 106 if (bluetoothState == BluetoothAdapter.STATE_ON) { 107 mLocalAdapter.startScanning(false); 108 } 109 } 110 111 private void sendDevicePickedIntent(BluetoothDevice device) { 112 Intent intent = new Intent(BluetoothDevicePicker.ACTION_DEVICE_SELECTED); 113 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); 114 if (mLaunchPackage != null && mLaunchClass != null) { 115 intent.setClassName(mLaunchPackage, mLaunchClass); 116 } 117 getActivity().sendBroadcast(intent); 118 } 119} 120