1
1
package com .microsoft .codepush .react ;
2
2
3
3
import com .facebook .react .ReactPackage ;
4
+ import com .facebook .react .bridge .ActivityEventListener ;
4
5
import com .facebook .react .bridge .JavaScriptModule ;
5
6
import com .facebook .react .bridge .LifecycleEventListener ;
6
7
import com .facebook .react .bridge .NativeModule ;
41
42
42
43
public class CodePush {
43
44
44
- private boolean resumablePendingUpdateAvailable = false ;
45
45
private boolean didUpdate = false ;
46
46
private Timer timer ;
47
47
private boolean usingTestFolder = false ;
@@ -87,7 +87,7 @@ public CodePush(String deploymentKey, Activity mainActivity) {
87
87
throw new CodePushUnknownException ("Unable to get package info for " + applicationContext .getPackageName (), e );
88
88
}
89
89
90
- checkForPendingUpdate ( /*needsRestart*/ false );
90
+ initializeUpdateAfterRestart ( );
91
91
}
92
92
93
93
public ReactPackage getReactPackage () {
@@ -144,52 +144,6 @@ private void cancelRollbackTimer() {
144
144
}
145
145
}
146
146
147
- private void checkForPendingUpdate (boolean needsRestart ) {
148
- SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
149
- String pendingUpdateString = settings .getString (PENDING_UPDATE_KEY , null );
150
-
151
- if (pendingUpdateString != null ) {
152
- try {
153
- JSONObject pendingUpdateJSON = new JSONObject (pendingUpdateString );
154
- String pendingHash = pendingUpdateJSON .getString (PENDING_UPDATE_HASH_KEY );
155
- String currentHash = codePushPackage .getCurrentPackageHash ();
156
- if (!pendingHash .equals (currentHash )) {
157
- throw new CodePushUnknownException ("Pending hash " + pendingHash +
158
- " and current hash " + currentHash + " are different" );
159
- }
160
-
161
- int rollbackTimeout = pendingUpdateJSON .getInt (PENDING_UPDATE_ROLLBACK_TIMEOUT_KEY );
162
- initializeUpdateWithRollbackTimeout (rollbackTimeout , needsRestart );
163
- settings .edit ().remove (PENDING_UPDATE_KEY ).commit ();
164
- } catch (JSONException e ) {
165
- // Should not happen.
166
- throw new CodePushUnknownException ("Unable to parse pending update metadata " +
167
- pendingUpdateString + " stored in SharedPreferences" , e );
168
- } catch (IOException e ) {
169
- // There is no current package hash.
170
- throw new CodePushUnknownException ("Should not register a pending update without a saving a current package" , e );
171
- }
172
- }
173
- }
174
-
175
- private void checkForPendingUpdateDuringResume () {
176
- if (resumablePendingUpdateAvailable ) {
177
- checkForPendingUpdate (/*needsRestart*/ true );
178
- }
179
- }
180
-
181
- private void initializeUpdateWithRollbackTimeout (int rollbackTimeout , boolean needsRestart ) {
182
- didUpdate = true ;
183
-
184
- if (needsRestart ) {
185
- codePushNativeModule .loadBundle ();
186
- }
187
-
188
- if (0 != rollbackTimeout ) {
189
- startRollbackTimer (rollbackTimeout );
190
- }
191
- }
192
-
193
147
private boolean isFailedHash (String packageHash ) {
194
148
SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
195
149
String failedUpdatesString = settings .getString (FAILED_UPDATES_KEY , null );
@@ -277,8 +231,32 @@ public void run() {
277
231
}, timeout );
278
232
}
279
233
234
+ private void initializeUpdateAfterRestart () {
235
+ SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
236
+ String pendingUpdateString = settings .getString (PENDING_UPDATE_KEY , null );
237
+
238
+ if (pendingUpdateString != null ) {
239
+ try {
240
+ didUpdate = true ;
241
+ JSONObject pendingUpdateJSON = new JSONObject (pendingUpdateString );
242
+ int rollbackTimeout = pendingUpdateJSON .getInt (PENDING_UPDATE_ROLLBACK_TIMEOUT_KEY );
243
+ if (0 != rollbackTimeout ) {
244
+ startRollbackTimer (rollbackTimeout );
245
+ }
246
+
247
+ settings .edit ().remove (PENDING_UPDATE_KEY ).commit ();
248
+ } catch (JSONException e ) {
249
+ // Should not happen.
250
+ throw new CodePushUnknownException ("Unable to parse pending update metadata " +
251
+ pendingUpdateString + " stored in SharedPreferences" , e );
252
+ }
253
+ }
254
+ }
255
+
280
256
private class CodePushNativeModule extends ReactContextBaseJavaModule {
281
257
258
+ private LifecycleEventListener lifecycleEventListener = null ;
259
+
282
260
private void loadBundle () {
283
261
Intent intent = mainActivity .getIntent ();
284
262
mainActivity .finish ();
@@ -289,15 +267,37 @@ private void loadBundle() {
289
267
public void installUpdate (ReadableMap updatePackage , int rollbackTimeout , int installMode , Promise promise ) {
290
268
try {
291
269
codePushPackage .installPackage (updatePackage );
270
+
271
+ String pendingHash = CodePushUtils .tryGetString (updatePackage , codePushPackage .PACKAGE_HASH_KEY );
272
+ if (pendingHash == null ) {
273
+ throw new CodePushUnknownException ("Update package to be installed has no hash." );
274
+ } else {
275
+ savePendingUpdate (pendingHash , rollbackTimeout );
276
+ }
277
+
292
278
if (installMode != CodePushInstallMode .IMMEDIATE .getValue ()) {
293
- resumablePendingUpdateAvailable = installMode == CodePushInstallMode .ON_NEXT_RESUME .getValue ();
294
- String pendingHash = CodePushUtils .tryGetString (updatePackage , codePushPackage .PACKAGE_HASH_KEY );
295
- if (pendingHash == null ) {
296
- throw new CodePushUnknownException ("Update package to be installed has no hash." );
297
- } else {
298
- savePendingUpdate (pendingHash , rollbackTimeout );
279
+ loadBundle ();
280
+ } else if (installMode == CodePushInstallMode .ON_NEXT_RESUME .getValue ()) {
281
+ // Ensure we do not add the listener twice.
282
+ if (lifecycleEventListener == null ) {
283
+ lifecycleEventListener = new LifecycleEventListener () {
284
+ @ Override
285
+ public void onHostResume () {
286
+ loadBundle ();
287
+ }
288
+
289
+ @ Override
290
+ public void onHostPause () {
291
+ }
292
+
293
+ @ Override
294
+ public void onHostDestroy () {
295
+ }
296
+ };
297
+ getReactApplicationContext ().addLifecycleEventListener (lifecycleEventListener );
299
298
}
300
299
}
300
+
301
301
promise .resolve ("" );
302
302
} catch (IOException e ) {
303
303
e .printStackTrace ();
@@ -377,12 +377,34 @@ public void setUsingTestFolder(boolean shouldUseTestFolder) {
377
377
378
378
@ ReactMethod
379
379
public void restartImmediateUpdate (int rollbackTimeout ) {
380
- initializeUpdateWithRollbackTimeout ( rollbackTimeout , /*needsRestart*/ true );
380
+ loadBundle ( );
381
381
}
382
382
383
383
@ ReactMethod
384
384
public void restartPendingUpdate () {
385
- checkForPendingUpdate (/*needsRestart*/ true );
385
+ SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
386
+ String pendingUpdateString = settings .getString (PENDING_UPDATE_KEY , null );
387
+
388
+ if (pendingUpdateString != null ) {
389
+ try {
390
+ JSONObject pendingUpdateJSON = new JSONObject (pendingUpdateString );
391
+ String pendingHash = pendingUpdateJSON .getString (PENDING_UPDATE_HASH_KEY );
392
+ String currentHash = codePushPackage .getCurrentPackageHash ();
393
+ if (!pendingHash .equals (currentHash )) {
394
+ throw new CodePushUnknownException ("Pending hash " + pendingHash +
395
+ " and current hash " + currentHash + " are different" );
396
+ }
397
+
398
+ loadBundle ();
399
+ } catch (JSONException e ) {
400
+ // Should not happen.
401
+ throw new CodePushUnknownException ("Unable to parse pending update metadata " +
402
+ pendingUpdateString + " stored in SharedPreferences" , e );
403
+ } catch (IOException e ) {
404
+ // There is no current package hash.
405
+ throw new CodePushUnknownException ("Should not register a pending update without a saving a current package" , e );
406
+ }
407
+ }
386
408
}
387
409
388
410
@ Override
@@ -414,21 +436,6 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactAppli
414
436
nativeModules .add (CodePush .this .codePushNativeModule );
415
437
nativeModules .add (dialogModule );
416
438
417
- reactApplicationContext .addLifecycleEventListener (new LifecycleEventListener () {
418
- @ Override
419
- public void onHostResume () {
420
- CodePush .this .checkForPendingUpdateDuringResume ();
421
- }
422
-
423
- @ Override
424
- public void onHostPause () {
425
- }
426
-
427
- @ Override
428
- public void onHostDestroy () {
429
- }
430
- });
431
-
432
439
return nativeModules ;
433
440
}
434
441
0 commit comments