New feature “Text and reading options” for SetupWizard, Wallpaper, and Settings (6/n).

- Create new preview preference.
1) It will be integrated with TextReadingPreferenceFragment, and sub
previews layouts like app launchers, conversation messages, and mail
content.

Bug: 211503117
Test: make -j64 RunSettingsRoboTests ROBOTEST_FILTER=TextReadingPreviewPreferenceTest
Change-Id: I2d82d58438ee3f96c5a8dbb1809a0d6bdf3a00a2
diff --git a/res/drawable/accessibility_text_reading_preview.xml b/res/drawable/accessibility_text_reading_preview.xml
new file mode 100644
index 0000000..54f3a42
--- /dev/null
+++ b/res/drawable/accessibility_text_reading_preview.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+    Copyright (C) 2022 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.
+-->
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
+    android:shape="rectangle">
+
+    <corners android:radius="28dp" />
+    <solid android:color="?androidprv:attr/colorSurface" />
+</shape>
diff --git a/res/layout/accessibility_text_reading_preview.xml b/res/layout/accessibility_text_reading_preview.xml
new file mode 100644
index 0000000..9caed30
--- /dev/null
+++ b/res/layout/accessibility_text_reading_preview.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+    Copyright (C) 2022 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.
+-->
+
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
+    android:paddingStart="?android:attr/listPreferredItemPaddingStart">
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:background="@drawable/accessibility_text_reading_preview"
+        android:clipChildren="true"
+        android:orientation="vertical"
+        android:padding="18dp">
+
+        <TextView
+            android:id="@+id/preview_label"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/screen_zoom_preview_title"
+            style="@style/AccessibilityTextReadingPreviewTitle" />
+
+        <androidx.viewpager.widget.ViewPager
+            android:id="@+id/preview_pager"
+            android:layout_width="match_parent"
+            android:layout_height="217dp"
+            android:contentDescription="@string/preview_pager_content_description" />
+
+        <com.android.settings.widget.DotsPageIndicator
+            android:id="@+id/page_indicator"
+            style="@style/PreviewPagerPageIndicator"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center_horizontal"
+            android:padding="6dp"
+            android:visibility="gone" />
+    </LinearLayout>
+</FrameLayout>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 838d63b..7e70144 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -730,6 +730,12 @@
         <item name="android:background">?android:attr/selectableItemBackground</item>
     </style>
 
+    <style name="AccessibilityTextReadingPreviewTitle"
+        parent="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title" >
+        <item name="android:layout_gravity">center_horizontal</item>
+        <item name="android:padding">6dp</item>
+    </style>
+
     <style name="Widget.PopupWindow.Settings"
            parent="@android:style/Widget.DeviceDefault.PopupWindow">
         <item name="android:clipToPadding">true</item>
diff --git a/src/com/android/settings/accessibility/TextReadingPreviewPreference.java b/src/com/android/settings/accessibility/TextReadingPreviewPreference.java
new file mode 100644
index 0000000..1b9cc4b
--- /dev/null
+++ b/src/com/android/settings/accessibility/TextReadingPreviewPreference.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2022 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.settings.accessibility;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.View;
+
+import androidx.preference.Preference;
+import androidx.preference.PreferenceViewHolder;
+import androidx.viewpager.widget.ViewPager;
+
+import com.android.internal.util.Preconditions;
+import com.android.settings.R;
+import com.android.settings.display.PreviewPagerAdapter;
+import com.android.settings.widget.DotsPageIndicator;
+
+/**
+ * A {@link Preference} that could show the preview related to the text and reading options.
+ */
+final class TextReadingPreviewPreference extends Preference {
+    private int mCurrentItem;
+    private PreviewPagerAdapter mPreviewAdapter;
+
+    TextReadingPreviewPreference(Context context) {
+        super(context);
+        init();
+    }
+
+    TextReadingPreviewPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        init();
+    }
+
+    TextReadingPreviewPreference(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+        init();
+    }
+
+    TextReadingPreviewPreference(Context context, AttributeSet attrs, int defStyleAttr,
+            int defStyleRes) {
+        super(context, attrs, defStyleAttr, defStyleRes);
+        init();
+    }
+
+    @Override
+    public void onBindViewHolder(PreferenceViewHolder holder) {
+        super.onBindViewHolder(holder);
+
+        final ViewPager viewPager = (ViewPager) holder.findViewById(R.id.preview_pager);
+        final DotsPageIndicator pageIndicator =
+                (DotsPageIndicator) holder.findViewById(R.id.page_indicator);
+        updateAdapterIfNeeded(viewPager, pageIndicator, mPreviewAdapter);
+        updatePagerAndIndicator(viewPager, pageIndicator);
+    }
+
+    void setPreviewAdapter(PreviewPagerAdapter previewAdapter) {
+        if (previewAdapter != mPreviewAdapter) {
+            mPreviewAdapter = previewAdapter;
+            notifyChanged();
+        }
+    }
+
+    void setCurrentItem(int currentItem) {
+        Preconditions.checkNotNull(mPreviewAdapter,
+                "Preview adapter is null, you should init the preview adapter first");
+
+        if (currentItem != mCurrentItem) {
+            mCurrentItem = currentItem;
+            notifyChanged();
+        }
+    }
+
+    int getCurrentItem() {
+        return mCurrentItem;
+    }
+
+    private void updateAdapterIfNeeded(ViewPager viewPager, DotsPageIndicator pageIndicator,
+            PreviewPagerAdapter previewAdapter) {
+        if (viewPager.getAdapter() == previewAdapter) {
+            return;
+        }
+
+        viewPager.setAdapter(previewAdapter);
+
+        if (previewAdapter != null) {
+            pageIndicator.setViewPager(viewPager);
+        } else {
+            mCurrentItem = 0;
+        }
+    }
+
+    private void updatePagerAndIndicator(ViewPager viewPager, DotsPageIndicator pageIndicator) {
+        if (viewPager.getAdapter() == null) {
+            return;
+        }
+
+        if (viewPager.getCurrentItem() != mCurrentItem) {
+            viewPager.setCurrentItem(mCurrentItem);
+        }
+
+        pageIndicator.setVisibility(
+                viewPager.getAdapter().getCount() > 1 ? View.VISIBLE : View.GONE);
+    }
+
+    private void init() {
+        setLayoutResource(R.layout.accessibility_text_reading_preview);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/accessibility/TextReadingPreviewPreferenceTest.java b/tests/robotests/src/com/android/settings/accessibility/TextReadingPreviewPreferenceTest.java
new file mode 100644
index 0000000..6b9395a
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/TextReadingPreviewPreferenceTest.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2022 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.settings.accessibility;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.content.res.Configuration;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.LinearLayout;
+
+import androidx.preference.PreferenceViewHolder;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.viewpager.widget.ViewPager;
+
+import com.android.settings.R;
+import com.android.settings.display.PreviewPagerAdapter;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+
+/**
+ * Tests for {@link TextReadingPreferenceFragment}.
+ */
+@RunWith(RobolectricTestRunner.class)
+public class TextReadingPreviewPreferenceTest {
+
+    private TextReadingPreviewPreference mTextReadingPreviewPreference;
+    private PreferenceViewHolder mHolder;
+    private ViewPager mViewPager;
+    private PreviewPagerAdapter mPreviewPagerAdapter;
+
+    @Before
+    public void setUp() {
+        final Context context = ApplicationProvider.getApplicationContext();
+        final int[] sampleResIds = new int[]{1, 2, 3, 4, 5, 6};
+        final Configuration[] configurations = createConfigurations(6);
+        mTextReadingPreviewPreference = new TextReadingPreviewPreference(context);
+        mPreviewPagerAdapter =
+                new PreviewPagerAdapter(context, /* isLayoutRtl= */ false, sampleResIds,
+                        configurations);
+        final LayoutInflater inflater = LayoutInflater.from(context);
+        final View view =
+                inflater.inflate(mTextReadingPreviewPreference.getLayoutResource(),
+                        new LinearLayout(context), false);
+        mHolder = PreferenceViewHolder.createInstanceForTests(view);
+        mViewPager = view.findViewById(R.id.preview_pager);
+    }
+
+    @Test
+    public void setPreviewerAdapter_success() {
+        mTextReadingPreviewPreference.setPreviewAdapter(mPreviewPagerAdapter);
+        mTextReadingPreviewPreference.onBindViewHolder(mHolder);
+
+        assertThat(mViewPager.getAdapter()).isEqualTo(mPreviewPagerAdapter);
+    }
+
+    @Test
+    public void setPreviewAdapterWithNull_resetCurrentItem() {
+        final int currentItem = 2;
+        mTextReadingPreviewPreference.setPreviewAdapter(mPreviewPagerAdapter);
+        mTextReadingPreviewPreference.setCurrentItem(currentItem);
+        mTextReadingPreviewPreference.onBindViewHolder(mHolder);
+
+        mTextReadingPreviewPreference.setPreviewAdapter(null);
+        mTextReadingPreviewPreference.onBindViewHolder(mHolder);
+
+        assertThat(mTextReadingPreviewPreference.getCurrentItem()).isEqualTo(0);
+    }
+
+    @Test
+    public void setCurrentItem_success() {
+        final int currentItem = 3;
+        mTextReadingPreviewPreference.setPreviewAdapter(mPreviewPagerAdapter);
+        mTextReadingPreviewPreference.onBindViewHolder(mHolder);
+
+        mTextReadingPreviewPreference.setCurrentItem(currentItem);
+        mTextReadingPreviewPreference.onBindViewHolder(mHolder);
+
+        assertThat(mViewPager.getCurrentItem()).isEqualTo(currentItem);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void setCurrentItemBeforeSetPreviewAdapter_throwNPE() {
+        final int currentItem = 5;
+
+        mTextReadingPreviewPreference.setCurrentItem(currentItem);
+    }
+
+    private static Configuration[] createConfigurations(int count) {
+        final Configuration[] configurations = new Configuration[count];
+        for (int i = 0; i < count; i++) {
+            configurations[i] = new Configuration();
+        }
+
+        return configurations;
+    }
+}