BluetoothDevicePreference.java revision a41e2f94b792e44872be87f40fce182e6b39f4ba
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.preference.Preference; 23import android.util.TypedValue; 24import android.view.View; 25import android.widget.ImageView; 26 27/** 28 * BluetoothDevicePreference is the preference type used to display each remote 29 * Bluetooth device in the Bluetooth Settings screen. 30 */ 31public class BluetoothDevicePreference extends Preference implements CachedBluetoothDevice.Callback { 32 private static final String TAG = "BluetoothDevicePreference"; 33 34 private static int sDimAlpha = Integer.MIN_VALUE; 35 36 private CachedBluetoothDevice mCachedDevice; 37 38 /** 39 * Cached local copy of whether the device is busy. This is only updated 40 * from {@link #onDeviceAttributesChanged(CachedBluetoothDevice)}. 41 */ 42 private boolean mIsBusy; 43 44 public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice) { 45 super(context); 46 47 if (sDimAlpha == Integer.MIN_VALUE) { 48 TypedValue outValue = new TypedValue(); 49 context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, outValue, true); 50 sDimAlpha = (int) (outValue.getFloat() * 255); 51 } 52 53 mCachedDevice = cachedDevice; 54 55 setLayoutResource(R.layout.preference_bluetooth); 56 57 cachedDevice.registerCallback(this); 58 59 onDeviceAttributesChanged(cachedDevice); 60 } 61 62 public CachedBluetoothDevice getCachedDevice() { 63 return mCachedDevice; 64 } 65 66 @Override 67 protected void onPrepareForRemoval() { 68 super.onPrepareForRemoval(); 69 mCachedDevice.unregisterCallback(this); 70 } 71 72 public void onDeviceAttributesChanged(CachedBluetoothDevice cachedDevice) { 73 74 /* 75 * The preference framework takes care of making sure the value has 76 * changed before proceeding. 77 */ 78 79 setTitle(mCachedDevice.getName()); 80 81 /* 82 * TODO: Showed "Paired" even though it was "Connected". This may be 83 * related to BluetoothHeadset not bound to the actual 84 * BluetoothHeadsetService when we got here. 85 */ 86 setSummary(mCachedDevice.getSummary()); 87 88 // Used to gray out the item 89 mIsBusy = mCachedDevice.isBusy(); 90 91 // Data has changed 92 notifyChanged(); 93 94 // This could affect ordering, so notify that also 95 notifyHierarchyChanged(); 96 } 97 98 @Override 99 public boolean isEnabled() { 100 // Temp fix until we have 2053751 fixed in the framework 101 setEnabled(true); 102 return super.isEnabled() && !mIsBusy; 103 } 104 105 @Override 106 protected void onBindView(View view) { 107 // Disable this view if the bluetooth enable/disable preference view is off 108 if (null != findPreferenceInHierarchy("bt_checkbox")){ 109 setDependency("bt_checkbox"); 110 } 111 112 super.onBindView(view); 113 114 ImageView btClass = (ImageView) view.findViewById(R.id.btClass); 115 btClass.setImageResource(mCachedDevice.getBtClassDrawable()); 116 btClass.setAlpha(isEnabled() ? 255 : sDimAlpha); 117 } 118 119 @Override 120 public int compareTo(Preference another) { 121 if (!(another instanceof BluetoothDevicePreference)) { 122 // Put other preference types above us 123 return 1; 124 } 125 126 return mCachedDevice.compareTo(((BluetoothDevicePreference) another).mCachedDevice); 127 } 128 129} 130