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.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.DialogInterface; 25 import android.content.Intent; 26 import android.content.IntentFilter; 27 import android.os.Bundle; 28 import android.widget.ListView; 29 import android.widget.Toast; 30 31 import com.android.cts.verifier.PassFailButtons; 32 import com.android.cts.verifier.R; 33 34 import java.util.ArrayList; 35 import java.util.List; 36 37 public class BleConnectionPriorityServerBaseActivity extends PassFailButtons.Activity { 38 39 public static final int CONNECTION_PRIORITY_HIGH = 0; 40 41 private TestAdapter mTestAdapter; 42 43 private Dialog mDialog; 44 45 @Override onCreate(Bundle savedInstanceState)46 protected void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 setContentView(R.layout.ble_connection_priority_server_test); 49 setPassFailButtonClickListeners(); 50 setInfoResources(R.string.ble_connection_priority_server_name, 51 R.string.ble_connection_priority_server_info, -1); 52 53 getPassButton().setEnabled(false); 54 55 mTestAdapter = new TestAdapter(this, setupTestList()); 56 ListView listView = (ListView) findViewById(R.id.ble_server_connection_tests); 57 listView.setAdapter(mTestAdapter); 58 59 startService(new Intent(this, BleConnectionPriorityServerService.class)); 60 } 61 62 @Override onResume()63 public void onResume() { 64 super.onResume(); 65 66 IntentFilter filter = new IntentFilter(); 67 filter.addAction(BleConnectionPriorityServerService.ACTION_BLUETOOTH_DISABLED); 68 filter.addAction(BleConnectionPriorityServerService.ACTION_CONNECTION_PRIORITY_FINISH); 69 filter.addAction(BleServerService.BLE_ADVERTISE_UNSUPPORTED); 70 filter.addAction(BleServerService.BLE_OPEN_FAIL); 71 filter.addAction(BleConnectionPriorityServerService.ACTION_START_CONNECTION_PRIORITY_TEST); 72 registerReceiver(mBroadcast, filter); 73 } 74 75 @Override onPause()76 public void onPause() { 77 super.onPause(); 78 unregisterReceiver(mBroadcast); 79 closeDialog(); 80 } 81 setupTestList()82 private List<Integer> setupTestList() { 83 ArrayList<Integer> testList = new ArrayList<Integer>(); 84 testList.add(R.string.ble_connection_priority_client_description); 85 return testList; 86 } 87 88 @Override onDestroy()89 public void onDestroy() { 90 super.onDestroy(); 91 stopService(new Intent(this, BleConnectionPriorityServerService.class)); 92 } 93 closeDialog()94 private void closeDialog() { 95 if (mDialog != null) { 96 mDialog.dismiss(); 97 mDialog = null; 98 } 99 } 100 showErrorDialog(int titleId, int messageId, boolean finish)101 private void showErrorDialog(int titleId, int messageId, boolean finish) { 102 closeDialog(); 103 104 AlertDialog.Builder builder = new AlertDialog.Builder(this) 105 .setTitle(titleId) 106 .setMessage(messageId); 107 if (finish) { 108 builder.setOnCancelListener(new Dialog.OnCancelListener() { 109 @Override 110 public void onCancel(DialogInterface dialog) { 111 finish(); 112 } 113 }); 114 } 115 mDialog = builder.create(); 116 mDialog.show(); 117 } 118 119 private BroadcastReceiver mBroadcast = new BroadcastReceiver() { 120 @Override 121 public void onReceive(Context context, Intent intent) { 122 boolean passedAll = false; 123 String action = intent.getAction(); 124 switch (action) { 125 case BleConnectionPriorityServerService.ACTION_BLUETOOTH_DISABLED: 126 new AlertDialog.Builder(context) 127 .setTitle(R.string.ble_bluetooth_disable_title) 128 .setMessage(R.string.ble_bluetooth_disable_message) 129 .setOnCancelListener(new Dialog.OnCancelListener() { 130 @Override 131 public void onCancel(DialogInterface dialog) { 132 finish(); 133 } 134 }) 135 .create().show(); 136 break; 137 case BleConnectionPriorityServerService.ACTION_START_CONNECTION_PRIORITY_TEST: 138 showProgressDialog(); 139 break; 140 case BleConnectionPriorityServerService.ACTION_CONNECTION_PRIORITY_FINISH: 141 String resultMsg = getString(R.string.ble_server_connection_priority_result_passed); 142 143 closeDialog(); 144 145 mTestAdapter.setTestPass(CONNECTION_PRIORITY_HIGH); 146 mDialog = new AlertDialog.Builder(BleConnectionPriorityServerBaseActivity.this) 147 .setMessage(resultMsg) 148 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 149 @Override 150 public void onClick(DialogInterface dialog, int which) { 151 closeDialog(); 152 } 153 }) 154 .setOnCancelListener(new DialogInterface.OnCancelListener() { 155 @Override 156 public void onCancel(DialogInterface dialog) { 157 closeDialog(); 158 } 159 }) 160 .create(); 161 mDialog.show(); 162 163 getPassButton().setEnabled(true); 164 mTestAdapter.notifyDataSetChanged(); 165 break; 166 167 case BleServerService.BLE_OPEN_FAIL: 168 setTestResultAndFinish(false); 169 runOnUiThread(new Runnable() { 170 @Override 171 public void run() { 172 Toast.makeText(BleConnectionPriorityServerBaseActivity.this, R.string.bt_open_failed_message, Toast.LENGTH_SHORT).show(); 173 } 174 }); 175 break; 176 case BleServerService.BLE_ADVERTISE_UNSUPPORTED: 177 showErrorDialog(R.string.bt_advertise_unsupported_title, R.string.bt_advertise_unsupported_message, true); 178 break; 179 } 180 181 } 182 }; 183 showProgressDialog()184 private synchronized void showProgressDialog() { 185 closeDialog(); 186 187 ProgressDialog dialog = new ProgressDialog(this); 188 dialog.setTitle(R.string.ble_test_running); 189 dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 190 dialog.setMessage(getString(R.string.ble_test_running_message)); 191 dialog.setCanceledOnTouchOutside(false); 192 mDialog = dialog; 193 mDialog.show(); 194 } 195 } 196