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

Commit 01eb71be authored by Rahul Sabnis's avatar Rahul Sabnis Committed by Automerger Merge Worker
Browse files

Merge "Adding @SystemApi annotation to Bluetooth energy stats." am: a12c77ab...

Merge "Adding @SystemApi annotation to Bluetooth energy stats." am: a12c77ab am: a125c31d am: f91171d3 am: 6f74b5dd

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1854179

Change-Id: I9e271a266c640fb739a5c394d0f5d091803143f4
parents bedc0626 6f74b5dd
Loading
Loading
Loading
Loading
+23 −0
Original line number Original line Diff line number Diff line
@@ -1941,6 +1941,22 @@ package android.bluetooth {
    field @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public static final String ACTION_CONNECTION_STATE_CHANGED = "android.bluetooth.a2dp-sink.profile.action.CONNECTION_STATE_CHANGED";
    field @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public static final String ACTION_CONNECTION_STATE_CHANGED = "android.bluetooth.a2dp-sink.profile.action.CONNECTION_STATE_CHANGED";
  }
  }
  public final class BluetoothActivityEnergyInfo implements android.os.Parcelable {
    method public int getBluetoothStackState();
    method public long getControllerEnergyUsed();
    method public long getControllerIdleTimeMillis();
    method public long getControllerRxTimeMillis();
    method public long getControllerTxTimeMillis();
    method public long getTimeStamp();
    method @NonNull public java.util.List<android.bluetooth.UidTraffic> getUidTraffic();
    method public boolean isValid();
    field public static final int BT_STACK_STATE_INVALID = 0; // 0x0
    field public static final int BT_STACK_STATE_STATE_ACTIVE = 1; // 0x1
    field public static final int BT_STACK_STATE_STATE_IDLE = 3; // 0x3
    field public static final int BT_STACK_STATE_STATE_SCANNING = 2; // 0x2
    field @NonNull public static final android.os.Parcelable.Creator<android.bluetooth.BluetoothActivityEnergyInfo> CREATOR;
  }
  public final class BluetoothAdapter {
  public final class BluetoothAdapter {
    method @RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT, android.Manifest.permission.BLUETOOTH_PRIVILEGED}) public boolean addOnMetadataChangedListener(@NonNull android.bluetooth.BluetoothDevice, @NonNull java.util.concurrent.Executor, @NonNull android.bluetooth.BluetoothAdapter.OnMetadataChangedListener);
    method @RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT, android.Manifest.permission.BLUETOOTH_PRIVILEGED}) public boolean addOnMetadataChangedListener(@NonNull android.bluetooth.BluetoothDevice, @NonNull java.util.concurrent.Executor, @NonNull android.bluetooth.BluetoothAdapter.OnMetadataChangedListener);
    method @RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT, android.Manifest.permission.BLUETOOTH_PRIVILEGED}) public boolean disable(boolean);
    method @RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT, android.Manifest.permission.BLUETOOTH_PRIVILEGED}) public boolean disable(boolean);
@@ -2240,6 +2256,13 @@ package android.bluetooth {
    method @NonNull public android.bluetooth.OobData.LeBuilder setRandomizerHash(@NonNull byte[]);
    method @NonNull public android.bluetooth.OobData.LeBuilder setRandomizerHash(@NonNull byte[]);
  }
  }
  public final class UidTraffic implements java.lang.Cloneable android.os.Parcelable {
    method public long getRxBytes();
    method public long getTxBytes();
    method public int getUid();
    field @NonNull public static final android.os.Parcelable.Creator<android.bluetooth.UidTraffic> CREATOR;
  }
}
}
package android.bluetooth.le {
package android.bluetooth.le {
+48 −17
Original line number Original line Diff line number Diff line
@@ -16,18 +16,25 @@


package android.bluetooth;
package android.bluetooth;


import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable;


import java.util.Arrays;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Collections;
import java.util.List;


/**
/**
 * Record of energy and activity information from controller and
 * Record of energy and activity information from controller and
 * underlying bt stack state.Timestamp the record with system
 * underlying bt stack state.Timestamp the record with system
 * time
 * time.
 *
 *
 * @hide
 * @hide
 */
 */
@SystemApi(client = SystemApi.Client.PRIVILEGED_APPS)
public final class BluetoothActivityEnergyInfo implements Parcelable {
public final class BluetoothActivityEnergyInfo implements Parcelable {
    private final long mTimestamp;
    private final long mTimestamp;
    private int mBluetoothStackState;
    private int mBluetoothStackState;
@@ -35,13 +42,24 @@ public final class BluetoothActivityEnergyInfo implements Parcelable {
    private long mControllerRxTimeMs;
    private long mControllerRxTimeMs;
    private long mControllerIdleTimeMs;
    private long mControllerIdleTimeMs;
    private long mControllerEnergyUsed;
    private long mControllerEnergyUsed;
    private UidTraffic[] mUidTraffic;
    private List<UidTraffic> mUidTraffic;

    /** @hide */
    @IntDef(prefix = { "BT_STACK_STATE_" }, value = {
            BT_STACK_STATE_INVALID,
            BT_STACK_STATE_STATE_ACTIVE,
            BT_STACK_STATE_STATE_SCANNING,
            BT_STACK_STATE_STATE_IDLE
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface BluetoothStackState {}


    public static final int BT_STACK_STATE_INVALID = 0;
    public static final int BT_STACK_STATE_INVALID = 0;
    public static final int BT_STACK_STATE_STATE_ACTIVE = 1;
    public static final int BT_STACK_STATE_STATE_ACTIVE = 1;
    public static final int BT_STACK_STATE_STATE_SCANNING = 2;
    public static final int BT_STACK_STATE_STATE_SCANNING = 2;
    public static final int BT_STACK_STATE_STATE_IDLE = 3;
    public static final int BT_STACK_STATE_STATE_IDLE = 3;


    /** @hide */
    public BluetoothActivityEnergyInfo(long timestamp, int stackState,
    public BluetoothActivityEnergyInfo(long timestamp, int stackState,
            long txTime, long rxTime, long idleTime, long energyUsed) {
            long txTime, long rxTime, long idleTime, long energyUsed) {
        mTimestamp = timestamp;
        mTimestamp = timestamp;
@@ -52,17 +70,18 @@ public final class BluetoothActivityEnergyInfo implements Parcelable {
        mControllerEnergyUsed = energyUsed;
        mControllerEnergyUsed = energyUsed;
    }
    }


    @SuppressWarnings("unchecked")
    /** @hide */
    BluetoothActivityEnergyInfo(Parcel in) {
    private BluetoothActivityEnergyInfo(Parcel in) {
        mTimestamp = in.readLong();
        mTimestamp = in.readLong();
        mBluetoothStackState = in.readInt();
        mBluetoothStackState = in.readInt();
        mControllerTxTimeMs = in.readLong();
        mControllerTxTimeMs = in.readLong();
        mControllerRxTimeMs = in.readLong();
        mControllerRxTimeMs = in.readLong();
        mControllerIdleTimeMs = in.readLong();
        mControllerIdleTimeMs = in.readLong();
        mControllerEnergyUsed = in.readLong();
        mControllerEnergyUsed = in.readLong();
        mUidTraffic = in.createTypedArray(UidTraffic.CREATOR);
        mUidTraffic = in.createTypedArrayList(UidTraffic.CREATOR);
    }
    }


    /** @hide */
    @Override
    @Override
    public String toString() {
    public String toString() {
        return "BluetoothActivityEnergyInfo{"
        return "BluetoothActivityEnergyInfo{"
@@ -72,11 +91,11 @@ public final class BluetoothActivityEnergyInfo implements Parcelable {
                + " mControllerRxTimeMs=" + mControllerRxTimeMs
                + " mControllerRxTimeMs=" + mControllerRxTimeMs
                + " mControllerIdleTimeMs=" + mControllerIdleTimeMs
                + " mControllerIdleTimeMs=" + mControllerIdleTimeMs
                + " mControllerEnergyUsed=" + mControllerEnergyUsed
                + " mControllerEnergyUsed=" + mControllerEnergyUsed
                + " mUidTraffic=" + Arrays.toString(mUidTraffic)
                + " mUidTraffic=" + mUidTraffic
                + " }";
                + " }";
    }
    }


    public static final @android.annotation.NonNull Parcelable.Creator<BluetoothActivityEnergyInfo> CREATOR =
    public static final @NonNull Parcelable.Creator<BluetoothActivityEnergyInfo> CREATOR =
            new Parcelable.Creator<BluetoothActivityEnergyInfo>() {
            new Parcelable.Creator<BluetoothActivityEnergyInfo>() {
                public BluetoothActivityEnergyInfo createFromParcel(Parcel in) {
                public BluetoothActivityEnergyInfo createFromParcel(Parcel in) {
                    return new BluetoothActivityEnergyInfo(in);
                    return new BluetoothActivityEnergyInfo(in);
@@ -87,9 +106,8 @@ public final class BluetoothActivityEnergyInfo implements Parcelable {
                }
                }
            };
            };



    /** @hide */
    @Override
    @Override
    @SuppressWarnings("unchecked")
    public void writeToParcel(Parcel out, int flags) {
    public void writeToParcel(Parcel out, int flags) {
        out.writeLong(mTimestamp);
        out.writeLong(mTimestamp);
        out.writeInt(mBluetoothStackState);
        out.writeInt(mBluetoothStackState);
@@ -97,17 +115,21 @@ public final class BluetoothActivityEnergyInfo implements Parcelable {
        out.writeLong(mControllerRxTimeMs);
        out.writeLong(mControllerRxTimeMs);
        out.writeLong(mControllerIdleTimeMs);
        out.writeLong(mControllerIdleTimeMs);
        out.writeLong(mControllerEnergyUsed);
        out.writeLong(mControllerEnergyUsed);
        out.writeTypedArray(mUidTraffic, flags);
        out.writeTypedList(mUidTraffic);
    }
    }


    /** @hide */
    @Override
    @Override
    public int describeContents() {
    public int describeContents() {
        return 0;
        return 0;
    }
    }


    /**
    /**
     * @return bt stack reported state
     * Get the Bluetooth stack state associated with the energy info.
     *
     * @return one of {@link #BluetoothStackState} states
     */
     */
    @BluetoothStackState
    public int getBluetoothStackState() {
    public int getBluetoothStackState() {
        return mBluetoothStackState;
        return mBluetoothStackState;
    }
    }
@@ -134,7 +156,7 @@ public final class BluetoothActivityEnergyInfo implements Parcelable {
    }
    }


    /**
    /**
     * product of current(mA), voltage(V) and time(ms)
     * Get the product of current (mA), voltage (V), and time (ms).
     *
     *
     * @return energy used
     * @return energy used
     */
     */
@@ -143,22 +165,31 @@ public final class BluetoothActivityEnergyInfo implements Parcelable {
    }
    }


    /**
    /**
     * @return timestamp(real time elapsed in milliseconds since boot) of record creation.
     * @return timestamp (real time elapsed in milliseconds since boot) of record creation
     */
     */
    public long getTimeStamp() {
    public long getTimeStamp() {
        return mTimestamp;
        return mTimestamp;
    }
    }


    public UidTraffic[] getUidTraffic() {
    /**
     * Get the {@link List} of each application {@link android.bluetooth.UidTraffic}.
     *
     * @return current {@link List} of {@link android.bluetooth.UidTraffic}
     */
    public @NonNull List<UidTraffic> getUidTraffic() {
        if (mUidTraffic == null) {
            return Collections.emptyList();
        }
        return mUidTraffic;
        return mUidTraffic;
    }
    }


    public void setUidTraffic(UidTraffic[] traffic) {
    /** @hide */
    public void setUidTraffic(List<UidTraffic> traffic) {
        mUidTraffic = traffic;
        mUidTraffic = traffic;
    }
    }


    /**
    /**
     * @return if the record is valid
     * @return true if the record is valid
     */
     */
    public boolean isValid() {
    public boolean isValid() {
        return ((mControllerTxTimeMs >= 0) && (mControllerRxTimeMs >= 0)
        return ((mControllerTxTimeMs >= 0) && (mControllerRxTimeMs >= 0)
+23 −6
Original line number Original line Diff line number Diff line
@@ -15,6 +15,7 @@
 */
 */
package android.bluetooth;
package android.bluetooth;


import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable;


@@ -23,27 +24,27 @@ import android.os.Parcelable;
 *
 *
 * @hide
 * @hide
 */
 */
public class UidTraffic implements Cloneable, Parcelable {
@SystemApi(client = SystemApi.Client.PRIVILEGED_APPS)
public final class UidTraffic implements Cloneable, Parcelable {
    private final int mAppUid;
    private final int mAppUid;
    private long mRxBytes;
    private long mRxBytes;
    private long mTxBytes;
    private long mTxBytes;


    public UidTraffic(int appUid) {
    /** @hide */
        mAppUid = appUid;
    }

    public UidTraffic(int appUid, long rx, long tx) {
    public UidTraffic(int appUid, long rx, long tx) {
        mAppUid = appUid;
        mAppUid = appUid;
        mRxBytes = rx;
        mRxBytes = rx;
        mTxBytes = tx;
        mTxBytes = tx;
    }
    }


    UidTraffic(Parcel in) {
    /** @hide */
    private UidTraffic(Parcel in) {
        mAppUid = in.readInt();
        mAppUid = in.readInt();
        mRxBytes = in.readLong();
        mRxBytes = in.readLong();
        mTxBytes = in.readLong();
        mTxBytes = in.readLong();
    }
    }


    /** @hide */
    @Override
    @Override
    public void writeToParcel(Parcel dest, int flags) {
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mAppUid);
        dest.writeInt(mAppUid);
@@ -51,44 +52,60 @@ public class UidTraffic implements Cloneable, Parcelable {
        dest.writeLong(mTxBytes);
        dest.writeLong(mTxBytes);
    }
    }


    /** @hide */
    public void setRxBytes(long bytes) {
    public void setRxBytes(long bytes) {
        mRxBytes = bytes;
        mRxBytes = bytes;
    }
    }


    /** @hide */
    public void setTxBytes(long bytes) {
    public void setTxBytes(long bytes) {
        mTxBytes = bytes;
        mTxBytes = bytes;
    }
    }


    /** @hide */
    public void addRxBytes(long bytes) {
    public void addRxBytes(long bytes) {
        mRxBytes += bytes;
        mRxBytes += bytes;
    }
    }


    /** @hide */
    public void addTxBytes(long bytes) {
    public void addTxBytes(long bytes) {
        mTxBytes += bytes;
        mTxBytes += bytes;
    }
    }


    /**
     * @return corresponding app Uid
     */
    public int getUid() {
    public int getUid() {
        return mAppUid;
        return mAppUid;
    }
    }


    /**
     * @return rx bytes count
     */
    public long getRxBytes() {
    public long getRxBytes() {
        return mRxBytes;
        return mRxBytes;
    }
    }


    /**
     * @return tx bytes count
     */
    public long getTxBytes() {
    public long getTxBytes() {
        return mTxBytes;
        return mTxBytes;
    }
    }


    /** @hide */
    @Override
    @Override
    public int describeContents() {
    public int describeContents() {
        return 0;
        return 0;
    }
    }


    /** @hide */
    @Override
    @Override
    public UidTraffic clone() {
    public UidTraffic clone() {
        return new UidTraffic(mAppUid, mRxBytes, mTxBytes);
        return new UidTraffic(mAppUid, mRxBytes, mTxBytes);
    }
    }


    /** @hide */
    @Override
    @Override
    public String toString() {
    public String toString() {
        return "UidTraffic{mAppUid=" + mAppUid + ", mRxBytes=" + mRxBytes + ", mTxBytes="
        return "UidTraffic{mAppUid=" + mAppUid + ", mRxBytes=" + mRxBytes + ", mTxBytes="
+5 −5
Original line number Original line Diff line number Diff line
@@ -12526,7 +12526,7 @@ public class BatteryStatsImpl extends BatteryStats {
            rxTimeMs = info.getControllerRxTimeMillis();
            rxTimeMs = info.getControllerRxTimeMillis();
            txTimeMs = info.getControllerTxTimeMillis();
            txTimeMs = info.getControllerTxTimeMillis();
            energy = info.getControllerEnergyUsed();
            energy = info.getControllerEnergyUsed();
            if (info.getUidTraffic() != null) {
            if (!info.getUidTraffic().isEmpty()) {
                for (UidTraffic traffic : info.getUidTraffic()) {
                for (UidTraffic traffic : info.getUidTraffic()) {
                    uidRxBytes.put(traffic.getUid(), traffic.getRxBytes());
                    uidRxBytes.put(traffic.getUid(), traffic.getRxBytes());
                    uidTxBytes.put(traffic.getUid(), traffic.getTxBytes());
                    uidTxBytes.put(traffic.getUid(), traffic.getTxBytes());
@@ -12677,10 +12677,10 @@ public class BatteryStatsImpl extends BatteryStats {
        long totalTxBytes = 0;
        long totalTxBytes = 0;
        long totalRxBytes = 0;
        long totalRxBytes = 0;
        final UidTraffic[] uidTraffic = info.getUidTraffic();
        final List<UidTraffic> uidTraffic = info.getUidTraffic();
        final int numUids = uidTraffic != null ? uidTraffic.length : 0;
        final int numUids = uidTraffic.size();
        for (int i = 0; i < numUids; i++) {
        for (int i = 0; i < numUids; i++) {
            final UidTraffic traffic = uidTraffic[i];
            final UidTraffic traffic = uidTraffic.get(i);
            final long rxBytes = traffic.getRxBytes() - mLastBluetoothActivityInfo.uidRxBytes.get(
            final long rxBytes = traffic.getRxBytes() - mLastBluetoothActivityInfo.uidRxBytes.get(
                    traffic.getUid());
                    traffic.getUid());
            final long txBytes = traffic.getTxBytes() - mLastBluetoothActivityInfo.uidTxBytes.get(
            final long txBytes = traffic.getTxBytes() - mLastBluetoothActivityInfo.uidTxBytes.get(
@@ -12703,7 +12703,7 @@ public class BatteryStatsImpl extends BatteryStats {
        if ((totalTxBytes != 0 || totalRxBytes != 0) && (leftOverRxTimeMs != 0
        if ((totalTxBytes != 0 || totalRxBytes != 0) && (leftOverRxTimeMs != 0
                || leftOverTxTimeMs != 0)) {
                || leftOverTxTimeMs != 0)) {
            for (int i = 0; i < numUids; i++) {
            for (int i = 0; i < numUids; i++) {
                final UidTraffic traffic = uidTraffic[i];
                final UidTraffic traffic = uidTraffic.get(i);
                final int uid = traffic.getUid();
                final int uid = traffic.getUid();
                final long rxBytes =
                final long rxBytes =
                        traffic.getRxBytes() - mLastBluetoothActivityInfo.uidRxBytes.get(uid);
                        traffic.getRxBytes() - mLastBluetoothActivityInfo.uidRxBytes.get(uid);
+6 −4
Original line number Original line Diff line number Diff line
@@ -33,6 +33,8 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;


import java.util.ArrayList;

@RunWith(AndroidJUnit4.class)
@RunWith(AndroidJUnit4.class)
@SmallTest
@SmallTest
public class BluetoothPowerCalculatorTest {
public class BluetoothPowerCalculatorTest {
@@ -105,10 +107,10 @@ public class BluetoothPowerCalculatorTest {
        final BluetoothActivityEnergyInfo info = new BluetoothActivityEnergyInfo(1000,
        final BluetoothActivityEnergyInfo info = new BluetoothActivityEnergyInfo(1000,
                BluetoothActivityEnergyInfo.BT_STACK_STATE_STATE_ACTIVE, 7000, 5000, 0,
                BluetoothActivityEnergyInfo.BT_STACK_STATE_STATE_ACTIVE, 7000, 5000, 0,
                reportedEnergyUc);
                reportedEnergyUc);
        info.setUidTraffic(new UidTraffic[]{
        info.setUidTraffic(new ArrayList<UidTraffic>(){{
                new UidTraffic(Process.BLUETOOTH_UID, 1000, 2000),
                add(new UidTraffic(Process.BLUETOOTH_UID, 1000, 2000));
                new UidTraffic(APP_UID, 3000, 4000)
                add(new UidTraffic(APP_UID, 3000, 4000));
        });
            }});
        mStatsRule.getBatteryStats().updateBluetoothStateLocked(info,
        mStatsRule.getBatteryStats().updateBluetoothStateLocked(info,
                consumedEnergyUc, 1000, 1000);
                consumedEnergyUc, 1000, 1000);
    }
    }
Loading