Merge "Fix TB can't visited all items in wallpapers list" into ub-launcher3-master
diff --git a/src/com/android/wallpaper/picker/CategoryFragment.java b/src/com/android/wallpaper/picker/CategoryFragment.java
index 5cf612c..09f41a4 100755
--- a/src/com/android/wallpaper/picker/CategoryFragment.java
+++ b/src/com/android/wallpaper/picker/CategoryFragment.java
@@ -74,6 +74,7 @@
 import com.android.wallpaper.widget.LockScreenOverlayUpdater;
 import com.android.wallpaper.widget.PreviewPager;
 import com.android.wallpaper.widget.WallpaperColorsLoader;
+import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate;
 
 import com.bumptech.glide.Glide;
 import com.bumptech.glide.MemoryCategory;
@@ -88,7 +89,8 @@
  * Displays the Main UI for picking a category of wallpapers to choose from.
  */
 public class CategoryFragment extends AppbarFragment
-        implements CategorySelectorFragmentHost, ThumbnailUpdater, WallpaperDestinationCallback {
+        implements CategorySelectorFragmentHost, ThumbnailUpdater, WallpaperDestinationCallback,
+        WallpaperPickerRecyclerViewAccessibilityDelegate.BottomSheetHost {
 
     private final Rect mPreviewLocalRect = new Rect();
     private final Rect mPreviewGlobalRect = new Rect();
diff --git a/src/com/android/wallpaper/picker/CategorySelectorFragment.java b/src/com/android/wallpaper/picker/CategorySelectorFragment.java
index ff0855d..0121c4f 100644
--- a/src/com/android/wallpaper/picker/CategorySelectorFragment.java
+++ b/src/com/android/wallpaper/picker/CategorySelectorFragment.java
@@ -28,7 +28,6 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.view.accessibility.AccessibilityEvent;
 import android.widget.ImageView;
 import android.widget.ProgressBar;
 import android.widget.TextView;
@@ -40,7 +39,6 @@
 import androidx.fragment.app.Fragment;
 import androidx.recyclerview.widget.GridLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
-import androidx.recyclerview.widget.RecyclerViewAccessibilityDelegate;
 
 import com.android.wallpaper.R;
 import com.android.wallpaper.asset.Asset;
@@ -49,9 +47,10 @@
 import com.android.wallpaper.module.UserEventLogger;
 import com.android.wallpaper.util.DisplayMetricsRetriever;
 import com.android.wallpaper.util.SizeCalculator;
+import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate;
+import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate.BottomSheetHost;
 
 import com.bumptech.glide.Glide;
-import com.google.android.material.bottomsheet.BottomSheetBehavior;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -89,16 +88,6 @@
          * Sets the title in the toolbar.
          */
         void setToolbarTitle(CharSequence title);
-
-        /**
-         * Expand the bottom sheet if it's not expanded.
-         */
-        void expandBottomSheet();
-
-        /**
-         * Get bottom sheet current state.
-         */
-        int getBottomSheetState();
     }
 
     private RecyclerView mImageGrid;
@@ -133,7 +122,8 @@
             return windowInsets;
         });
         mImageGrid.setAccessibilityDelegateCompat(
-                new CategoryRecyclerViewAccessibilityDelegate(mImageGrid));
+                new WallpaperPickerRecyclerViewAccessibilityDelegate(
+                        mImageGrid, (BottomSheetHost) getParentFragment(), getNumColumns()));
         getCategorySelectorFragmentHost().setToolbarTitle(getText(R.string.wallpaper_title));
 
         return view;
@@ -465,29 +455,4 @@
             return 1;
         }
     }
-
-    private class CategoryRecyclerViewAccessibilityDelegate
-            extends RecyclerViewAccessibilityDelegate {
-
-        CategoryRecyclerViewAccessibilityDelegate(@NonNull RecyclerView recyclerView) {
-            super(recyclerView);
-        }
-
-        @Override
-        public boolean onRequestSendAccessibilityEvent(
-                ViewGroup host, View child, AccessibilityEvent event) {
-            int itemPos;
-            if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED) {
-                itemPos = mImageGrid.getChildLayoutPosition(child);
-
-                // Expand the bottom sheet when TB travel to second column.
-                if (getCategorySelectorFragmentHost() != null
-                        && (getCategorySelectorFragmentHost()).getBottomSheetState()
-                        != BottomSheetBehavior.STATE_EXPANDED && itemPos >= getNumColumns()) {
-                    (getCategorySelectorFragmentHost()).expandBottomSheet();
-                }
-            }
-            return super.onRequestSendAccessibilityEvent(host, child, event);
-        }
-    }
 }
diff --git a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
index 2fee8f1..88c9966 100755
--- a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
+++ b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
@@ -91,6 +91,8 @@
 import com.android.wallpaper.util.SizeCalculator;
 import com.android.wallpaper.widget.BottomActionBar;
 import com.android.wallpaper.widget.WallpaperInfoView;
+import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate;
+import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate.BottomSheetHost;
 
 import com.bumptech.glide.Glide;
 import com.bumptech.glide.MemoryCategory;
@@ -449,6 +451,9 @@
             return windowInsets;
         });
 
+        mImageGrid.setAccessibilityDelegateCompat(
+                new WallpaperPickerRecyclerViewAccessibilityDelegate(
+                        mImageGrid, (BottomSheetHost) getParentFragment(), getNumColumns()));
         maybeSetUpImageGrid();
         setUpBottomSheet();
         return view;
diff --git a/src/com/android/wallpaper/widget/WallpaperPickerRecyclerViewAccessibilityDelegate.java b/src/com/android/wallpaper/widget/WallpaperPickerRecyclerViewAccessibilityDelegate.java
new file mode 100644
index 0000000..b983780
--- /dev/null
+++ b/src/com/android/wallpaper/widget/WallpaperPickerRecyclerViewAccessibilityDelegate.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.wallpaper.widget;
+
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.accessibility.AccessibilityEvent;
+
+import androidx.recyclerview.widget.RecyclerView;
+import androidx.recyclerview.widget.RecyclerViewAccessibilityDelegate;
+
+import com.google.android.material.bottomsheet.BottomSheetBehavior;
+
+/**
+ * An AccessibilityDelegate class used to fix some RecyclerView-related Talkback issues.
+ * <p>
+ * This is to fix a bug which TalkBack can't visit all wallpaper category/wallpaper items.
+ */
+public class WallpaperPickerRecyclerViewAccessibilityDelegate
+        extends RecyclerViewAccessibilityDelegate {
+
+    /**
+     * Interface to be implemented by an Fragment hosting a
+     * {@link WallpaperPickerRecyclerViewAccessibilityDelegate}
+     */
+    public interface BottomSheetHost {
+        /**
+         * Expands the bottom sheet if it's not expanded.
+         */
+        void expandBottomSheet();
+
+        /**
+         * Gets bottom sheet current state.
+         */
+        int getBottomSheetState();
+    }
+
+    private final RecyclerView mGridRecyclerView;
+    private final BottomSheetHost mBottomSheetHost;
+    private final int mColumns;
+
+    public WallpaperPickerRecyclerViewAccessibilityDelegate(
+            RecyclerView recyclerView, BottomSheetHost bottomSheetHost, int columns) {
+        super(recyclerView);
+        mGridRecyclerView = recyclerView;
+        mBottomSheetHost = bottomSheetHost;
+        mColumns = columns;
+    }
+
+    @Override
+    public boolean onRequestSendAccessibilityEvent(
+            ViewGroup host, View child, AccessibilityEvent event) {
+        if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED) {
+            int itemPos = mGridRecyclerView.getChildLayoutPosition(child);
+
+            // Expand the bottom sheet when TB travel to second column.
+            if (mBottomSheetHost != null && mBottomSheetHost.getBottomSheetState()
+                    != BottomSheetBehavior.STATE_EXPANDED && itemPos >= mColumns) {
+                mBottomSheetHost.expandBottomSheet();
+            }
+        }
+        return super.onRequestSendAccessibilityEvent(host, child, event);
+    }
+}