-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathUpdateModuleImpl.java
More file actions
327 lines (279 loc) · 13.4 KB
/
UpdateModuleImpl.java
File metadata and controls
327 lines (279 loc) · 13.4 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package cn.reactnative.modules.update;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactDelegate;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.UiThreadUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;
public class UpdateModuleImpl {
public static final String NAME = "Pushy";
/**
* 获取字段的兼容性方法,尝试带m前缀和不带m前缀的字段名
* @param clazz 目标类
* @param fieldName 基础字段名(不带m前缀)
* @return 找到的字段对象
* @throws NoSuchFieldException 如果两种命名都找不到字段
*/
private static Field getCompatibleField(Class<?> clazz, String fieldName) throws NoSuchFieldException {
// 首先尝试带m前缀的字段名
try {
return clazz.getDeclaredField("m" + capitalize(fieldName));
} catch (NoSuchFieldException e) {
// 如果找不到带m前缀的,尝试不带m前缀的
try {
return clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException e2) {
// 如果都找不到,抛出异常并包含两种尝试的信息
throw new NoSuchFieldException("Field not found with either name: m" + capitalize(fieldName) + " or " + fieldName);
}
}
}
/**
* 首字母大写的辅助方法
*/
private static String capitalize(String str) {
if (str == null || str.length() == 0) {
return str;
}
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
public static void downloadFullUpdate(UpdateContext updateContext, final ReadableMap options, final Promise promise) {
String url = options.getString("updateUrl");
String hash = options.getString("hash");
updateContext.downloadFullUpdate(url, hash, new UpdateContext.DownloadFileListener() {
@Override
public void onDownloadCompleted(DownloadTaskParams params) {
promise.resolve(null);
}
@Override
public void onDownloadFailed(Throwable error) {
promise.reject(error);
}
});
}
public static void downloadAndInstallApk(UpdateContext updateContext, final ReadableMap options, final Promise promise) {
String url = options.getString("url");
String hash = options.getString("hash");
String target = options.getString("target");
updateContext.downloadFile(url, hash, target, new UpdateContext.DownloadFileListener() {
@Override
public void onDownloadCompleted(DownloadTaskParams params) {
UpdateModule.installApk(params.targetFile);
promise.resolve(null);
}
@Override
public void onDownloadFailed(Throwable error) {
promise.reject(error);
}
});
}
public static void installApk(String url) {
File toInstall = new File(url);
UpdateModule.installApk(toInstall);
}
public static void downloadPatchFromPackage(UpdateContext updateContext, final ReadableMap options, final Promise promise) {
String url = options.getString("updateUrl");
String hash = options.getString("hash");
updateContext.downloadPatchFromApk(url, hash, new UpdateContext.DownloadFileListener() {
@Override
public void onDownloadCompleted(DownloadTaskParams params) {
promise.resolve(null);
}
@Override
public void onDownloadFailed(Throwable error) {
promise.reject(error);
}
});
}
public static void downloadPatchFromPpk(UpdateContext updateContext, final ReadableMap options, final Promise promise) {
try {
String url = options.getString("updateUrl");
String hash = options.getString("hash");
String originHash = options.getString("originHash");
updateContext.downloadPatchFromPpk(url, hash, originHash, new UpdateContext.DownloadFileListener() {
@Override
public void onDownloadCompleted(DownloadTaskParams params) {
promise.resolve(null);
}
@Override
public void onDownloadFailed(Throwable error) {
promise.reject(error);
}
});
}catch (Exception e){
promise.reject("downloadPatchFromPpk failed: "+e.getMessage());
}
}
public static void reloadUpdate(final UpdateContext updateContext, final ReactApplicationContext mContext, final ReadableMap options, final Promise promise) {
final String hash = options.getString("hash");
restartApp(updateContext, mContext, hash, promise);
}
public static void restartApp(final UpdateContext updateContext, final ReactApplicationContext mContext, final String hash, final Promise promise) {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
// 如果提供了 hash,则切换版本
if (hash != null && updateContext != null) {
updateContext.switchVersion(hash);
}
final Context application = mContext.getApplicationContext();
final Context application = mContext.getApplicationContext();
String updateBundlePath = updateContext.getBundleUrl(application);
JSBundleLoader loader;
if (updateBundlePath != null) {
loader = JSBundleLoader.createFileLoader(updateBundlePath);
} else {
String bundleAssetName = "index.android.bundle";
try {
ReactInstanceManager defaultInstanceManager = ((ReactApplication) application).getReactNativeHost().getReactInstanceManager();
String rnBundleAssetName = defaultInstanceManager.getBundleAssetName();
if (rnBundleAssetName != null && !rnBundleAssetName.isEmpty()) {
bundleAssetName = rnBundleAssetName;
}
} catch (Exception e) {
Log.e(NAME, "Failed to get default asset name from ReactNativeHost: " + e.getMessage());
}
loader = JSBundleLoader.createAssetLoader(application, bundleAssetName, false);
}
try {
ReactInstanceManager instanceManager = updateContext.getCustomReactInstanceManager();
if (instanceManager == null) {
instanceManager = ((ReactApplication) application).getReactNativeHost().getReactInstanceManager();
}
try {
Field loadField = instanceManager.getClass().getDeclaredField("mBundleLoader");
loadField.setAccessible(true);
loadField.set(instanceManager, loader);
} catch (Throwable err) {
Field jsBundleField = instanceManager.getClass().getDeclaredField("mJSBundleFile");
jsBundleField.setAccessible(true);
jsBundleField.set(instanceManager, UpdateContext.getBundleUrl(application));
}
instanceManager.recreateReactContextInBackground();
promise.resolve(true);
} catch (Throwable err) {
final Activity currentActivity = mContext.getCurrentActivity();
if (currentActivity == null) {
promise.reject(err);
return;
}
try {
java.lang.reflect.Method getReactDelegateMethod =
ReactActivity.class.getMethod("getReactDelegate");
ReactDelegate reactDelegate = (ReactDelegate)
getReactDelegateMethod.invoke(currentActivity);
Field reactHostField = getCompatibleField(ReactDelegate.class, "reactHost");
reactHostField.setAccessible(true);
Object reactHost = reactHostField.get(reactDelegate);
Field devSupport = getCompatibleField(reactHost.getClass(), "useDevSupport");
devSupport.setAccessible(true);
devSupport.set(reactHost, false);
// Access the ReactHostDelegate field (compatible with mReactHostDelegate/reactHostDelegate)
Field reactHostDelegateField = getCompatibleField(reactHost.getClass(), "reactHostDelegate");
reactHostDelegateField.setAccessible(true);
Object reactHostDelegate = reactHostDelegateField.get(reactHost);
String bundleFieldName = "jsBundleLoader";
if (reactHostDelegate.getClass().getCanonicalName().equals("expo.modules.ExpoReactHostFactory.ExpoReactHostDelegate")) {
bundleFieldName = "_jsBundleLoader";
}
// Modify the jsBundleLoader field
Field jsBundleLoaderField = reactHostDelegate.getClass().getDeclaredField(bundleFieldName);
jsBundleLoaderField.setAccessible(true);
jsBundleLoaderField.set(reactHostDelegate, loader);
// Get the reload method with a String parameter
java.lang.reflect.Method reloadMethod = reactHost.getClass().getMethod("reload", String.class);
// Invoke the reload method with a reason
reloadMethod.invoke(reactHost, "react-native-update");
promise.resolve(true);
} catch (Throwable e) {
currentActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
currentActivity.recreate();
}
});
promise.resolve(true);
}
}
}
});
}
public static void restartApp(final ReactApplicationContext mContext, final Promise promise) {
restartApp(null, mContext, null, promise);
}
public static void setNeedUpdate(final UpdateContext updateContext, final ReadableMap options, final Promise promise) {
final String hash = options.getString("hash");
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
updateContext.switchVersion(hash);
promise.resolve(true);
} catch (Throwable err) {
promise.reject("switchVersionLater failed: "+err.getMessage());
Log.e("pushy", "switchVersionLater failed", err);
}
}
});
}
public static void markSuccess(final UpdateContext updateContext, final Promise promise) {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
updateContext.markSuccess();
promise.resolve(true);
}
});
}
public static void setUuid(final UpdateContext updateContext, final String uuid, final Promise promise) {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
updateContext.setKv("uuid", uuid);
promise.resolve(true);
}
});
}
public static boolean check(String json) {
ObjectMapper mapper = new ObjectMapper();
try {
mapper.readValue(json, Map.class);
return true;
} catch (IOException e) {
return false;
}
}
public static void setLocalHashInfo(final UpdateContext updateContext, final String hash, final String info, final Promise promise) {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
if (check(info)) {
updateContext.setKv("hash_" + hash, info);
promise.resolve(true);
} else {
updateContext.setKv("hash_" + hash, info);
promise.reject("setLocalHashInfo failed: invalid json string");
}
}
});
}
public static void getLocalHashInfo(UpdateContext updateContext, final String hash, final Promise promise) {
String value = updateContext.getKv("hash_" + hash);
if (check(value)) {
promise.resolve(value);
} else {
promise.reject("getLocalHashInfo failed: invalid json string");
}
}
}