HeadsetProfile.java revision 436b29e68e6608bed9e8e7d54385b8f62d89208e
1/*
2 * Copyright (C) 2011 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 android.bluetooth.BluetoothClass;
20import android.bluetooth.BluetoothDevice;
21import android.bluetooth.BluetoothHeadset;
22import android.bluetooth.BluetoothProfile;
23import android.bluetooth.BluetoothUuid;
24import android.content.Context;
25import android.os.Handler;
26import android.os.ParcelUuid;
27import android.util.Log;
28
29import com.android.settings.R;
30
31import java.util.ArrayList;
32import java.util.List;
33
34/**
35 * HeadsetProfile handles Bluetooth HFP and Headset profiles.
36 */
37final class HeadsetProfile implements LocalBluetoothProfile {
38    private static final String TAG = "HeadsetProfile";
39
40    private BluetoothHeadset mService;
41    private boolean mProfileReady;
42
43    private final LocalBluetoothAdapter mLocalAdapter;
44    private final CachedBluetoothDeviceManager mDeviceManager;
45    private final LocalBluetoothProfileManager mProfileManager;
46
47    static final ParcelUuid[] UUIDS = {
48        BluetoothUuid.HSP,
49        BluetoothUuid.Handsfree,
50    };
51
52    static final String NAME = "HEADSET";
53
54    // Order of this profile in device profiles list
55    private static final int ORDINAL = 0;
56
57    // These callbacks run on the main thread.
58    private final class HeadsetServiceListener
59            implements BluetoothProfile.ServiceListener {
60
61        public void onServiceConnected(int profile, BluetoothProfile proxy) {
62            mService = (BluetoothHeadset) proxy;
63            mProfileReady = true;
64            // We just bound to the service, so refresh the UI of the
65            // headset device.
66            List<BluetoothDevice> deviceList = mService.getConnectedDevices();
67            if (deviceList.isEmpty()) {
68                return;
69            }
70            BluetoothDevice firstDevice = deviceList.get(0);
71            CachedBluetoothDevice device = mDeviceManager.findDevice(firstDevice);
72            // we may add a new device here, but generally this should not happen
73            if (device == null) {
74                Log.w(TAG, "HeadsetProfile found new device: " + firstDevice);
75                device = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, firstDevice);
76            }
77            device.onProfileStateChanged(HeadsetProfile.this,
78                    BluetoothProfile.STATE_CONNECTED);
79
80            mProfileManager.callServiceConnectedListeners();
81        }
82
83        public void onServiceDisconnected(int profile) {
84            mProfileReady = false;
85            mService = null;
86            mProfileManager.callServiceDisconnectedListeners();
87        }
88    }
89
90    // TODO(): The calls must get queued if mService becomes null.
91    // It can happen when the phone app crashes for some reason.
92    // All callers should have service listeners. Dock Service is the only
93    // one right now.
94    HeadsetProfile(Context context, LocalBluetoothAdapter adapter,
95            CachedBluetoothDeviceManager deviceManager,
96            LocalBluetoothProfileManager profileManager) {
97        mLocalAdapter = adapter;
98        mDeviceManager = deviceManager;
99        mProfileManager = profileManager;
100        mLocalAdapter.getProfileProxy(context, new HeadsetServiceListener(),
101                BluetoothProfile.HEADSET);
102    }
103
104    public boolean isConnectable() {
105        return true;
106    }
107
108    public boolean isAutoConnectable() {
109        return true;
110    }
111
112    public boolean connect(BluetoothDevice device) {
113        List<BluetoothDevice> sinks = mService.getConnectedDevices();
114        if (sinks != null) {
115            for (BluetoothDevice sink : sinks) {
116                mService.disconnect(sink);
117            }
118        }
119        return mService.connect(device);
120    }
121
122    public boolean disconnect(BluetoothDevice device) {
123        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
124        if (!deviceList.isEmpty() && deviceList.get(0).equals(device)) {
125            // Downgrade priority as user is disconnecting the headset.
126            if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
127                mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
128            }
129            return mService.disconnect(device);
130        } else {
131            return false;
132        }
133    }
134
135    public int getConnectionStatus(BluetoothDevice device) {
136        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
137
138        return !deviceList.isEmpty() && deviceList.get(0).equals(device)
139                ? mService.getConnectionState(device)
140                : BluetoothProfile.STATE_DISCONNECTED;
141    }
142
143    public boolean isPreferred(BluetoothDevice device) {
144        return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
145    }
146
147    public int getPreferred(BluetoothDevice device) {
148        return mService.getPriority(device);
149    }
150
151    public void setPreferred(BluetoothDevice device, boolean preferred) {
152        if (preferred) {
153            if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
154                mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
155            }
156        } else {
157            mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
158        }
159    }
160
161    public synchronized boolean isProfileReady() {
162        return mProfileReady;
163    }
164
165    public String toString() {
166        return NAME;
167    }
168
169    public int getOrdinal() {
170        return ORDINAL;
171    }
172
173    public int getNameResource() {
174        return R.string.bluetooth_profile_headset;
175    }
176
177    public int getDisconnectResource() {
178        return R.string.bluetooth_disconnect_headset_profile;
179    }
180
181    public int getSummaryResourceForDevice(BluetoothDevice device) {
182        int state = mService.getConnectionState(device);
183        switch (state) {
184            case BluetoothProfile.STATE_DISCONNECTED:
185                return R.string.bluetooth_headset_profile_summary_use_for;
186
187            case BluetoothProfile.STATE_CONNECTED:
188                return R.string.bluetooth_headset_profile_summary_connected;
189
190            default:
191                return Utils.getConnectionStateSummary(state);
192        }
193    }
194
195    public int getDrawableResource(BluetoothClass btClass) {
196        return R.drawable.ic_bt_headset_hfp;
197    }
198}
199