OSDN Git Service

Merge commit '593a7049f52d46a4e47162e2e3f2c5730e8ccfd8' into mergefix
[android-x86/packages-apps-Settings.git] / src / com / android / settings / bluetooth / BluetoothDevicePreference.java
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
17 package com.android.settings.bluetooth;
18
19 import com.android.settings.R;
20
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;
26
27 /**
28  * BluetoothDevicePreference is the preference type used to display each remote
29  * Bluetooth device in the Bluetooth Settings screen.
30  */
31 public 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     private int mAccessibleProfile;
38
39     /**
40      * Cached local copy of whether the device is busy. This is only updated
41      * from {@link #onDeviceAttributesChanged(CachedBluetoothDevice)}.
42      */
43     private boolean mIsBusy;
44
45     public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice,
46             int accessibleProfile) {
47         super(context);
48
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);
53         }
54
55         mCachedDevice = cachedDevice;
56         mAccessibleProfile = accessibleProfile;
57
58         setLayoutResource(R.layout.preference_bluetooth);
59
60         cachedDevice.registerCallback(this);
61
62         onDeviceAttributesChanged(cachedDevice);
63     }
64
65     public CachedBluetoothDevice getCachedDevice() {
66         return mCachedDevice;
67     }
68
69     @Override
70     protected void onPrepareForRemoval() {
71         super.onPrepareForRemoval();
72         mCachedDevice.unregisterCallback(this);
73     }
74
75     public void onDeviceAttributesChanged(CachedBluetoothDevice cachedDevice) {
76
77         /*
78          * The preference framework takes care of making sure the value has
79          * changed before proceeding.
80          */
81
82         setTitle(mCachedDevice.getName());
83
84         /*
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.
88          */
89         setSummary(mCachedDevice.getSummary(mAccessibleProfile));
90
91         // Used to gray out the item
92         mIsBusy = mCachedDevice.isBusy();
93
94         // Data has changed
95         notifyChanged();
96
97         // This could affect ordering, so notify that also
98         notifyHierarchyChanged();
99     }
100
101     @Override
102     public boolean isEnabled() {
103         // Temp fix until we have 2053751 fixed in the framework
104         setEnabled(true);
105         return super.isEnabled() && !mIsBusy;
106     }
107
108     @Override
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");
113         }
114
115         super.onBindView(view);
116
117         ImageView btClass = (ImageView) view.findViewById(R.id.btClass);
118         btClass.setImageResource(mCachedDevice.getBtClassDrawable());
119         btClass.setAlpha(isEnabled() ? 255 : sDimAlpha);
120     }
121
122     @Override
123     public int compareTo(Preference another) {
124         if (!(another instanceof BluetoothDevicePreference)) {
125             // Put other preference types above us
126             return 1;
127         }
128
129         return mCachedDevice.compareTo(((BluetoothDevicePreference) another).mCachedDevice);
130     }
131
132 }