diff options
author | Zhomart Mukhamejanov <zhomart@google.com> | 2018-05-24 18:11:47 +0200 |
---|---|---|
committer | Zhomart Mukhamejanov <zhomart@google.com> | 2018-05-31 23:13:05 +0200 |
commit | 7671f68ab858a75f269fbddefb4840b87078a425 (patch) | |
tree | 1a60112937e5a2d94d8e7be5e43066f36f31de13 /updater_sample/src | |
parent | Merge "updater: Drop the 'blocks' parameter in LoadStash()." (diff) | |
download | android_bootable_recovery-7671f68ab858a75f269fbddefb4840b87078a425.tar android_bootable_recovery-7671f68ab858a75f269fbddefb4840b87078a425.tar.gz android_bootable_recovery-7671f68ab858a75f269fbddefb4840b87078a425.tar.bz2 android_bootable_recovery-7671f68ab858a75f269fbddefb4840b87078a425.tar.lz android_bootable_recovery-7671f68ab858a75f269fbddefb4840b87078a425.tar.xz android_bootable_recovery-7671f68ab858a75f269fbddefb4840b87078a425.tar.zst android_bootable_recovery-7671f68ab858a75f269fbddefb4840b87078a425.zip |
Diffstat (limited to 'updater_sample/src')
-rw-r--r-- | updater_sample/src/com/example/android/systemupdatersample/UpdateManager.java | 76 | ||||
-rw-r--r-- | updater_sample/src/com/example/android/systemupdatersample/ui/MainActivity.java | 4 |
2 files changed, 79 insertions, 1 deletions
diff --git a/updater_sample/src/com/example/android/systemupdatersample/UpdateManager.java b/updater_sample/src/com/example/android/systemupdatersample/UpdateManager.java index c4c8c9c27..f5c2ea5ab 100644 --- a/updater_sample/src/com/example/android/systemupdatersample/UpdateManager.java +++ b/updater_sample/src/com/example/android/systemupdatersample/UpdateManager.java @@ -384,11 +384,85 @@ public class UpdateManager { updateEngineApplyPayload(builder.build()); } + /** + * Verifies if mUpdaterState matches mUpdateEngineStatus. + * If they don't match, runs applyPayload to trigger onPayloadApplicationComplete + * callback, which updates mUpdaterState. + */ + private void ensureCorrectUpdaterState() { + // When mUpdaterState is one of IDLE, PAUSED, ERROR, SLOT_SWITCH_REQUIRED + // then mUpdateEngineStatus must be IDLE. + // When mUpdaterState is RUNNING, + // then mUpdateEngineStatus must not be IDLE or UPDATED_NEED_REBOOT. + // When mUpdaterState is REBOOT_REQUIRED, + // then mUpdateEngineStatus must be UPDATED_NEED_REBOOT. + int state = mUpdaterState.get(); + int updateEngineStatus = mUpdateEngineStatus.get(); + if (state == UpdaterState.IDLE + || state == UpdaterState.ERROR + || state == UpdaterState.PAUSED + || state == UpdaterState.SLOT_SWITCH_REQUIRED) { + ensureUpdateEngineStatusIdle(state, updateEngineStatus); + } else if (state == UpdaterState.RUNNING) { + ensureUpdateEngineStatusRunning(state, updateEngineStatus); + } else if (state == UpdaterState.REBOOT_REQUIRED) { + ensureUpdateEngineStatusReboot(state, updateEngineStatus); + } + } + + private void ensureUpdateEngineStatusIdle(int state, int updateEngineStatus) { + if (updateEngineStatus == UpdateEngine.UpdateStatusConstants.IDLE) { + return; + } + // It might happen when update is started not from the sample app. + // To make the sample app simple, we won't handle this case. + throw new RuntimeException("When mUpdaterState is " + state + + " mUpdateEngineStatus expected to be " + + UpdateEngine.UpdateStatusConstants.IDLE + + ", but it is " + updateEngineStatus); + } + + private void ensureUpdateEngineStatusRunning(int state, int updateEngineStatus) { + if (updateEngineStatus != UpdateEngine.UpdateStatusConstants.UPDATED_NEED_REBOOT + && updateEngineStatus != UpdateEngine.UpdateStatusConstants.IDLE) { + return; + } + // Re-apply latest update. It makes update_engine to invoke + // onPayloadApplicationComplete callback. The callback notifies + // if update was successful or not. + updateEngineReApplyPayload(); + } + + private void ensureUpdateEngineStatusReboot(int state, int updateEngineStatus) { + if (updateEngineStatus == UpdateEngine.UpdateStatusConstants.UPDATED_NEED_REBOOT) { + return; + } + // This might happen when update is installed by other means, + // and sample app is not aware of it. To make the sample app simple, + // we won't handle this case. + throw new RuntimeException("When mUpdaterState is " + state + + " mUpdateEngineStatus expected to be " + + UpdateEngine.UpdateStatusConstants.UPDATED_NEED_REBOOT + + ", but it is " + updateEngineStatus); + } + + /** + * Invoked by update_engine whenever update status or progress changes. + * It's also guaranteed to be invoked when app binds to the update_engine, except + * when update_engine fails to initialize (as defined in + * system/update_engine/binder_service_android.cc in + * function BinderUpdateEngineAndroidService::bind). + * + * @param status one of {@link UpdateEngine.UpdateStatusConstants}. + * @param progress a number from 0.0 to 1.0. + */ private void onStatusUpdate(int status, float progress) { int previousStatus = mUpdateEngineStatus.get(); mUpdateEngineStatus.set(status); mProgress.set(progress); + ensureCorrectUpdaterState(); + getOnProgressUpdateCallback().ifPresent(callback -> callback.accept(progress)); if (previousStatus != status) { @@ -413,7 +487,7 @@ public class UpdateManager { } /** - * Helper class to delegate {@code update_engine} callbacks to UpdateManager + * Helper class to delegate {@code update_engine} callback invocations to UpdateManager. */ class UpdateEngineCallbackImpl extends UpdateEngineCallback { @Override diff --git a/updater_sample/src/com/example/android/systemupdatersample/ui/MainActivity.java b/updater_sample/src/com/example/android/systemupdatersample/ui/MainActivity.java index 0b571cc81..1de72c2d6 100644 --- a/updater_sample/src/com/example/android/systemupdatersample/ui/MainActivity.java +++ b/updater_sample/src/com/example/android/systemupdatersample/ui/MainActivity.java @@ -108,12 +108,16 @@ public class MainActivity extends Activity { @Override protected void onResume() { super.onResume(); + // TODO(zhomart) load saved states + // Binding to UpdateEngine invokes onStatusUpdate callback, + // persisted updater state has to be loaded and prepared beforehand. this.mUpdateManager.bind(); } @Override protected void onPause() { this.mUpdateManager.unbind(); + // TODO(zhomart) save state super.onPause(); } |