1 /* 2 * Copyright (C) 2009 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.tests; 18 19 import android.app.Activity; 20 import android.bluetooth.BluetoothAdapter; 21 import android.bluetooth.BluetoothDevice; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.os.Bundle; 27 import android.util.Log; 28 import android.view.View; 29 import android.view.View.OnClickListener; 30 import android.widget.ArrayAdapter; 31 import android.widget.Button; 32 import android.widget.ListView; 33 34 public class BluetoothRequestPermissionTest extends Activity { 35 private static final String TAG = "BluetoothRequestPermissionTest"; 36 BluetoothAdapter mAdapter; 37 private ArrayAdapter<String> mMsgAdapter; 38 39 // Discoverable button alternates between 20 second timeout and no timeout. 40 private boolean mDiscoveryWithTimeout = true; 41 42 private class BtOnClickListener implements OnClickListener { 43 final boolean mEnableOnly; // enable or enable + discoverable 44 BtOnClickListener(boolean enableOnly)45 public BtOnClickListener(boolean enableOnly) { 46 mEnableOnly = enableOnly; 47 } 48 onClick(View v)49 public void onClick(View v) { 50 requestPermission(mEnableOnly); 51 } 52 } 53 54 private class BtScanOnClickListener implements OnClickListener { onClick(View v)55 public void onClick(View v) { 56 Button scanButton = (Button) v; 57 if (mAdapter.isDiscovering()) { 58 mAdapter.cancelDiscovery(); 59 scanButton.setText(R.string.start_scan); 60 } else { 61 mAdapter.startDiscovery(); 62 scanButton.setText(R.string.stop_scan); 63 } 64 } 65 } 66 67 @Override onCreate(Bundle icicle)68 public void onCreate(Bundle icicle) { 69 super.onCreate(icicle); 70 setContentView(R.layout.bluetooth_request_permission_test); 71 mAdapter = BluetoothAdapter.getDefaultAdapter(); 72 73 Button enable = (Button) findViewById(R.id.enable); 74 enable.setOnClickListener(new BtOnClickListener(true /* enable */)); 75 76 Button discoverable = (Button) findViewById(R.id.discoverable); 77 discoverable.setOnClickListener(new BtOnClickListener(false /* enable & discoverable */)); 78 79 Button scanButton = (Button) findViewById(R.id.scan); 80 scanButton.setOnClickListener(new BtScanOnClickListener()); 81 if (mAdapter.isDiscovering()) { 82 scanButton.setText(R.string.stop_scan); 83 } else { 84 scanButton.setText(R.string.start_scan); 85 } 86 87 mMsgAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); 88 89 ListView listView = (ListView) findViewById(R.id.msg_container); 90 listView.setAdapter(mMsgAdapter); 91 92 IntentFilter filter = new IntentFilter(); 93 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 94 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 95 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); 96 filter.addAction(BluetoothDevice.ACTION_FOUND); 97 registerReceiver(mReceiver, filter); 98 addMsg("Initialized"); 99 } 100 requestPermission(boolean enableOnly)101 void requestPermission(boolean enableOnly) { 102 Intent i = new Intent(); 103 if (enableOnly) { 104 addMsg("Starting activity to enable bt"); 105 i.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE); 106 } else { 107 addMsg("Starting activity to enable bt + discovery"); 108 i.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 109 // Discoverability duration toggles between 20 seconds and no timeout. 110 int timeout = (mDiscoveryWithTimeout ? 20 : 0); 111 i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, timeout); 112 mDiscoveryWithTimeout = !mDiscoveryWithTimeout; 113 } 114 startActivityForResult(i, 1); 115 } 116 117 @Override onActivityResult(int requestCode, int resultCode, Intent data)118 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 119 if (requestCode != 1) { 120 Log.e(TAG, "Unexpected onActivityResult " + requestCode + " " + resultCode); 121 return; 122 } 123 124 if (resultCode == Activity.RESULT_CANCELED) { 125 addMsg("Result = RESULT_CANCELED"); 126 } else if (resultCode == Activity.RESULT_OK) { 127 addMsg("Result = RESULT_OK (not expected for discovery)"); 128 } else { 129 addMsg("Result = " + resultCode); 130 } 131 } 132 133 @Override onDestroy()134 protected void onDestroy() { 135 super.onDestroy(); 136 unregisterReceiver(mReceiver); 137 } 138 addMsg(String msg)139 private void addMsg(String msg) { 140 mMsgAdapter.add(msg); 141 Log.d(TAG, "msg"); 142 } 143 144 private BroadcastReceiver mReceiver = new BroadcastReceiver() { 145 @Override 146 public void onReceive(Context context, Intent intent) { 147 if (intent == null) 148 return; 149 String action = intent.getAction(); 150 if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { 151 String stateStr = "???"; 152 switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR)) { 153 case BluetoothAdapter.STATE_OFF: 154 stateStr = "off"; 155 break; 156 case BluetoothAdapter.STATE_TURNING_ON: 157 stateStr = "turning on"; 158 break; 159 case BluetoothAdapter.STATE_ON: 160 stateStr = "on"; 161 break; 162 case BluetoothAdapter.STATE_TURNING_OFF: 163 stateStr = "turning off"; 164 break; 165 } 166 addMsg("Bluetooth status = " + stateStr); 167 } else if (action.equals(BluetoothDevice.ACTION_FOUND)) { 168 String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); 169 addMsg("Found: " + name); 170 } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) { 171 addMsg("Scan started..."); 172 } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { 173 addMsg("Scan ended"); 174 } 175 } 176 }; 177 } 178