1 /*
2  * Copyright (C) 2013 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 com.android.cts.verifier.PassFailButtons;
20 import com.android.cts.verifier.R;
21 
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.os.Bundle;
27 import android.view.View;
28 import android.view.View.OnClickListener;
29 import android.widget.Button;
30 import android.widget.Toast;
31 
32 public class BleButtonActivity extends PassFailButtons.Activity {
33 
34     static final int DISCOVER_SERVICE = 0;
35     static final int DISCONNECT = 1;
36 
37     private int mName;
38     private int mInfo;
39     private int mButtonText;
40     private int mCommand;
41     private String mFilter;
42     private String mMessage;
43 
BleButtonActivity(int target)44     BleButtonActivity(int target) {
45         if (target == DISCOVER_SERVICE) {
46             mName = R.string.ble_discover_service_name;
47             mInfo = R.string.ble_discover_service_info;
48             mButtonText = R.string.ble_discover_service;
49             mCommand = BleClientService.COMMAND_DISCOVER_SERVICE;
50             mFilter = BleClientService.BLE_SERVICES_DISCOVERED;
51             mMessage = "Service discovered.";
52         } else if (target == DISCONNECT) {
53             mName = R.string.ble_client_disconnect_name;
54             mInfo = R.string.ble_client_disconnect_name;
55             mButtonText = R.string.ble_disconnect;
56             mCommand = BleClientService.COMMAND_DISCONNECT;
57             mFilter = BleClientService.BLE_BLUETOOTH_DISCONNECTED;
58             mMessage = "Bluetooth LE disconnected.";
59         }
60     }
61 
62     @Override
onCreate(Bundle savedInstanceState)63     public void onCreate(Bundle savedInstanceState) {
64         super.onCreate(savedInstanceState);
65         setContentView(R.layout.ble_button);
66         setPassFailButtonClickListeners();
67         setInfoResources(mName, mInfo, -1);
68         getPassButton().setEnabled(false);
69 
70         ((Button) findViewById(R.id.ble_button)).setText(mButtonText);
71         ((Button) findViewById(R.id.ble_button)).setOnClickListener(new OnClickListener() {
72             @Override
73             public void onClick(View v) {
74                 Intent intent = new Intent(BleButtonActivity.this, BleClientService.class);
75                 intent.putExtra(BleClientService.EXTRA_COMMAND, mCommand);
76                 startService(intent);
77             }
78         });
79     }
80 
81     @Override
onResume()82     public void onResume() {
83         super.onResume();
84         IntentFilter filter = new IntentFilter();
85         filter.addAction(mFilter);
86         registerReceiver(onBroadcast, filter);
87     }
88 
89     @Override
onPause()90     public void onPause() {
91         super.onPause();
92         unregisterReceiver(onBroadcast);
93     }
94 
showMessage(String msg)95     private void showMessage(String msg) {
96         Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
97     }
98 
99     private BroadcastReceiver onBroadcast = new BroadcastReceiver() {
100         @Override
101         public void onReceive(Context context, Intent intent) {
102             showMessage(mMessage);
103             getPassButton().setEnabled(true);
104         }
105     };
106 }