Debian Codegraph @

core/java/android/app/admin/SystemUpdateInfo.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 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.app.admin;
18
19 import android.annotation.IntDef;
20 import android.annotation.Nullable;
21 import android.os.Build;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 import android.util.Log;
25 import android.util.TypedXmlPullParser;
26 import android.util.TypedXmlSerializer;
27
28 import org.xmlpull.v1.XmlPullParserException;
29
30 import java.io.IOException;
31 import java.lang.annotation.Retention;
32 import java.lang.annotation.RetentionPolicy;
33 import java.util.Objects;
34
35 /**
36 * A class containing information about a pending system update.
37 */
38 public final class SystemUpdateInfo implements Parcelable {
39 private static final String TAG = "SystemUpdateInfo";
40
41 /**
42 * Represents it is unknown whether the system update is a security patch.
43 */
44 public static final int SECURITY_PATCH_STATE_UNKNOWN = 0;
45
46 /**
47 * Represents the system update is not a security patch.
48 */
49 public static final int SECURITY_PATCH_STATE_FALSE = 1;
50
51 /**
52 * Represents the system update is a security patch.
53 */
54 public static final int SECURITY_PATCH_STATE_TRUE = 2;
55
56 /** @hide */
57 @Retention(RetentionPolicy.SOURCE)
58 @IntDef(prefix = { "SECURITY_PATCH_STATE_" }, value = {
59 SECURITY_PATCH_STATE_FALSE,
60 SECURITY_PATCH_STATE_TRUE,
61 SECURITY_PATCH_STATE_UNKNOWN
62 })
63 public @interface SecurityPatchState {}
64
65 private static final String ATTR_RECEIVED_TIME = "received-time";
66 private static final String ATTR_SECURITY_PATCH_STATE = "security-patch-state";
67 // Tag used to store original build fingerprint to detect when the update is applied.
68 private static final String ATTR_ORIGINAL_BUILD = "original-build";
69
70 private final long mReceivedTime;
71 @SecurityPatchState
72 private final int mSecurityPatchState;
73
74 private SystemUpdateInfo(long receivedTime, @SecurityPatchState int securityPatchState) {
75 this.mReceivedTime = receivedTime;
76 this.mSecurityPatchState = securityPatchState;
77 }
78
79 private SystemUpdateInfo(Parcel in) {
80 mReceivedTime = in.readLong();
81 mSecurityPatchState = in.readInt();
82 }
83
84 /** @hide */
85 @Nullable
86 public static SystemUpdateInfo of(long receivedTime) {
87 return receivedTime == -1
88 ? null : new SystemUpdateInfo(receivedTime, SECURITY_PATCH_STATE_UNKNOWN);
89 }
90
91 /** @hide */
92 @Nullable
93 public static SystemUpdateInfo of(long receivedTime, boolean isSecurityPatch) {
94 return receivedTime == -1 ? null : new SystemUpdateInfo(receivedTime,
95 isSecurityPatch ? SECURITY_PATCH_STATE_TRUE : SECURITY_PATCH_STATE_FALSE);
96 }
97
98 /**
99 * Gets time when the update was first available in milliseconds since midnight, January 1,
100 * 1970 UTC.
101 * @return Time in milliseconds as given by {@link System#currentTimeMillis()}
102 */
103 public long getReceivedTime() {
104 return mReceivedTime;
105 }
106
107 /**
108 * Gets whether the update is a security patch.
109 * @return {@link #SECURITY_PATCH_STATE_FALSE}, {@link #SECURITY_PATCH_STATE_TRUE}, or
110 * {@link #SECURITY_PATCH_STATE_UNKNOWN}.
111 */
112 @SecurityPatchState
113 public int getSecurityPatchState() {
114 return mSecurityPatchState;
115 }
116
117 public static final @android.annotation.NonNull Creator<SystemUpdateInfo> CREATOR =
118 new Creator<SystemUpdateInfo>() {
119 @Override
120 public SystemUpdateInfo createFromParcel(Parcel in) {
121 return new SystemUpdateInfo(in);
122 }
123
124 @Override
125 public SystemUpdateInfo[] newArray(int size) {
126 return new SystemUpdateInfo[size];
127 }
128 };
129
130 /** @hide */
131 public void writeToXml(TypedXmlSerializer out, String tag) throws IOException {
132 out.startTag(null, tag);
133 out.attributeLong(null, ATTR_RECEIVED_TIME, mReceivedTime);
134 out.attributeInt(null, ATTR_SECURITY_PATCH_STATE, mSecurityPatchState);
135 out.attribute(null, ATTR_ORIGINAL_BUILD , Build.FINGERPRINT);
136 out.endTag(null, tag);
137 }
138
139 /** @hide */
140 @Nullable
141 public static SystemUpdateInfo readFromXml(TypedXmlPullParser parser) {
142 // If an OTA has been applied (build fingerprint has changed), discard stale info.
143 final String buildFingerprint = parser.getAttributeValue(null, ATTR_ORIGINAL_BUILD );
144 if (!Build.FINGERPRINT.equals(buildFingerprint)) {
145 return null;
146 }
147 try {
148 final long receivedTime =
149 parser.getAttributeLong(null, ATTR_RECEIVED_TIME);
150 final int securityPatchState =
151 parser.getAttributeInt(null, ATTR_SECURITY_PATCH_STATE);
152 return new SystemUpdateInfo(receivedTime, securityPatchState);
153 } catch (XmlPullParserException e) {
154 Log.w(TAG, "Load xml failed", e);
155 return null;
156 }
157 }
158
159 @Override
160 public int describeContents() {
161 return 0;
162 }
163
164 @Override
165 public void writeToParcel(Parcel dest, int flags) {
166 dest.writeLong(getReceivedTime());
167 dest.writeInt(getSecurityPatchState());
168 }
169
170 @Override
171 public String toString() {
172 return String.format("SystemUpdateInfo (receivedTime = %d, securityPatchState = %s)",
173 mReceivedTime, securityPatchStateToString(mSecurityPatchState));
174 }
175
176 private static String securityPatchStateToString(@SecurityPatchState int state) {
177 switch (state) {
178 case SECURITY_PATCH_STATE_FALSE:
179 return "false";
180 case SECURITY_PATCH_STATE_TRUE:
181 return "true";
182 case SECURITY_PATCH_STATE_UNKNOWN:
183 return "unknown";
184 default:
185 throw new IllegalArgumentException("Unrecognized security patch state: " + state);
186 }
187 }
188
189 @Override
190 public boolean equals(@Nullable Object o) {
191 if (this == o) return true;
192 if (o == null || getClass() != o.getClass()) return false;
193 SystemUpdateInfo that = (SystemUpdateInfo) o;
194 return mReceivedTime == that.mReceivedTime
195 && mSecurityPatchState == that.mSecurityPatchState;
196 }
197
198 @Override
199 public int hashCode() {
200 return Objects.hash(mReceivedTime, mSecurityPatchState);
201 }
202 }