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.util.Log;
28 import android.view.View;
29 import android.view.View.OnClickListener;
30 import android.widget.Button;
31 import android.widget.Toast;
32 
33 public class BleAdvertiserPowerLevelActivity extends PassFailButtons.Activity {
34 
35     @Override
onCreate(Bundle savedInstanceState)36     public void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38         setContentView(R.layout.ble_advertiser_power_level);
39         setPassFailButtonClickListeners();
40         setInfoResources(R.string.ble_power_level_name,
41                          R.string.ble_power_level_info, -1);
42 
43         ((Button) findViewById(R.id.ble_power_level_start))
44             .setOnClickListener(new OnClickListener() {
45                 @Override
46                 public void onClick(View v) {
47                     Intent intent = new Intent(BleAdvertiserPowerLevelActivity.this,
48                                                BleAdvertiserService.class);
49                     intent.putExtra(BleAdvertiserService.EXTRA_COMMAND,
50                                     BleAdvertiserService.COMMAND_START_POWER_LEVEL);
51                     startService(intent);
52                 }
53             });
54         ((Button) findViewById(R.id.ble_power_level_stop))
55             .setOnClickListener(new OnClickListener() {
56                 @Override
57                 public void onClick(View v) {
58                     stopAdvertising();
59                 }
60             });
61 
62     }
63 
64     @Override
onResume()65     public void onResume() {
66         super.onResume();
67         IntentFilter filter = new IntentFilter();
68         filter.addAction(BleAdvertiserService.BLE_START_POWER_LEVEL);
69         filter.addAction(BleAdvertiserService.BLE_STOP_POWER_LEVEL);
70         filter.addAction(BleAdvertiserService.BLE_ADV_NOT_SUPPORT);
71         registerReceiver(onBroadcast, filter);
72     }
73 
74     @Override
onPause()75     public void onPause() {
76         super.onPause();
77         unregisterReceiver(onBroadcast);
78     }
79 
80     @Override
onDestroy()81     public void onDestroy() {
82         super.onDestroy();
83         stopAdvertising();
84     }
85 
stopAdvertising()86     private void stopAdvertising() {
87         Intent intent = new Intent(BleAdvertiserPowerLevelActivity.this,
88                                    BleAdvertiserService.class);
89         intent.putExtra(BleAdvertiserService.EXTRA_COMMAND,
90                         BleAdvertiserService.COMMAND_STOP_POWER_LEVEL);
91         startService(intent);
92     }
93 
showMessage(String msg)94     private void showMessage(String msg) {
95         Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
96     }
97 
pass()98     private void pass() {
99         this.setTestResultAndFinish(true);
100     }
101 
102     private BroadcastReceiver onBroadcast = new BroadcastReceiver() {
103         @Override
104         public void onReceive(Context context, Intent intent) {
105             switch (intent.getAction()) {
106                 case BleAdvertiserService.BLE_START_POWER_LEVEL:
107                     showMessage("Start advertising, please hold for a while.");
108                     break;
109                 case BleAdvertiserService.BLE_STOP_POWER_LEVEL:
110                     showMessage("Stop advertising");
111                     break;
112                 case BleAdvertiserService.BLE_ADV_NOT_SUPPORT:
113                     pass();
114                     break;
115             }
116         }
117     };
118 }
119