1 /* 2 * Copyright (C) 2021 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 com.android.car.telemetry.sessioncontroller; 18 19 import android.annotation.NonNull; 20 import android.os.PersistableBundle; 21 22 import com.android.car.telemetry.publisher.Constants; 23 24 import java.util.Objects; 25 26 /** 27 * {@link SessionAnnotation} is an immutable value class that encapsulates relevant information 28 * about session state change event. Two {@link SessionAnnotation} objects are equal if all their 29 * respective public fields are equal by value. 30 */ 31 public class SessionAnnotation { 32 public final int sessionId; 33 public final int sessionState; 34 public final long createdAtSinceBootMillis; // Milliseconds since boot. 35 public final long createdAtMillis; // Current time in milliseconds - unix time. 36 // to capture situations in later analysis when the data might be affected by a sudden reboot 37 // due to a crash. Populated from sys.boot.reason property. 38 public final String bootReason; 39 public final int bootCount; 40 SessionAnnotation( int sessionId, @SessionController.SessionControllerState int sessionState, long createdAtSinceBootMillis, long createdAtMillis, String bootReason, int bootCount)41 public SessionAnnotation( 42 int sessionId, 43 @SessionController.SessionControllerState int sessionState, 44 long createdAtSinceBootMillis, 45 long createdAtMillis, 46 String bootReason, 47 int bootCount) { 48 this.sessionId = sessionId; 49 this.sessionState = sessionState; 50 // including deep sleep, similar to SystemClock.elapsedRealtime() 51 this.createdAtSinceBootMillis = createdAtSinceBootMillis; 52 this.createdAtMillis = createdAtMillis; // unix time 53 this.bootReason = bootReason; 54 this.bootCount = bootCount; 55 } 56 57 @Override hashCode()58 public int hashCode() { 59 return Objects.hash(sessionId, sessionState, createdAtSinceBootMillis, createdAtMillis, 60 bootReason, bootCount); 61 } 62 63 @Override toString()64 public String toString() { 65 return new StringBuilder() 66 .append("{") 67 .append(Constants.ANNOTATION_BUNDLE_KEY_SESSION_ID).append(": ") 68 .append(sessionId).append(", ") 69 .append(Constants.ANNOTATION_BUNDLE_KEY_SESSION_STATE).append(": ") 70 .append(sessionState).append(", ") 71 .append(Constants.ANNOTATION_BUNDLE_KEY_CREATED_AT_SINCE_BOOT_MILLIS).append(": ") 72 .append(createdAtSinceBootMillis).append(", ") 73 .append(Constants.ANNOTATION_BUNDLE_KEY_CREATED_AT_MILLIS).append(": ") 74 .append(createdAtMillis).append(", ") 75 .append(Constants.ANNOTATION_BUNDLE_KEY_BOOT_REASON).append(": ") 76 .append(bootReason) 77 .append(Constants.ANNOTATION_BUNDLE_KEY_BOOT_COUNT).append(": ") 78 .append(bootCount) 79 .append("}") 80 .toString(); 81 } 82 83 /** 84 * Two SessionAnnotation objects are equal if all values of its public 85 * 86 * @param obj the reference object with which to compare. 87 */ 88 @Override equals(Object obj)89 public boolean equals(Object obj) { 90 if (obj == this) { 91 return true; 92 } 93 if (!(obj instanceof SessionAnnotation)) { 94 return false; 95 } 96 SessionAnnotation other = (SessionAnnotation) obj; 97 return sessionId == other.sessionId 98 && sessionState == other.sessionState 99 && createdAtSinceBootMillis == other.createdAtSinceBootMillis 100 && createdAtMillis == other.createdAtMillis 101 && Objects.equals(bootReason, other.bootReason) 102 && bootCount == other.bootCount; 103 } 104 105 /** 106 * Adds annotations to a provided {@link PersistableBundle} object. 107 * 108 * @param bundle A {@link PersistableBundle} that we want to get the annotations to. 109 */ addAnnotationsToBundle(@onNull PersistableBundle bundle)110 public void addAnnotationsToBundle(@NonNull PersistableBundle bundle) { 111 bundle.putInt(Constants.ANNOTATION_BUNDLE_KEY_SESSION_ID, sessionId); 112 bundle.putInt(Constants.ANNOTATION_BUNDLE_KEY_SESSION_STATE, sessionState); 113 bundle.putLong(Constants.ANNOTATION_BUNDLE_KEY_CREATED_AT_SINCE_BOOT_MILLIS, 114 createdAtSinceBootMillis); 115 bundle.putLong(Constants.ANNOTATION_BUNDLE_KEY_CREATED_AT_MILLIS, createdAtMillis); 116 bundle.putString(Constants.ANNOTATION_BUNDLE_KEY_BOOT_REASON, bootReason); 117 bundle.putInt(Constants.ANNOTATION_BUNDLE_KEY_BOOT_COUNT, bootCount); 118 } 119 120 } 121