• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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 
DevicePickerFragment()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 mStartScanOnResume;
51 
52     @Override
addPreferencesForActivity()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
initDevicePreference(BluetoothDevicePreference preference)65     void initDevicePreference(BluetoothDevicePreference preference) {
66         preference.setWidgetLayoutResource(R.layout.preference_empty_list);
67     }
68 
69     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)70     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
71         menu.add(Menu.NONE, MENU_ID_REFRESH, 0, R.string.bluetooth_search_for_devices)
72                 .setEnabled(true)
73                 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
74         super.onCreateOptionsMenu(menu, inflater);
75     }
76 
77     @Override
onOptionsItemSelected(MenuItem item)78     public boolean onOptionsItemSelected(MenuItem item) {
79         switch (item.getItemId()) {
80             case MENU_ID_REFRESH:
81                 mLocalAdapter.startScanning(true);
82                 return true;
83         }
84         return super.onOptionsItemSelected(item);
85     }
86 
87     @Override
getMetricsCategory()88     protected int getMetricsCategory() {
89         return MetricsEvent.BLUETOOTH_DEVICE_PICKER;
90     }
91 
92     @Override
onCreate(Bundle savedInstanceState)93     public void onCreate(Bundle savedInstanceState) {
94         super.onCreate(savedInstanceState);
95         getActivity().setTitle(getString(R.string.device_picker));
96         UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
97         mStartScanOnResume = !um.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH)
98                 && (savedInstanceState == null);  // don't start scan after rotation
99         setHasOptionsMenu(true);
100     }
101 
102     @Override
onResume()103     public void onResume() {
104         super.onResume();
105         addCachedDevices();
106         if (mStartScanOnResume) {
107             mLocalAdapter.startScanning(true);
108             mStartScanOnResume = false;
109         }
110     }
111 
112     @Override
onDevicePreferenceClick(BluetoothDevicePreference btPreference)113     void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
114         mLocalAdapter.stopScanning();
115         LocalBluetoothPreferences.persistSelectedDeviceInPicker(
116                 getActivity(), mSelectedDevice.getAddress());
117         if ((btPreference.getCachedDevice().getBondState() ==
118                 BluetoothDevice.BOND_BONDED) || !mNeedAuth) {
119             sendDevicePickedIntent(mSelectedDevice);
120             finish();
121         } else {
122             super.onDevicePreferenceClick(btPreference);
123         }
124     }
125 
onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState)126     public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice,
127             int bondState) {
128         if (bondState == BluetoothDevice.BOND_BONDED) {
129             BluetoothDevice device = cachedDevice.getDevice();
130             if (device.equals(mSelectedDevice)) {
131                 sendDevicePickedIntent(device);
132                 finish();
133             }
134         }
135     }
136 
137     @Override
onBluetoothStateChanged(int bluetoothState)138     public void onBluetoothStateChanged(int bluetoothState) {
139         super.onBluetoothStateChanged(bluetoothState);
140 
141         if (bluetoothState == BluetoothAdapter.STATE_ON) {
142             mLocalAdapter.startScanning(false);
143         }
144     }
145 
sendDevicePickedIntent(BluetoothDevice device)146     private void sendDevicePickedIntent(BluetoothDevice device) {
147         Intent intent = new Intent(BluetoothDevicePicker.ACTION_DEVICE_SELECTED);
148         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
149         if (mLaunchPackage != null && mLaunchClass != null) {
150             intent.setClassName(mLaunchPackage, mLaunchClass);
151         }
152         getActivity().sendBroadcast(intent);
153     }
154 }
155