Snap for 7989966 from c476673fe4bf92558c9d7e06d5f9d1724b8b5261 to sc-v2-release

Change-Id: I4d5817742c3bcd65294ddb52bcc9bfd667101150
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 5109b4f..f2d0f21 100755
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -69,6 +69,9 @@
     <!-- Content description indicating that the selected option is currently being previewed. [CHAR_LIMIT=NONE] -->
     <string name="option_previewed_description"><xliff:g name="style_name">%1$s</xliff:g>, currently previewed</string>
 
+    <!-- Content description indicating that the focused option can be selected and previewed if clicked. [CHAR_LIMIT=NONE] -->
+    <string name="option_change_applied_previewed_description"><xliff:g name="style_name">%1$s</xliff:g>, change selected and previewed</string>
+
     <!-- Sample text used to show a preview of a selected font [CHAR LIMIT=3] -->
     <string name="theme_font_example" translatable="false">ABC</string>
 
diff --git a/src/com/android/customization/picker/grid/GridFragment.java b/src/com/android/customization/picker/grid/GridFragment.java
index e09bcbe..9896edc 100644
--- a/src/com/android/customization/picker/grid/GridFragment.java
+++ b/src/com/android/customization/picker/grid/GridFragment.java
@@ -17,12 +17,14 @@
 
 import static com.android.wallpaper.widget.BottomActionBar.BottomAction.APPLY_TEXT;
 
+import android.content.Context;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.SurfaceView;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.accessibility.AccessibilityManager;
 import android.widget.Toast;
 
 import androidx.annotation.NonNull;
@@ -109,6 +111,12 @@
         setUpToolbar(view);
         mContent = view.findViewById(R.id.content_section);
         mOptionsContainer = view.findViewById(R.id.options_container);
+        AccessibilityManager accessibilityManager =
+                (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
+        if (accessibilityManager.isEnabled()) {
+            // Make Talkback focus won't reset when notifyDataSetChange
+            mOptionsContainer.setItemAnimator(null);
+        }
         mLoading = view.findViewById(R.id.loading_indicator);
         mError = view.findViewById(R.id.error_section);
 
diff --git a/src/com/android/customization/widget/OptionSelectorController.java b/src/com/android/customization/widget/OptionSelectorController.java
index aa54f4f..0f1b6a6 100644
--- a/src/com/android/customization/widget/OptionSelectorController.java
+++ b/src/com/android/customization/widget/OptionSelectorController.java
@@ -116,8 +116,6 @@
         if (!mOptions.contains(option)) {
             throw new IllegalArgumentException("Invalid option");
         }
-        updateActivatedStatus(mSelectedOption, false);
-        updateActivatedStatus(option, true);
         T lastSelectedOption = mSelectedOption;
         mSelectedOption = option;
         mAdapter.notifyItemChanged(mOptions.indexOf(option));
@@ -151,38 +149,6 @@
         }
     }
 
-    private void updateActivatedStatus(T option, boolean isActivated) {
-        int index = mOptions.indexOf(option);
-        if (index < 0) {
-            return;
-        }
-        RecyclerView.ViewHolder holder = mContainer.findViewHolderForAdapterPosition(index);
-        if (holder != null && holder.itemView != null) {
-            holder.itemView.setActivated(isActivated);
-
-            if (holder instanceof TileViewHolder) {
-                TileViewHolder tileHolder = (TileViewHolder) holder;
-                if (isActivated) {
-                    if (option == mAppliedOption && mCheckmarkStyle != CheckmarkStyle.NONE) {
-                        tileHolder.setContentDescription(mContainer.getContext(), option,
-                            R.string.option_applied_previewed_description);
-                    } else {
-                        tileHolder.setContentDescription(mContainer.getContext(), option,
-                            R.string.option_previewed_description);
-                    }
-                } else if (option == mAppliedOption && mCheckmarkStyle != CheckmarkStyle.NONE) {
-                    tileHolder.setContentDescription(mContainer.getContext(), option,
-                        R.string.option_applied_description);
-                } else {
-                    tileHolder.resetContentDescription();
-                }
-            }
-        } else {
-            // Item is not visible, make sure the item is re-bound when it becomes visible
-            mAdapter.notifyItemChanged(index);
-        }
-    }
-
     /**
      * Notify that a given option has changed.
      * @param option the option that changed
@@ -228,7 +194,16 @@
                 }
                 holder.itemView.setActivated(option.equals(mSelectedOption));
                 option.bindThumbnailTile(holder.tileView);
-                holder.itemView.setOnClickListener(view -> setSelectedOption(option));
+                holder.itemView.setOnClickListener(view -> {
+                    setSelectedOption(option);
+                    String title = option.getTitle();
+                    int stringId = R.string.option_previewed_description;
+                    if (mSelectedOption.equals(mAppliedOption)) {
+                        stringId = R.string.option_applied_previewed_description;
+                    }
+                    CharSequence cd = holder.itemView.getContext().getString(stringId, title);
+                    view.announceForAccessibility(cd);
+                });
 
                 Resources res = mContainer.getContext().getResources();
                 if (mCheckmarkStyle == CheckmarkStyle.CORNER && option.equals(mAppliedOption)) {
@@ -237,14 +212,14 @@
                                     mContainer.getContext().getTheme()),
                             Gravity.BOTTOM | Gravity.RIGHT,
                             res.getDimensionPixelSize(R.dimen.check_size),
-                            res.getDimensionPixelOffset(R.dimen.check_offset));
+                            res.getDimensionPixelOffset(R.dimen.check_offset), true);
                 } else if (mCheckmarkStyle == CheckmarkStyle.CENTER
                         && option.equals(mAppliedOption)) {
                     drawCheckmark(option, holder,
                             res.getDrawable(R.drawable.check_circle_grey_large,
                                     mContainer.getContext().getTheme()),
                             Gravity.CENTER, res.getDimensionPixelSize(R.dimen.center_check_size),
-                            0);
+                            0, true);
                 }  else if (mCheckmarkStyle == CheckmarkStyle.CENTER_CHANGE_COLOR_WHEN_NOT_SELECTED
                         && option.equals(mAppliedOption)) {
                     int drawableRes = option.equals(mSelectedOption)
@@ -254,12 +229,22 @@
                             res.getDrawable(drawableRes,
                                     mContainer.getContext().getTheme()),
                             Gravity.CENTER, res.getDimensionPixelSize(R.dimen.center_check_size),
-                            0);
+                            0, option.equals(mSelectedOption));
                 } else if (option.equals(mAppliedOption)) {
                     // Initialize with "previewed" description if we don't show checkmark
                     holder.setContentDescription(mContainer.getContext(), option,
                         R.string.option_previewed_description);
                 } else if (mCheckmarkStyle != CheckmarkStyle.NONE) {
+                    if (mCheckmarkStyle == CheckmarkStyle.CENTER_CHANGE_COLOR_WHEN_NOT_SELECTED) {
+                        if (option.equals(mSelectedOption)) {
+                            holder.setContentDescription(mContainer.getContext(), option,
+                                    R.string.option_previewed_description);
+                        } else {
+                            holder.setContentDescription(mContainer.getContext(), option,
+                                    R.string.option_change_applied_previewed_description);
+                        }
+                    }
+
                     holder.tileView.setForeground(null);
                 }
             }
@@ -271,7 +256,7 @@
 
             private void drawCheckmark(CustomizationOption<?> option, TileViewHolder holder,
                     Drawable checkmark, int gravity, @Dimension int checkSize,
-                    @Dimension int checkOffset) {
+                    @Dimension int checkOffset, boolean currentlyPreviewed) {
                 Drawable frame = holder.tileView.getForeground();
                 Drawable[] layers = {frame, checkmark};
                 if (frame == null) {
@@ -289,8 +274,13 @@
                 holder.tileView.setForeground(checkedFrame);
 
                 // Initialize the currently applied option
-                holder.setContentDescription(mContainer.getContext(), option,
-                        R.string.option_applied_previewed_description);
+                if (currentlyPreviewed) {
+                    holder.setContentDescription(mContainer.getContext(), option,
+                            R.string.option_applied_previewed_description);
+                } else {
+                    holder.setContentDescription(mContainer.getContext(), option,
+                            R.string.option_applied_description);
+                }
             }
         };