Add scroll animation and onResume support for profile button

* Profile button should hide when scrolling down and it should re-appear
when scrolling up.
* Profile button should update if cross profile values change while
PhotoPicker session is going on.
* Remove locks from values that will be accessed by UI thread in
onClickListener. Those locks are not required as we always update the
values before accessing them. So, we are not updating them parallely,
they are always updated sequentially.

Bug: 190727775
Bug: 197742347
Test: manual (See videos attached)
Change-Id: Iabfdad271ab9cc3c51fae4ca589e751964e1947c
Merged-In: Iabfdad271ab9cc3c51fae4ca589e751964e1947c
(cherry picked from commit 1ca27fa75ce2616619f10308bd3bef5af2594492)
diff --git a/src/com/android/providers/media/photopicker/data/UserIdManager.java b/src/com/android/providers/media/photopicker/data/UserIdManager.java
index f2cded5..1f265de 100644
--- a/src/com/android/providers/media/photopicker/data/UserIdManager.java
+++ b/src/com/android/providers/media/photopicker/data/UserIdManager.java
@@ -19,6 +19,7 @@
 import static androidx.core.util.Preconditions.checkNotNull;
 
 import android.annotation.Nullable;
+import android.annotation.WorkerThread;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -121,6 +122,12 @@
     void setIntentAndCheckRestrictions(Intent intent);
 
     /**
+     * Updates cross profile restrictions values
+     */
+    @WorkerThread
+    void updateCrossProfileValues();
+
+    /**
      * Creates an implementation of {@link UserIdManager}.
      */
     static UserIdManager create(Context context) {
@@ -149,9 +156,7 @@
 
         private Intent mIntent = null;
         // Set default values to negative case, only set as false if checks pass.
-        @GuardedBy("mLock")
         private boolean mIsBlockedByAdmin = true;
-        @GuardedBy("mLock")
         private boolean mIsWorkProfileOff = true;
 
         private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@@ -224,9 +229,7 @@
         @Override
         public void setIntentAndCheckRestrictions(Intent intent) {
             mIntent = intent;
-            synchronized (mLock) {
-                setCrossProfileValues();
-            }
+            updateCrossProfileValues();
         }
 
         public boolean isCurrentUserSelected() {
@@ -253,8 +256,7 @@
 
         private void setUserIds() {
             synchronized (mLock) {
-                setUserIdsInternal();
-                setCrossProfileValues();
+                setUserIdsInternalLocked();
             }
         }
 
@@ -265,7 +267,7 @@
         }
 
         @GuardedBy("mLock")
-        private void setUserIdsInternal() {
+        private void setUserIdsInternalLocked() {
             UserManager userManager =  mContext.getSystemService(UserManager.class);
             if (userManager == null) {
                 Log.e(TAG, "Cannot obtain user manager");
@@ -311,19 +313,21 @@
 
         @Override
         public boolean isWorkProfileOff() {
-            synchronized (mLock) {
-                return mIsWorkProfileOff;
-            }
+            return mIsWorkProfileOff;
         }
 
         @Override
         public boolean isBlockedByAdmin() {
-            synchronized (mLock) {
-                return mIsBlockedByAdmin;
-            }
+            return mIsBlockedByAdmin;
         }
 
-        @GuardedBy("mLock")
+        @Override
+        @WorkerThread
+        public void updateCrossProfileValues() {
+            setCrossProfileValues();
+        }
+
+        @WorkerThread
         private void setCrossProfileValues() {
             final PackageManager packageManager = mContext.getPackageManager();
             // 1. Check if PICK_IMAGES intent is allowed by admin to show cross user content
diff --git a/src/com/android/providers/media/photopicker/ui/TabFragment.java b/src/com/android/providers/media/photopicker/ui/TabFragment.java
index 80908b6..be92ce1 100644
--- a/src/com/android/providers/media/photopicker/ui/TabFragment.java
+++ b/src/com/android/providers/media/photopicker/ui/TabFragment.java
@@ -28,11 +28,13 @@
 import androidx.appcompat.content.res.AppCompatResources;
 import androidx.fragment.app.Fragment;
 import androidx.lifecycle.ViewModelProvider;
+import androidx.recyclerview.widget.RecyclerView;
 
 import com.android.providers.media.R;
 import com.android.providers.media.photopicker.PhotoPickerActivity;
 import com.android.providers.media.photopicker.data.UserIdManager;
 import com.android.providers.media.photopicker.viewmodel.PickerViewModel;
+import com.android.providers.media.util.ForegroundThread;
 
 import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;
 
@@ -74,10 +76,6 @@
 
         mProfileButton = view.findViewById(R.id.profile_button);
         mUserIdManager = mPickerViewModel.getUserIdManager();
-        if (mUserIdManager.isMultiUserProfiles()) {
-            mProfileButton.setVisibility(View.VISIBLE);
-            setUpProfileButton();
-        }
 
         final boolean canSelectMultiple = mPickerViewModel.canSelectMultiple();
         if (canSelectMultiple) {
@@ -107,25 +105,69 @@
                 mRecyclerView.setPadding(0, 0, 0, dimen);
 
                 if (mUserIdManager.isMultiUserProfiles()) {
-                    if (selectedItemList.size() > 0) {
-                        mProfileButton.hide();
+                    if (shouldShowProfileButton()) {
+                        mProfileButton.show();
                     } else {
-                        if (!mHideProfileButton) {
-                            mProfileButton.show();
-                        }
+                        mProfileButton.hide();
                     }
                 }
             });
         }
     }
 
+    @Override
+    public void onResume() {
+        super.onResume();
+
+        updateProfileButtonAsync();
+    }
+
+    private void updateProfileButtonAsync() {
+        ForegroundThread.getExecutor().execute(() -> {
+            mUserIdManager.updateCrossProfileValues();
+
+            getActivity().runOnUiThread(() -> setUpProfileButton());
+        });
+    }
+
     private void setUpProfileButton() {
-        // TODO(b/190727775): Update profile button values onResume(). also re-check cross-profile
-        //  restrictions.
+        if (!mUserIdManager.isMultiUserProfiles()) {
+            if (mProfileButton.getVisibility() == View.VISIBLE) {
+                mProfileButton.setVisibility(View.GONE);
+                mRecyclerView.clearOnScrollListeners();
+            }
+            return;
+        }
+
+        if (shouldShowProfileButton()) {
+            mProfileButton.setVisibility(View.VISIBLE);
+
+            // TODO(b/199473568): Set up listeners for profile button only once for a fragment or
+            // when the value of isMultiUserProfile changes to true
+            mProfileButton.setOnClickListener(v -> onClickProfileButton());
+            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
+                @Override
+                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
+                    super.onScrolled(recyclerView, dx, dy);
+                    if (dy > 0) {
+                        mProfileButton.hide();
+                    } else {
+                        if (shouldShowProfileButton()) {
+                            mProfileButton.show();
+                        }
+                    }
+                }
+            });
+        }
+
         updateProfileButtonContent(mUserIdManager.isManagedUserSelected());
         updateProfileButtonColor(/* isDisabled */ !mUserIdManager.isCrossProfileAllowed());
+    }
 
-        mProfileButton.setOnClickListener(v -> onClickProfileButton());
+    private boolean shouldShowProfileButton() {
+        return (!mPickerViewModel.canSelectMultiple() ||
+                mPickerViewModel.getSelectedItems().getValue().size() == 0) &&
+                !mHideProfileButton;
     }
 
     private void onClickProfileButton() {
@@ -190,13 +232,11 @@
     }
 
     protected void hideProfileButton(boolean hide) {
+        mHideProfileButton = hide;
         if (hide) {
             mProfileButton.hide();
-            mHideProfileButton = true;
-        } else if (!hide && mUserIdManager.isMultiUserProfiles()
-                && mPickerViewModel.getSelectedItems().getValue().size() == 0) {
+        } else if (mUserIdManager.isMultiUserProfiles() && shouldShowProfileButton()) {
             mProfileButton.show();
-            mHideProfileButton = false;
         }
     }