1 /*
2  * Copyright 2018 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.content.BroadcastReceiver;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.os.Bundle;
29 import android.util.Log;
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 BleCocServerTestBaseActivity extends PassFailButtons.Activity {
40 
41     public static final boolean DEBUG = true;
42     public static final String TAG = "BleCocServerTestBaseActivity";
43 
44     private final int TEST_BLE_LE_CONNECTED = 0;
45     private final int TEST_BLE_LISTENER_CREATED = 1;
46     private final int TEST_BLE_PSM_READ = 2;
47     private final int TEST_BLE_COC_CONNECTED = 3;
48     private final int TEST_BLE_CONNECTION_TYPE_CHECKED = 4;
49     private final int TEST_BLE_DATA_8BYTES_READ = 5;
50     private final int TEST_BLE_DATA_8BYTES_SENT = 6;
51     private final int TEST_BLE_DATA_EXCHANGED = 7;
52     private final int TEST_BLE_SERVER_DISCONNECTED = 8;
53     private static final int PASS_FLAG_ALL = 0x01FF;
54 
55     private TestAdapter mTestAdapter;
56     private long mPassed;
57 
58     @Override
onCreate(Bundle savedInstanceState)59     public void onCreate(Bundle savedInstanceState) {
60         super.onCreate(savedInstanceState);
61         setContentView(R.layout.ble_server_start);
62         setPassFailButtonClickListeners();
63         setInfoResources(R.string.ble_coc_server_start_name,
64                          R.string.ble_server_start_info, -1);
65         getPassButton().setEnabled(false);
66 
67         mTestAdapter = new TestAdapter(this, setupTestList());
68         ListView listView = (ListView) findViewById(R.id.ble_server_tests);
69         listView.setAdapter(mTestAdapter);
70 
71         mPassed = 0;
72     }
73 
74     @Override
onResume()75     public void onResume() {
76         super.onResume();
77 
78         IntentFilter filter = new IntentFilter();
79 
80         filter.addAction(BleCocServerService.BLE_LE_CONNECTED);
81         filter.addAction(BleCocServerService.BLE_COC_LISTENER_CREATED);
82         filter.addAction(BleCocServerService.BLE_PSM_READ);
83         filter.addAction(BleCocServerService.BLE_COC_CONNECTED);
84         filter.addAction(BleCocServerService.BLE_CONNECTION_TYPE_CHECKED);
85         filter.addAction(BleCocServerService.BLE_DATA_8BYTES_READ);
86         filter.addAction(BleCocServerService.BLE_DATA_8BYTES_SENT);
87         filter.addAction(BleCocServerService.BLE_DATA_LARGEBUF_READ);
88 
89         filter.addAction(BleCocServerService.BLE_BLUETOOTH_MISMATCH_SECURE);
90         filter.addAction(BleCocServerService.BLE_BLUETOOTH_MISMATCH_INSECURE);
91         filter.addAction(BleCocServerService.BLE_SERVER_DISCONNECTED);
92 
93         filter.addAction(BleCocServerService.BLE_BLUETOOTH_DISABLED);
94         filter.addAction(BleCocServerService.BLE_OPEN_FAIL);
95         filter.addAction(BleCocServerService.BLE_ADVERTISE_UNSUPPORTED);
96         filter.addAction(BleCocServerService.BLE_ADD_SERVICE_FAIL);
97 
98         registerReceiver(mBroadcast, filter, RECEIVER_EXPORTED);
99     }
100 
101     @Override
onPause()102     public void onPause() {
103         super.onPause();
104         unregisterReceiver(mBroadcast);
105     }
106 
107     @Override
onDestroy()108     public void onDestroy() {
109         super.onDestroy();
110     }
111 
setupTestList()112     private List<Integer> setupTestList() {
113         ArrayList<Integer> testList = new ArrayList<Integer>();
114         testList.add(R.string.ble_coc_server_le_connect);
115         testList.add(R.string.ble_coc_server_create_listener);
116         testList.add(R.string.ble_coc_server_psm_read);
117         testList.add(R.string.ble_coc_server_connection);
118         testList.add(R.string.ble_coc_server_check_connection_type);
119         testList.add(R.string.ble_coc_server_receive_data_8bytes);
120         testList.add(R.string.ble_coc_server_send_data_8bytes);
121         testList.add(R.string.ble_coc_server_data_exchange);
122         testList.add(R.string.ble_server_receiving_disconnect);
123         return testList;
124     }
125 
showErrorDialog(int titleId, int messageId, boolean finish)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 BroadcastReceiver mBroadcast = new BroadcastReceiver() {
142         @Override
143         public void onReceive(Context context, Intent intent) {
144             String action = intent.getAction();
145             if (DEBUG) {
146                 Log.d(TAG, "BroadcastReceiver.onReceive: action=" + action);
147             }
148             String newAction = null;
149             final Intent startIntent = new Intent(BleCocServerTestBaseActivity.this, BleCocServerService.class);
150 
151             switch (action) {
152             case BleCocServerService.BLE_BLUETOOTH_DISABLED:
153                 showErrorDialog(R.string.ble_bluetooth_disable_title, R.string.ble_bluetooth_disable_message, true);
154                 break;
155             case BleCocServerService.BLE_LE_CONNECTED:
156                 mTestAdapter.setTestPass(TEST_BLE_LE_CONNECTED);
157                 mPassed |= (1 << TEST_BLE_LE_CONNECTED);
158                 break;
159             case BleCocServerService.BLE_COC_LISTENER_CREATED:
160                 mTestAdapter.setTestPass(TEST_BLE_LISTENER_CREATED);
161                 mPassed |= (1 << TEST_BLE_LISTENER_CREATED);
162                 break;
163             case BleCocServerService.BLE_PSM_READ:
164                 mTestAdapter.setTestPass(TEST_BLE_PSM_READ);
165                 mPassed |= (1 << TEST_BLE_PSM_READ);
166                 break;
167             case BleCocServerService.BLE_COC_CONNECTED:
168                 mTestAdapter.setTestPass(TEST_BLE_COC_CONNECTED);
169                 mPassed |= (1 << TEST_BLE_COC_CONNECTED);
170                 break;
171             case BleCocServerService.BLE_CONNECTION_TYPE_CHECKED:
172                 mTestAdapter.setTestPass(TEST_BLE_CONNECTION_TYPE_CHECKED);
173                 mPassed |= (1 << TEST_BLE_CONNECTION_TYPE_CHECKED);
174                 break;
175             case BleCocServerService.BLE_DATA_8BYTES_READ:
176                 mTestAdapter.setTestPass(TEST_BLE_DATA_8BYTES_READ);
177                 mPassed |= (1 << TEST_BLE_DATA_8BYTES_READ);
178                 // send the next action to send 8 bytes
179                 newAction = BleCocServerService.BLE_COC_SERVER_ACTION_SEND_DATA_8BYTES;
180                 break;
181             case BleCocServerService.BLE_DATA_8BYTES_SENT:
182                 mTestAdapter.setTestPass(TEST_BLE_DATA_8BYTES_SENT);
183                 mPassed |= (1 << TEST_BLE_DATA_8BYTES_SENT);
184                 // send the next action to send 8 bytes
185                 newAction = BleCocServerService.BLE_COC_SERVER_ACTION_EXCHANGE_DATA;
186                 break;
187             case BleCocServerService.BLE_DATA_LARGEBUF_READ:
188                 mTestAdapter.setTestPass(TEST_BLE_DATA_EXCHANGED);
189                 mPassed |= (1 << TEST_BLE_DATA_EXCHANGED);
190                 // Disconnect
191                 newAction = BleCocServerService.BLE_COC_SERVER_ACTION_DISCONNECT;
192                 break;
193             case BleCocServerService.BLE_SERVER_DISCONNECTED:
194                 mTestAdapter.setTestPass(TEST_BLE_SERVER_DISCONNECTED);
195                 mPassed |= (1 << TEST_BLE_SERVER_DISCONNECTED);
196                 // all tests done
197                 break;
198             case BleCocServerService.BLE_BLUETOOTH_MISMATCH_SECURE:
199                 showErrorDialog(R.string.ble_bluetooth_mismatch_title, R.string.ble_bluetooth_mismatch_secure_message, true);
200                 break;
201             case BleCocServerService.BLE_BLUETOOTH_MISMATCH_INSECURE:
202                 showErrorDialog(R.string.ble_bluetooth_mismatch_title, R.string.ble_bluetooth_mismatch_insecure_message, true);
203                 break;
204             case BleCocServerService.BLE_ADVERTISE_UNSUPPORTED:
205                 showErrorDialog(R.string.bt_advertise_unsupported_title, R.string.bt_advertise_unsupported_message, true);
206                 break;
207             case BleCocServerService.BLE_OPEN_FAIL:
208                 setTestResultAndFinish(false);
209                 runOnUiThread(new Runnable() {
210                     @Override
211                     public void run() {
212                         Toast.makeText(BleCocServerTestBaseActivity.this, R.string.bt_open_failed_message, Toast.LENGTH_SHORT).show();
213                     }
214                 });
215                 break;
216             case BleCocServerService.BLE_ADD_SERVICE_FAIL:
217                 showErrorDialog(R.string.bt_add_service_failed_title, R.string.bt_add_service_failed_message, true);
218                 break;
219             default:
220                 if (DEBUG) {
221                     Log.d(TAG, "Note: BroadcastReceiver.onReceive: unhandled action=" + action);
222                 }
223             }
224 
225             mTestAdapter.notifyDataSetChanged();
226 
227             if (newAction != null) {
228                 Log.d(TAG, "Starting " + newAction);
229                 startIntent.setAction(newAction);
230 
231                 startService(startIntent);
232             }
233 
234             if (mPassed == PASS_FLAG_ALL) {
235                 Log.d(TAG, "All Tests Passed.");
236                 getPassButton().setEnabled(true);
237             }
238         }
239     };
240 }
241