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

Commit 08f32b32 authored by pramod kotreshappa's avatar pramod kotreshappa Committed by Bruno Martins
Browse files

Add Broadcast profile id

Add broadcast profile to BluetoothProfile

CRs-fixed: 2856233
Change-Id: I17a4eb21ea9cc74a28349a8e0883ed2249838f3c
parent 2fbeeea1
Loading
Loading
Loading
Loading
+82 −0
Original line number Diff line number Diff line
@@ -84,6 +84,9 @@ import java.util.WeakHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Represents the local device Bluetooth adapter. The {@link BluetoothAdapter}
@@ -2004,6 +2007,22 @@ public final class BluetoothAdapter {
        return false;
    }

    /** @hide */
    @RequiresPermission(Manifest.permission.BLUETOOTH)
    public boolean isBroadcastActive() {
        try {
            mServiceLock.readLock().lock();
            if (mService != null) {
                return mService.isBroadcastActive();
            }
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        } finally {
            mServiceLock.readLock().unlock();
        }
        return false;
    }

    /**
     * Connects all enabled and supported bluetooth profiles between the local and remote device.
     * Connection is asynchronous and you should listen to each profile's broadcast intent
@@ -3056,6 +3075,8 @@ public final class BluetoothAdapter {
        } else if (profile == BluetoothProfile.HID_DEVICE) {
            BluetoothHidDevice hidDevice = new BluetoothHidDevice(context, listener, this);
            return true;
        } else if (profile == BluetoothProfile.BROADCAST) {
            return getBroadcastProfile(context, listener);
        } else if (profile == BluetoothProfile.HEARING_AID) {
            if (isHearingAidProfileSupported()) {
                BluetoothHearingAid hearingAid = new BluetoothHearingAid(context, listener, this);
@@ -3153,6 +3174,9 @@ public final class BluetoothAdapter {
                BluetoothHidDevice hidDevice = (BluetoothHidDevice) proxy;
                hidDevice.close();
                break;
            case BluetoothProfile.BROADCAST:
                closeBroadcastProfile(proxy);
                break;
            case BluetoothProfile.HEARING_AID:
                BluetoothHearingAid hearingAid = (BluetoothHearingAid) proxy;
                hearingAid.close();
@@ -3167,6 +3191,64 @@ public final class BluetoothAdapter {
        }
    }

    private boolean getBroadcastProfile(Context context,
                                      BluetoothProfile.ServiceListener listener) {
        boolean ret = true;
        Class<?> broadcastClass = null;
        Constructor bcastConstructor = null;
        Object broadcastObj = null;
        try {
            broadcastClass = Class.forName("android.bluetooth.BluetoothBroadcast");
        } catch (ClassNotFoundException ex) {
            Log.e(TAG, "no BluetoothBroadcast: exists");
        }
        if (broadcastClass != null) {
            try {
               bcastConstructor =
                            broadcastClass.getDeclaredConstructor(new Class[]{Context.class,
                                                  BluetoothProfile.ServiceListener.class});
            } catch (NoSuchMethodException ex) {
               Log.e(TAG, "bcastConstructor: NoSuchMethodException: gdm" + ex);
            }
        }
        if (bcastConstructor != null) {
            try {
                broadcastObj = bcastConstructor.newInstance(context, listener);
            } catch (InstantiationException | IllegalAccessException |
                InvocationTargetException ex) {
                ex.printStackTrace();
            }
        }
        if (broadcastObj == null) {
            return false;
        }
        return true;
    }

    private void closeBroadcastProfile(BluetoothProfile proxy) {
        Class<?> broadcastClass = null;
        Method broadcastClose = null;
        try {
            broadcastClass = Class.forName("android.bluetooth.BluetootBroadcast");
        } catch (ClassNotFoundException ex) {
            Log.e(TAG, "no BluetoothBroadcast: exists");
        }
        if (broadcastClass != null) {
            try {
                broadcastClose =  broadcastClass.getDeclaredMethod("close", null);
            } catch (NoSuchMethodException e) {
                Log.e(TAG, "no Broadcast:close method exists");
            }
            if (broadcastClose != null) {
                try {
                    broadcastClose.invoke(proxy, null);
                } catch(IllegalAccessException | InvocationTargetException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    private static final IBluetoothManagerCallback sManagerCallback =
            new IBluetoothManagerCallback.Stub() {
                public void onBluetoothServiceUp(IBluetooth bluetoothService) {
+8 −1
Original line number Diff line number Diff line
@@ -218,13 +218,18 @@ public interface BluetoothProfile {
     */
    public int GROUP_CLIENT = 23;

    /**
     * Broadcast
     * @hide
     */
    public int BROADCAST = 24;
    /**
     * 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 = 23;
    int MAX_PROFILE_ID = 24;

    /**
     * Default priority for devices that we try to auto-connect to and
@@ -422,6 +427,8 @@ public interface BluetoothProfile {
                return "OPP";
            case HEARING_AID:
                return "HEARING_AID";
            case BROADCAST:
                return "BROADCAST";
            default:
                return "UNKNOWN_PROFILE";
        }
+6 −1
Original line number Diff line number Diff line
@@ -83,7 +83,8 @@ public class BluetoothModeChangeHelper {

    @VisibleForTesting
    public boolean isA2dpOrHearingAidConnected() {
        return isA2dpConnected() || isHearingAidConnected();
        return isA2dpConnected() || isHearingAidConnected() ||
               isBroadcastActive();
    }

    @VisibleForTesting
@@ -142,4 +143,8 @@ public class BluetoothModeChangeHelper {
        }
        return hearingAid.getConnectedDevices().size() > 0;
    }

    private boolean isBroadcastActive() {
        return mAdapter.isBroadcastActive();
    }
}