OSDN Git Service

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