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

Commit d8526d67 authored by Adam Lesinski's avatar Adam Lesinski
Browse files

Fix race in reportActivityInfo causing NPE

Bug:27162966
Change-Id: Id43898440824c1bbb24cef5a23a3f2a4fcde0534
parent 1fafcd99
Loading
Loading
Loading
Loading
+56 −49
Original line number Diff line number Diff line
@@ -102,6 +102,7 @@ public class AdapterService extends Service {
    //For Debugging only
    private static int sRefCount=0;

    private final Object mEnergyInfoLock = new Object();
    private int mStackReportedState;
    private int mTxTimeTotalMs;
    private int mRxTimeTotalMs;
@@ -2024,8 +2025,10 @@ public class AdapterService extends Service {

    private BluetoothActivityEnergyInfo reportActivityInfo() {
        enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, "Need BLUETOOTH permission");
        BluetoothActivityEnergyInfo info =
            new BluetoothActivityEnergyInfo(SystemClock.elapsedRealtime(), mStackReportedState,
        synchronized (mEnergyInfoLock) {
            final BluetoothActivityEnergyInfo info = new BluetoothActivityEnergyInfo(
                    SystemClock.elapsedRealtime(),
                    mStackReportedState,
                    mTxTimeTotalMs, mRxTimeTotalMs, mIdleTimeTotalMs,
                    mEnergyUsedTotalVoltAmpSecMicro);

@@ -2061,6 +2064,7 @@ public class AdapterService extends Service {
            mEnergyUsedTotalVoltAmpSecMicro = 0;
            return info;
        }
    }

    public int getTotalNumOfTrackableAdvertisements() {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
@@ -2171,17 +2175,19 @@ public class AdapterService extends Service {
            throws RemoteException {
        if (ctrl_state >= BluetoothActivityEnergyInfo.BT_STACK_STATE_INVALID &&
                ctrl_state <= BluetoothActivityEnergyInfo.BT_STACK_STATE_STATE_IDLE) {
            mStackReportedState = ctrl_state;
            mTxTimeTotalMs += tx_time;
            mRxTimeTotalMs += rx_time;
            mIdleTimeTotalMs += idle_time;
            // Energy is product of mA, V and ms. If the chipset doesn't
            // report it, we have to compute it from time
            if (energy_used == 0) {
                energy_used = (long)((mTxTimeTotalMs * getTxCurrentMa()
                    + mRxTimeTotalMs * getRxCurrentMa()
                    + mIdleTimeTotalMs * getIdleCurrentMa()) * getOperatingVolt());
                energy_used = (long)((tx_time * getTxCurrentMa()
                        + rx_time * getRxCurrentMa()
                        + idle_time * getIdleCurrentMa()) * getOperatingVolt());
            }

            synchronized (mEnergyInfoLock) {
                mStackReportedState = ctrl_state;
                mTxTimeTotalMs += tx_time;
                mRxTimeTotalMs += rx_time;
                mIdleTimeTotalMs += idle_time;
                mEnergyUsedTotalVoltAmpSecMicro += energy_used;

                for (UidTraffic traffic : data) {
@@ -2194,6 +2200,7 @@ public class AdapterService extends Service {
                    }
                }
            }
        }

        debugLog("energyInfoCallback() status = " + status +
                "tx_time = " + tx_time + "rx_time = " + rx_time +