1 /* 2 * Copyright (C) 2022 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.sdksandbox; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 import java.lang.annotation.Retention; 25 import java.lang.annotation.RetentionPolicy; 26 import java.util.Objects; 27 28 /** 29 * To be used to send latency information from sandbox to system system via callback 30 * 31 * @hide 32 */ 33 public final class SandboxLatencyInfo implements Parcelable { 34 @IntDef( 35 prefix = "SANDBOX_STATUS_", 36 value = { 37 SANDBOX_STATUS_SUCCESS, 38 SANDBOX_STATUS_FAILED_AT_SANDBOX, 39 SANDBOX_STATUS_FAILED_AT_SDK, 40 }) 41 @Retention(RetentionPolicy.SOURCE) 42 public @interface SandboxStatus {} 43 44 public static final int SANDBOX_STATUS_SUCCESS = 1; 45 public static final int SANDBOX_STATUS_FAILED_AT_SANDBOX = 2; 46 public static final int SANDBOX_STATUS_FAILED_AT_SDK = 3; 47 48 private final long mTimeSystemServerCalledSandbox; 49 private long mTimeSandboxReceivedCallFromSystemServer = -1; 50 private long mTimeSandboxCalledSdk = -1; 51 private long mTimeSdkCallCompleted = -1; 52 private long mTimeSandboxCalledSystemServer = -1; 53 private @SandboxStatus int mSandboxStatus = SANDBOX_STATUS_SUCCESS; 54 55 public static final @NonNull Parcelable.Creator<SandboxLatencyInfo> CREATOR = 56 new Parcelable.Creator<SandboxLatencyInfo>() { 57 public SandboxLatencyInfo createFromParcel(Parcel in) { 58 return new SandboxLatencyInfo(in); 59 } 60 61 public SandboxLatencyInfo[] newArray(int size) { 62 return new SandboxLatencyInfo[size]; 63 } 64 }; 65 SandboxLatencyInfo(long timeSystemServerCalledSandbox)66 public SandboxLatencyInfo(long timeSystemServerCalledSandbox) { 67 mTimeSystemServerCalledSandbox = timeSystemServerCalledSandbox; 68 } 69 SandboxLatencyInfo(Parcel in)70 private SandboxLatencyInfo(Parcel in) { 71 mTimeSystemServerCalledSandbox = in.readLong(); 72 mTimeSandboxReceivedCallFromSystemServer = in.readLong(); 73 mTimeSandboxCalledSdk = in.readLong(); 74 mTimeSdkCallCompleted = in.readLong(); 75 mTimeSandboxCalledSystemServer = in.readLong(); 76 mSandboxStatus = in.readInt(); 77 } 78 79 @Override equals(Object object)80 public boolean equals(Object object) { 81 if (this == object) return true; 82 if (!(object instanceof SandboxLatencyInfo)) return false; 83 SandboxLatencyInfo that = (SandboxLatencyInfo) object; 84 return mTimeSystemServerCalledSandbox == that.mTimeSystemServerCalledSandbox 85 && mTimeSandboxReceivedCallFromSystemServer 86 == that.mTimeSandboxReceivedCallFromSystemServer 87 && mTimeSandboxCalledSdk == that.mTimeSandboxCalledSdk 88 && mTimeSdkCallCompleted == that.mTimeSdkCallCompleted 89 && mTimeSandboxCalledSystemServer == that.mTimeSandboxCalledSystemServer 90 && mSandboxStatus == that.mSandboxStatus; 91 } 92 93 @Override hashCode()94 public int hashCode() { 95 return Objects.hash( 96 mTimeSystemServerCalledSandbox, 97 mTimeSandboxReceivedCallFromSystemServer, 98 mTimeSandboxCalledSdk, 99 mTimeSdkCallCompleted, 100 mTimeSandboxCalledSystemServer, 101 mSandboxStatus); 102 } 103 104 @Override writeToParcel(@onNull Parcel out, int flags)105 public void writeToParcel(@NonNull Parcel out, int flags) { 106 out.writeLong(mTimeSystemServerCalledSandbox); 107 out.writeLong(mTimeSandboxReceivedCallFromSystemServer); 108 out.writeLong(mTimeSandboxCalledSdk); 109 out.writeLong(mTimeSdkCallCompleted); 110 out.writeLong(mTimeSandboxCalledSystemServer); 111 out.writeInt(mSandboxStatus); 112 } 113 114 @Override describeContents()115 public int describeContents() { 116 return 0; 117 } 118 getTimeSystemServerCalledSandbox()119 public long getTimeSystemServerCalledSandbox() { 120 return mTimeSystemServerCalledSandbox; 121 } 122 setTimeSandboxReceivedCallFromSystemServer( long timeSandboxReceivedCallFromSystemServer)123 public void setTimeSandboxReceivedCallFromSystemServer( 124 long timeSandboxReceivedCallFromSystemServer) { 125 mTimeSandboxReceivedCallFromSystemServer = timeSandboxReceivedCallFromSystemServer; 126 } 127 getTimeSandboxCalledSdk()128 public long getTimeSandboxCalledSdk() { 129 return mTimeSandboxCalledSdk; 130 } 131 setTimeSandboxCalledSdk(long timeSandboxCalledSdk)132 public void setTimeSandboxCalledSdk(long timeSandboxCalledSdk) { 133 mTimeSandboxCalledSdk = timeSandboxCalledSdk; 134 } 135 setTimeSdkCallCompleted(long timeSdkCallCompleted)136 public void setTimeSdkCallCompleted(long timeSdkCallCompleted) { 137 mTimeSdkCallCompleted = timeSdkCallCompleted; 138 } 139 getTimeSandboxCalledSystemServer()140 public long getTimeSandboxCalledSystemServer() { 141 return mTimeSandboxCalledSystemServer; 142 } 143 setTimeSandboxCalledSystemServer(long timeSandboxCalledSystemServer)144 public void setTimeSandboxCalledSystemServer(long timeSandboxCalledSystemServer) { 145 mTimeSandboxCalledSystemServer = timeSandboxCalledSystemServer; 146 } 147 setSandboxStatus(@andboxStatus int sandboxStatus)148 public void setSandboxStatus(@SandboxStatus int sandboxStatus) { 149 mSandboxStatus = sandboxStatus; 150 } 151 getSandboxLatency()152 public int getSandboxLatency() { 153 int latencySandbox = 154 (int) (mTimeSandboxCalledSystemServer - mTimeSandboxReceivedCallFromSystemServer); 155 final int latencySdk = getSdkLatency(); 156 if (latencySdk != -1) { 157 latencySandbox -= latencySdk; 158 } 159 return latencySandbox; 160 } 161 getSdkLatency()162 public int getSdkLatency() { 163 if (mTimeSandboxCalledSdk != -1 && mTimeSdkCallCompleted != -1) { 164 return ((int) (mTimeSdkCallCompleted - mTimeSandboxCalledSdk)); 165 } 166 return -1; 167 } 168 getLatencySystemServerToSandbox()169 public int getLatencySystemServerToSandbox() { 170 return ((int) (mTimeSandboxReceivedCallFromSystemServer - mTimeSystemServerCalledSandbox)); 171 } 172 isSuccessfulAtSdk()173 public boolean isSuccessfulAtSdk() { 174 return mSandboxStatus != SANDBOX_STATUS_FAILED_AT_SDK; 175 } 176 isSuccessfulAtSandbox()177 public boolean isSuccessfulAtSandbox() { 178 return mSandboxStatus != SANDBOX_STATUS_FAILED_AT_SANDBOX; 179 } 180 } 181