BluetoothDevicePreference.java revision 39ef225e7c44a48aa9cfdf5c56ecd4ddfb95ae89
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.settings.bluetooth;
18
19import com.android.settings.R;
20
21import android.content.Context;
22import android.graphics.drawable.Drawable;
23import android.preference.Preference;
24import android.util.TypedValue;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
28import android.view.View.OnClickListener;
29import android.widget.ImageView;
30
31import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;
32
33import java.util.Map;
34
35/**
36 * BluetoothDevicePreference is the preference type used to display each remote
37 * Bluetooth device in the Bluetooth Settings screen.
38 */
39public class BluetoothDevicePreference extends Preference implements
40        CachedBluetoothDevice.Callback, OnClickListener {
41    private static final String TAG = "BluetoothDevicePreference";
42
43    private static int sDimAlpha = Integer.MIN_VALUE;
44
45    private final CachedBluetoothDevice mCachedDevice;
46
47    private ImageView mDeviceSettings;
48
49    private OnClickListener mOnSettingsClickListener;
50
51    /**
52     * Cached local copy of whether the device is busy. This is only updated
53     * from {@link #onDeviceAttributesChanged()}.
54     */
55    private boolean mIsBusy;
56
57    public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice) {
58        super(context);
59
60        if (sDimAlpha == Integer.MIN_VALUE) {
61            TypedValue outValue = new TypedValue();
62            context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, outValue, true);
63            sDimAlpha = (int) (outValue.getFloat() * 255);
64        }
65
66        mCachedDevice = cachedDevice;
67
68        setWidgetLayoutResource(R.layout.preference_bluetooth);
69
70        cachedDevice.registerCallback(this);
71
72        onDeviceAttributesChanged();
73    }
74
75    public CachedBluetoothDevice getCachedDevice() {
76        return mCachedDevice;
77    }
78
79    public void setOnSettingsClickListener(OnClickListener listener) {
80        mOnSettingsClickListener = listener;
81    }
82
83    @Override
84    protected void onPrepareForRemoval() {
85        super.onPrepareForRemoval();
86        mCachedDevice.unregisterCallback(this);
87    }
88
89    public void onDeviceAttributesChanged() {
90
91        /*
92         * The preference framework takes care of making sure the value has
93         * changed before proceeding.
94         */
95
96        setTitle(mCachedDevice.getName());
97
98        /*
99         * TODO: Showed "Paired" even though it was "Connected". This may be
100         * related to BluetoothHeadset not bound to the actual
101         * BluetoothHeadsetService when we got here.
102         */
103        setSummary(mCachedDevice.getSummary());
104
105        // Used to gray out the item
106        mIsBusy = mCachedDevice.isBusy();
107
108        // Data has changed
109        notifyChanged();
110
111        // This could affect ordering, so notify that also
112        notifyHierarchyChanged();
113    }
114
115    @Override
116    public boolean isEnabled() {
117        // Temp fix until we have 2053751 fixed in the framework
118        setEnabled(true);
119        return super.isEnabled() && !mIsBusy;
120    }
121
122    @Override
123    protected void onBindView(View view) {
124        // Disable this view if the bluetooth enable/disable preference view is off
125        if (null != findPreferenceInHierarchy("bt_checkbox")) {
126            setDependency("bt_checkbox");
127        }
128
129        super.onBindView(view);
130
131        ImageView btClass = (ImageView) view.findViewById(android.R.id.icon);
132        btClass.setImageResource(mCachedDevice.getBtClassDrawable());
133        btClass.setAlpha(!mIsBusy ? 255 : sDimAlpha);
134
135        mDeviceSettings = (ImageView) view.findViewById(R.id.deviceDetails);
136        if (mOnSettingsClickListener != null) {
137            mDeviceSettings.setOnClickListener(this);
138            mDeviceSettings.setTag(mCachedDevice);
139            mDeviceSettings.setAlpha(!mIsBusy ? 255 : sDimAlpha);
140        } else { // Hide the settings icon and divider
141            mDeviceSettings.setVisibility(View.GONE);
142            ImageView divider = (ImageView) view.findViewById(R.id.divider);
143            if (divider != null) {
144                divider.setVisibility(View.GONE);
145            }
146        }
147
148        LayoutInflater inflater = (LayoutInflater)
149                getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
150        ViewGroup profilesGroup = (ViewGroup) view.findViewById(R.id.profileIcons);
151        Map<Profile, Drawable> profileIcons = mCachedDevice.getProfileIcons();
152        for (Profile profile : profileIcons.keySet()) {
153            Drawable icon = profileIcons.get(profile);
154            inflater.inflate(R.layout.profile_icon_small, profilesGroup, true);
155            ImageView imageView =
156                    (ImageView) profilesGroup.getChildAt(profilesGroup.getChildCount() - 1);
157            imageView.setImageDrawable(icon);
158            boolean profileEnabled = mCachedDevice.isConnectedProfile(profile);
159            imageView.setAlpha(profileEnabled ? 255 : sDimAlpha);
160        }
161    }
162
163    public void onClick(View v) {
164        if (v == mDeviceSettings) {
165            if (mOnSettingsClickListener != null) mOnSettingsClickListener.onClick(v);
166        }
167    }
168
169    @Override
170    public int compareTo(Preference another) {
171        if (!(another instanceof BluetoothDevicePreference)) {
172            // Put other preference types above us
173            return 1;
174        }
175
176        return mCachedDevice.compareTo(((BluetoothDevicePreference) another).mCachedDevice);
177    }
178
179}
180