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

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

Bluetooth: TWSP: Support Battery Status information display



Reference changes to use the Battery status Intent and display
in Settings UI

 Conflicts:
	packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java

CRs-fixed: 2467643

Change-Id: Ibc81611529df1d1c8ff0453768eb494a8957b9c2
Signed-off-by: default avatarVolodymyr Zhdanov <wight554@gmail.com>
parent 624f769a
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
@@ -283,6 +283,41 @@ public final class BluetoothHeadset implements BluetoothProfile {
     */
    public static final int STATE_AUDIO_CONNECTED = 12;

    /**
     * Intent used to broadcast the Battery status of TWS+ devices
     *
     * <p>This intent will have 2 extras:
     * <ul>
     * <li> {@link #EXTRA_HF_TWSP_BATTERY_STATE} - Current Battey state of TWS+
     *      device. 0 for Discharging, 1 for Charging
     * <\li>
     * <li> {@link #EXTRA_HF_TWSP_BATTERY_LEVEL} - Current Battey charging level
     *      in percentage of TWS+ device.
     * <\li>
     *
     * @hide
     */
    public static final String ACTION_HF_TWSP_BATTERY_STATE_CHANGED =
            "android.bluetooth.headset.action.HF_TWSP_BATTERY_STATE_CHANGED";

    /**
     * A int extra field in {@link #EXTRA_HF_TWSP_BATTERY_STATE}
     * intents that contains the battery state of TWS+ device
     *
     * @hide
     */
    public static final String EXTRA_HF_TWSP_BATTERY_STATE =
            "android.bluetooth.headset.extra.HF_TWSP_BATTERY_STATE";

    /**
     * A int extra field in {@link #EXTRA_HF_TWSP_BATTERY_LEVEL}
     * intents that contains the value of battery level in percentage for TWS+ device
     * @hide
     */
    public static final String EXTRA_HF_TWSP_BATTERY_LEVEL =
            "android.bluetooth.headset.extra.HF_TWSP_BATTERY_LEVEL";


    /**
     * Intent used to broadcast the headset's indicator status
     *
+2 −0
Original line number Diff line number Diff line
@@ -172,6 +172,8 @@
        android:name="android.bluetooth.headset.action.HF_INDICATORS_VALUE_CHANGED" />
    <protected-broadcast
        android:name="android.bluetooth.headset.profile.action.ACTIVE_DEVICE_CHANGED" />
    <protected-broadcast
        android:name="android.bluetooth.headset.action.HF_TWSP_BATTERY_STATE_CHANGED" />
    <protected-broadcast
        android:name="android.bluetooth.headsetclient.profile.action.CONNECTION_STATE_CHANGED" />
    <protected-broadcast
+20 −0
Original line number Diff line number Diff line
@@ -108,6 +108,8 @@ public class BluetoothEventManager {
        addHandler(BluetoothDevice.ACTION_CLASS_CHANGED, new ClassChangedHandler());
        addHandler(BluetoothDevice.ACTION_UUID, new UuidChangedHandler());
        addHandler(BluetoothDevice.ACTION_BATTERY_LEVEL_CHANGED, new BatteryLevelChangedHandler());
        addHandler(BluetoothHeadset.ACTION_HF_TWSP_BATTERY_STATE_CHANGED ,
                  new TwspBatteryLevelChangedHandler());

        // Active device broadcasts
        addHandler(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED, new ActiveDeviceChangedHandler());
@@ -426,6 +428,24 @@ public class BluetoothEventManager {
        }
    }

    private class TwspBatteryLevelChangedHandler implements Handler {
        public void onReceive(Context context, Intent intent,
                BluetoothDevice device) {
            CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device);
            if (cachedDevice != null) {
                cachedDevice.mTwspBatteryState =
                          intent.getIntExtra(
                              BluetoothHeadset.EXTRA_HF_TWSP_BATTERY_STATE, -1);
                cachedDevice.mTwspBatteryLevel =
                          intent.getIntExtra(
                              BluetoothHeadset.EXTRA_HF_TWSP_BATTERY_LEVEL, -1);
                Log.i(TAG, cachedDevice + ": mTwspBatteryState: " + cachedDevice.mTwspBatteryState
                    + "mTwspBatteryLevel: " + cachedDevice.mTwspBatteryLevel);
                cachedDevice.refresh();
            }
        }
    }

    private class ActiveDeviceChangedHandler implements Handler {
        @Override
        public void onReceive(Context context, Intent intent, BluetoothDevice device) {
+29 −4
Original line number Diff line number Diff line
@@ -83,6 +83,8 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>

    private final Collection<Callback> mCallbacks = new CopyOnWriteArrayList<>();

    public int mTwspBatteryState;
    public int mTwspBatteryLevel;
    /**
     * Last time a bt profile auto-connect was attempted.
     * If an ACTION_UUID intent comes in within
@@ -132,6 +134,8 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
        mDevice = device;
        fillData();
        mHiSyncId = BluetoothHearingAid.HI_SYNC_ID_INVALID;
        mTwspBatteryState = -1;
        mTwspBatteryLevel = -1;
    }

    /* Gets Device for seondary TWS device
@@ -232,6 +236,10 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
                mProfiles.remove(profile);
                mRemovedProfiles.add(profile);
                mLocalNapRoleConnected = false;
            } else if (profile instanceof HeadsetProfile
                    && newProfileState == BluetoothProfile.STATE_DISCONNECTED) {
                mTwspBatteryState = -1;
                mTwspBatteryLevel = -1;
            }
        }

@@ -983,12 +991,29 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
        // BluetoothDevice.BATTERY_LEVEL_BLUETOOTH_OFF, or BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
        // any other value should be a framework bug. Thus assume here that if value is greater
        // than BluetoothDevice.BATTERY_LEVEL_UNKNOWN, it must be valid

        if (mDevice.isTwsPlusDevice() && mTwspBatteryState != -1 &&
           mTwspBatteryLevel != -1) {
            String s = "TWSP: ";
            String chargingState;
            if (mTwspBatteryState == 1) {
                chargingState = "Charging, ";
            } else {
                chargingState = "Discharging, ";
            }
            s = s.concat (chargingState);
            s = s.concat(
                 com.android.settingslib.Utils.formatPercentage(mTwspBatteryLevel));
            batteryLevelPercentageString = s;
            Log.i(TAG, "UI string" + batteryLevelPercentageString);
        } else {
            final int batteryLevel = getBatteryLevel();
            if (batteryLevel > BluetoothDevice.BATTERY_LEVEL_UNKNOWN) {
                // TODO: name com.android.settingslib.bluetooth.Utils something different
                batteryLevelPercentageString =
                    com.android.settingslib.Utils.formatPercentage(batteryLevel);
            }
        }

        int stringRes = R.string.bluetooth_pairing;
        //when profile is connected, information would be available
+3 −0
Original line number Diff line number Diff line
@@ -224,6 +224,9 @@ public class CachedBluetoothDeviceManager {
                    cachedDevice.setJustDiscovered(false);
                    mCachedDevices.remove(i);
                }
                //Clear if there any Tws battery info on BT turning OFF
                cachedDevice.mTwspBatteryState = -1;
                cachedDevice.mTwspBatteryLevel = -1;
            }
        }
    }