Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 44a275dd authored by Peter_Liang's avatar Peter_Liang Committed by PETER LIANG
Browse files

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
parent 5cdebd79
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
<?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>
+54 −0
Original line number Diff line number Diff line
<?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>
+6 −0
Original line number Diff line number Diff line
@@ -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>
+123 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}
+115 −0
Original line number Diff line number Diff line
/*
 * 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;
    }
}