Home | History | Annotate | Download | only in bluetooth
      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 
     37     private BluetoothA2dpSink mService;
     38     private boolean mIsProfileReady;
     39 
     40     private final CachedBluetoothDeviceManager mDeviceManager;
     41 
     42     static final ParcelUuid[] SRC_UUIDS = {
     43         BluetoothUuid.AudioSource,
     44         BluetoothUuid.AdvAudioDist,
     45     };
     46 
     47     static final String NAME = "A2DPSink";
     48     private final LocalBluetoothProfileManager mProfileManager;
     49 
     50     // Order of this profile in device profiles list
     51     private static final int ORDINAL = 5;
     52 
     53     // These callbacks run on the main thread.
     54     private final class A2dpSinkServiceListener
     55             implements BluetoothProfile.ServiceListener {
     56 
     57         public void onServiceConnected(int profile, BluetoothProfile proxy) {
     58             mService = (BluetoothA2dpSink) proxy;
     59             // We just bound to the service, so refresh the UI for any connected A2DP devices.
     60             List<BluetoothDevice> deviceList = mService.getConnectedDevices();
     61             while (!deviceList.isEmpty()) {
     62                 BluetoothDevice nextDevice = deviceList.remove(0);
     63                 CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
     64                 // we may add a new device here, but generally this should not happen
     65                 if (device == null) {
     66                     Log.w(TAG, "A2dpSinkProfile found new device: " + nextDevice);
     67                     device = mDeviceManager.addDevice(nextDevice);
     68                 }
     69                 device.onProfileStateChanged(A2dpSinkProfile.this, BluetoothProfile.STATE_CONNECTED);
     70                 device.refresh();
     71             }
     72             mIsProfileReady=true;
     73         }
     74 
     75         public void onServiceDisconnected(int profile) {
     76             mIsProfileReady=false;
     77         }
     78     }
     79 
     80     public boolean isProfileReady() {
     81         return mIsProfileReady;
     82     }
     83 
     84     @Override
     85     public int getProfileId() {
     86         return BluetoothProfile.A2DP_SINK;
     87     }
     88 
     89     A2dpSinkProfile(Context context, CachedBluetoothDeviceManager deviceManager,
     90             LocalBluetoothProfileManager profileManager) {
     91         mDeviceManager = deviceManager;
     92         mProfileManager = profileManager;
     93         BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, new A2dpSinkServiceListener(),
     94                 BluetoothProfile.A2DP_SINK);
     95     }
     96 
     97     public boolean accessProfileEnabled() {
     98         return true;
     99     }
    100 
    101     public boolean isAutoConnectable() {
    102         return true;
    103     }
    104 
    105     public List<BluetoothDevice> getConnectedDevices() {
    106         if (mService == null) {
    107             return new ArrayList<BluetoothDevice>(0);
    108         }
    109         return mService.getDevicesMatchingConnectionStates(
    110               new int[] {BluetoothProfile.STATE_CONNECTED,
    111                          BluetoothProfile.STATE_CONNECTING,
    112                          BluetoothProfile.STATE_DISCONNECTING});
    113     }
    114 
    115     public boolean connect(BluetoothDevice device) {
    116         if (mService == null) {
    117             return false;
    118         }
    119         return mService.connect(device);
    120     }
    121 
    122     public boolean disconnect(BluetoothDevice device) {
    123         if (mService == null) {
    124             return false;
    125         }
    126         // Downgrade priority as user is disconnecting the headset.
    127         if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
    128             mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
    129         }
    130         return mService.disconnect(device);
    131     }
    132 
    133     public int getConnectionStatus(BluetoothDevice device) {
    134         if (mService == null) {
    135             return BluetoothProfile.STATE_DISCONNECTED;
    136         }
    137         return mService.getConnectionState(device);
    138     }
    139 
    140     public boolean isPreferred(BluetoothDevice device) {
    141         if (mService == null) {
    142             return false;
    143         }
    144         return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
    145     }
    146 
    147     public int getPreferred(BluetoothDevice device) {
    148         if (mService == null) {
    149             return BluetoothProfile.PRIORITY_OFF;
    150         }
    151         return mService.getPriority(device);
    152     }
    153 
    154     public void setPreferred(BluetoothDevice device, boolean preferred) {
    155         if (mService == null) {
    156             return;
    157         }
    158         if (preferred) {
    159             if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
    160                 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
    161             }
    162         } else {
    163             mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
    164         }
    165     }
    166 
    167     boolean isA2dpPlaying() {
    168         if (mService == null) {
    169             return false;
    170         }
    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 BluetoothUtils.getConnectionStateSummary(state);
    204         }
    205     }
    206 
    207     public int getDrawableResource(BluetoothClass btClass) {
    208         return com.android.internal.R.drawable.ic_bt_headphones_a2dp;
    209     }
    210 
    211     protected void finalize() {
    212         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