1 /* 2 * Copyright (C) 2016 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 android.app.AlertDialog; 20 import android.app.Dialog; 21 import android.app.ProgressDialog; 22 import android.bluetooth.BluetoothAdapter; 23 import android.bluetooth.BluetoothManager; 24 import android.content.BroadcastReceiver; 25 import android.content.Context; 26 import android.content.DialogInterface; 27 import android.content.Intent; 28 import android.content.IntentFilter; 29 import android.os.Bundle; 30 import android.os.Handler; 31 import android.widget.ListView; 32 import android.widget.Toast; 33 34 import com.android.cts.verifier.PassFailButtons; 35 import com.android.cts.verifier.R; 36 37 import java.util.ArrayList; 38 import java.util.List; 39 40 public class BleConnectionPriorityClientBaseActivity extends PassFailButtons.Activity { 41 42 private TestAdapter mTestAdapter; 43 private int mPassed = 0; 44 private Dialog mDialog; 45 46 private static final int BLE_CONNECTION_HIGH = 0; 47 private static final int BLE_CONNECTION_BALANCED = 1; 48 private static final int BLE_CONNECTION_LOW = 2; 49 50 private static final int PASSED_HIGH = 0x1; 51 private static final int PASSED_BALANCED = 0x2; 52 private static final int PASSED_LOW = 0x4; 53 private static final int ALL_PASSED = 0x7; 54 55 private boolean mSecure; 56 57 private Handler mHandler; 58 private int mCurrentTest = -1; 59 60 @Override 61 protected void onCreate(Bundle savedInstanceState) { 62 super.onCreate(savedInstanceState); 63 setContentView(R.layout.ble_connection_priority_client_test); 64 setPassFailButtonClickListeners(); 65 setInfoResources(R.string.ble_connection_priority_client_name, 66 R.string.ble_connection_priority_client_info, -1); 67 getPassButton().setEnabled(false); 68 69 mHandler = new Handler(); 70 71 mTestAdapter = new TestAdapter(this, setupTestList()); 72 ListView listView = (ListView) findViewById(R.id.ble_client_connection_tests); 73 listView.setAdapter(mTestAdapter); 74 } 75 76 @Override 77 public void onResume() { 78 super.onResume(); 79 80 IntentFilter filter = new IntentFilter(); 81 filter.addAction(BleConnectionPriorityClientService.ACTION_BLUETOOTH_DISABLED); 82 filter.addAction(BleConnectionPriorityClientService.ACTION_CONNECTION_SERVICES_DISCOVERED); 83 filter.addAction(BleConnectionPriorityClientService.ACTION_FINISH_CONNECTION_PRIORITY_HIGH); 84 filter.addAction(BleConnectionPriorityClientService.ACTION_FINISH_CONNECTION_PRIORITY_BALANCED); 85 filter.addAction(BleConnectionPriorityClientService.ACTION_FINISH_CONNECTION_PRIORITY_LOW_POWER); 86 filter.addAction(BleConnectionPriorityClientService.ACTION_BLUETOOTH_MISMATCH_SECURE); 87 filter.addAction(BleConnectionPriorityClientService.ACTION_BLUETOOTH_MISMATCH_INSECURE); 88 filter.addAction(BleConnectionPriorityClientService.ACTION_FINISH_DISCONNECT); 89 registerReceiver(mBroadcast, filter); 90 } 91 92 @Override 93 public void onPause() { 94 super.onPause(); 95 unregisterReceiver(mBroadcast); 96 closeDialog(); 97 } 98 99 protected void setSecure(boolean secure) { 100 mSecure = secure; 101 } 102 103 public boolean isSecure() { 104 return mSecure; 105 } 106 107 private synchronized void closeDialog() { 108 if (mDialog != null) { 109 mDialog.dismiss(); 110 mDialog = null; 111 } 112 } 113 114 private synchronized void showProgressDialog() { 115 closeDialog(); 116 117 ProgressDialog dialog = new ProgressDialog(this); 118 dialog.setTitle(R.string.ble_test_running); 119 dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 120 dialog.setMessage(getString(R.string.ble_test_running_message)); 121 dialog.setCanceledOnTouchOutside(false); 122 mDialog = dialog; 123 mDialog.show(); 124 } 125 126 private void showErrorDialog(int titleId, int messageId, boolean finish) { 127 AlertDialog.Builder builder = new AlertDialog.Builder(this) 128 .setTitle(titleId) 129 .setMessage(messageId); 130 if (finish) { 131 builder.setOnCancelListener(new Dialog.OnCancelListener() { 132 @Override 133 public void onCancel(DialogInterface dialog) { 134 finish(); 135 } 136 }); 137 } 138 builder.create().show(); 139 } 140 141 private List<Integer> setupTestList() { 142 ArrayList<Integer> testList = new ArrayList<Integer>(); 143 testList.add(R.string.ble_connection_priority_client_high); 144 testList.add(R.string.ble_connection_priority_client_balanced); 145 testList.add(R.string.ble_connection_priority_client_low); 146 return testList; 147 } 148 149 private void executeNextTest(long delay) { 150 mHandler.postDelayed(new Runnable() { 151 @Override 152 public void run() { 153 executeNextTestImpl(); 154 } 155 }, delay); 156 } 157 private void executeNextTestImpl() { 158 switch (mCurrentTest) { 159 case -1: { 160 mCurrentTest = BLE_CONNECTION_HIGH; 161 Intent intent = new Intent(this, BleConnectionPriorityClientService.class); 162 intent.setAction(BleConnectionPriorityClientService.ACTION_CONNECTION_PRIORITY_HIGH); 163 startService(intent); 164 String msg = getString(R.string.ble_client_connection_priority) 165 + getString(R.string.ble_connection_priority_high); 166 Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); 167 } 168 break; 169 case BLE_CONNECTION_BALANCED: { 170 mCurrentTest = BLE_CONNECTION_LOW; 171 Intent intent = new Intent(this, BleConnectionPriorityClientService.class); 172 intent.setAction(BleConnectionPriorityClientService.ACTION_CONNECTION_PRIORITY_LOW_POWER); 173 startService(intent); 174 String msg = getString(R.string.ble_client_connection_priority) 175 + getString(R.string.ble_connection_priority_low); 176 Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); 177 } 178 break; 179 case BLE_CONNECTION_HIGH: { 180 mCurrentTest = BLE_CONNECTION_BALANCED; 181 Intent intent = new Intent(this, BleConnectionPriorityClientService.class); 182 intent.setAction(BleConnectionPriorityClientService.ACTION_CONNECTION_PRIORITY_BALANCED); 183 startService(intent); 184 String msg = getString(R.string.ble_client_connection_priority) 185 + getString(R.string.ble_connection_priority_balanced); 186 Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); 187 } 188 break; 189 case BLE_CONNECTION_LOW: 190 // all test done 191 closeDialog(); 192 if (mPassed == ALL_PASSED) { 193 Intent intent = new Intent(this, BleConnectionPriorityClientService.class); 194 intent.setAction(BleConnectionPriorityClientService.ACTION_DISCONNECT); 195 startService(intent); 196 } 197 break; 198 default: 199 // something went wrong 200 closeDialog(); 201 break; 202 } 203 } 204 205 public boolean shouldRebootBluetoothAfterTest() { 206 return false; 207 } 208 209 private BroadcastReceiver mBroadcast = new BroadcastReceiver() { 210 @Override 211 public void onReceive(Context context, Intent intent) { 212 String action = intent.getAction(); 213 switch (action) { 214 case BleConnectionPriorityClientService.ACTION_BLUETOOTH_DISABLED: 215 new AlertDialog.Builder(context) 216 .setTitle(R.string.ble_bluetooth_disable_title) 217 .setMessage(R.string.ble_bluetooth_disable_message) 218 .setOnCancelListener(new Dialog.OnCancelListener() { 219 @Override 220 public void onCancel(DialogInterface dialog) { 221 finish(); 222 } 223 }) 224 .create().show(); 225 break; 226 case BleConnectionPriorityClientService.ACTION_CONNECTION_SERVICES_DISCOVERED: 227 showProgressDialog(); 228 executeNextTest(3000); 229 break; 230 case BleConnectionPriorityClientService.ACTION_FINISH_CONNECTION_PRIORITY_HIGH: 231 mTestAdapter.setTestPass(BLE_CONNECTION_HIGH); 232 mPassed |= PASSED_HIGH; 233 executeNextTest(1000); 234 break; 235 case BleConnectionPriorityClientService.ACTION_FINISH_CONNECTION_PRIORITY_BALANCED: 236 mTestAdapter.setTestPass(BLE_CONNECTION_BALANCED); 237 mPassed |= PASSED_BALANCED; 238 executeNextTest(1000); 239 break; 240 case BleConnectionPriorityClientService.ACTION_FINISH_CONNECTION_PRIORITY_LOW_POWER: 241 mTestAdapter.setTestPass(BLE_CONNECTION_LOW); 242 mPassed |= PASSED_LOW; 243 executeNextTest(100); 244 break; 245 case BleConnectionPriorityClientService.ACTION_BLUETOOTH_MISMATCH_SECURE: 246 showErrorDialog(R.string.ble_bluetooth_mismatch_title, R.string.ble_bluetooth_mismatch_secure_message, true); 247 break; 248 case BleConnectionPriorityClientService.ACTION_BLUETOOTH_MISMATCH_INSECURE: 249 showErrorDialog(R.string.ble_bluetooth_mismatch_title, R.string.ble_bluetooth_mismatch_insecure_message, true); 250 break; 251 case BleConnectionPriorityClientService.ACTION_FINISH_DISCONNECT: 252 if (shouldRebootBluetoothAfterTest()) { 253 mBtPowerSwitcher.executeSwitching(); 254 } else { 255 getPassButton().setEnabled(true); 256 } 257 break; 258 } 259 mTestAdapter.notifyDataSetChanged(); 260 } 261 }; 262 263 private static final long BT_ON_DELAY = 10000; 264 private final BluetoothPowerSwitcher mBtPowerSwitcher = new BluetoothPowerSwitcher(); 265 private class BluetoothPowerSwitcher extends BroadcastReceiver { 266 267 private boolean mIsSwitching = false; 268 private BluetoothAdapter mAdapter; 269 270 public void executeSwitching() { 271 if (mAdapter == null) { 272 BluetoothManager btMgr = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 273 mAdapter = btMgr.getAdapter(); 274 } 275 276 if (!mIsSwitching) { 277 IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 278 registerReceiver(this, filter); 279 mIsSwitching = true; 280 mHandler.postDelayed(new Runnable() { 281 @Override 282 public void run() { 283 mAdapter.disable(); 284 } 285 }, 1000); 286 showProgressDialog(); 287 } 288 } 289 290 @Override 291 public void onReceive(Context context, Intent intent) { 292 if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { 293 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); 294 if (state == BluetoothAdapter.STATE_OFF) { 295 mHandler.postDelayed(new Runnable() { 296 @Override 297 public void run() { 298 mAdapter.enable(); 299 } 300 }, BT_ON_DELAY); 301 } else if (state == BluetoothAdapter.STATE_ON) { 302 mIsSwitching = false; 303 unregisterReceiver(this); 304 getPassButton().setEnabled(true); 305 closeDialog(); 306 } 307 } 308 } 309 } 310 } 311