OSDN Git Service

Make the BT class icon visible.
[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 android.app.AlertDialog;
20 import android.bluetooth.BluetoothClass;
21 import android.bluetooth.BluetoothDevice;
22 import android.bluetooth.BluetoothProfile;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.graphics.drawable.Drawable;
26 import android.preference.Preference;
27 import android.text.TextUtils;
28 import android.util.Log;
29 import android.util.TypedValue;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.View.OnClickListener;
33 import android.view.ViewGroup;
34 import android.widget.ImageView;
35
36 import com.android.settings.R;
37
38 import java.util.List;
39
40 /**
41  * BluetoothDevicePreference is the preference type used to display each remote
42  * Bluetooth device in the Bluetooth Settings screen.
43  */
44 public final class BluetoothDevicePreference extends Preference implements
45         CachedBluetoothDevice.Callback, OnClickListener {
46     private static final String TAG = "BluetoothDevicePreference";
47
48     private static int sDimAlpha = Integer.MIN_VALUE;
49
50     private final CachedBluetoothDevice mCachedDevice;
51
52     private ImageView mDeviceSettings;
53
54     private OnClickListener mOnSettingsClickListener;
55
56     private AlertDialog mDisconnectDialog;
57
58     public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice) {
59         super(context);
60
61         if (sDimAlpha == Integer.MIN_VALUE) {
62             TypedValue outValue = new TypedValue();
63             context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, outValue, true);
64             sDimAlpha = (int) (outValue.getFloat() * 255);
65         }
66
67         mCachedDevice = cachedDevice;
68
69         setWidgetLayoutResource(R.layout.preference_bluetooth);
70
71         mCachedDevice.registerCallback(this);
72
73         onDeviceAttributesChanged();
74     }
75
76     CachedBluetoothDevice getCachedDevice() {
77         return mCachedDevice;
78     }
79
80     public void setOnSettingsClickListener(OnClickListener listener) {
81         mOnSettingsClickListener = listener;
82     }
83
84     @Override
85     protected void onPrepareForRemoval() {
86         super.onPrepareForRemoval();
87         mCachedDevice.unregisterCallback(this);
88         if (mDisconnectDialog != null) {
89             mDisconnectDialog.dismiss();
90             mDisconnectDialog = null;
91         }
92     }
93
94     public void onDeviceAttributesChanged() {
95         /*
96          * The preference framework takes care of making sure the value has
97          * changed before proceeding. It will also call notifyChanged() if
98          * any preference info has changed from the previous value.
99          */
100         setTitle(mCachedDevice.getName());
101
102         setSummary(getConnectionSummary());
103
104         // Used to gray out the item
105         setEnabled(!mCachedDevice.isBusy());
106
107         // This could affect ordering, so notify that
108         notifyHierarchyChanged();
109     }
110
111     @Override
112     protected void onBindView(View view) {
113         // Disable this view if the bluetooth enable/disable preference view is off
114         if (null != findPreferenceInHierarchy("bt_checkbox")) {
115             setDependency("bt_checkbox");
116         }
117
118         super.onBindView(view);
119
120         ImageView btClass = (ImageView) view.findViewById(android.R.id.icon);
121         btClass.setImageResource(getBtClassDrawable());
122         btClass.setAlpha(isEnabled() ? 255 : sDimAlpha);
123         btClass.setVisibility(View.VISIBLE);
124         mDeviceSettings = (ImageView) view.findViewById(R.id.deviceDetails);
125         if (mOnSettingsClickListener != null) {
126             mDeviceSettings.setOnClickListener(this);
127             mDeviceSettings.setTag(mCachedDevice);
128             mDeviceSettings.setAlpha(isEnabled() ? 255 : sDimAlpha);
129         } else { // Hide the settings icon and divider
130             mDeviceSettings.setVisibility(View.GONE);
131             View divider = view.findViewById(R.id.divider);
132             if (divider != null) {
133                 divider.setVisibility(View.GONE);
134             }
135         }
136
137         LayoutInflater inflater = (LayoutInflater)
138                 getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
139         ViewGroup profilesGroup = (ViewGroup) view.findViewById(R.id.profileIcons);
140         for (LocalBluetoothProfile profile : mCachedDevice.getProfiles()) {
141             int iconResource = profile.getDrawableResource(mCachedDevice.getBtClass());
142             if (iconResource != 0) {
143                 Drawable icon = getContext().getResources().getDrawable(iconResource);
144                 inflater.inflate(R.layout.profile_icon_small, profilesGroup, true);
145                 ImageView imageView =
146                         (ImageView) profilesGroup.getChildAt(profilesGroup.getChildCount() - 1);
147                 imageView.setImageDrawable(icon);
148                 boolean profileEnabled = mCachedDevice.isConnectedProfile(profile);
149                 imageView.setAlpha(profileEnabled ? 255 : sDimAlpha);
150             }
151         }
152     }
153
154     public void onClick(View v) {
155         if (v == mDeviceSettings) {
156             if (mOnSettingsClickListener != null) {
157                 mOnSettingsClickListener.onClick(v);
158             }
159         }
160     }
161
162     public boolean equals(Object o) {
163         if ((o == null) || !(o instanceof BluetoothDevicePreference)) {
164             return false;
165         }
166         return mCachedDevice.equals(
167                 ((BluetoothDevicePreference) o).mCachedDevice);
168     }
169
170     public int hashCode() {
171         return mCachedDevice.hashCode();
172     }
173
174     @Override
175     public int compareTo(Preference another) {
176         if (!(another instanceof BluetoothDevicePreference)) {
177             // Put other preference types above us
178             return 1;
179         }
180
181         return mCachedDevice
182                 .compareTo(((BluetoothDevicePreference) another).mCachedDevice);
183     }
184
185     void onClicked() {
186         int bondState = mCachedDevice.getBondState();
187
188         if (mCachedDevice.isConnected()) {
189             askDisconnect();
190         } else if (bondState == BluetoothDevice.BOND_BONDED) {
191             mCachedDevice.connect(true);
192         } else if (bondState == BluetoothDevice.BOND_NONE) {
193             pair();
194         }
195     }
196
197     // Show disconnect confirmation dialog for a device.
198     private void askDisconnect() {
199         Context context = getContext();
200         String name = mCachedDevice.getName();
201         if (TextUtils.isEmpty(name)) {
202             name = context.getString(R.string.bluetooth_device);
203         }
204         String message = context.getString(R.string.bluetooth_disconnect_blank, name);
205
206         DialogInterface.OnClickListener disconnectListener = new DialogInterface.OnClickListener() {
207             public void onClick(DialogInterface dialog, int which) {
208                 mCachedDevice.disconnect();
209             }
210         };
211
212         mDisconnectDialog = Utils.showDisconnectDialog(context,
213                 mDisconnectDialog, disconnectListener, name, message);
214     }
215
216     private void pair() {
217         if (!mCachedDevice.startPairing()) {
218             Utils.showError(getContext(), mCachedDevice.getName(),
219                     R.string.bluetooth_pairing_error_message);
220         }
221     }
222
223     private int getConnectionSummary() {
224         final CachedBluetoothDevice cachedDevice = mCachedDevice;
225         final BluetoothDevice device = cachedDevice.getDevice();
226
227         // if any profiles are connected or busy, return that status
228         for (LocalBluetoothProfile profile : cachedDevice.getProfiles()) {
229             int connectionStatus = cachedDevice.getProfileConnectionState(profile);
230
231             if (connectionStatus != BluetoothProfile.STATE_DISCONNECTED) {
232                 return Utils.getConnectionStateSummary(connectionStatus);
233             }
234         }
235
236         switch (cachedDevice.getBondState()) {
237             case BluetoothDevice.BOND_BONDED:
238                 return R.string.bluetooth_paired;
239             case BluetoothDevice.BOND_BONDING:
240                 return R.string.bluetooth_pairing;
241             case BluetoothDevice.BOND_NONE:
242                 return R.string.bluetooth_not_connected;
243             default:
244                 return 0;
245         }
246     }
247
248     private int getBtClassDrawable() {
249         BluetoothClass btClass = mCachedDevice.getBtClass();
250         if (btClass != null) {
251             switch (btClass.getMajorDeviceClass()) {
252                 case BluetoothClass.Device.Major.COMPUTER:
253                     return R.drawable.ic_bt_laptop;
254
255                 case BluetoothClass.Device.Major.PHONE:
256                     return R.drawable.ic_bt_cellphone;
257
258                 case BluetoothClass.Device.Major.PERIPHERAL:
259                     return HidProfile.getHidClassDrawable(btClass);
260
261                 case BluetoothClass.Device.Major.IMAGING:
262                     return R.drawable.ic_bt_imaging;
263
264                 default:
265                     // unrecognized device class; continue
266             }
267         } else {
268             Log.w(TAG, "mBtClass is null");
269         }
270
271         List<LocalBluetoothProfile> profiles = mCachedDevice.getProfiles();
272         for (LocalBluetoothProfile profile : profiles) {
273             int resId = profile.getDrawableResource(btClass);
274             if (resId != 0) {
275                 return resId;
276             }
277         }
278         if (btClass != null) {
279             if (btClass.doesClassMatch(BluetoothClass.PROFILE_A2DP)) {
280                 return R.drawable.ic_bt_headphones_a2dp;
281
282             }
283             if (btClass.doesClassMatch(BluetoothClass.PROFILE_HEADSET)) {
284                 return R.drawable.ic_bt_headset_hfp;
285             }
286         }
287         return 0;
288     }
289 }