Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c0870bcb authored by Bhakthavatsala Raghavendra's avatar Bhakthavatsala Raghavendra Committed by Bruno Martins
Browse files

Bluetooth: Add BC profile entry

Add  BC profile entry and override constructor for CachedBluetoothDevice

CRs-fixed: 2853618

Change-Id: I750139a482280ce3409989582ffad50aed20e081
parent 08f32b32
Loading
Loading
Loading
Loading
+91 −0
Original line number Diff line number Diff line
@@ -3006,6 +3006,92 @@ public final class BluetoothAdapter {
        return null;
    }

    private void closeBCProfile(BluetoothProfile proxy) {
        Class<?> bshClass = null;
        Method bshClose = null;
        try {
            bshClass = Class.forName("android.bluetooth.BluetoothSyncHelper");
        } catch (ClassNotFoundException ex) {
            Log.e(TAG, "no BSH: exists");
            bshClass = null;
        }
        if (bshClass != null) {
            Log.d(TAG, "Able to get BSH class handle");
            try {
                bshClose =  bshClass.getDeclaredMethod("close", null);
            } catch (NoSuchMethodException e) {
                Log.e(TAG, "no BSH:isSupported method exists");
            }
            if (bshClose != null) {
                try {
                   bshClose.invoke(proxy, null);
                } catch(IllegalAccessException e) {
                   Log.e(TAG, "bshClose IllegalAccessException");
                } catch (InvocationTargetException e) {
                   Log.e(TAG, "bshClose InvocationTargetException");
                }
            }
        }
        Log.d(TAG, "CloseBCProfile returns");
    }

    private boolean getBCProfile(Context context, BluetoothProfile.ServiceListener sl) {
        boolean ret = true;
        boolean isProfileSupported = false;
        Class<?> bshClass = null;
        Method bshSupported = null;
        Constructor bshCons = null;
        Object bshObj = null;
        try {
            bshClass = Class.forName("android.bluetooth.BluetoothSyncHelper");
        } catch (ClassNotFoundException ex) {
            Log.e(TAG, "no BSH: exists");
            bshClass = null;
        }
        if (bshClass != null) {
            Log.d(TAG, "Able to get BSH class handle");
            try {
                bshSupported =  bshClass.getDeclaredMethod("isSupported", null);
            } catch (NoSuchMethodException e) {
                Log.e(TAG, "no BSH:isSupported method exists: gdm");
            }
            try {
                bshCons =
                  bshClass.getDeclaredConstructor(
                    new Class[]{Context.class,
                        BluetoothProfile.ServiceListener.class});
            } catch (NoSuchMethodException ex) {
                Log.e(TAG, "bshCons: NoSuchMethodException: gdm" + ex);
            }
        }
        if (bshClass != null && bshSupported != null && bshCons != null) {
            try {
                isProfileSupported = (boolean)bshSupported.invoke(null, null);
            } catch(IllegalAccessException e) {
                Log.e(TAG, "BSH:isSupported IllegalAccessException");
            } catch (InvocationTargetException e) {
                Log.e(TAG, "BSH:isSupported InvocationTargetException");
            }
            if (isProfileSupported) {
                try {
                    bshObj = bshCons.newInstance(
                                       context, sl);
                } catch (InstantiationException ex) {
                    Log.e(TAG, "bshCons InstantiationException:" + ex);
                } catch (IllegalAccessException ex) {
                    Log.e(TAG, "bshCons InstantiationException:" + ex);
                } catch (InvocationTargetException ex) {
                    Log.e(TAG, "bshCons InvocationTargetException:" + ex);
                }
             }
        }
        if (bshObj == null) {
            ret = false;
        }
        Log.d(TAG, "getBCService returns" + ret);
        return ret;
    }

    /**
     * Get the profile proxy object associated with the profile.
     *
@@ -3077,6 +3163,8 @@ public final class BluetoothAdapter {
            return true;
        } else if (profile == BluetoothProfile.BROADCAST) {
            return getBroadcastProfile(context, listener);
        } else if (profile == BluetoothProfile.BC_PROFILE) {
            return getBCProfile(context, listener);
        } else if (profile == BluetoothProfile.HEARING_AID) {
            if (isHearingAidProfileSupported()) {
                BluetoothHearingAid hearingAid = new BluetoothHearingAid(context, listener, this);
@@ -3177,6 +3265,9 @@ public final class BluetoothAdapter {
            case BluetoothProfile.BROADCAST:
                closeBroadcastProfile(proxy);
                break;
            case BluetoothProfile.BC_PROFILE:
                closeBCProfile(proxy);
                break;
            case BluetoothProfile.HEARING_AID:
                BluetoothHearingAid hearingAid = (BluetoothHearingAid) proxy;
                hearingAid.close();
+20 −1
Original line number Diff line number Diff line
@@ -223,13 +223,32 @@ public interface BluetoothProfile {
     * @hide
     */
    public int BROADCAST = 24;

   /**
     * BC_PROFILE
     * @hide
     */
    public static final int BC_PROFILE = 25;

    /**
     * PC_PROFILE
     * @hide
     */
    public static final int PC_PROFILE = 26;

   /**
     * CC_SERVER
     * @hide
     */
    public static final int CC_SERVER = 27;

    /**
     * Max profile ID. This value should be updated whenever a new profile is added to match
     * the largest value assigned to a profile.
     *
     * @hide
     */
    int MAX_PROFILE_ID = 24;
    int MAX_PROFILE_ID = 27;

    /**
     * Default priority for devices that we try to auto-connect to and
+43 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2020, The Linux Foundation. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.
    * Neither the name of The Linux Foundation nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <!-- Bluetooth settings.  The user-visible string that is used whenever
     referring to BC profile. -->
    <string name="bluetooth_profile_bc">BC Profile</string>
    <!-- Bluetooth settings.  Connection options screen.
    The summary for the profile checkbox preference when BC is connected. -->
    <string name="bluetooth_bc_profile_summary_connected">Connected to BA server
    </string>
    <!-- Bluetooth settings.  Connection options screen.
    The summary for the BC checkbox preference that describes how checking it
    will set the BC profile as preferred. -->
    <string name="bluetooth_bc_profile_summary_use_for">Use for BA</string>
</resources>
+21 −0
Original line number Diff line number Diff line
@@ -42,6 +42,8 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.UUID;

/**
@@ -63,6 +65,8 @@ public class BluetoothEventManager {
    private final android.os.Handler mReceiverHandler;
    private final UserHandle mUserHandle;
    private final Context mContext;
    private final String ACT_BROADCAST_SOURCE_INFO =
          "android.bluetooth.BroadcastAudioSAManager.action.BROADCAST_SOURCE_INFO";

    interface Handler {
        void onReceive(Context context, Intent intent, BluetoothDevice device);
@@ -130,6 +134,23 @@ public class BluetoothEventManager {
        addHandler(BluetoothDevice.ACTION_ACL_CONNECTED, new AclStateChangedHandler());
        addHandler(BluetoothDevice.ACTION_ACL_DISCONNECTED, new AclStateChangedHandler());
        addHandler(BluetoothA2dp.ACTION_CODEC_CONFIG_CHANGED, new A2dpCodecConfigChangedHandler());
        Object sourceInfoHandler = null;
        try {
           Class<?> classSourceInfoHandler =
               Class.forName("com.android.settingslib.bluetooth.BroadcastSourceInfoHandler");
           Constructor ctor;
           ctor = classSourceInfoHandler.getDeclaredConstructor(
                      new Class[] {CachedBluetoothDeviceManager.class});
           sourceInfoHandler = ctor.newInstance(mDeviceManager);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
                  | InstantiationException | InvocationTargetException e) {
              e.printStackTrace();
        }
        if (sourceInfoHandler != null) {
           Log.d(TAG, "adding SourceInfo Handler");
           addHandler(ACT_BROADCAST_SOURCE_INFO,
                    (Handler)sourceInfoHandler);
        }

        registerAdapterIntentReceiver();
    }
+52 −2
Original line number Diff line number Diff line
@@ -51,6 +51,9 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

/**
 * CachedBluetoothDevice represents a remote Bluetooth device. It contains
@@ -149,6 +152,17 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
        mTwspBatteryLevel = -1;
    }

     CachedBluetoothDevice(CachedBluetoothDevice cachedDevice) {
        mContext = cachedDevice.mContext;
        mLocalAdapter = BluetoothAdapter.getDefaultAdapter();
        mProfileManager = cachedDevice.mProfileManager;
        mDevice = cachedDevice.mDevice;
        fillData();
        mHiSyncId = BluetoothHearingAid.HI_SYNC_ID_INVALID;
        mTwspBatteryState = -1;
        mTwspBatteryLevel = -1;
    }

    private void initDrawableCache() {
        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        int cacheSize = maxMemory / 8;
@@ -809,16 +823,52 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
        return new ArrayList<>(mProfiles);
    }

    public boolean isBASeeker() {
        if (mDevice == null) {
            Log.e(TAG, "isBASeeker: mDevice is null");
            return false;
        }
        boolean ret = false;
        Class<?> bCProfileClass = null;
        String BC_PROFILE_CLASS = "com.android.settingslib.bluetooth.BCProfile";
        Method baSeeker;
        try {
            bCProfileClass = Class.forName(BC_PROFILE_CLASS);
            baSeeker = bCProfileClass.getDeclaredMethod("isBASeeker", BluetoothDevice.class);
            ret = (boolean)baSeeker.invoke(null, mDevice);
        } catch (ClassNotFoundException | NoSuchMethodException
                 | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return ret;
    }

    public List<LocalBluetoothProfile> getConnectableProfiles() {
        List<LocalBluetoothProfile> connectableProfiles =
                new ArrayList<LocalBluetoothProfile>();
        Class<?> bCProfileClass = null;
        String BC_PROFILE_CLASS = "com.android.settingslib.bluetooth.BCProfile";
        try {
            bCProfileClass = Class.forName(BC_PROFILE_CLASS);
        } catch (ClassNotFoundException ex) {
            Log.e(TAG, "no BCProfileClass: exists");
            bCProfileClass = null;
        }
        synchronized (mProfileLock) {
            for (LocalBluetoothProfile profile : mProfiles) {
                if (bCProfileClass != null && bCProfileClass.isInstance(profile)) {
                    if (isBASeeker()) {
                        connectableProfiles.add(profile);
                    } else {
                        Log.d(TAG, "BC profile is not enabled for" + mDevice);
                    }
                } else {
                    if (profile.accessProfileEnabled()) {
                       connectableProfiles.add(profile);
                    }
                }
            }
        }
        return connectableProfiles;
    }

Loading