BluetoothDevicePreference.java revision 487843886dc1c822ee568bb0d77e3f78356dfb87
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
17package com.android.settings.bluetooth;
18
19import com.android.settings.R;
20
21import android.content.Context;
22import android.preference.Preference;
23import android.util.TypedValue;
24import android.view.View;
25import android.widget.ImageView;
26
27/**
28 * BluetoothDevicePreference is the preference type used to display each remote
29 * Bluetooth device in the Bluetooth Settings screen.
30 */
31public class BluetoothDevicePreference extends Preference implements LocalBluetoothDevice.Callback {
32    private static final String TAG = "BluetoothDevicePreference";
33
34    private static int sDimAlpha = Integer.MIN_VALUE;
35
36    private LocalBluetoothDevice mLocalDevice;
37
38    /**
39     * Cached local copy of whether the device is busy. This is only updated
40     * from {@link #onDeviceAttributesChanged(LocalBluetoothDevice)}.
41     */
42    private boolean mIsBusy;
43
44    public BluetoothDevicePreference(Context context, LocalBluetoothDevice localDevice) {
45        super(context);
46
47        if (sDimAlpha == Integer.MIN_VALUE) {
48            TypedValue outValue = new TypedValue();
49            context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, outValue, true);
50            sDimAlpha = (int) (outValue.getFloat() * 255);
51        }
52
53        mLocalDevice = localDevice;
54
55        setLayoutResource(R.layout.preference_bluetooth);
56
57        localDevice.registerCallback(this);
58
59        onDeviceAttributesChanged(localDevice);
60    }
61
62    public LocalBluetoothDevice getDevice() {
63        return mLocalDevice;
64    }
65
66    @Override
67    protected void onPrepareForRemoval() {
68        super.onPrepareForRemoval();
69        mLocalDevice.unregisterCallback(this);
70    }
71
72    public void onDeviceAttributesChanged(LocalBluetoothDevice device) {
73
74        /*
75         * The preference framework takes care of making sure the value has
76         * changed before proceeding.
77         */
78
79        setTitle(mLocalDevice.getName());
80
81        /*
82         * TODO: Showed "Paired" even though it was "Connected". This may be
83         * related to BluetoothHeadset not bound to the actual
84         * BluetoothHeadsetService when we got here.
85         */
86        setSummary(mLocalDevice.getSummary());
87
88        // Used to gray out the item
89        mIsBusy = mLocalDevice.isBusy();
90
91        // Data has changed
92        notifyChanged();
93
94        // This could affect ordering, so notify that also
95        notifyHierarchyChanged();
96    }
97
98    @Override
99    public boolean isEnabled() {
100        return super.isEnabled() && !mIsBusy;
101    }
102
103    @Override
104    protected void onBindView(View view) {
105        // Disable this view if the bluetooth enable/disable preference view is off
106        setDependency("bt_checkbox");
107
108        super.onBindView(view);
109
110        ImageView btClass = (ImageView) view.findViewById(R.id.btClass);
111        btClass.setImageResource(mLocalDevice.getBtClassDrawable());
112        btClass.setAlpha(isEnabled() ? 255 : sDimAlpha);
113    }
114
115    @Override
116    public int compareTo(Preference another) {
117        if (!(another instanceof BluetoothDevicePreference)) {
118            // Put other preference types above us
119            return 1;
120        }
121
122        return mLocalDevice.compareTo(((BluetoothDevicePreference) another).mLocalDevice);
123    }
124
125}
126