Merge changes from topic "max_height_a11y_illustration" into sc-v2-dev

* changes:
  Extends to support the restricted setMaxHeight() for illustrationPreference.
  Fix the bounds of the illustration view is larger than the background view in a foldable device.
This commit is contained in:
PETER LIANG
2021-09-28 05:43:21 +00:00
committed by Android (Google) Code Review
3 changed files with 74 additions and 3 deletions

View File

@@ -34,17 +34,21 @@
android:orientation="vertical"> android:orientation="vertical">
<ImageView <ImageView
android:id="@+id/background_view"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:scaleType="centerInside" android:layout_gravity="center"
android:adjustViewBounds="true"
android:src="@drawable/protection_background"/> android:src="@drawable/protection_background"/>
<com.airbnb.lottie.LottieAnimationView <com.airbnb.lottie.LottieAnimationView
android:id="@+id/lottie_view" android:id="@+id/lottie_view"
android:adjustViewBounds="true"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" /> android:layout_gravity="center"
android:maxWidth="@dimen/settingslib_illustration_width"
android:maxHeight="@dimen/settingslib_illustration_height"
android:adjustViewBounds="true"/>
<FrameLayout <FrameLayout
android:id="@+id/middleground_layout" android:id="@+id/middleground_layout"

View File

@@ -17,6 +17,7 @@
package com.android.settingslib.widget; package com.android.settingslib.widget;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.drawable.Animatable; import android.graphics.drawable.Animatable;
import android.graphics.drawable.Animatable2; import android.graphics.drawable.Animatable2;
@@ -50,7 +51,9 @@ public class IllustrationPreference extends Preference {
private static final String TAG = "IllustrationPreference"; private static final String TAG = "IllustrationPreference";
private static final boolean IS_ENABLED_LOTTIE_ADAPTIVE_COLOR = false; private static final boolean IS_ENABLED_LOTTIE_ADAPTIVE_COLOR = false;
private static final int SIZE_UNSPECIFIED = -1;
private int mMaxHeight = SIZE_UNSPECIFIED;
private int mImageResId; private int mImageResId;
private boolean mIsAutoScale; private boolean mIsAutoScale;
private Uri mImageUri; private Uri mImageUri;
@@ -98,6 +101,8 @@ public class IllustrationPreference extends Preference {
public void onBindViewHolder(PreferenceViewHolder holder) { public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder); super.onBindViewHolder(holder);
final ImageView backgroundView =
(ImageView) holder.findViewById(R.id.background_view);
final FrameLayout middleGroundLayout = final FrameLayout middleGroundLayout =
(FrameLayout) holder.findViewById(R.id.middleground_layout); (FrameLayout) holder.findViewById(R.id.middleground_layout);
final LottieAnimationView illustrationView = final LottieAnimationView illustrationView =
@@ -115,6 +120,7 @@ public class IllustrationPreference extends Preference {
illustrationFrame.setLayoutParams(lp); illustrationFrame.setLayoutParams(lp);
handleImageWithAnimation(illustrationView); handleImageWithAnimation(illustrationView);
handleImageFrameMaxHeight(backgroundView, illustrationView);
if (mIsAutoScale) { if (mIsAutoScale) {
illustrationView.setScaleType(mIsAutoScale illustrationView.setScaleType(mIsAutoScale
@@ -220,6 +226,19 @@ public class IllustrationPreference extends Preference {
return mImageUri; return mImageUri;
} }
/**
* Sets the maximum height of the views, still use the specific one if the maximum height was
* larger than the specific height from XML.
*
* @param maxHeight the maximum height of the frame views in terms of pixels.
*/
public void setMaxHeight(int maxHeight) {
if (maxHeight != mMaxHeight) {
mMaxHeight = maxHeight;
notifyChanged();
}
}
private void resetImageResourceCache() { private void resetImageResourceCache() {
mImageDrawable = null; mImageDrawable = null;
mImageUri = null; mImageUri = null;
@@ -274,6 +293,23 @@ public class IllustrationPreference extends Preference {
} }
} }
private void handleImageFrameMaxHeight(ImageView backgroundView, ImageView illustrationView) {
if (mMaxHeight == SIZE_UNSPECIFIED) {
return;
}
final Resources res = backgroundView.getResources();
final int frameWidth = res.getDimensionPixelSize(R.dimen.settingslib_illustration_width);
final int frameHeight = res.getDimensionPixelSize(R.dimen.settingslib_illustration_height);
final int restrictedMaxHeight = Math.min(mMaxHeight, frameHeight);
backgroundView.setMaxHeight(restrictedMaxHeight);
illustrationView.setMaxHeight(restrictedMaxHeight);
// Ensures the illustration view size is smaller than or equal to the background view size.
final float aspectRatio = (float) frameWidth / frameHeight;
illustrationView.setMaxWidth((int) (restrictedMaxHeight * aspectRatio));
}
private void startAnimation(Drawable drawable) { private void startAnimation(Drawable drawable) {
if (!(drawable instanceof Animatable)) { if (!(drawable instanceof Animatable)) {
return; return;

View File

@@ -55,6 +55,7 @@ public class IllustrationPreferenceTest {
@Mock @Mock
private ViewGroup mRootView; private ViewGroup mRootView;
private Uri mImageUri; private Uri mImageUri;
private ImageView mBackgroundView;
private LottieAnimationView mAnimationView; private LottieAnimationView mAnimationView;
private IllustrationPreference mPreference; private IllustrationPreference mPreference;
private PreferenceViewHolder mViewHolder; private PreferenceViewHolder mViewHolder;
@@ -66,6 +67,7 @@ public class IllustrationPreferenceTest {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mImageUri = new Uri.Builder().build(); mImageUri = new Uri.Builder().build();
mBackgroundView = new ImageView(mContext);
mAnimationView = spy(new LottieAnimationView(mContext)); mAnimationView = spy(new LottieAnimationView(mContext));
mMiddleGroundLayout = new FrameLayout(mContext); mMiddleGroundLayout = new FrameLayout(mContext);
final FrameLayout illustrationFrame = new FrameLayout(mContext); final FrameLayout illustrationFrame = new FrameLayout(mContext);
@@ -73,6 +75,7 @@ public class IllustrationPreferenceTest {
new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)); ViewGroup.LayoutParams.WRAP_CONTENT));
doReturn(mMiddleGroundLayout).when(mRootView).findViewById(R.id.middleground_layout); doReturn(mMiddleGroundLayout).when(mRootView).findViewById(R.id.middleground_layout);
doReturn(mBackgroundView).when(mRootView).findViewById(R.id.background_view);
doReturn(mAnimationView).when(mRootView).findViewById(R.id.lottie_view); doReturn(mAnimationView).when(mRootView).findViewById(R.id.lottie_view);
doReturn(illustrationFrame).when(mRootView).findViewById(R.id.illustration_frame); doReturn(illustrationFrame).when(mRootView).findViewById(R.id.illustration_frame);
mViewHolder = spy(PreferenceViewHolder.createInstanceForTests(mRootView)); mViewHolder = spy(PreferenceViewHolder.createInstanceForTests(mRootView));
@@ -155,4 +158,32 @@ public class IllustrationPreferenceTest {
verify(mAnimationView).setFailureListener(any()); verify(mAnimationView).setFailureListener(any());
} }
@Test
public void setMaxHeight_smallerThanRestrictedHeight_matchResult() {
final int restrictedHeight =
mContext.getResources().getDimensionPixelSize(
R.dimen.settingslib_illustration_height);
final int maxHeight = restrictedHeight - 200;
mPreference.setMaxHeight(maxHeight);
mPreference.onBindViewHolder(mViewHolder);
assertThat(mBackgroundView.getMaxHeight()).isEqualTo(maxHeight);
assertThat(mAnimationView.getMaxHeight()).isEqualTo(maxHeight);
}
@Test
public void setMaxHeight_largerThanRestrictedHeight_specificHeight() {
final int restrictedHeight =
mContext.getResources().getDimensionPixelSize(
R.dimen.settingslib_illustration_height);
final int maxHeight = restrictedHeight + 200;
mPreference.setMaxHeight(maxHeight);
mPreference.onBindViewHolder(mViewHolder);
assertThat(mBackgroundView.getMaxHeight()).isEqualTo(restrictedHeight);
assertThat(mAnimationView.getMaxHeight()).isEqualTo(restrictedHeight);
}
} }