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.cts.verifier.bluetooth; 18 19 import com.android.cts.verifier.R; 20 21 import android.app.Activity; 22 import android.app.AlertDialog; 23 import android.bluetooth.BluetoothAdapter; 24 import android.bluetooth.BluetoothDevice; 25 import android.content.BroadcastReceiver; 26 import android.content.Context; 27 import android.content.DialogInterface; 28 import android.content.Intent; 29 import android.content.IntentFilter; 30 import android.os.Bundle; 31 import android.view.View; 32 import android.view.Window; 33 import android.view.View.OnClickListener; 34 import android.widget.AdapterView; 35 import android.widget.ArrayAdapter; 36 import android.widget.Button; 37 import android.widget.ListView; 38 import android.widget.TextView; 39 import android.widget.AdapterView.OnItemClickListener; 40 41 import java.util.Set; 42 43 /** 44 * {@link Activity} that shows a list of paired and new devices and returns the device selected 45 * by the user. When the user selects a paired device, it forwards them to the Bluetooth settings 46 * page, so that they can unpair it for the test. 47 */ 48 public class DevicePickerActivity extends Activity { 49 50 public static final String EXTRA_DEVICE_ADDRESS = "deviceAddress"; 51 52 private static final int ENABLE_BLUETOOTH_REQUEST = 1; 53 54 private BluetoothAdapter mBluetoothAdapter; 55 56 private DiscoveryReceiver mReceiver; 57 58 private ArrayAdapter<Device> mNewDevicesAdapter; 59 60 private ArrayAdapter<Device> mPairedDevicesAdapter; 61 62 private TextView mEmptyNewView; 63 64 @Override onCreate(Bundle savedInstanceState)65 protected void onCreate(Bundle savedInstanceState) { 66 super.onCreate(savedInstanceState); 67 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 68 setContentView(R.layout.bt_device_picker); 69 70 mPairedDevicesAdapter = new ArrayAdapter<Device>(this, R.layout.bt_device_name); 71 ListView pairedDevicesListView = (ListView) findViewById(R.id.bt_paired_devices); 72 pairedDevicesListView.setAdapter(mPairedDevicesAdapter); 73 pairedDevicesListView.setOnItemClickListener(new PairedDeviceClickListener()); 74 75 View emptyPairedView = findViewById(R.id.bt_empty_paired_devices); 76 pairedDevicesListView.setEmptyView(emptyPairedView); 77 78 mNewDevicesAdapter = new ArrayAdapter<Device>(this, R.layout.bt_device_name); 79 ListView newDevicesListView = (ListView) findViewById(R.id.bt_new_devices); 80 newDevicesListView.setAdapter(mNewDevicesAdapter); 81 newDevicesListView.setOnItemClickListener(new NewDeviceClickListener()); 82 83 mEmptyNewView = (TextView) findViewById(R.id.bt_empty_new_devices); 84 newDevicesListView.setEmptyView(mEmptyNewView); 85 86 mReceiver = new DiscoveryReceiver(); 87 IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 88 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 89 filter.addAction(BluetoothDevice.ACTION_FOUND); 90 registerReceiver(mReceiver, filter); 91 92 Button scanButton = (Button) findViewById(R.id.bt_scan_button); 93 scanButton.setOnClickListener(new OnClickListener() { 94 @Override 95 public void onClick(View v) { 96 scan(); 97 } 98 }); 99 100 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 101 if (mBluetoothAdapter.isEnabled()) { 102 scan(); 103 } else { 104 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 105 startActivityForResult(intent, ENABLE_BLUETOOTH_REQUEST); 106 } 107 } 108 109 @Override onActivityResult(int requestCode, int resultCode, Intent data)110 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 111 super.onActivityResult(requestCode, resultCode, data); 112 if (requestCode == ENABLE_BLUETOOTH_REQUEST) { 113 if (resultCode == RESULT_OK) { 114 scan(); 115 } else { 116 setResult(RESULT_CANCELED); 117 finish(); 118 } 119 } 120 } 121 scan()122 private void scan() { 123 populatePairedDevices(); 124 mNewDevicesAdapter.clear(); 125 if (mBluetoothAdapter.isDiscovering()) { 126 mBluetoothAdapter.cancelDiscovery(); 127 } 128 mBluetoothAdapter.startDiscovery(); 129 } 130 populatePairedDevices()131 private void populatePairedDevices() { 132 mPairedDevicesAdapter.clear(); 133 Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 134 for (BluetoothDevice device : pairedDevices) { 135 mPairedDevicesAdapter.add(Device.fromBluetoothDevice(device)); 136 } 137 } 138 139 @Override onDestroy()140 protected void onDestroy() { 141 super.onDestroy(); 142 if (mBluetoothAdapter != null) { 143 mBluetoothAdapter.cancelDiscovery(); 144 } 145 unregisterReceiver(mReceiver); 146 } 147 148 class NewDeviceClickListener implements OnItemClickListener { 149 @Override onItemClick(AdapterView<?> parent, View view, int position, long id)150 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 151 Intent data = new Intent(); 152 Device device = (Device) parent.getItemAtPosition(position); 153 data.putExtra(EXTRA_DEVICE_ADDRESS, device.mAddress); 154 setResult(RESULT_OK, data); 155 finish(); 156 } 157 } 158 159 class PairedDeviceClickListener implements OnItemClickListener { 160 @Override onItemClick(AdapterView<?> parent, View view, int position, long id)161 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 162 new AlertDialog.Builder(DevicePickerActivity.this) 163 .setIcon(android.R.drawable.ic_dialog_alert) 164 .setMessage(R.string.bt_unpair) 165 .setPositiveButton(R.string.bt_settings, new DialogInterface.OnClickListener() { 166 @Override 167 public void onClick(DialogInterface dialog, int which) { 168 if (mBluetoothAdapter != null) { 169 mBluetoothAdapter.cancelDiscovery(); 170 } 171 Intent intent = new Intent(); 172 intent.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS); 173 startActivity(intent); 174 } 175 }) 176 .show(); 177 } 178 } 179 180 class DiscoveryReceiver extends BroadcastReceiver { 181 @Override onReceive(Context context, Intent intent)182 public void onReceive(Context context, Intent intent) { 183 if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(intent.getAction())) { 184 mEmptyNewView.setText(R.string.bt_scanning); 185 setProgressBarIndeterminateVisibility(true); 186 } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent.getAction())) { 187 mEmptyNewView.setText(R.string.bt_no_devices); 188 setProgressBarIndeterminateVisibility(false); 189 } else if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) { 190 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 191 if (device.getBondState() != BluetoothDevice.BOND_BONDED) { 192 mNewDevicesAdapter.add(Device.fromBluetoothDevice(device)); 193 } 194 } 195 } 196 } 197 198 static class Device { 199 200 String mName; 201 202 String mAddress; 203 Device(String name, String address)204 Device(String name, String address) { 205 mName = name; 206 mAddress = address; 207 } 208 209 @Override toString()210 public String toString() { 211 return mName + "\n" + mAddress; 212 } 213 fromBluetoothDevice(BluetoothDevice device)214 static Device fromBluetoothDevice(BluetoothDevice device) { 215 return new Device(device.getName() != null ? device.getName() : "", 216 device.getAddress()); 217 } 218 } 219 } 220