updater_sample: Add suspend/resume update

- Add suspend/resume buttons.
- UpdateManager: add suspend/resume control methods.
- UpdaterState: fix transitions.

Test: on the device
Bug: 77150010
Change-Id: I174edd32401f8232b5071eb1a2758a4704779801
Signed-off-by: Zhomart Mukhamejanov <zhomart@google.com>
This commit is contained in:
Zhomart Mukhamejanov 2018-05-31 10:47:09 -07:00
parent e36b6ec97c
commit 16db994fad
4 changed files with 81 additions and 14 deletions

View file

@ -197,6 +197,29 @@
android:text="Reset" /> android:text="Reset" />
</LinearLayout> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:orientation="horizontal">
<Button
android:id="@+id/buttonSuspend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onSuspendClick"
android:text="Suspend" />
<Button
android:id="@+id/buttonResume"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onResumeClick"
android:text="Resume" />
</LinearLayout>
<TextView <TextView
android:id="@+id/textViewUpdateInfo" android:id="@+id/textViewUpdateInfo"
android:layout_width="wrap_content" android:layout_width="wrap_content"

View file

@ -197,6 +197,24 @@ public class UpdateManager {
} }
} }
/**
* Suspend running update.
*/
public synchronized void suspend() throws UpdaterState.InvalidTransitionException {
Log.d(TAG, "suspend invoked");
setUpdaterState(UpdaterState.PAUSED);
mUpdateEngine.cancel();
}
/**
* Resume suspended update.
*/
public synchronized void resume() throws UpdaterState.InvalidTransitionException {
Log.d(TAG, "resume invoked");
setUpdaterState(UpdaterState.RUNNING);
updateEngineReApplyPayload();
}
/** /**
* Updates {@link this.mState} and if state is changed, * Updates {@link this.mState} and if state is changed,
* it also notifies {@link this.mOnStateChangeCallback}. * it also notifies {@link this.mOnStateChangeCallback}.
@ -237,10 +255,6 @@ public class UpdateManager {
/** /**
* Requests update engine to stop any ongoing update. If an update has been applied, * Requests update engine to stop any ongoing update. If an update has been applied,
* leave it as is. * leave it as is.
*
* <p>Sometimes it's possible that the
* update engine would throw an error when the method is called, and the only way to
* handle it is to catch the exception.</p>
*/ */
public synchronized void cancelRunningUpdate() throws UpdaterState.InvalidTransitionException { public synchronized void cancelRunningUpdate() throws UpdaterState.InvalidTransitionException {
Log.d(TAG, "cancelRunningUpdate invoked"); Log.d(TAG, "cancelRunningUpdate invoked");
@ -250,10 +264,6 @@ public class UpdateManager {
/** /**
* Resets update engine to IDLE state. If an update has been applied it reverts it. * Resets update engine to IDLE state. If an update has been applied it reverts it.
*
* <p>Sometimes it's possible that the
* update engine would throw an error when the method is called, and the only way to
* handle it is to catch the exception.</p>
*/ */
public synchronized void resetUpdate() throws UpdaterState.InvalidTransitionException { public synchronized void resetUpdate() throws UpdaterState.InvalidTransitionException {
Log.d(TAG, "resetUpdate invoked"); Log.d(TAG, "resetUpdate invoked");
@ -506,7 +516,7 @@ public class UpdateManager {
synchronizeUpdaterStateWithUpdateEngineStatus(); synchronizeUpdaterStateWithUpdateEngineStatus();
} }
getOnProgressUpdateCallback().ifPresent(callback -> callback.accept(progress)); getOnProgressUpdateCallback().ifPresent(callback -> callback.accept(mProgress.get()));
if (previousStatus != status) { if (previousStatus != status) {
getOnEngineStatusUpdateCallback().ifPresent(callback -> callback.accept(status)); getOnEngineStatusUpdateCallback().ifPresent(callback -> callback.accept(status));

View file

@ -52,12 +52,12 @@ public class UpdaterState {
*/ */
private static final ImmutableMap<Integer, ImmutableSet<Integer>> TRANSITIONS = private static final ImmutableMap<Integer, ImmutableSet<Integer>> TRANSITIONS =
ImmutableMap.<Integer, ImmutableSet<Integer>>builder() ImmutableMap.<Integer, ImmutableSet<Integer>>builder()
.put(IDLE, ImmutableSet.of(ERROR, RUNNING)) .put(IDLE, ImmutableSet.of(IDLE, ERROR, RUNNING))
.put(RUNNING, ImmutableSet.of(
ERROR, PAUSED, REBOOT_REQUIRED, SLOT_SWITCH_REQUIRED))
.put(PAUSED, ImmutableSet.of(ERROR, RUNNING, IDLE))
.put(SLOT_SWITCH_REQUIRED, ImmutableSet.of(ERROR, IDLE))
.put(ERROR, ImmutableSet.of(IDLE)) .put(ERROR, ImmutableSet.of(IDLE))
.put(RUNNING, ImmutableSet.of(
IDLE, ERROR, PAUSED, REBOOT_REQUIRED, SLOT_SWITCH_REQUIRED))
.put(PAUSED, ImmutableSet.of(ERROR, RUNNING, IDLE))
.put(SLOT_SWITCH_REQUIRED, ImmutableSet.of(ERROR, REBOOT_REQUIRED, IDLE))
.put(REBOOT_REQUIRED, ImmutableSet.of(IDLE)) .put(REBOOT_REQUIRED, ImmutableSet.of(IDLE))
.build(); .build();

View file

@ -55,6 +55,8 @@ public class MainActivity extends Activity {
private Button mButtonApplyConfig; private Button mButtonApplyConfig;
private Button mButtonStop; private Button mButtonStop;
private Button mButtonReset; private Button mButtonReset;
private Button mButtonSuspend;
private Button mButtonResume;
private ProgressBar mProgressBar; private ProgressBar mProgressBar;
private TextView mTextViewUpdaterState; private TextView mTextViewUpdaterState;
private TextView mTextViewEngineStatus; private TextView mTextViewEngineStatus;
@ -79,6 +81,8 @@ public class MainActivity extends Activity {
this.mButtonApplyConfig = findViewById(R.id.buttonApplyConfig); this.mButtonApplyConfig = findViewById(R.id.buttonApplyConfig);
this.mButtonStop = findViewById(R.id.buttonStop); this.mButtonStop = findViewById(R.id.buttonStop);
this.mButtonReset = findViewById(R.id.buttonReset); this.mButtonReset = findViewById(R.id.buttonReset);
this.mButtonSuspend = findViewById(R.id.buttonSuspend);
this.mButtonResume = findViewById(R.id.buttonResume);
this.mProgressBar = findViewById(R.id.progressBar); this.mProgressBar = findViewById(R.id.progressBar);
this.mTextViewUpdaterState = findViewById(R.id.textViewUpdaterState); this.mTextViewUpdaterState = findViewById(R.id.textViewUpdaterState);
this.mTextViewEngineStatus = findViewById(R.id.textViewEngineStatus); this.mTextViewEngineStatus = findViewById(R.id.textViewEngineStatus);
@ -208,10 +212,35 @@ public class MainActivity extends Activity {
} }
} }
/**
* suspend button clicked
*/
public void onSuspendClick(View view) {
try {
mUpdateManager.suspend();
} catch (UpdaterState.InvalidTransitionException e) {
Log.e(TAG, "Failed to suspend running update", e);
}
}
/**
* resume button clicked
*/
public void onResumeClick(View view) {
try {
uiResetWidgets();
uiResetEngineText();
mUpdateManager.resume();
} catch (UpdaterState.InvalidTransitionException e) {
Log.e(TAG, "Failed to resume running update", e);
}
}
/** /**
* switch slot button clicked * switch slot button clicked
*/ */
public void onSwitchSlotClick(View view) { public void onSwitchSlotClick(View view) {
uiResetWidgets();
mUpdateManager.setSwitchSlotOnReboot(); mUpdateManager.setSwitchSlotOnReboot();
} }
@ -289,6 +318,8 @@ public class MainActivity extends Activity {
mButtonApplyConfig.setEnabled(false); mButtonApplyConfig.setEnabled(false);
mButtonStop.setEnabled(false); mButtonStop.setEnabled(false);
mButtonReset.setEnabled(false); mButtonReset.setEnabled(false);
mButtonSuspend.setEnabled(false);
mButtonResume.setEnabled(false);
mProgressBar.setEnabled(false); mProgressBar.setEnabled(false);
mProgressBar.setVisibility(ProgressBar.INVISIBLE); mProgressBar.setVisibility(ProgressBar.INVISIBLE);
mButtonSwitchSlot.setEnabled(false); mButtonSwitchSlot.setEnabled(false);
@ -303,6 +334,7 @@ public class MainActivity extends Activity {
private void uiStateIdle() { private void uiStateIdle() {
uiResetWidgets(); uiResetWidgets();
mButtonReset.setEnabled(true);
mSpinnerConfigs.setEnabled(true); mSpinnerConfigs.setEnabled(true);
mButtonReload.setEnabled(true); mButtonReload.setEnabled(true);
mButtonApplyConfig.setEnabled(true); mButtonApplyConfig.setEnabled(true);
@ -314,6 +346,7 @@ public class MainActivity extends Activity {
mProgressBar.setEnabled(true); mProgressBar.setEnabled(true);
mProgressBar.setVisibility(ProgressBar.VISIBLE); mProgressBar.setVisibility(ProgressBar.VISIBLE);
mButtonStop.setEnabled(true); mButtonStop.setEnabled(true);
mButtonSuspend.setEnabled(true);
} }
private void uiStatePaused() { private void uiStatePaused() {
@ -321,6 +354,7 @@ public class MainActivity extends Activity {
mButtonReset.setEnabled(true); mButtonReset.setEnabled(true);
mProgressBar.setEnabled(true); mProgressBar.setEnabled(true);
mProgressBar.setVisibility(ProgressBar.VISIBLE); mProgressBar.setVisibility(ProgressBar.VISIBLE);
mButtonResume.setEnabled(true);
} }
private void uiStateSlotSwitchRequired() { private void uiStateSlotSwitchRequired() {