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 static android.content.Context.RECEIVER_EXPORTED; 20 21 import android.bluetooth.BluetoothAdapter; 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.ToggleButton; 31 32 import com.android.cts.verifier.PassFailButtons; 33 import com.android.cts.verifier.R; 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 private static final int START_DISABLE_BLUETOOTH_REQUEST = 2; 46 47 private BluetoothAdapter mBluetoothAdapter; 48 49 private BluetoothBroadcastReceiver mReceiver; 50 51 private ToggleButton mToggleButton; 52 53 private int mNumDisabledTimes = 0; 54 55 private int mNumEnabledTimes = 0; 56 57 @Override onCreate(Bundle savedInstanceState)58 protected void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 setContentView(R.layout.bt_toggle); 61 setPassFailButtonClickListeners(); 62 63 mReceiver = new BluetoothBroadcastReceiver(); 64 IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 65 registerReceiver(mReceiver, filter, RECEIVER_EXPORTED); 66 67 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 68 69 getPassButton().setEnabled(false); 70 71 mToggleButton = (ToggleButton) findViewById(R.id.bt_toggle_button); 72 mToggleButton.setChecked(mBluetoothAdapter.isEnabled()); 73 mToggleButton.setOnClickListener(new OnClickListener() { 74 @Override 75 public void onClick(View v) { 76 if (mToggleButton.isChecked()) { 77 enableBluetooth(); 78 } else { 79 disableBluetooth(); 80 } 81 } 82 }); 83 } 84 enableBluetooth()85 private void enableBluetooth() { 86 mToggleButton.setEnabled(false); 87 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 88 startActivityForResult(intent, START_ENABLE_BLUETOOTH_REQUEST); 89 } 90 disableBluetooth()91 private void disableBluetooth() { 92 mToggleButton.setEnabled(false); 93 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISABLE); 94 startActivityForResult(intent, START_DISABLE_BLUETOOTH_REQUEST); 95 } 96 97 @Override onActivityResult(int requestCode, int resultCode, Intent data)98 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 99 super.onActivityResult(requestCode, resultCode, data); 100 switch (requestCode) { 101 case START_ENABLE_BLUETOOTH_REQUEST: 102 boolean enabledBluetooth = RESULT_OK == resultCode; 103 mToggleButton.setChecked(enabledBluetooth); 104 mToggleButton.setEnabled(true); 105 break; 106 case START_DISABLE_BLUETOOTH_REQUEST: 107 boolean disabledBluetooth = RESULT_OK == resultCode; 108 mToggleButton.setChecked(!disabledBluetooth); 109 mToggleButton.setEnabled(true); 110 break; 111 } 112 } 113 114 115 class BluetoothBroadcastReceiver extends BroadcastReceiver { 116 @Override onReceive(Context context, Intent intent)117 public void onReceive(Context context, Intent intent) { 118 int previousState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, -1); 119 int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); 120 Log.i(TAG, "Previous state: " + previousState + " New state: " + newState); 121 122 if (BluetoothAdapter.STATE_OFF == newState 123 && (BluetoothAdapter.STATE_ON == previousState 124 || BluetoothAdapter.STATE_TURNING_OFF == previousState)) { 125 mNumDisabledTimes++; 126 } 127 128 if (BluetoothAdapter.STATE_ON == newState 129 && (BluetoothAdapter.STATE_OFF == previousState 130 || BluetoothAdapter.STATE_TURNING_ON == previousState)) { 131 mNumEnabledTimes++; 132 } 133 134 mToggleButton.setChecked(mBluetoothAdapter.isEnabled()); 135 getPassButton().setEnabled(mNumDisabledTimes > 0 && mNumEnabledTimes > 0); 136 } 137 } 138 139 @Override onDestroy()140 protected void onDestroy() { 141 super.onDestroy(); 142 unregisterReceiver(mReceiver); 143 } 144 } 145