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.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.os.Bundle;
27 import android.widget.ListView;
28 import android.widget.Toast;
29 
30 import com.android.cts.verifier.PassFailButtons;
31 import com.android.cts.verifier.R;
32 
33 import java.util.ArrayList;
34 import java.util.List;
35 
36 public class BleEncryptedServerBaseActivity extends PassFailButtons.Activity {
37 
38     private TestAdapter mTestAdapter;
39     private int mAllPassed;
40 
41     private final int WAIT_WRITE_ENCRIPTED_CHARACTERISTIC = 0;
42     private final int WAIT_READ_ENCRIPTED_CHARACTERISTIC = 1;
43     private final int WAIT_WRITE_ENCRIPTED_DESCRIPTOR = 2;
44     private final int WAIT_READ_ENCRIPTED_DESCRIPTOR = 3;
45 
46     @Override
onCreate(Bundle savedInstanceState)47     protected void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         setContentView(R.layout.ble_encrypted_server_test);
50         setPassFailButtonClickListeners();
51         setInfoResources(R.string.ble_encrypted_server_name,
52                 R.string.ble_encrypted_server_info, -1);
53 
54         getPassButton().setEnabled(false);
55 
56         mTestAdapter = new TestAdapter(this, setupTestList());
57         ListView listView = (ListView) findViewById(R.id.ble_server_enctypted_tests);
58         listView.setAdapter(mTestAdapter);
59 
60         startService(new Intent(this, BleEncryptedServerService.class));
61     }
62 
63     @Override
onResume()64     public void onResume() {
65         super.onResume();
66 
67         IntentFilter filter = new IntentFilter();
68         filter.addAction(BleEncryptedServerService.INTENT_BLUETOOTH_DISABLED);
69         filter.addAction(BleEncryptedServerService.INTENT_WAIT_WRITE_ENCRYPTED_CHARACTERISTIC);
70         filter.addAction(BleEncryptedServerService.INTENT_WAIT_READ_ENCRYPTED_CHARACTERISTIC);
71         filter.addAction(BleEncryptedServerService.INTENT_WAIT_WRITE_ENCRYPTED_DESCRIPTOR);
72         filter.addAction(BleEncryptedServerService.INTENT_WAIT_READ_ENCRYPTED_DESCRIPTOR);
73         filter.addAction(BleServerService.BLE_ADVERTISE_UNSUPPORTED);
74         filter.addAction(BleServerService.BLE_OPEN_FAIL);
75         registerReceiver(mBroadcast, filter);
76     }
77 
78     @Override
onPause()79     public void onPause() {
80         super.onPause();
81         unregisterReceiver(mBroadcast);
82     }
83 
setupTestList()84     private List<Integer> setupTestList() {
85         ArrayList<Integer> testList = new ArrayList<Integer>();
86         testList.add(R.string.ble_server_write_characteristic_need_encrypted);
87         testList.add(R.string.ble_server_read_characteristic_need_encrypted);
88         testList.add(R.string.ble_server_write_descriptor_need_encrypted);
89         testList.add(R.string.ble_server_read_descriptor_need_encrypted);
90         return testList;
91     }
92 
93     @Override
onDestroy()94     public void onDestroy() {
95         super.onDestroy();
96         stopService(new Intent(this, BleConnectionPriorityServerService.class));
97     }
98 
99     private BroadcastReceiver mBroadcast = new BroadcastReceiver() {
100         @Override
101         public void onReceive(Context context, Intent intent) {
102             String action = intent.getAction();
103             switch (action) {
104             case BleEncryptedServerService.INTENT_BLUETOOTH_DISABLED:
105                 new AlertDialog.Builder(context)
106                         .setTitle(R.string.ble_bluetooth_disable_title)
107                         .setMessage(R.string.ble_bluetooth_disable_message)
108                         .setOnCancelListener(new Dialog.OnCancelListener() {
109                             @Override
110                             public void onCancel(DialogInterface dialog) {
111                                 finish();
112                             }
113                         })
114                         .create().show();
115                 break;
116             case BleEncryptedServerService.INTENT_WAIT_WRITE_ENCRYPTED_CHARACTERISTIC:
117                 mTestAdapter.setTestPass(WAIT_WRITE_ENCRIPTED_CHARACTERISTIC);
118                 mAllPassed |= 0x01;
119                 break;
120             case BleEncryptedServerService.INTENT_WAIT_READ_ENCRYPTED_CHARACTERISTIC:
121                 mTestAdapter.setTestPass(WAIT_READ_ENCRIPTED_CHARACTERISTIC);
122                 mAllPassed |= 0x02;
123                 break;
124             case BleEncryptedServerService.INTENT_WAIT_WRITE_ENCRYPTED_DESCRIPTOR:
125                 mTestAdapter.setTestPass(WAIT_WRITE_ENCRIPTED_DESCRIPTOR);
126                 mAllPassed |= 0x04;
127                 break;
128             case BleEncryptedServerService.INTENT_WAIT_READ_ENCRYPTED_DESCRIPTOR:
129                 mTestAdapter.setTestPass(WAIT_READ_ENCRIPTED_DESCRIPTOR);
130                 mAllPassed |= 0x08;
131                 break;
132             case BleServerService.BLE_ADVERTISE_UNSUPPORTED:
133                 showErrorDialog(R.string.bt_advertise_unsupported_title, R.string.bt_advertise_unsupported_message, true);
134                 break;
135             case BleServerService.BLE_OPEN_FAIL:
136                 setTestResultAndFinish(false);
137                 runOnUiThread(new Runnable() {
138                     @Override
139                     public void run() {
140                         Toast.makeText(BleEncryptedServerBaseActivity.this, R.string.bt_open_failed_message, Toast.LENGTH_SHORT).show();
141                     }
142                 });
143                 break;
144             }
145             mTestAdapter.notifyDataSetChanged();
146             if (mAllPassed == 0x0F) {
147                 getPassButton().setEnabled(true);
148             }
149         }
150     };
151 
showErrorDialog(int titleId, int messageId, boolean finish)152     private void showErrorDialog(int titleId, int messageId, boolean finish) {
153         AlertDialog.Builder builder = new AlertDialog.Builder(this)
154                 .setTitle(titleId)
155                 .setMessage(messageId);
156         if (finish) {
157             builder.setOnCancelListener(new Dialog.OnCancelListener() {
158                 @Override
159                 public void onCancel(DialogInterface dialog) {
160                     finish();
161                 }
162             });
163         }
164         builder.create().show();
165     }
166 }
167