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 underlying bt stack state.Timestamp 33 * the record with system time. 34 * 35 * @hide 36 */ 37 @SystemApi(client = SystemApi.Client.PRIVILEGED_APPS) 38 public final class BluetoothActivityEnergyInfo implements Parcelable { 39 private final long mTimestamp; 40 private int mBluetoothStackState; 41 private long mControllerTxTimeMs; 42 private long mControllerRxTimeMs; 43 private long mControllerIdleTimeMs; 44 private long mControllerEnergyUsed; 45 private List<UidTraffic> mUidTraffic; 46 47 /** @hide */ 48 @IntDef( 49 prefix = {"BT_STACK_STATE_"}, 50 value = { 51 BT_STACK_STATE_INVALID, 52 BT_STACK_STATE_STATE_ACTIVE, 53 BT_STACK_STATE_STATE_SCANNING, 54 BT_STACK_STATE_STATE_IDLE 55 }) 56 @Retention(RetentionPolicy.SOURCE) 57 public @interface BluetoothStackState {} 58 59 public static final int BT_STACK_STATE_INVALID = 0; 60 public static final int BT_STACK_STATE_STATE_ACTIVE = 1; 61 public static final int BT_STACK_STATE_STATE_SCANNING = 2; 62 public static final int BT_STACK_STATE_STATE_IDLE = 3; 63 64 /** @hide */ BluetoothActivityEnergyInfo( long timestamp, int stackState, long txTime, long rxTime, long idleTime, long energyUsed)65 public BluetoothActivityEnergyInfo( 66 long timestamp, 67 int stackState, 68 long txTime, 69 long rxTime, 70 long idleTime, 71 long energyUsed) { 72 mTimestamp = timestamp; 73 mBluetoothStackState = stackState; 74 mControllerTxTimeMs = txTime; 75 mControllerRxTimeMs = rxTime; 76 mControllerIdleTimeMs = idleTime; 77 mControllerEnergyUsed = energyUsed; 78 } 79 80 /** @hide */ BluetoothActivityEnergyInfo(Parcel in)81 private BluetoothActivityEnergyInfo(Parcel in) { 82 mTimestamp = in.readLong(); 83 mBluetoothStackState = in.readInt(); 84 mControllerTxTimeMs = in.readLong(); 85 mControllerRxTimeMs = in.readLong(); 86 mControllerIdleTimeMs = in.readLong(); 87 mControllerEnergyUsed = in.readLong(); 88 mUidTraffic = in.createTypedArrayList(UidTraffic.CREATOR); 89 } 90 91 /** @hide */ 92 @Override toString()93 public String toString() { 94 return "BluetoothActivityEnergyInfo{" 95 + " mTimestamp=" 96 + mTimestamp 97 + " mBluetoothStackState=" 98 + mBluetoothStackState 99 + " mControllerTxTimeMs=" 100 + mControllerTxTimeMs 101 + " mControllerRxTimeMs=" 102 + mControllerRxTimeMs 103 + " mControllerIdleTimeMs=" 104 + mControllerIdleTimeMs 105 + " mControllerEnergyUsed=" 106 + mControllerEnergyUsed 107 + " mUidTraffic=" 108 + mUidTraffic 109 + " }"; 110 } 111 112 public static final @NonNull Parcelable.Creator<BluetoothActivityEnergyInfo> CREATOR = 113 new Parcelable.Creator<BluetoothActivityEnergyInfo>() { 114 public BluetoothActivityEnergyInfo createFromParcel(Parcel in) { 115 return new BluetoothActivityEnergyInfo(in); 116 } 117 118 public BluetoothActivityEnergyInfo[] newArray(int size) { 119 return new BluetoothActivityEnergyInfo[size]; 120 } 121 }; 122 123 /** @hide */ 124 @Override writeToParcel(Parcel out, int flags)125 public void writeToParcel(Parcel out, int flags) { 126 out.writeLong(mTimestamp); 127 out.writeInt(mBluetoothStackState); 128 out.writeLong(mControllerTxTimeMs); 129 out.writeLong(mControllerRxTimeMs); 130 out.writeLong(mControllerIdleTimeMs); 131 out.writeLong(mControllerEnergyUsed); 132 out.writeTypedList(mUidTraffic); 133 } 134 135 /** @hide */ 136 @Override describeContents()137 public int describeContents() { 138 return 0; 139 } 140 141 /** 142 * Get the Bluetooth stack state associated with the energy info. 143 * 144 * @return one of {@link #BluetoothStackState} states 145 */ 146 @BluetoothStackState getBluetoothStackState()147 public int getBluetoothStackState() { 148 return mBluetoothStackState; 149 } 150 151 /** 152 * @return tx time in ms 153 */ getControllerTxTimeMillis()154 public long getControllerTxTimeMillis() { 155 return mControllerTxTimeMs; 156 } 157 158 /** 159 * @return rx time in ms 160 */ getControllerRxTimeMillis()161 public long getControllerRxTimeMillis() { 162 return mControllerRxTimeMs; 163 } 164 165 /** 166 * @return idle time in ms 167 */ getControllerIdleTimeMillis()168 public long getControllerIdleTimeMillis() { 169 return mControllerIdleTimeMs; 170 } 171 172 /** 173 * Get the product of current (mA), voltage (V), and time (ms). 174 * 175 * @return energy used 176 */ getControllerEnergyUsed()177 public long getControllerEnergyUsed() { 178 return mControllerEnergyUsed; 179 } 180 181 /** 182 * @return timestamp (real time elapsed in milliseconds since boot) of record creation 183 */ getTimestampMillis()184 public @ElapsedRealtimeLong long getTimestampMillis() { 185 return mTimestamp; 186 } 187 188 /** 189 * Get the {@link List} of each application {@link android.bluetooth.UidTraffic}. 190 * 191 * @return current {@link List} of {@link android.bluetooth.UidTraffic} 192 */ getUidTraffic()193 public @NonNull List<UidTraffic> getUidTraffic() { 194 if (mUidTraffic == null) { 195 return Collections.emptyList(); 196 } 197 return mUidTraffic; 198 } 199 200 /** @hide */ setUidTraffic(List<UidTraffic> traffic)201 public void setUidTraffic(List<UidTraffic> traffic) { 202 mUidTraffic = traffic; 203 } 204 205 /** 206 * @return true if the record Tx time, Rx time, and Idle time are more than 0. 207 */ isValid()208 public boolean isValid() { 209 return ((mControllerTxTimeMs >= 0) 210 && (mControllerRxTimeMs >= 0) 211 && (mControllerIdleTimeMs >= 0)); 212 } 213 } 214