| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view.displayhash; |
| 18 |
|
| 19 |
import android.annotation.CurrentTimeMillisLong; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.SystemApi; |
| 22 |
import android.graphics.Rect; |
| 23 |
import android.os.Parcel; |
| 24 |
import android.os.Parcelable; |
| 25 |
|
| 26 |
import com.android.internal.util.AnnotationValidations; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public final class DisplayHash implements Parcelable { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
@CurrentTimeMillisLong |
| 36 |
private final long mTimeMillis; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
@NonNull |
| 43 |
private final Rect mBoundsInWindow; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
@NonNull |
| 49 |
private final String mHashAlgorithm; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
@NonNull |
| 55 |
private final byte[] mImageHash; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
@NonNull |
| 62 |
private final byte[] mHmac; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
@SystemApi |
| 78 |
public DisplayHash(@CurrentTimeMillisLong long timeMillis, @NonNull Rect boundsInWindow, |
| 79 |
@NonNull String hashAlgorithm, @NonNull byte[] imageHash, @NonNull byte[] hmac) { |
| 80 |
mTimeMillis = timeMillis; |
| 81 |
mBoundsInWindow = boundsInWindow; |
| 82 |
AnnotationValidations.validate(NonNull.class, null, mBoundsInWindow); |
| 83 |
mHashAlgorithm = hashAlgorithm; |
| 84 |
AnnotationValidations.validate(NonNull.class, null, mHashAlgorithm); |
| 85 |
mImageHash = imageHash; |
| 86 |
AnnotationValidations.validate(NonNull.class, null, mImageHash); |
| 87 |
mHmac = hmac; |
| 88 |
AnnotationValidations.validate(NonNull.class, null, mHmac); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
@SystemApi |
| 97 |
@CurrentTimeMillisLong |
| 98 |
public long getTimeMillis() { |
| 99 |
return mTimeMillis; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
@SystemApi |
| 109 |
@NonNull |
| 110 |
public Rect getBoundsInWindow() { |
| 111 |
return mBoundsInWindow; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
@SystemApi |
| 120 |
@NonNull |
| 121 |
public String getHashAlgorithm() { |
| 122 |
return mHashAlgorithm; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
@SystemApi |
| 131 |
@NonNull |
| 132 |
public byte[] getImageHash() { |
| 133 |
return mImageHash; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
@SystemApi |
| 143 |
@NonNull |
| 144 |
public byte[] getHmac() { |
| 145 |
return mHmac; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
@Override |
| 150 |
public String toString() { |
| 151 |
return "DisplayHash { " |
| 152 |
+ "timeMillis = " + mTimeMillis + ", " |
| 153 |
+ "boundsInWindow = " + mBoundsInWindow + ", " |
| 154 |
+ "hashAlgorithm = " + mHashAlgorithm + ", " |
| 155 |
+ "imageHash = " + byteArrayToString(mImageHash) + ", " |
| 156 |
+ "hmac = " + byteArrayToString(mHmac) |
| 157 |
+ " }"; |
| 158 |
} |
| 159 |
|
| 160 |
private String byteArrayToString(byte[] byteArray) { |
| 161 |
if (byteArray == null) { |
| 162 |
return "null"; |
| 163 |
} |
| 164 |
int iMax = byteArray.length - 1; |
| 165 |
if (iMax == -1) { |
| 166 |
return "[]"; |
| 167 |
} |
| 168 |
|
| 169 |
StringBuilder b = new StringBuilder(); |
| 170 |
b.append('['); |
| 171 |
for (int i = 0; ; i++) { |
| 172 |
String formatted = String.format("%02X", byteArray[i] & 0xFF); |
| 173 |
b.append(formatted); |
| 174 |
if (i == iMax) { |
| 175 |
return b.append(']').toString(); |
| 176 |
} |
| 177 |
b.append(", "); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
@SystemApi |
| 183 |
@Override |
| 184 |
public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 185 |
dest.writeLong(mTimeMillis); |
| 186 |
dest.writeTypedObject(mBoundsInWindow, flags); |
| 187 |
dest.writeString(mHashAlgorithm); |
| 188 |
dest.writeByteArray(mImageHash); |
| 189 |
dest.writeByteArray(mHmac); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
@SystemApi |
| 194 |
@Override |
| 195 |
public int describeContents() { |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
private DisplayHash(@NonNull Parcel in) { |
| 200 |
mTimeMillis = in.readLong(); |
| 201 |
Rect boundsInWindow = in.readTypedObject(Rect.CREATOR); |
| 202 |
String hashAlgorithm = in.readString(); |
| 203 |
byte[] imageHash = in.createByteArray(); |
| 204 |
byte[] hmac = in.createByteArray(); |
| 205 |
|
| 206 |
mBoundsInWindow = boundsInWindow; |
| 207 |
AnnotationValidations.validate(NonNull.class, null, mBoundsInWindow); |
| 208 |
mHashAlgorithm = hashAlgorithm; |
| 209 |
AnnotationValidations.validate(NonNull.class, null, mHashAlgorithm); |
| 210 |
mImageHash = imageHash; |
| 211 |
AnnotationValidations.validate(NonNull.class, null, mImageHash); |
| 212 |
mHmac = hmac; |
| 213 |
AnnotationValidations.validate(NonNull.class, null, mHmac); |
| 214 |
} |
| 215 |
|
| 216 |
@NonNull |
| 217 |
public static final Parcelable.Creator<DisplayHash> CREATOR = |
| 218 |
new Parcelable.Creator<DisplayHash>() { |
| 219 |
@Override |
| 220 |
public DisplayHash[] newArray(int size) { |
| 221 |
return new DisplayHash[size]; |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public DisplayHash createFromParcel(@NonNull Parcel in) { |
| 226 |
return new DisplayHash(in); |
| 227 |
} |
| 228 |
}; |
| 229 |
} |