1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.bluetooth; 18 19 import android.annotation.ElapsedRealtimeLong; 20 import android.annotation.IntDef; 21 import android.annotation.NonNull; 22 import android.annotation.SystemApi; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 26 import java.lang.annotation.Retention; 27 import java.lang.annotation.RetentionPolicy; 28 import java.util.Collections; 29 import java.util.List; 30 31 /** 32 * Record of energy and activity information from controller and 33 * underlying bt stack state.Timestamp the record with system 34 * time. 35 * 36 * @hide 37 */ 38 @SystemApi(client = SystemApi.Client.PRIVILEGED_APPS) 39 public final class BluetoothActivityEnergyInfo implements Parcelable { 40 private final long mTimestamp; 41 private int mBluetoothStackState; 42 private long mControllerTxTimeMs; 43 private long mControllerRxTimeMs; 44 private long mControllerIdleTimeMs; 45 private long mControllerEnergyUsed; 46 private List<UidTraffic> mUidTraffic; 47 48 /** @hide */ 49 @IntDef(prefix = { "BT_STACK_STATE_" }, value = { 50 BT_STACK_STATE_INVALID, 51 BT_STACK_STATE_STATE_ACTIVE, 52 BT_STACK_STATE_STATE_SCANNING, 53 BT_STACK_STATE_STATE_IDLE 54 }) 55 @Retention(RetentionPolicy.SOURCE) 56 public @interface BluetoothStackState {} 57 58 public static final int BT_STACK_STATE_INVALID = 0; 59 public static final int BT_STACK_STATE_STATE_ACTIVE = 1; 60 public static final int BT_STACK_STATE_STATE_SCANNING = 2; 61 public static final int BT_STACK_STATE_STATE_IDLE = 3; 62 63 /** @hide */ BluetoothActivityEnergyInfo(long timestamp, int stackState, long txTime, long rxTime, long idleTime, long energyUsed)64 public BluetoothActivityEnergyInfo(long timestamp, int stackState, 65 long txTime, long rxTime, long idleTime, long energyUsed) { 66 mTimestamp = timestamp; 67 mBluetoothStackState = stackState; 68 mControllerTxTimeMs = txTime; 69 mControllerRxTimeMs = rxTime; 70 mControllerIdleTimeMs = idleTime; 71 mControllerEnergyUsed = energyUsed; 72 } 73 74 /** @hide */ BluetoothActivityEnergyInfo(Parcel in)75 private BluetoothActivityEnergyInfo(Parcel in) { 76 mTimestamp = in.readLong(); 77 mBluetoothStackState = in.readInt(); 78 mControllerTxTimeMs = in.readLong(); 79 mControllerRxTimeMs = in.readLong(); 80 mControllerIdleTimeMs = in.readLong(); 81 mControllerEnergyUsed = in.readLong(); 82 mUidTraffic = in.createTypedArrayList(UidTraffic.CREATOR); 83 } 84 85 /** @hide */ 86 @Override toString()87 public String toString() { 88 return "BluetoothActivityEnergyInfo{" 89 + " mTimestamp=" + mTimestamp 90 + " mBluetoothStackState=" + mBluetoothStackState 91 + " mControllerTxTimeMs=" + mControllerTxTimeMs 92 + " mControllerRxTimeMs=" + mControllerRxTimeMs 93 + " mControllerIdleTimeMs=" + mControllerIdleTimeMs 94 + " mControllerEnergyUsed=" + mControllerEnergyUsed 95 + " mUidTraffic=" + mUidTraffic 96 + " }"; 97 } 98 99 public static final @NonNull Parcelable.Creator<BluetoothActivityEnergyInfo> CREATOR = 100 new Parcelable.Creator<BluetoothActivityEnergyInfo>() { 101 public BluetoothActivityEnergyInfo createFromParcel(Parcel in) { 102 return new BluetoothActivityEnergyInfo(in); 103 } 104 105 public BluetoothActivityEnergyInfo[] newArray(int size) { 106 return new BluetoothActivityEnergyInfo[size]; 107 } 108 }; 109 110 /** @hide */ 111 @Override writeToParcel(Parcel out, int flags)112 public void writeToParcel(Parcel out, int flags) { 113 out.writeLong(mTimestamp); 114 out.writeInt(mBluetoothStackState); 115 out.writeLong(mControllerTxTimeMs); 116 out.writeLong(mControllerRxTimeMs); 117 out.writeLong(mControllerIdleTimeMs); 118 out.writeLong(mControllerEnergyUsed); 119 out.writeTypedList(mUidTraffic); 120 } 121 122 /** @hide */ 123 @Override describeContents()124 public int describeContents() { 125 return 0; 126 } 127 128 /** 129 * Get the Bluetooth stack state associated with the energy info. 130 * 131 * @return one of {@link #BluetoothStackState} states 132 */ 133 @BluetoothStackState getBluetoothStackState()134 public int getBluetoothStackState() { 135 return mBluetoothStackState; 136 } 137 138 /** 139 * @return tx time in ms 140 */ getControllerTxTimeMillis()141 public long getControllerTxTimeMillis() { 142 return mControllerTxTimeMs; 143 } 144 145 /** 146 * @return rx time in ms 147 */ getControllerRxTimeMillis()148 public long getControllerRxTimeMillis() { 149 return mControllerRxTimeMs; 150 } 151 152 /** 153 * @return idle time in ms 154 */ getControllerIdleTimeMillis()155 public long getControllerIdleTimeMillis() { 156 return mControllerIdleTimeMs; 157 } 158 159 /** 160 * Get the product of current (mA), voltage (V), and time (ms). 161 * 162 * @return energy used 163 */ getControllerEnergyUsed()164 public long getControllerEnergyUsed() { 165 return mControllerEnergyUsed; 166 } 167 168 /** 169 * @return timestamp (real time elapsed in milliseconds since boot) of record creation 170 */ getTimestampMillis()171 public @ElapsedRealtimeLong long getTimestampMillis() { 172 return mTimestamp; 173 } 174 175 /** 176 * Get the {@link List} of each application {@link android.bluetooth.UidTraffic}. 177 * 178 * @return current {@link List} of {@link android.bluetooth.UidTraffic} 179 */ getUidTraffic()180 public @NonNull List<UidTraffic> getUidTraffic() { 181 if (mUidTraffic == null) { 182 return Collections.emptyList(); 183 } 184 return mUidTraffic; 185 } 186 187 /** @hide */ setUidTraffic(List<UidTraffic> traffic)188 public void setUidTraffic(List<UidTraffic> traffic) { 189 mUidTraffic = traffic; 190 } 191 192 /** 193 * @return true if the record Tx time, Rx time, and Idle time are more than 0. 194 */ isValid()195 public boolean isValid() { 196 return ((mControllerTxTimeMs >= 0) && (mControllerRxTimeMs >= 0) 197 && (mControllerIdleTimeMs >= 0)); 198 } 199 } 200