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 com.android.settings.R;
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;
33 * BluetoothDevicePreference is the preference type used to display each remote
34 * Bluetooth device in the Bluetooth Settings screen.
36 public class BluetoothDevicePreference extends Preference implements
37 CachedBluetoothDevice.Callback, OnClickListener {
38 private static final String TAG = "BluetoothDevicePreference";
40 private static int sDimAlpha = Integer.MIN_VALUE;
42 private CachedBluetoothDevice mCachedDevice;
43 private int mAccessibleProfile;
45 private ImageView mDeviceSettings;
46 private OnClickListener mOnSettingsClickListener;
49 * Cached local copy of whether the device is busy. This is only updated
50 * from {@link #onDeviceAttributesChanged(CachedBluetoothDevice)}.
52 private boolean mIsBusy;
54 public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice,
55 int accessibleProfile) {
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);
64 mCachedDevice = cachedDevice;
65 mAccessibleProfile = accessibleProfile;
67 setLayoutResource(R.layout.preference_bluetooth);
69 cachedDevice.registerCallback(this);
71 onDeviceAttributesChanged(cachedDevice);
74 public CachedBluetoothDevice getCachedDevice() {
78 public void setOnSettingsClickListener(OnClickListener listener) {
79 mOnSettingsClickListener = listener;
83 protected void onPrepareForRemoval() {
84 super.onPrepareForRemoval();
85 mCachedDevice.unregisterCallback(this);
88 public void onDeviceAttributesChanged(CachedBluetoothDevice cachedDevice) {
91 * The preference framework takes care of making sure the value has
92 * changed before proceeding.
95 setTitle(mCachedDevice.getName());
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.
102 setSummary(mCachedDevice.getSummary(mAccessibleProfile));
104 // Used to gray out the item
105 mIsBusy = mCachedDevice.isBusy();
110 // This could affect ordering, so notify that also
111 notifyHierarchyChanged();
115 public boolean isEnabled() {
116 // Temp fix until we have 2053751 fixed in the framework
118 return super.isEnabled() && !mIsBusy;
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");
128 super.onBindView(view);
130 ImageView btClass = (ImageView) view.findViewById(R.id.btClass);
131 btClass.setImageResource(mCachedDevice.getBtClassDrawable());
132 btClass.setAlpha(isEnabled() ? 255 : sDimAlpha);
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);
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);
157 public void onClick(View v) {
158 if (v == mDeviceSettings) {
159 if (mOnSettingsClickListener != null) mOnSettingsClickListener.onClick(v);
164 public int compareTo(Preference another) {
165 if (!(another instanceof BluetoothDevicePreference)) {
166 // Put other preference types above us
170 return mCachedDevice.compareTo(((BluetoothDevicePreference) another).mCachedDevice);