BluetoothController.java revision 0f167d75667e1e4f731c2ab1166d345572a4ef46
1/* 2 * Copyright (C) 2008 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 17package com.android.systemui.statusbar.policy; 18 19import java.util.ArrayList; 20 21import android.bluetooth.BluetoothAdapter; 22import android.content.BroadcastReceiver; 23import android.content.Context; 24import android.content.Intent; 25import android.content.IntentFilter; 26import android.view.View; 27import android.widget.ImageView; 28 29import com.android.systemui.R; 30 31public class BluetoothController extends BroadcastReceiver { 32 private static final String TAG = "StatusBar.BluetoothController"; 33 34 private Context mContext; 35 private ArrayList<ImageView> mIconViews = new ArrayList<ImageView>(); 36 37 private int mIconId = R.drawable.stat_sys_data_bluetooth; 38 private int mContentDescriptionId = 0; 39 private boolean mEnabled; 40 41 public BluetoothController(Context context) { 42 mContext = context; 43 44 IntentFilter filter = new IntentFilter(); 45 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); 46 filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); 47 context.registerReceiver(this, filter); 48 49 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 50 handleAdapterStateChange(adapter.getState()); 51 handleConnectionStateChange(adapter.getConnectionState()); 52 refreshViews(); 53 } 54 55 public void addIconView(ImageView v) { 56 mIconViews.add(v); 57 } 58 59 @Override 60 public void onReceive(Context context, Intent intent) { 61 final String action = intent.getAction(); 62 63 if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { 64 handleAdapterStateChange( 65 intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)); 66 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) { 67 handleConnectionStateChange( 68 intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 69 BluetoothAdapter.STATE_DISCONNECTED)); 70 } 71 refreshViews(); 72 } 73 74 public void handleAdapterStateChange(int adapterState) { 75 mEnabled = (adapterState == BluetoothAdapter.STATE_ON); 76 } 77 78 public void handleConnectionStateChange(int connectionState) { 79 final boolean connected = (connectionState == BluetoothAdapter.STATE_CONNECTED); 80 if (connected) { 81 mIconId = R.drawable.stat_sys_data_bluetooth_connected; 82 mContentDescriptionId = R.string.accessibility_bluetooth_connected; 83 } else { 84 mIconId = R.drawable.stat_sys_data_bluetooth; 85 mContentDescriptionId = R.string.accessibility_bluetooth_disconnected; 86 } 87 } 88 89 public void refreshViews() { 90 int N = mIconViews.size(); 91 for (int i=0; i<N; i++) { 92 ImageView v = mIconViews.get(i); 93 v.setImageResource(mIconId); 94 v.setVisibility(mEnabled ? View.VISIBLE : View.GONE); 95 v.setContentDescription((mContentDescriptionId == 0) 96 ? null 97 : mContext.getString(mContentDescriptionId)); 98 } 99 } 100} 101