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