-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathUpdateContext.java
More file actions
309 lines (260 loc) Β· 10.6 KB
/
UpdateContext.java
File metadata and controls
309 lines (260 loc) Β· 10.6 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package cn.reactnative.modules.update;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import com.facebook.react.ReactInstanceManager;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.io.File;
import java.io.IOException;
public class UpdateContext {
private Context context;
private File rootDir;
private Executor executor;
public static boolean DEBUG = true;
private static ReactInstanceManager mReactInstanceManager;
private static boolean isUsingBundleUrl = false;
// Singleton instance
private static UpdateContext sInstance;
private static final Object sLock = new Object();
public UpdateContext(Context context) {
this.context = context;
this.executor = Executors.newSingleThreadExecutor();
this.rootDir = new File(context.getFilesDir(), "_update");
if (!rootDir.exists()) {
rootDir.mkdir();
}
this.sp = context.getSharedPreferences("update", Context.MODE_PRIVATE);
String packageVersion = getPackageVersion();
String buildTime = getBuildTime();
String storedPackageVersion = this.sp.getString("packageVersion", null);
String storedBuildTime = this.sp.getString("buildTime", null);
SharedPreferences.Editor editor = this.sp.edit();
boolean packageVersionChanged = storedPackageVersion == null || !packageVersion.equals(storedPackageVersion);
boolean buildTimeChanged = storedBuildTime == null || !buildTime.equals(storedBuildTime);
if (packageVersionChanged || buildTimeChanged) {
// Execute cleanUp before clearing SharedPreferences to avoid race condition
this.cleanUp();
editor.clear();
editor.putString("packageVersion", packageVersion);
editor.putString("buildTime", buildTime);
editor.apply();
}
}
public String getRootDir() {
return rootDir.toString();
}
public String getPackageVersion() {
PackageManager pm = context.getPackageManager();
PackageInfo pi = null;
try {
pi = pm.getPackageInfo(context.getPackageName(), 0);
return pi.versionName;
} catch( PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
public String getBuildTime() {
return context.getString(R.string.pushy_build_time);
}
public boolean getIsUsingBundleUrl() {
return isUsingBundleUrl;
}
public interface DownloadFileListener {
void onDownloadCompleted(DownloadTaskParams params);
void onDownloadFailed(Throwable error);
}
public void downloadFullUpdate(String url, String hash, DownloadFileListener listener) {
DownloadTaskParams params = new DownloadTaskParams();
params.type = DownloadTaskParams.TASK_TYPE_PATCH_FULL;
params.url = url;
params.hash = hash;
params.listener = listener;
params.targetFile = new File(rootDir, hash + ".ppk");
params.unzipDirectory = new File(rootDir, hash);
new DownloadTask(context).executeOnExecutor(this.executor, params);
}
public void downloadFile(String url, String hash, String fileName, DownloadFileListener listener) {
DownloadTaskParams params = new DownloadTaskParams();
params.type = DownloadTaskParams.TASK_TYPE_PLAIN_DOWNLOAD;
params.url = url;
params.hash = hash;
params.listener = listener;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N && fileName.equals("update.apk")) {
params.targetFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "pushy_update.apk");
} else {
params.targetFile = new File(rootDir, fileName);
try {
SafeZipFile.validatePath(params.targetFile, rootDir);
} catch (IOException e) {
throw new SecurityException("Illegal fileName: " + fileName);
}
}
// params.unzipDirectory = new File(rootDir, hash);
new DownloadTask(context).executeOnExecutor(this.executor, params);
}
public void downloadPatchFromApk(String url, String hash, DownloadFileListener listener) {
DownloadTaskParams params = new DownloadTaskParams();
params.type = DownloadTaskParams.TASK_TYPE_PATCH_FROM_APK;
params.url = url;
params.hash = hash;
params.listener = listener;
params.targetFile = new File(rootDir, hash + ".apk.patch");
params.unzipDirectory = new File(rootDir, hash);
new DownloadTask(context).executeOnExecutor(this.executor, params);
}
public void downloadPatchFromPpk(String url, String hash, String originHash, DownloadFileListener listener) {
DownloadTaskParams params = new DownloadTaskParams();
params.type = DownloadTaskParams.TASK_TYPE_PATCH_FROM_PPK;
params.url = url;
params.hash = hash;
params.originHash = originHash;
params.listener = listener;
params.targetFile = new File(rootDir, originHash + "-" + hash + ".ppk.patch");
params.unzipDirectory = new File(rootDir, hash);
params.originDirectory = new File(rootDir, originHash);
new DownloadTask(context).executeOnExecutor(this.executor, params);
}
private SharedPreferences sp;
public void switchVersion(String hash) {
if (!new File(rootDir, hash+"/index.bundlejs").exists()) {
throw new Error("Bundle version " + hash + " not found.");
}
String lastVersion = getCurrentVersion();
SharedPreferences.Editor editor = sp.edit();
editor.putString("currentVersion", hash);
if (lastVersion != null && !lastVersion.equals(hash)) {
editor.putString("lastVersion", lastVersion);
}
editor.putBoolean("firstTime", true);
editor.putBoolean("firstTimeOk", false);
editor.putString("rolledBackVersion", null);
editor.apply();
}
public void setKv(String key, String value) {
SharedPreferences.Editor editor = sp.edit();
editor.putString(key, value);
editor.apply();
}
public String getKv(String key) {
return sp.getString(key, null);
}
public String getCurrentVersion() {
return sp.getString("currentVersion", null);
}
public boolean isFirstTime() {
return sp.getBoolean("firstTime", false);
}
public String rolledBackVersion() {
return sp.getString("rolledBackVersion", null);
}
public void markSuccess() {
if (!BuildConfig.DEBUG) {
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("firstTimeOk", true);
String lastVersion = sp.getString("lastVersion", null);
String curVersion = sp.getString("currentVersion", null);
if (lastVersion != null && !lastVersion.equals(curVersion)) {
editor.remove("lastVersion");
editor.remove("hash_" + lastVersion);
}
editor.apply();
this.cleanUp();
}
}
public void clearFirstTime() {
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("firstTime", false);
editor.apply();
this.cleanUp();
}
public void clearRollbackMark() {
SharedPreferences.Editor editor = sp.edit();
editor.putString("rolledBackVersion", null);
editor.apply();
this.cleanUp();
}
public static void setCustomInstanceManager(ReactInstanceManager instanceManager) {
mReactInstanceManager = instanceManager;
}
public ReactInstanceManager getCustomReactInstanceManager() {
return mReactInstanceManager;
}
/**
* Get singleton instance of UpdateContext
*/
public static UpdateContext getInstance(Context context) {
if (sInstance == null) {
synchronized (sLock) {
if (sInstance == null) {
sInstance = new UpdateContext(context.getApplicationContext());
}
}
}
return sInstance;
}
public static String getBundleUrl(Context context) {
return getInstance(context).getBundleUrl();
}
public static String getBundleUrl(Context context, String defaultAssetsUrl) {
return getInstance(context).getBundleUrl(defaultAssetsUrl);
}
public String getBundleUrl() {
return this.getBundleUrl((String) null);
}
public String getBundleUrl(String defaultAssetsUrl) {
isUsingBundleUrl = true;
String currentVersion = getCurrentVersion();
if (currentVersion == null) {
return defaultAssetsUrl;
}
// Test should rollback.
if (!sp.getBoolean("firstTime", false)) {
if (!sp.getBoolean("firstTimeOk", true)) {
// Not firstTime, but not ok, so we roll back.
currentVersion = this.rollBack();
}
}
while (currentVersion != null) {
File bundleFile = new File(rootDir, currentVersion+"/index.bundlejs");
if (!bundleFile.exists()) {
Log.e("getBundleUrl", "Bundle version " + currentVersion + " not found.");
currentVersion = this.rollBack();
continue;
}
return bundleFile.toString();
}
return defaultAssetsUrl;
}
private String rollBack() {
String lastVersion = sp.getString("lastVersion", null);
String currentVersion = sp.getString("currentVersion", null);
SharedPreferences.Editor editor = sp.edit();
if (lastVersion == null) {
editor.remove("currentVersion");
} else {
editor.remove("lastVersion");
editor.putString("currentVersion", lastVersion);
}
editor.putBoolean("firstTimeOk", true);
editor.putBoolean("firstTime", false);
editor.putString("rolledBackVersion", currentVersion);
editor.apply();
return lastVersion;
}
private void cleanUp() {
DownloadTaskParams params = new DownloadTaskParams();
params.type = DownloadTaskParams.TASK_TYPE_CLEANUP;
params.hash = sp.getString("currentVersion", null);
params.originHash = sp.getString("lastVersion", null);
params.unzipDirectory = rootDir;
new DownloadTask(context).executeOnExecutor(this.executor, params);
}
}