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;
31 import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;
36 * BluetoothDevicePreference is the preference type used to display each remote
37 * Bluetooth device in the Bluetooth Settings screen.
39 public class BluetoothDevicePreference extends Preference implements
40 CachedBluetoothDevice.Callback, OnClickListener {
41 private static final String TAG = "BluetoothDevicePreference";
43 private static int sDimAlpha = Integer.MIN_VALUE;
45 private final CachedBluetoothDevice mCachedDevice;
47 private ImageView mDeviceSettings;
49 private OnClickListener mOnSettingsClickListener;
52 * Cached local copy of whether the device is busy. This is only updated
53 * from {@link #onDeviceAttributesChanged()}.
55 private boolean mIsBusy;
57 public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice) {
60 if (sDimAlpha == Integer.MIN_VALUE) {
61 TypedValue outValue = new TypedValue();
62 context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, outValue, true);
63 sDimAlpha = (int) ((outValue.getFloat() * 255) * 0.5);
66 mCachedDevice = cachedDevice;
68 setWidgetLayoutResource(R.layout.preference_bluetooth);
70 cachedDevice.registerCallback(this);
72 onDeviceAttributesChanged();
75 public CachedBluetoothDevice getCachedDevice() {
79 public void setOnSettingsClickListener(OnClickListener listener) {
80 mOnSettingsClickListener = listener;
84 protected void onPrepareForRemoval() {
85 super.onPrepareForRemoval();
86 mCachedDevice.unregisterCallback(this);
89 public void onDeviceAttributesChanged() {
92 * The preference framework takes care of making sure the value has
93 * changed before proceeding.
96 setTitle(mCachedDevice.getName());
99 * TODO: Showed "Paired" even though it was "Connected". This may be
100 * related to BluetoothHeadset not bound to the actual
101 * BluetoothHeadsetService when we got here.
103 setSummary(mCachedDevice.getSummary());
105 // Used to gray out the item
106 mIsBusy = mCachedDevice.isBusy();
111 // This could affect ordering, so notify that also
112 notifyHierarchyChanged();
116 public boolean isEnabled() {
117 // Temp fix until we have 2053751 fixed in the framework
119 return super.isEnabled() && !mIsBusy;
123 protected void onBindView(View view) {
124 // Disable this view if the bluetooth enable/disable preference view is off
125 if (null != findPreferenceInHierarchy("bt_checkbox")) {
126 setDependency("bt_checkbox");
129 super.onBindView(view);
131 ImageView btClass = (ImageView) view.findViewById(android.R.id.icon);
132 btClass.setImageResource(mCachedDevice.getBtClassDrawable());
133 btClass.setAlpha(!mIsBusy ? 255 : sDimAlpha);
135 mDeviceSettings = (ImageView) view.findViewById(R.id.deviceDetails);
136 if (mOnSettingsClickListener != null) {
137 mDeviceSettings.setOnClickListener(this);
138 mDeviceSettings.setTag(mCachedDevice);
139 mDeviceSettings.setAlpha(!mIsBusy ? 255 : sDimAlpha);
140 } else { // Hide the settings icon and divider
141 mDeviceSettings.setVisibility(View.GONE);
142 ImageView divider = (ImageView) view.findViewById(R.id.divider);
143 if (divider != null) {
144 divider.setVisibility(View.GONE);
148 LayoutInflater inflater = (LayoutInflater)
149 getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
150 ViewGroup profilesGroup = (ViewGroup) view.findViewById(R.id.profileIcons);
151 Map<Profile, Drawable> profileIcons = mCachedDevice.getProfileIcons();
152 for (Profile profile : profileIcons.keySet()) {
153 Drawable icon = profileIcons.get(profile);
154 inflater.inflate(R.layout.profile_icon_small, profilesGroup, true);
155 ImageView imageView =
156 (ImageView) profilesGroup.getChildAt(profilesGroup.getChildCount() - 1);
157 imageView.setImageDrawable(icon);
158 boolean profileEnabled = mCachedDevice.isConnectedProfile(profile);
159 imageView.setAlpha(profileEnabled ? 255 : sDimAlpha);
163 public void onClick(View v) {
164 if (v == mDeviceSettings) {
165 if (mOnSettingsClickListener != null) mOnSettingsClickListener.onClick(v);
170 public int compareTo(Preference another) {
171 if (!(another instanceof BluetoothDevicePreference)) {
172 // Put other preference types above us
176 return mCachedDevice.compareTo(((BluetoothDevicePreference) another).mCachedDevice);