BluetoothController.java revision 6179ea3196e9306d3f14361fe9ef14191b1edba6
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 boolean mEnabled; 39 40 public BluetoothController(Context context) { 41 mContext = context; 42 43 IntentFilter filter = new IntentFilter(); 44 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); 45 filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); 46 context.registerReceiver(this, filter); 47 } 48 49 public void addIconView(ImageView v) { 50 mIconViews.add(v); 51 } 52 53 @Override 54 public void onReceive(Context context, Intent intent) { 55 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 56 BluetoothAdapter.STATE_DISCONNECTED); 57 int contentDescriptionResId = 0; 58 59 if (state == BluetoothAdapter.STATE_CONNECTED) { 60 mIconId = R.drawable.stat_sys_data_bluetooth_connected; 61 contentDescriptionResId = R.string.accessibility_bluetooth_connected; 62 } else { 63 mIconId = R.drawable.stat_sys_data_bluetooth; 64 contentDescriptionResId = R.string.accessibility_bluetooth_disconnected; 65 } 66 67 int N = mIconViews.size(); 68 for (int i=0; i<N; i++) { 69 ImageView v = mIconViews.get(i); 70 v.setImageResource(mIconId); 71 v.setVisibility(mEnabled ? View.VISIBLE : View.GONE); 72 v.setContentDescription(mContext.getString(contentDescriptionResId)); 73 } 74 } 75} 76