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 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.view.Menu; 27 import android.view.MenuInflater; 28 import android.view.MenuItem; 29 30 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 31 import com.android.settings.R; 32 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 33 34 import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH; 35 36 /** 37 * BluetoothSettings is the Settings screen for Bluetooth configuration and 38 * connection management. 39 */ 40 public final class DevicePickerFragment extends DeviceListPreferenceFragment { 41 private static final int MENU_ID_REFRESH = Menu.FIRST; 42 43 public DevicePickerFragment() { 44 super(null /* Not tied to any user restrictions. */); 45 } 46 47 private boolean mNeedAuth; 48 private String mLaunchPackage; 49 private String mLaunchClass; 50 private boolean mStartScanOnStart; 51 52 @Override 53 void addPreferencesForActivity() { 54 addPreferencesFromResource(R.xml.device_picker); 55 56 Intent intent = getActivity().getIntent(); 57 mNeedAuth = intent.getBooleanExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false); 58 setFilter(intent.getIntExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE, 59 BluetoothDevicePicker.FILTER_TYPE_ALL)); 60 mLaunchPackage = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE); 61 mLaunchClass = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS); 62 } 63 64 @Override 65 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 66 menu.add(Menu.NONE, MENU_ID_REFRESH, 0, R.string.bluetooth_search_for_devices) 67 .setEnabled(true) 68 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); 69 super.onCreateOptionsMenu(menu, inflater); 70 } 71 72 @Override 73 public boolean onOptionsItemSelected(MenuItem item) { 74 switch (item.getItemId()) { 75 case MENU_ID_REFRESH: 76 mLocalAdapter.startScanning(true); 77 return true; 78 } 79 return super.onOptionsItemSelected(item); 80 } 81 82 @Override 83 public int getMetricsCategory() { 84 return MetricsEvent.BLUETOOTH_DEVICE_PICKER; 85 } 86 87 @Override 88 public void onCreate(Bundle savedInstanceState) { 89 super.onCreate(savedInstanceState); 90 getActivity().setTitle(getString(R.string.device_picker)); 91 UserManager um = (UserManager) getSystemService(Context.USER_SERVICE); 92 mStartScanOnStart = !um.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH) 93 && (savedInstanceState == null); // don't start scan after rotation 94 setHasOptionsMenu(true); 95 } 96 97 @Override 98 public void onStart() { 99 super.onStart(); 100 addCachedDevices(); 101 if (mStartScanOnStart) { 102 mLocalAdapter.startScanning(true); 103 mStartScanOnStart = false; 104 } 105 } 106 107 @Override 108 void onDevicePreferenceClick(BluetoothDevicePreference btPreference) { 109 mLocalAdapter.stopScanning(); 110 LocalBluetoothPreferences.persistSelectedDeviceInPicker( 111 getActivity(), mSelectedDevice.getAddress()); 112 if ((btPreference.getCachedDevice().getBondState() == 113 BluetoothDevice.BOND_BONDED) || !mNeedAuth) { 114 sendDevicePickedIntent(mSelectedDevice); 115 finish(); 116 } else { 117 super.onDevicePreferenceClick(btPreference); 118 } 119 } 120 121 public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, 122 int bondState) { 123 if (bondState == BluetoothDevice.BOND_BONDED) { 124 BluetoothDevice device = cachedDevice.getDevice(); 125 if (device.equals(mSelectedDevice)) { 126 sendDevicePickedIntent(device); 127 finish(); 128 } 129 } 130 } 131 132 @Override 133 public void onBluetoothStateChanged(int bluetoothState) { 134 super.onBluetoothStateChanged(bluetoothState); 135 136 if (bluetoothState == BluetoothAdapter.STATE_ON) { 137 mLocalAdapter.startScanning(false); 138 } 139 } 140 141 private void sendDevicePickedIntent(BluetoothDevice device) { 142 Intent intent = new Intent(BluetoothDevicePicker.ACTION_DEVICE_SELECTED); 143 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); 144 if (mLaunchPackage != null && mLaunchClass != null) { 145 intent.setClassName(mLaunchPackage, mLaunchClass); 146 } 147 getActivity().sendBroadcast(intent); 148 } 149 } 150