Fix reset button being disabled.

- When doing factory reset, we will launch the account credential
confirmation. If this fails, the settings page is re-initialized.
This steps might trigger multiple layout changes for the scrollview
before it is finalized. However, we are removing the global layout
listener once we receive the first update, which could result in the
reset button being disabled incorrectly, as the scroll state is
calculated based on wrong view heights. Remove the call that remove
the layout listener, so that we can still receive further updates.

- also remove the scroll listener once we enable the reset button
from scroll, as no more action is really needed on suceeding scroll.

Change-Id: I6ec1f592991629c15e5ad2bcb29fdd679d598f70
Fixes: 73298075
Test: make RunSettingsRoboTests
diff --git a/src/com/android/settings/MasterClear.java b/src/com/android/settings/MasterClear.java
index f66b1f9..9ad32e2 100644
--- a/src/com/android/settings/MasterClear.java
+++ b/src/com/android/settings/MasterClear.java
@@ -73,7 +73,7 @@
  *
  * This is the initial screen.
  */
-public class MasterClear extends InstrumentedFragment {
+public class MasterClear extends InstrumentedFragment implements OnGlobalLayoutListener {
     private static final String TAG = "MasterClear";
 
     @VisibleForTesting static final int KEYGUARD_REQUEST = 55;
@@ -86,20 +86,17 @@
     static final String ERASE_ESIMS_EXTRA = "erase_esim";
 
     private View mContentView;
-    private Button mInitiateButton;
+    @VisibleForTesting Button mInitiateButton;
     private View mExternalStorageContainer;
     @VisibleForTesting CheckBox mExternalStorage;
     private View mEsimStorageContainer;
     @VisibleForTesting CheckBox mEsimStorage;
-    private ScrollView mScrollView;
+    @VisibleForTesting ScrollView mScrollView;
 
-    private final OnGlobalLayoutListener mOnGlobalLayoutListener = new OnGlobalLayoutListener() {
-        @Override
-        public void onGlobalLayout() {
-            mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
-            mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
-        }
-    };
+    @Override
+    public void onGlobalLayout() {
+        mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
+    }
 
     @Override
     public void onCreate(@Nullable Bundle savedInstanceState) {
@@ -259,13 +256,16 @@
      */
     @VisibleForTesting
     void establishInitialState() {
-        mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
+        mInitiateButton = mContentView.findViewById(R.id.initiate_master_clear);
         mInitiateButton.setOnClickListener(mInitiateListener);
         mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
-        mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
+        mExternalStorage = mContentView.findViewById(R.id.erase_external);
         mEsimStorageContainer = mContentView.findViewById(R.id.erase_esim_container);
-        mEsimStorage = (CheckBox) mContentView.findViewById(R.id.erase_esim);
-        mScrollView = (ScrollView) mContentView.findViewById(R.id.master_clear_scrollview);
+        mEsimStorage = mContentView.findViewById(R.id.erase_esim);
+        if (mScrollView != null) {
+            mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
+        }
+        mScrollView = mContentView.findViewById(R.id.master_clear_scrollview);
 
         /*
          * If the external storage is emulated, it will be erased with a factory
@@ -322,8 +322,8 @@
 
         final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
         loadAccountList(um);
-        StringBuffer contentDescription = new StringBuffer();
-        View masterClearContainer = mContentView.findViewById(R.id.master_clear_container);
+        final StringBuffer contentDescription = new StringBuffer();
+        final View masterClearContainer = mContentView.findViewById(R.id.master_clear_container);
         getContentDescription(masterClearContainer, contentDescription);
         masterClearContainer.setContentDescription(contentDescription);
 
@@ -334,12 +334,13 @@
                 int oldScrollY) {
                 if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
                     mInitiateButton.setEnabled(true);
+                    mScrollView.setOnScrollChangeListener(null);
                 }
             }
         });
 
         // Set the initial state of the initiateButton
-        mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
+        mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(this);
     }
 
     /**