Merge changes from topic "max_height_a11y_illustration" into sc-v2-dev am: c1fb8bb5d2

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15810429

Change-Id: I8cbdf783a357c34f9fbf998f4e92df8bbaa0272a
This commit is contained in:
PETER LIANG
2021-09-28 05:58:53 +00:00
committed by Automerger Merge Worker
3 changed files with 74 additions and 3 deletions

View File

@@ -34,17 +34,21 @@
android:orientation="vertical">
<ImageView
android:id="@+id/background_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:src="@drawable/protection_background"/>
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/lottie_view"
android:adjustViewBounds="true"
android:layout_width="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
android:id="@+id/middleground_layout"

View File

@@ -17,6 +17,7 @@
package com.android.settingslib.widget;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Animatable2;
@@ -50,7 +51,9 @@ public class IllustrationPreference extends Preference {
private static final String TAG = "IllustrationPreference";
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 boolean mIsAutoScale;
private Uri mImageUri;
@@ -98,6 +101,8 @@ public class IllustrationPreference extends Preference {
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
final ImageView backgroundView =
(ImageView) holder.findViewById(R.id.background_view);
final FrameLayout middleGroundLayout =
(FrameLayout) holder.findViewById(R.id.middleground_layout);
final LottieAnimationView illustrationView =
@@ -115,6 +120,7 @@ public class IllustrationPreference extends Preference {
illustrationFrame.setLayoutParams(lp);
handleImageWithAnimation(illustrationView);
handleImageFrameMaxHeight(backgroundView, illustrationView);
if (mIsAutoScale) {
illustrationView.setScaleType(mIsAutoScale
@@ -220,6 +226,19 @@ public class IllustrationPreference extends Preference {
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() {
mImageDrawable = 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) {
if (!(drawable instanceof Animatable)) {
return;

View File

@@ -55,6 +55,7 @@ public class IllustrationPreferenceTest {
@Mock
private ViewGroup mRootView;
private Uri mImageUri;
private ImageView mBackgroundView;
private LottieAnimationView mAnimationView;
private IllustrationPreference mPreference;
private PreferenceViewHolder mViewHolder;
@@ -66,6 +67,7 @@ public class IllustrationPreferenceTest {
MockitoAnnotations.initMocks(this);
mImageUri = new Uri.Builder().build();
mBackgroundView = new ImageView(mContext);
mAnimationView = spy(new LottieAnimationView(mContext));
mMiddleGroundLayout = new FrameLayout(mContext);
final FrameLayout illustrationFrame = new FrameLayout(mContext);
@@ -73,6 +75,7 @@ public class IllustrationPreferenceTest {
new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
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(illustrationFrame).when(mRootView).findViewById(R.id.illustration_frame);
mViewHolder = spy(PreferenceViewHolder.createInstanceForTests(mRootView));
@@ -155,4 +158,32 @@ public class IllustrationPreferenceTest {
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);
}
}