• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.app.AlertDialog;
23 import android.app.ProgressDialog;
24 import android.bluetooth.BluetoothAdapter;
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.os.Bundle;
30 import android.util.Log;
31 import android.view.View;
32 import android.view.View.OnClickListener;
33 import android.widget.ToggleButton;
34 
35 /**
36  * Activity for testing that Bluetooth can be disabled and enabled properly. The activity shows
37  * a button that toggles Bluetooth by disabling it via {@link BluetoothAdapter#disable()} and
38  * enabling it via the Intent action {@link BluetoothAdapter#ACTION_REQUEST_ENABLE}.
39  */
40 public class BluetoothToggleActivity extends PassFailButtons.Activity {
41 
42     private static final String TAG = BluetoothToggleActivity.class.getName();
43 
44     private static final int START_ENABLE_BLUETOOTH_REQUEST = 1;
45 
46     private BluetoothAdapter mBluetoothAdapter;
47 
48     private BluetoothBroadcastReceiver mReceiver;
49 
50     private ProgressDialog mDisablingDialog;
51 
52     private ToggleButton mToggleButton;
53 
54     private int mNumDisabledTimes = 0;
55 
56     private int mNumEnabledTimes = 0;
57 
58     @Override
onCreate(Bundle savedInstanceState)59     protected void onCreate(Bundle savedInstanceState) {
60         super.onCreate(savedInstanceState);
61         setContentView(R.layout.bt_toggle);
62         setPassFailButtonClickListeners();
63 
64         mReceiver = new BluetoothBroadcastReceiver();
65         IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
66         registerReceiver(mReceiver, filter);
67 
68         mDisablingDialog = new ProgressDialog(this);
69         mDisablingDialog.setMessage(getString(R.string.bt_disabling));
70 
71         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
72 
73         getPassButton().setEnabled(false);
74 
75         mToggleButton = (ToggleButton) findViewById(R.id.bt_toggle_button);
76         mToggleButton.setChecked(mBluetoothAdapter.isEnabled());
77         mToggleButton.setOnClickListener(new OnClickListener() {
78             @Override
79             public void onClick(View v) {
80                 if (mToggleButton.isChecked()) {
81                     enableBluetooth();
82                 } else {
83                     disableBluetooth();
84                 }
85             }
86         });
87     }
88 
enableBluetooth()89     private void enableBluetooth() {
90         mDisablingDialog.hide();
91         mToggleButton.setEnabled(false);
92         Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
93         startActivityForResult(intent, START_ENABLE_BLUETOOTH_REQUEST);
94     }
95 
96     @Override
onActivityResult(int requestCode, int resultCode, Intent data)97     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
98         super.onActivityResult(requestCode, resultCode, data);
99         switch (requestCode) {
100             case START_ENABLE_BLUETOOTH_REQUEST:
101                 boolean enabledBluetooth = RESULT_OK == resultCode;
102                 mToggleButton.setChecked(enabledBluetooth);
103                 mToggleButton.setEnabled(true);
104                 break;
105         }
106     }
107 
disableBluetooth()108     private void disableBluetooth() {
109         mDisablingDialog.show();
110         mToggleButton.setEnabled(false);
111         if (!mBluetoothAdapter.disable()) {
112             mDisablingDialog.hide();
113             mToggleButton.setEnabled(true);
114             new AlertDialog.Builder(this)
115                 .setIcon(android.R.drawable.ic_dialog_alert)
116                 .setMessage(R.string.bt_disabling_error)
117                 .setPositiveButton(android.R.string.ok, null)
118                 .show();
119         }
120     }
121 
122     class BluetoothBroadcastReceiver extends BroadcastReceiver {
123         @Override
onReceive(Context context, Intent intent)124         public void onReceive(Context context, Intent intent) {
125             int previousState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, -1);
126             int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
127             Log.i(TAG, "Previous state: " + previousState + " New state: " + newState);
128 
129             if (BluetoothAdapter.STATE_OFF == newState
130                     && (BluetoothAdapter.STATE_ON == previousState
131                             || BluetoothAdapter.STATE_TURNING_OFF == previousState)) {
132                 mNumDisabledTimes++;
133             }
134 
135             if (BluetoothAdapter.STATE_ON == newState
136                     && (BluetoothAdapter.STATE_OFF == previousState
137                             || BluetoothAdapter.STATE_TURNING_ON == previousState)) {
138                 mNumEnabledTimes++;
139             }
140 
141             if (BluetoothAdapter.STATE_OFF == newState) {
142                 mDisablingDialog.hide();
143                 mToggleButton.setEnabled(true);
144             }
145 
146             mToggleButton.setChecked(mBluetoothAdapter.isEnabled());
147             getPassButton().setEnabled(mNumDisabledTimes > 0 &&  mNumEnabledTimes > 0);
148         }
149     }
150 
151     @Override
onDestroy()152     protected void onDestroy() {
153         super.onDestroy();
154         unregisterReceiver(mReceiver);
155     }
156 }
157