Merge "updater_sample: validate state only once" am: 02ee7bf29d
am: 8bb3085865

Change-Id: I9921a274b17ce7ef2d64cb1f36337dc3f02aebf4
diff --git a/updater_sample/src/com/example/android/systemupdatersample/UpdateManager.java b/updater_sample/src/com/example/android/systemupdatersample/UpdateManager.java
index 145cc83..2fe04bd 100644
--- a/updater_sample/src/com/example/android/systemupdatersample/UpdateManager.java
+++ b/updater_sample/src/com/example/android/systemupdatersample/UpdateManager.java
@@ -64,6 +64,9 @@
 
     private AtomicBoolean mManualSwitchSlotRequired = new AtomicBoolean(true);
 
+    /** Validate state only once when app binds to UpdateEngine. */
+    private AtomicBoolean mStateValidityEnsured = new AtomicBoolean(false);
+
     @GuardedBy("mLock")
     private UpdateData mLastUpdateData = null;
 
@@ -90,6 +93,7 @@
      * Binds to {@link UpdateEngine}.
      */
     public void bind() {
+        mStateValidityEnsured.set(false);
         this.mUpdateEngine.bind(mUpdateEngineCallback);
     }
 
@@ -468,7 +472,10 @@
         mUpdateEngineStatus.set(status);
         mProgress.set(progress);
 
-        ensureCorrectUpdaterState();
+        if (!mStateValidityEnsured.getAndSet(true)) {
+            // We ensure correct state once only when sample app is bound to UpdateEngine.
+            ensureCorrectUpdaterState();
+        }
 
         getOnProgressUpdateCallback().ifPresent(callback -> callback.accept(progress));