2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.bluetooth;
19 import com.android.settings.R;
21 import android.content.Context;
22 import android.preference.Preference;
23 import android.util.TypedValue;
24 import android.view.View;
25 import android.widget.ImageView;
28 * BluetoothDevicePreference is the preference type used to display each remote
29 * Bluetooth device in the Bluetooth Settings screen.
31 public class BluetoothDevicePreference extends Preference implements CachedBluetoothDevice.Callback {
32 private static final String TAG = "BluetoothDevicePreference";
34 private static int sDimAlpha = Integer.MIN_VALUE;
36 private CachedBluetoothDevice mCachedDevice;
37 private int mAccessibleProfile;
40 * Cached local copy of whether the device is busy. This is only updated
41 * from {@link #onDeviceAttributesChanged(CachedBluetoothDevice)}.
43 private boolean mIsBusy;
45 public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice,
46 int accessibleProfile) {
49 if (sDimAlpha == Integer.MIN_VALUE) {
50 TypedValue outValue = new TypedValue();
51 context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, outValue, true);
52 sDimAlpha = (int) (outValue.getFloat() * 255);
55 mCachedDevice = cachedDevice;
56 mAccessibleProfile = accessibleProfile;
58 setLayoutResource(R.layout.preference_bluetooth);
60 cachedDevice.registerCallback(this);
62 onDeviceAttributesChanged(cachedDevice);
65 public CachedBluetoothDevice getCachedDevice() {
70 protected void onPrepareForRemoval() {
71 super.onPrepareForRemoval();
72 mCachedDevice.unregisterCallback(this);
75 public void onDeviceAttributesChanged(CachedBluetoothDevice cachedDevice) {
78 * The preference framework takes care of making sure the value has
79 * changed before proceeding.
82 setTitle(mCachedDevice.getName());
85 * TODO: Showed "Paired" even though it was "Connected". This may be
86 * related to BluetoothHeadset not bound to the actual
87 * BluetoothHeadsetService when we got here.
89 setSummary(mCachedDevice.getSummary(mAccessibleProfile));
91 // Used to gray out the item
92 mIsBusy = mCachedDevice.isBusy();
97 // This could affect ordering, so notify that also
98 notifyHierarchyChanged();
102 public boolean isEnabled() {
103 // Temp fix until we have 2053751 fixed in the framework
105 return super.isEnabled() && !mIsBusy;
109 protected void onBindView(View view) {
110 // Disable this view if the bluetooth enable/disable preference view is off
111 if (null != findPreferenceInHierarchy("bt_checkbox")){
112 setDependency("bt_checkbox");
115 super.onBindView(view);
117 ImageView btClass = (ImageView) view.findViewById(R.id.btClass);
118 btClass.setImageResource(mCachedDevice.getBtClassDrawable());
119 btClass.setAlpha(isEnabled() ? 255 : sDimAlpha);
123 public int compareTo(Preference another) {
124 if (!(another instanceof BluetoothDevicePreference)) {
125 // Put other preference types above us
129 return mCachedDevice.compareTo(((BluetoothDevicePreference) another).mCachedDevice);