diff options
6 files changed, 106 insertions, 33 deletions
diff --git a/core/api/system-current.txt b/core/api/system-current.txt index 13f7cfb94124..d085b0cfd862 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -1935,6 +1935,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"; } + 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 { 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); @@ -2234,6 +2250,13 @@ package android.bluetooth { 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 { diff --git a/core/java/android/bluetooth/BluetoothActivityEnergyInfo.java b/core/java/android/bluetooth/BluetoothActivityEnergyInfo.java index df065bf1bf8d..f371c6d78221 100644 --- a/core/java/android/bluetooth/BluetoothActivityEnergyInfo.java +++ b/core/java/android/bluetooth/BluetoothActivityEnergyInfo.java @@ -16,18 +16,25 @@ package android.bluetooth; +import android.annotation.IntDef; +import android.annotation.NonNull; +import android.annotation.SystemApi; import android.os.Parcel; 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 * underlying bt stack state.Timestamp the record with system - * time + * time. * * @hide */ +@SystemApi(client = SystemApi.Client.PRIVILEGED_APPS) public final class BluetoothActivityEnergyInfo implements Parcelable { private final long mTimestamp; private int mBluetoothStackState; @@ -35,13 +42,24 @@ public final class BluetoothActivityEnergyInfo implements Parcelable { private long mControllerRxTimeMs; private long mControllerIdleTimeMs; 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_STATE_ACTIVE = 1; public static final int BT_STACK_STATE_STATE_SCANNING = 2; public static final int BT_STACK_STATE_STATE_IDLE = 3; + /** @hide */ public BluetoothActivityEnergyInfo(long timestamp, int stackState, long txTime, long rxTime, long idleTime, long energyUsed) { mTimestamp = timestamp; @@ -52,17 +70,18 @@ public final class BluetoothActivityEnergyInfo implements Parcelable { mControllerEnergyUsed = energyUsed; } - @SuppressWarnings("unchecked") - BluetoothActivityEnergyInfo(Parcel in) { + /** @hide */ + private BluetoothActivityEnergyInfo(Parcel in) { mTimestamp = in.readLong(); mBluetoothStackState = in.readInt(); mControllerTxTimeMs = in.readLong(); mControllerRxTimeMs = in.readLong(); mControllerIdleTimeMs = in.readLong(); mControllerEnergyUsed = in.readLong(); - mUidTraffic = in.createTypedArray(UidTraffic.CREATOR); + mUidTraffic = in.createTypedArrayList(UidTraffic.CREATOR); } + /** @hide */ @Override public String toString() { return "BluetoothActivityEnergyInfo{" @@ -72,11 +91,11 @@ public final class BluetoothActivityEnergyInfo implements Parcelable { + " mControllerRxTimeMs=" + mControllerRxTimeMs + " mControllerIdleTimeMs=" + mControllerIdleTimeMs + " 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>() { public BluetoothActivityEnergyInfo createFromParcel(Parcel in) { return new BluetoothActivityEnergyInfo(in); @@ -87,9 +106,8 @@ public final class BluetoothActivityEnergyInfo implements Parcelable { } }; - + /** @hide */ @Override - @SuppressWarnings("unchecked") public void writeToParcel(Parcel out, int flags) { out.writeLong(mTimestamp); out.writeInt(mBluetoothStackState); @@ -97,17 +115,21 @@ public final class BluetoothActivityEnergyInfo implements Parcelable { out.writeLong(mControllerRxTimeMs); out.writeLong(mControllerIdleTimeMs); out.writeLong(mControllerEnergyUsed); - out.writeTypedArray(mUidTraffic, flags); + out.writeTypedList(mUidTraffic); } + /** @hide */ @Override public int describeContents() { 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() { 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 */ @@ -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() { 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; } - public void setUidTraffic(UidTraffic[] traffic) { + /** @hide */ + public void setUidTraffic(List<UidTraffic> traffic) { mUidTraffic = traffic; } /** - * @return if the record is valid + * @return true if the record is valid */ public boolean isValid() { return ((mControllerTxTimeMs >= 0) && (mControllerRxTimeMs >= 0) diff --git a/core/java/android/bluetooth/UidTraffic.java b/core/java/android/bluetooth/UidTraffic.java index 2ee786a59091..9982fa6121e4 100644 --- a/core/java/android/bluetooth/UidTraffic.java +++ b/core/java/android/bluetooth/UidTraffic.java @@ -15,6 +15,7 @@ */ package android.bluetooth; +import android.annotation.SystemApi; import android.os.Parcel; import android.os.Parcelable; @@ -23,27 +24,27 @@ import android.os.Parcelable; * * @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 long mRxBytes; private long mTxBytes; - public UidTraffic(int appUid) { - mAppUid = appUid; - } - + /** @hide */ public UidTraffic(int appUid, long rx, long tx) { mAppUid = appUid; mRxBytes = rx; mTxBytes = tx; } - UidTraffic(Parcel in) { + /** @hide */ + private UidTraffic(Parcel in) { mAppUid = in.readInt(); mRxBytes = in.readLong(); mTxBytes = in.readLong(); } + /** @hide */ @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(mAppUid); @@ -51,44 +52,60 @@ public class UidTraffic implements Cloneable, Parcelable { dest.writeLong(mTxBytes); } + /** @hide */ public void setRxBytes(long bytes) { mRxBytes = bytes; } + /** @hide */ public void setTxBytes(long bytes) { mTxBytes = bytes; } + /** @hide */ public void addRxBytes(long bytes) { mRxBytes += bytes; } + /** @hide */ public void addTxBytes(long bytes) { mTxBytes += bytes; } + /** + * @return corresponding app Uid + */ public int getUid() { return mAppUid; } + /** + * @return rx bytes count + */ public long getRxBytes() { return mRxBytes; } + /** + * @return tx bytes count + */ public long getTxBytes() { return mTxBytes; } + /** @hide */ @Override public int describeContents() { return 0; } + /** @hide */ @Override public UidTraffic clone() { return new UidTraffic(mAppUid, mRxBytes, mTxBytes); } + /** @hide */ @Override public String toString() { return "UidTraffic{mAppUid=" + mAppUid + ", mRxBytes=" + mRxBytes + ", mTxBytes=" diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java index 8c63f38494ea..630851d126cc 100644 --- a/core/java/com/android/internal/os/BatteryStatsImpl.java +++ b/core/java/com/android/internal/os/BatteryStatsImpl.java @@ -12143,7 +12143,7 @@ public class BatteryStatsImpl extends BatteryStats { rxTimeMs = info.getControllerRxTimeMillis(); txTimeMs = info.getControllerTxTimeMillis(); energy = info.getControllerEnergyUsed(); - if (info.getUidTraffic() != null) { + if (!info.getUidTraffic().isEmpty()) { for (UidTraffic traffic : info.getUidTraffic()) { uidRxBytes.put(traffic.getUid(), traffic.getRxBytes()); uidTxBytes.put(traffic.getUid(), traffic.getTxBytes()); @@ -12294,10 +12294,10 @@ public class BatteryStatsImpl extends BatteryStats { long totalTxBytes = 0; long totalRxBytes = 0; - final UidTraffic[] uidTraffic = info.getUidTraffic(); - final int numUids = uidTraffic != null ? uidTraffic.length : 0; + final List<UidTraffic> uidTraffic = info.getUidTraffic(); + final int numUids = uidTraffic.size(); 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( traffic.getUid()); final long txBytes = traffic.getTxBytes() - mLastBluetoothActivityInfo.uidTxBytes.get( @@ -12320,7 +12320,7 @@ public class BatteryStatsImpl extends BatteryStats { if ((totalTxBytes != 0 || totalRxBytes != 0) && (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) { for (int i = 0; i < numUids; i++) { - final UidTraffic traffic = uidTraffic[i]; + final UidTraffic traffic = uidTraffic.get(i); final int uid = traffic.getUid(); final long rxBytes = traffic.getRxBytes() - mLastBluetoothActivityInfo.uidRxBytes.get(uid); diff --git a/core/tests/coretests/src/com/android/internal/os/BluetoothPowerCalculatorTest.java b/core/tests/coretests/src/com/android/internal/os/BluetoothPowerCalculatorTest.java index 5c8479406c03..d361da95a1b9 100644 --- a/core/tests/coretests/src/com/android/internal/os/BluetoothPowerCalculatorTest.java +++ b/core/tests/coretests/src/com/android/internal/os/BluetoothPowerCalculatorTest.java @@ -33,6 +33,8 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; +import java.util.ArrayList; + @RunWith(AndroidJUnit4.class) @SmallTest public class BluetoothPowerCalculatorTest { @@ -105,10 +107,10 @@ public class BluetoothPowerCalculatorTest { final BluetoothActivityEnergyInfo info = new BluetoothActivityEnergyInfo(1000, BluetoothActivityEnergyInfo.BT_STACK_STATE_STATE_ACTIVE, 7000, 5000, 0, reportedEnergyUc); - info.setUidTraffic(new UidTraffic[]{ - new UidTraffic(Process.BLUETOOTH_UID, 1000, 2000), - new UidTraffic(APP_UID, 3000, 4000) - }); + info.setUidTraffic(new ArrayList<UidTraffic>(){{ + add(new UidTraffic(Process.BLUETOOTH_UID, 1000, 2000)); + add(new UidTraffic(APP_UID, 3000, 4000)); + }}); mStatsRule.getBatteryStats().updateBluetoothStateLocked(info, consumedEnergyUc, 1000, 1000); } diff --git a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java index 61770ea1c1c2..ba3123431b3c 100644 --- a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java +++ b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java @@ -1601,7 +1601,7 @@ public class StatsPullAtomService extends SystemService { int pullBluetoothBytesTransferLocked(int atomTag, List<StatsEvent> pulledData) { BluetoothActivityEnergyInfo info = fetchBluetoothData(); - if (info == null || info.getUidTraffic() == null) { + if (info == null) { return StatsManager.PULL_SKIP; } for (UidTraffic traffic : info.getUidTraffic()) { |