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 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;
36 import com.android.settings.R;
38 import java.util.List;
41 * BluetoothDevicePreference is the preference type used to display each remote
42 * Bluetooth device in the Bluetooth Settings screen.
44 public final class BluetoothDevicePreference extends Preference implements
45 CachedBluetoothDevice.Callback, OnClickListener {
46 private static final String TAG = "BluetoothDevicePreference";
48 private static int sDimAlpha = Integer.MIN_VALUE;
50 private final CachedBluetoothDevice mCachedDevice;
52 private ImageView mDeviceSettings;
54 private OnClickListener mOnSettingsClickListener;
56 private AlertDialog mDisconnectDialog;
58 public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice) {
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);
67 mCachedDevice = cachedDevice;
69 setWidgetLayoutResource(R.layout.preference_bluetooth);
71 mCachedDevice.registerCallback(this);
73 onDeviceAttributesChanged();
76 CachedBluetoothDevice getCachedDevice() {
80 public void setOnSettingsClickListener(OnClickListener listener) {
81 mOnSettingsClickListener = listener;
85 protected void onPrepareForRemoval() {
86 super.onPrepareForRemoval();
87 mCachedDevice.unregisterCallback(this);
88 if (mDisconnectDialog != null) {
89 mDisconnectDialog.dismiss();
90 mDisconnectDialog = null;
94 public void onDeviceAttributesChanged() {
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.
100 setTitle(mCachedDevice.getName());
102 setSummary(getConnectionSummary());
104 // Used to gray out the item
105 setEnabled(!mCachedDevice.isBusy());
107 // This could affect ordering, so notify that
108 notifyHierarchyChanged();
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");
118 super.onBindView(view);
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);
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);
154 public void onClick(View v) {
155 if (v == mDeviceSettings) {
156 if (mOnSettingsClickListener != null) {
157 mOnSettingsClickListener.onClick(v);
162 public boolean equals(Object o) {
163 if ((o == null) || !(o instanceof BluetoothDevicePreference)) {
166 return mCachedDevice.equals(
167 ((BluetoothDevicePreference) o).mCachedDevice);
170 public int hashCode() {
171 return mCachedDevice.hashCode();
175 public int compareTo(Preference another) {
176 if (!(another instanceof BluetoothDevicePreference)) {
177 // Put other preference types above us
182 .compareTo(((BluetoothDevicePreference) another).mCachedDevice);
186 int bondState = mCachedDevice.getBondState();
188 if (mCachedDevice.isConnected()) {
190 } else if (bondState == BluetoothDevice.BOND_BONDED) {
191 mCachedDevice.connect(true);
192 } else if (bondState == BluetoothDevice.BOND_NONE) {
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);
204 String message = context.getString(R.string.bluetooth_disconnect_blank, name);
206 DialogInterface.OnClickListener disconnectListener = new DialogInterface.OnClickListener() {
207 public void onClick(DialogInterface dialog, int which) {
208 mCachedDevice.disconnect();
212 mDisconnectDialog = Utils.showDisconnectDialog(context,
213 mDisconnectDialog, disconnectListener, name, message);
216 private void pair() {
217 if (!mCachedDevice.startPairing()) {
218 Utils.showError(getContext(), mCachedDevice.getName(),
219 R.string.bluetooth_pairing_error_message);
223 private int getConnectionSummary() {
224 final CachedBluetoothDevice cachedDevice = mCachedDevice;
225 final BluetoothDevice device = cachedDevice.getDevice();
227 // if any profiles are connected or busy, return that status
228 for (LocalBluetoothProfile profile : cachedDevice.getProfiles()) {
229 int connectionStatus = cachedDevice.getProfileConnectionState(profile);
231 if (connectionStatus != BluetoothProfile.STATE_DISCONNECTED) {
232 return Utils.getConnectionStateSummary(connectionStatus);
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;
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;
255 case BluetoothClass.Device.Major.PHONE:
256 return R.drawable.ic_bt_cellphone;
258 case BluetoothClass.Device.Major.PERIPHERAL:
259 return HidProfile.getHidClassDrawable(btClass);
261 case BluetoothClass.Device.Major.IMAGING:
262 return R.drawable.ic_bt_imaging;
265 // unrecognized device class; continue
268 Log.w(TAG, "mBtClass is null");
271 List<LocalBluetoothProfile> profiles = mCachedDevice.getProfiles();
272 for (LocalBluetoothProfile profile : profiles) {
273 int resId = profile.getDrawableResource(btClass);
278 if (btClass != null) {
279 if (btClass.doesClassMatch(BluetoothClass.PROFILE_A2DP)) {
280 return R.drawable.ic_bt_headphones_a2dp;
283 if (btClass.doesClassMatch(BluetoothClass.PROFILE_HEADSET)) {
284 return R.drawable.ic_bt_headset_hfp;