forked from CyanogenMod/android_packages_apps_CMUpdater
/
UpdateInfo.java
246 lines (206 loc) · 6.12 KB
/
UpdateInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright (C) 2012 The CyanogenMod Project
*
* * Licensed under the GNU GPLv2 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl-2.0.txt
*/
package com.cyanogenmod.updater.misc;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import com.cyanogenmod.updater.utils.Utils;
import java.io.Serializable;
import java.util.Arrays;
public class UpdateInfo implements Parcelable, Serializable {
private static final long serialVersionUID = 5499890003569313403L;
public enum Type {
UNKNOWN,
STABLE,
RC,
SNAPSHOT,
NIGHTLY
};
private String mUiName;
private String mFileName;
private String mVersion;
private Type mType;
private long mBuildDate;
private String mDownloadUrl;
private String mMd5Sum;
private String mChangeLog;
private Boolean mIsNewerThanInstalled;
public UpdateInfo(String fileName, long date, String url,
String md5, Type type, String changeLog) {
initializeName(fileName);
mBuildDate = date;
mDownloadUrl = url;
mMd5Sum = md5;
mType = type;
mChangeLog = changeLog;
}
public UpdateInfo(String fileName, String changeLog) {
this(fileName, 0, null, null, Type.UNKNOWN, changeLog);
}
private UpdateInfo(Parcel in) {
readFromParcel(in);
}
/**
* Return changelog
*/
public String getChangeLog() {
return mChangeLog;
}
/**
* Get name for UI display
*/
public String getName() {
return mUiName;
}
/**
* Get version
*/
public String getVersion() {
return mVersion;
}
/**
* Get file name
*/
public String getFileName() {
return mFileName;
}
/**
* Get build type
*/
public Type getType() {
return mType;
}
/**
* Get MD5
*/
public String getMD5Sum() {
return mMd5Sum;
}
/**
* Get build date
*/
public long getDate() {
return mBuildDate;
}
/**
* Get download location
*/
public String getDownloadUrl() {
return mDownloadUrl;
}
public void setChangeLog(String changeLog) {
mChangeLog = changeLog;
}
public boolean isNewerThanInstalled() {
if (mIsNewerThanInstalled != null) {
return mIsNewerThanInstalled;
}
int[] installedVersion = canonicalizeVersion(Utils.getInstalledVersion(false));
int[] ourVersion = canonicalizeVersion(mVersion);
if (installedVersion.length < ourVersion.length) {
installedVersion = Arrays.copyOf(installedVersion, ourVersion.length);
} else if (installedVersion.length > ourVersion.length) {
ourVersion = Arrays.copyOf(ourVersion, installedVersion.length);
}
for (int i = 0; i < ourVersion.length; i++) {
if (ourVersion[i] > installedVersion[i]) {
mIsNewerThanInstalled = true;
break;
}
if (ourVersion[i] < installedVersion[i]) {
mIsNewerThanInstalled = false;
break;
}
}
if (mIsNewerThanInstalled == null) {
// Version strings match, so compare build dates.
mIsNewerThanInstalled = mBuildDate > Utils.getInstalledBuildDate();
}
return mIsNewerThanInstalled;
}
private int[] canonicalizeVersion(String versionString) {
String[] parts = versionString.split("\\.");
int[] version = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
try {
version[i] = Integer.valueOf(parts[i]);
} catch (NumberFormatException e) {
version[i] = 0;
}
}
return version;
}
private void initializeName(String fileName) {
mFileName = fileName;
if (!TextUtils.isEmpty(fileName)) {
mUiName = extractUiName(fileName);
mVersion = fileName.replaceAll(".*?([0-9.]+?)-.+","$1");
} else {
mUiName = null;
mVersion = null;
}
}
public static String extractUiName(String fileName) {
String deviceType = Utils.getDeviceType();
String uiName = fileName.replaceAll("\\.zip$", "");
return uiName.replaceAll("-" + deviceType + "-?", "");
}
@Override
public String toString() {
return "UpdateInfo: " + mFileName;
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof UpdateInfo)) {
return false;
}
UpdateInfo ui = (UpdateInfo) o;
return TextUtils.equals(mFileName, ui.mFileName)
&& mType.equals(ui.mType)
&& mBuildDate == ui.mBuildDate
&& TextUtils.equals(mDownloadUrl, ui.mDownloadUrl)
&& TextUtils.equals(mMd5Sum, ui.mMd5Sum);
}
public static final Parcelable.Creator<UpdateInfo> CREATOR = new Parcelable.Creator<UpdateInfo>() {
public UpdateInfo createFromParcel(Parcel in) {
return new UpdateInfo(in);
}
public UpdateInfo[] newArray(int size) {
return new UpdateInfo[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(mUiName);
out.writeString(mFileName);
out.writeString(mVersion);
out.writeString(mType.toString());
out.writeLong(mBuildDate);
out.writeString(mDownloadUrl);
out.writeString(mMd5Sum);
out.writeString(mChangeLog);
}
private void readFromParcel(Parcel in) {
mUiName = in.readString();
mFileName = in.readString();
mVersion = in.readString();
mType = Enum.valueOf(Type.class, in.readString());
mBuildDate = in.readLong();
mDownloadUrl = in.readString();
mMd5Sum = in.readString();
mChangeLog = in.readString();
}
}