Fix issue of the empty view is shown too early in PhotoPicker

Test: manual
Bug: 209335429
Change-Id: I9e594cc049291a8719c54fec908bd18bad06bfb0
diff --git a/src/com/android/providers/media/photopicker/ui/AlbumsTabFragment.java b/src/com/android/providers/media/photopicker/ui/AlbumsTabFragment.java
index 8834f9d..09596fa 100644
--- a/src/com/android/providers/media/photopicker/ui/AlbumsTabFragment.java
+++ b/src/com/android/providers/media/photopicker/ui/AlbumsTabFragment.java
@@ -46,10 +46,14 @@
 
         mBottomBarGap = getResources().getDimensionPixelSize(R.dimen.picker_album_bottom_bar_gap);
 
+        setEmptyMessage(R.string.picker_albums_empty_message);
+
         final AlbumsTabAdapter adapter = new AlbumsTabAdapter(mImageLoader, this::onItemClick,
                 mPickerViewModel.hasMimeTypeFilter());
         mPickerViewModel.getCategories().observe(this, categoryList -> {
             adapter.updateCategoryList(categoryList);
+            // Handle emptyView's visibility
+            updateVisibilityForEmptyView(/* shouldShowEmptyView */ categoryList.size() == 0);
         });
         final GridLayoutManager layoutManager = new GridLayoutManager(getContext(), COLUMN_COUNT);
         final AlbumsTabItemDecoration itemDecoration = new AlbumsTabItemDecoration(
@@ -82,11 +86,6 @@
         return bottomBarSize + mBottomBarGap;
     }
 
-    @Override
-    protected String getEmptyMessage() {
-        return getString(R.string.picker_albums_empty_message);
-    }
-
     /**
      * Create the albums tab fragment and add it into the FragmentManager
      *
diff --git a/src/com/android/providers/media/photopicker/ui/AutoFitRecyclerView.java b/src/com/android/providers/media/photopicker/ui/AutoFitRecyclerView.java
index 81bd941..3ea47c9 100644
--- a/src/com/android/providers/media/photopicker/ui/AutoFitRecyclerView.java
+++ b/src/com/android/providers/media/photopicker/ui/AutoFitRecyclerView.java
@@ -18,7 +18,6 @@
 
 import android.content.Context;
 import android.util.AttributeSet;
-import android.view.View;
 
 import androidx.annotation.Nullable;
 import androidx.recyclerview.widget.GridLayoutManager;
@@ -32,50 +31,6 @@
     private int mColumnWidth = -1;
     private int mMinimumSpanCount = 2;
     private boolean mIsGridLayout;
-    private View mEmptyView;
-    private AdapterDataObserver mAdapterDataObserver = new AdapterDataObserver() {
-        @Override
-        public void onChanged() {
-            super.onChanged();
-            checkIsEmpty();
-        }
-
-        /**
-         * If the user triggers {@link RecyclerView.Adapter#notifyItemInserted(int)}, this method
-         * will be triggered. We also need to check whether the dataset is empty or not to decide
-         * the visibility of the empty view.
-         */
-        @Override
-        public void onItemRangeInserted(int positionStart, int itemCount) {
-            super.onItemRangeInserted(positionStart, itemCount);
-            checkIsEmpty();
-        }
-
-        /**
-         * If the user triggers {@link RecyclerView.Adapter#notifyItemRemoved(int)}, this method
-         * will be triggered. We also need to check whether the dataset is empty or not to decide
-         * the visibility of the empty view.
-         */
-        @Override
-        public void onItemRangeRemoved(int positionStart, int itemCount) {
-            super.onItemRangeRemoved(positionStart, itemCount);
-            checkIsEmpty();
-        }
-
-        private void checkIsEmpty() {
-            if (mEmptyView == null) {
-                return;
-            }
-
-            if (getAdapter().getItemCount() == 0) {
-                mEmptyView.setVisibility(VISIBLE);
-                setVisibility(GONE);
-            } else {
-                mEmptyView.setVisibility(GONE);
-                setVisibility(VISIBLE);
-            }
-        }
-    };
 
     public AutoFitRecyclerView(Context context) {
         super(context);
@@ -102,25 +57,7 @@
     @Override
     public void setLayoutManager(@Nullable RecyclerView.LayoutManager layoutManager) {
         super.setLayoutManager(layoutManager);
-        if (layoutManager instanceof GridLayoutManager) {
-            mIsGridLayout = true;
-        }
-    }
-
-    @Override
-    public void setAdapter(@Nullable RecyclerView.Adapter adapter) {
-        super.setAdapter(adapter);
-        if (adapter != null) {
-            adapter.registerAdapterDataObserver(mAdapterDataObserver);
-        }
-        mAdapterDataObserver.onChanged();
-    }
-
-    /**
-     * Set the empty view. If the empty view is not null, when the item count is zero, it is shown.
-     */
-    public void setEmptyView(@Nullable View emptyView) {
-        mEmptyView = emptyView;
+        mIsGridLayout = (layoutManager instanceof GridLayoutManager);
     }
 
     public void setColumnWidth(int columnWidth) {
diff --git a/src/com/android/providers/media/photopicker/ui/PhotosTabFragment.java b/src/com/android/providers/media/photopicker/ui/PhotosTabFragment.java
index e9b37db..5e8d2ac 100644
--- a/src/com/android/providers/media/photopicker/ui/PhotosTabFragment.java
+++ b/src/com/android/providers/media/photopicker/ui/PhotosTabFragment.java
@@ -17,7 +17,6 @@
 
 import static com.android.providers.media.photopicker.ui.PhotosTabAdapter.COLUMN_COUNT;
 
-import android.icu.text.MessageFormat;
 import android.os.Bundle;
 import android.text.TextUtils;
 import android.view.View;
@@ -40,9 +39,7 @@
 import com.google.android.material.snackbar.Snackbar;
 
 import java.text.NumberFormat;
-import java.util.HashMap;
 import java.util.Locale;
-import java.util.Map;
 import src.com.android.providers.media.util.StringUtils;
 
 /**
@@ -80,14 +77,21 @@
 
         final PhotosTabAdapter adapter = new PhotosTabAdapter(mSelection, mImageLoader,
                 this::onItemClick, this::onItemLongClick);
-
+        setEmptyMessage(R.string.picker_photos_empty_message);
         mIsDefaultCategory = TextUtils.equals(Category.CATEGORY_DEFAULT, mCategoryType);
         if (mIsDefaultCategory) {
             mPickerViewModel.getItems().observe(this, itemList -> {
                 adapter.updateItemList(itemList);
+                // Handle emptyView's visibility
+                updateVisibilityForEmptyView(/* shouldShowEmptyView */ itemList.size() == 0);
             });
         } else {
             mPickerViewModel.getCategoryItems(mCategoryType).observe(this, itemList -> {
+                // If the item count of the albums is zero, albums are not shown on the Albums tab.
+                // The user can't launch the album items page when the album has zero items. So, we
+                // don't need to show emptyView in the case.
+                updateVisibilityForEmptyView(/* shouldShowEmptyView */ false);
+
                 adapter.updateItemList(itemList);
             });
         }
@@ -140,11 +144,6 @@
         }
     }
 
-    @Override
-    protected String getEmptyMessage() {
-        return getString(R.string.picker_photos_empty_message);
-    }
-
     private void onItemClick(@NonNull View view) {
         if (mSelection.canSelectMultiple()) {
             final boolean isSelectedBefore = view.isSelected();
diff --git a/src/com/android/providers/media/photopicker/ui/TabFragment.java b/src/com/android/providers/media/photopicker/ui/TabFragment.java
index 1ab6229..cb82794 100644
--- a/src/com/android/providers/media/photopicker/ui/TabFragment.java
+++ b/src/com/android/providers/media/photopicker/ui/TabFragment.java
@@ -54,9 +54,10 @@
 
     private int mBottomBarSize;
     private ExtendedFloatingActionButton mProfileButton;
-    private TextView mEmptyTextView;
     private UserIdManager mUserIdManager;
     private boolean mHideProfileButton;
+    private View mEmptyView;
+    private TextView mEmptyTextView;
 
     @Override
     @NonNull
@@ -72,13 +73,13 @@
 
         mImageLoader = new ImageLoader(getContext());
         mRecyclerView = view.findViewById(R.id.picker_tab_recyclerview);
-        View emptyView = view.findViewById(android.R.id.empty);
-        mRecyclerView.setEmptyView(emptyView);
-        mEmptyTextView = emptyView.findViewById(R.id.empty_text_view);
         mRecyclerView.setHasFixedSize(true);
         mPickerViewModel = new ViewModelProvider(requireActivity()).get(PickerViewModel.class);
         mSelection = mPickerViewModel.getSelection();
 
+        mEmptyView = view.findViewById(android.R.id.empty);
+        mEmptyTextView = mEmptyView.findViewById(R.id.empty_text_view);
+
         mProfileButton = view.findViewById(R.id.profile_button);
         mUserIdManager = mPickerViewModel.getUserIdManager();
 
@@ -124,8 +125,6 @@
     @Override
     public void onResume() {
         super.onResume();
-
-        mEmptyTextView.setText(getEmptyMessage());
         updateProfileButtonAsync();
     }
 
@@ -238,13 +237,6 @@
         return bottomBarSize;
     }
 
-    /**
-     * Get the messages to show on empty view
-     */
-    protected String getEmptyMessage() {
-        return getString(R.string.picker_photos_empty_message);
-    }
-
     protected void hideProfileButton(boolean hide) {
         mHideProfileButton = hide;
         if (hide) {
@@ -254,6 +246,19 @@
         }
     }
 
+    protected void setEmptyMessage(int resId) {
+        mEmptyTextView.setText(resId);
+    }
+
+    /**
+     * If we show the {@link #mEmptyView}, hide the {@link #mRecyclerView}. If we don't hide the
+     * {@link #mEmptyView}, show the {@link #mRecyclerView}
+     */
+    protected void updateVisibilityForEmptyView(boolean shouldShowEmptyView) {
+        mEmptyView.setVisibility(shouldShowEmptyView ? View.VISIBLE : View.GONE);
+        mRecyclerView.setVisibility(shouldShowEmptyView ? View.GONE : View.VISIBLE);
+    }
+
     private static String generateAddButtonString(Context context, int size) {
         final String sizeString = NumberFormat.getInstance(Locale.getDefault()).format(size);
         final String template = context.getString(R.string.picker_add_button_multi_select);