Extends to support the restricted setMaxHeight() for illustrationPreference.

Bug: 191721423
Test: make RunSettingsLibRoboTests ROBOTEST_FILTER=com.android.settingslib.widget.IllustrationPreferenceTest
Change-Id: I5ed91b1d7f284881959176d748c10c4a5b234567
This commit is contained in:
Peter_Liang
2021-09-11 14:39:19 +08:00
parent d53e689d73
commit ba9aabb6fd
3 changed files with 68 additions and 0 deletions

View File

@@ -34,6 +34,7 @@
android:orientation="vertical">
<ImageView
android:id="@+id/background_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"

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);
}
}