| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 37 |
|
| 38 |
public final class SystemUpdateInfo implements Parcelable { |
| 39 |
private static final String TAG = "SystemUpdateInfo"; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public static final int SECURITY_PATCH_STATE_UNKNOWN = 0; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public static final int SECURITY_PATCH_STATE_FALSE = 1; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public static final int SECURITY_PATCH_STATE_TRUE = 2; |
| 55 |
|
| 56 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public long getReceivedTime() { |
| 104 |
return mReceivedTime; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 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 |
|
| 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 |
|
| 140 |
@Nullable |
| 141 |
public static SystemUpdateInfo readFromXml(TypedXmlPullParser parser) { |
| 142 |
|
| 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 |
} |