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