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
17package com.android.settingslib.bluetooth;
18
19import android.bluetooth.BluetoothA2dpSink;
20import android.bluetooth.BluetoothAdapter;
21import android.bluetooth.BluetoothClass;
22import android.bluetooth.BluetoothDevice;
23import android.bluetooth.BluetoothProfile;
24import android.bluetooth.BluetoothUuid;
25import android.content.Context;
26import android.os.ParcelUuid;
27import android.util.Log;
28
29import com.android.settingslib.R;
30
31import java.util.ArrayList;
32import java.util.List;
33
34final 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
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
78        public void onServiceDisconnected(int profile) {
79            if (V) Log.d(TAG,"Bluetooth service disconnected");
80            mIsProfileReady=false;
81        }
82    }
83
84    public boolean isProfileReady() {
85        return mIsProfileReady;
86    }
87
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
98    public boolean isConnectable() {
99        return true;
100    }
101
102    public boolean isAutoConnectable() {
103        return true;
104    }
105
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
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            for (BluetoothDevice src : srcs) {
126                mService.disconnect(src);
127            }
128        }
129        return mService.connect(device);
130    }
131
132    public boolean disconnect(BluetoothDevice device) {
133        if (mService == null) return false;
134        // Downgrade priority as user is disconnecting the headset.
135        if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON){
136            mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
137        }
138        return mService.disconnect(device);
139    }
140
141    public int getConnectionStatus(BluetoothDevice device) {
142        if (mService == null) {
143            return BluetoothProfile.STATE_DISCONNECTED;
144        }
145        return mService.getConnectionState(device);
146    }
147
148    public boolean isPreferred(BluetoothDevice device) {
149        if (mService == null) return false;
150        return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
151    }
152
153    public int getPreferred(BluetoothDevice device) {
154        if (mService == null) return BluetoothProfile.PRIORITY_OFF;
155        return mService.getPriority(device);
156    }
157
158    public void setPreferred(BluetoothDevice device, boolean preferred) {
159        if (mService == null) return;
160        if (preferred) {
161            if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
162                mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
163            }
164        } else {
165            mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
166        }
167    }
168
169    boolean isA2dpPlaying() {
170        if (mService == null) return false;
171        List<BluetoothDevice> srcs = mService.getConnectedDevices();
172        if (!srcs.isEmpty()) {
173            if (mService.isA2dpPlaying(srcs.get(0))) {
174                return true;
175            }
176        }
177        return false;
178    }
179
180    public String toString() {
181        return NAME;
182    }
183
184    public int getOrdinal() {
185        return ORDINAL;
186    }
187
188    public int getNameResource(BluetoothDevice device) {
189        // we need to have same string in UI for even SINK Media Audio.
190        return R.string.bluetooth_profile_a2dp;
191    }
192
193    public int getSummaryResourceForDevice(BluetoothDevice device) {
194        int state = getConnectionStatus(device);
195        switch (state) {
196            case BluetoothProfile.STATE_DISCONNECTED:
197                return R.string.bluetooth_a2dp_profile_summary_use_for;
198
199            case BluetoothProfile.STATE_CONNECTED:
200                return R.string.bluetooth_a2dp_profile_summary_connected;
201
202            default:
203                return Utils.getConnectionStateSummary(state);
204        }
205    }
206
207    public int getDrawableResource(BluetoothClass btClass) {
208        return R.drawable.ic_bt_headphones_a2dp;
209    }
210
211    protected void finalize() {
212        if (V) Log.d(TAG, "finalize()");
213        if (mService != null) {
214            try {
215                BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.A2DP_SINK,
216                                                                       mService);
217                mService = null;
218            }catch (Throwable t) {
219                Log.w(TAG, "Error cleaning up A2DP proxy", t);
220            }
221        }
222    }
223}
224