Home | History | Annotate | Download | only in bluetooth
      1 /*
      2  * Copyright (C) 2010 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.settings.bluetooth;
     18 
     19 import android.bluetooth.BluetoothAdapter;
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.provider.Settings;
     25 import android.widget.CompoundButton;
     26 import android.widget.Switch;
     27 import android.widget.Toast;
     28 
     29 import com.android.settings.R;
     30 import com.android.settings.WirelessSettings;
     31 
     32 /**
     33  * BluetoothEnabler is a helper to manage the Bluetooth on/off checkbox
     34  * preference. It turns on/off Bluetooth and ensures the summary of the
     35  * preference reflects the current state.
     36  */
     37 public final class BluetoothEnabler implements CompoundButton.OnCheckedChangeListener {
     38     private final Context mContext;
     39     private Switch mSwitch;
     40 
     41     private final LocalBluetoothAdapter mLocalAdapter;
     42     private final IntentFilter mIntentFilter;
     43 
     44     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
     45         @Override
     46         public void onReceive(Context context, Intent intent) {
     47             int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
     48             handleStateChanged(state);
     49         }
     50     };
     51 
     52     public BluetoothEnabler(Context context, Switch switch_) {
     53         mContext = context;
     54         mSwitch = switch_;
     55 
     56         LocalBluetoothManager manager = LocalBluetoothManager.getInstance(context);
     57         if (manager == null) {
     58             // Bluetooth is not supported
     59             mLocalAdapter = null;
     60             mSwitch.setEnabled(false);
     61         } else {
     62             mLocalAdapter = manager.getBluetoothAdapter();
     63         }
     64         mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
     65     }
     66 
     67     public void resume() {
     68         if (mLocalAdapter == null) {
     69             mSwitch.setEnabled(false);
     70             return;
     71         }
     72 
     73         // Bluetooth state is not sticky, so set it manually
     74         handleStateChanged(mLocalAdapter.getBluetoothState());
     75 
     76         mContext.registerReceiver(mReceiver, mIntentFilter);
     77         mSwitch.setOnCheckedChangeListener(this);
     78     }
     79 
     80     public void pause() {
     81         if (mLocalAdapter == null) {
     82             return;
     83         }
     84 
     85         mContext.unregisterReceiver(mReceiver);
     86         mSwitch.setOnCheckedChangeListener(null);
     87     }
     88 
     89     public void setSwitch(Switch switch_) {
     90         if (mSwitch == switch_) return;
     91         mSwitch.setOnCheckedChangeListener(null);
     92         mSwitch = switch_;
     93         mSwitch.setOnCheckedChangeListener(this);
     94 
     95         int bluetoothState = BluetoothAdapter.STATE_OFF;
     96         if (mLocalAdapter != null) bluetoothState = mLocalAdapter.getBluetoothState();
     97         boolean isOn = bluetoothState == BluetoothAdapter.STATE_ON;
     98         boolean isOff = bluetoothState == BluetoothAdapter.STATE_OFF;
     99         mSwitch.setChecked(isOn);
    100         mSwitch.setEnabled(isOn || isOff);
    101     }
    102 
    103     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    104         // Show toast message if Bluetooth is not allowed in airplane mode
    105         if (isChecked &&
    106                 !WirelessSettings.isRadioAllowed(mContext, Settings.System.RADIO_BLUETOOTH)) {
    107             Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
    108             // Reset switch to off
    109             buttonView.setChecked(false);
    110         }
    111 
    112         if (mLocalAdapter != null) {
    113             mLocalAdapter.setBluetoothEnabled(isChecked);
    114         }
    115         mSwitch.setEnabled(false);
    116     }
    117 
    118     void handleStateChanged(int state) {
    119         switch (state) {
    120             case BluetoothAdapter.STATE_TURNING_ON:
    121                 mSwitch.setEnabled(false);
    122                 break;
    123             case BluetoothAdapter.STATE_ON:
    124                 mSwitch.setChecked(true);
    125                 mSwitch.setEnabled(true);
    126                 break;
    127             case BluetoothAdapter.STATE_TURNING_OFF:
    128                 mSwitch.setEnabled(false);
    129                 break;
    130             case BluetoothAdapter.STATE_OFF:
    131                 mSwitch.setChecked(false);
    132                 mSwitch.setEnabled(true);
    133                 break;
    134             default:
    135                 mSwitch.setChecked(false);
    136                 mSwitch.setEnabled(true);
    137         }
    138     }
    139 }
    140