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 = false;
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        if (adapter != null) {
51            handleAdapterStateChange(adapter.getState());
52            handleConnectionStateChange(adapter.getConnectionState());
53        }
54        refreshViews();
55    }
56
57    public void addIconView(ImageView v) {
58        mIconViews.add(v);
59    }
60
61    @Override
62    public void onReceive(Context context, Intent intent) {
63        final String action = intent.getAction();
64
65        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
66            handleAdapterStateChange(
67                    intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR));
68        } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
69            handleConnectionStateChange(
70                    intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
71                        BluetoothAdapter.STATE_DISCONNECTED));
72        }
73        refreshViews();
74    }
75
76    public void handleAdapterStateChange(int adapterState) {
77        mEnabled = (adapterState == BluetoothAdapter.STATE_ON);
78    }
79
80    public void handleConnectionStateChange(int connectionState) {
81        final boolean connected = (connectionState == BluetoothAdapter.STATE_CONNECTED);
82        if (connected) {
83            mIconId = R.drawable.stat_sys_data_bluetooth_connected;
84            mContentDescriptionId = R.string.accessibility_bluetooth_connected;
85        } else {
86            mIconId = R.drawable.stat_sys_data_bluetooth;
87            mContentDescriptionId = R.string.accessibility_bluetooth_disconnected;
88        }
89    }
90
91    public void refreshViews() {
92        int N = mIconViews.size();
93        for (int i=0; i<N; i++) {
94            ImageView v = mIconViews.get(i);
95            v.setImageResource(mIconId);
96            v.setVisibility(mEnabled ? View.VISIBLE : View.GONE);
97            v.setContentDescription((mContentDescriptionId == 0)
98                    ? null
99                    : mContext.getString(mContentDescriptionId));
100        }
101    }
102}
103