• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 
17 package com.android.settingslib.bluetooth;
18 
19 import android.bluetooth.BluetoothA2dpSink;
20 import android.bluetooth.BluetoothAdapter;
21 import android.bluetooth.BluetoothClass;
22 import android.bluetooth.BluetoothDevice;
23 import android.bluetooth.BluetoothProfile;
24 import android.bluetooth.BluetoothUuid;
25 import android.content.Context;
26 import android.os.ParcelUuid;
27 import android.util.Log;
28 
29 import com.android.settingslib.R;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 final class A2dpSinkProfile implements LocalBluetoothProfile {
35     private static final String TAG = "A2dpSinkProfile";
36     private static boolean V = true;
37 
38     private BluetoothA2dpSink mService;
39     private boolean mIsProfileReady;
40 
41     private final LocalBluetoothAdapter mLocalAdapter;
42     private final CachedBluetoothDeviceManager mDeviceManager;
43 
44     static final ParcelUuid[] SRC_UUIDS = {
45         BluetoothUuid.AudioSource,
46         BluetoothUuid.AdvAudioDist,
47     };
48 
49     static final String NAME = "A2DPSink";
50     private final LocalBluetoothProfileManager mProfileManager;
51 
52     // Order of this profile in device profiles list
53     private static final int ORDINAL = 5;
54 
55     // These callbacks run on the main thread.
56     private final class A2dpSinkServiceListener
57             implements BluetoothProfile.ServiceListener {
58 
onServiceConnected(int profile, BluetoothProfile proxy)59         public void onServiceConnected(int profile, BluetoothProfile proxy) {
60             if (V) Log.d(TAG,"Bluetooth service connected");
61             mService = (BluetoothA2dpSink) proxy;
62             // We just bound to the service, so refresh the UI for any connected A2DP devices.
63             List<BluetoothDevice> deviceList = mService.getConnectedDevices();
64             while (!deviceList.isEmpty()) {
65                 BluetoothDevice nextDevice = deviceList.remove(0);
66                 CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
67                 // we may add a new device here, but generally this should not happen
68                 if (device == null) {
69                     Log.w(TAG, "A2dpSinkProfile found new device: " + nextDevice);
70                     device = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, nextDevice);
71                 }
72                 device.onProfileStateChanged(A2dpSinkProfile.this, BluetoothProfile.STATE_CONNECTED);
73                 device.refresh();
74             }
75             mIsProfileReady=true;
76         }
77 
onServiceDisconnected(int profile)78         public void onServiceDisconnected(int profile) {
79             if (V) Log.d(TAG,"Bluetooth service disconnected");
80             mIsProfileReady=false;
81         }
82     }
83 
isProfileReady()84     public boolean isProfileReady() {
85         return mIsProfileReady;
86     }
87 
A2dpSinkProfile(Context context, LocalBluetoothAdapter adapter, CachedBluetoothDeviceManager deviceManager, LocalBluetoothProfileManager profileManager)88     A2dpSinkProfile(Context context, LocalBluetoothAdapter adapter,
89             CachedBluetoothDeviceManager deviceManager,
90             LocalBluetoothProfileManager profileManager) {
91         mLocalAdapter = adapter;
92         mDeviceManager = deviceManager;
93         mProfileManager = profileManager;
94         mLocalAdapter.getProfileProxy(context, new A2dpSinkServiceListener(),
95                 BluetoothProfile.A2DP_SINK);
96     }
97 
isConnectable()98     public boolean isConnectable() {
99         return true;
100     }
101 
isAutoConnectable()102     public boolean isAutoConnectable() {
103         return true;
104     }
105 
getConnectedDevices()106     public List<BluetoothDevice> getConnectedDevices() {
107         if (mService == null) return new ArrayList<BluetoothDevice>(0);
108         return mService.getDevicesMatchingConnectionStates(
109               new int[] {BluetoothProfile.STATE_CONNECTED,
110                          BluetoothProfile.STATE_CONNECTING,
111                          BluetoothProfile.STATE_DISCONNECTING});
112     }
113 
connect(BluetoothDevice device)114     public boolean connect(BluetoothDevice device) {
115         if (mService == null) return false;
116         List<BluetoothDevice> srcs = getConnectedDevices();
117         if (srcs != null) {
118             for (BluetoothDevice src : srcs) {
119                 if (src.equals(device)) {
120                     // Connect to same device, Ignore it
121                     Log.d(TAG,"Ignoring Connect");
122                     return true;
123                 }
124             }
125         }
126         return mService.connect(device);
127     }
128 
disconnect(BluetoothDevice device)129     public boolean disconnect(BluetoothDevice device) {
130         if (mService == null) return false;
131         // Downgrade priority as user is disconnecting the headset.
132         if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON){
133             mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
134         }
135         return mService.disconnect(device);
136     }
137 
getConnectionStatus(BluetoothDevice device)138     public int getConnectionStatus(BluetoothDevice device) {
139         if (mService == null) {
140             return BluetoothProfile.STATE_DISCONNECTED;
141         }
142         return mService.getConnectionState(device);
143     }
144 
isPreferred(BluetoothDevice device)145     public boolean isPreferred(BluetoothDevice device) {
146         if (mService == null) return false;
147         return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
148     }
149 
getPreferred(BluetoothDevice device)150     public int getPreferred(BluetoothDevice device) {
151         if (mService == null) return BluetoothProfile.PRIORITY_OFF;
152         return mService.getPriority(device);
153     }
154 
setPreferred(BluetoothDevice device, boolean preferred)155     public void setPreferred(BluetoothDevice device, boolean preferred) {
156         if (mService == null) return;
157         if (preferred) {
158             if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
159                 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
160             }
161         } else {
162             mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
163         }
164     }
165 
isA2dpPlaying()166     boolean isA2dpPlaying() {
167         if (mService == null) return false;
168         List<BluetoothDevice> srcs = mService.getConnectedDevices();
169         if (!srcs.isEmpty()) {
170             if (mService.isA2dpPlaying(srcs.get(0))) {
171                 return true;
172             }
173         }
174         return false;
175     }
176 
toString()177     public String toString() {
178         return NAME;
179     }
180 
getOrdinal()181     public int getOrdinal() {
182         return ORDINAL;
183     }
184 
getNameResource(BluetoothDevice device)185     public int getNameResource(BluetoothDevice device) {
186         // we need to have same string in UI for even SINK Media Audio.
187         return R.string.bluetooth_profile_a2dp;
188     }
189 
getSummaryResourceForDevice(BluetoothDevice device)190     public int getSummaryResourceForDevice(BluetoothDevice device) {
191         int state = getConnectionStatus(device);
192         switch (state) {
193             case BluetoothProfile.STATE_DISCONNECTED:
194                 return R.string.bluetooth_a2dp_profile_summary_use_for;
195 
196             case BluetoothProfile.STATE_CONNECTED:
197                 return R.string.bluetooth_a2dp_profile_summary_connected;
198 
199             default:
200                 return Utils.getConnectionStateSummary(state);
201         }
202     }
203 
getDrawableResource(BluetoothClass btClass)204     public int getDrawableResource(BluetoothClass btClass) {
205         return R.drawable.ic_bt_headphones_a2dp;
206     }
207 
finalize()208     protected void finalize() {
209         if (V) Log.d(TAG, "finalize()");
210         if (mService != null) {
211             try {
212                 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.A2DP_SINK,
213                                                                        mService);
214                 mService = null;
215             }catch (Throwable t) {
216                 Log.w(TAG, "Error cleaning up A2DP proxy", t);
217             }
218         }
219     }
220 }
221