From 06df1f5e98fbfc06cfdd97037d13899cb805ad77 Mon Sep 17 00:00:00 2001 From: Fabian Kozynski Date: Mon, 20 Jul 2020 15:48:59 -0400 Subject: [PATCH] Add support for colors in PageIndicator Adds proper support for colors in PageIndicator (via android:tint attribute). By default, it will use the accent color. Test: manual Fixes: 157767613 Change-Id: I41288e182938d31c41b82f4a95ce3ac6b50c65e3 --- .../systemui/media/MediaCarouselController.kt | 2 +- .../android/systemui/qs/PageIndicator.java | 57 +++++++++++++++---- .../android/systemui/qs/PagedTileLayout.java | 1 - 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaCarouselController.kt b/packages/SystemUI/src/com/android/systemui/media/MediaCarouselController.kt index e12b7dd259a56..075318b3f1f7f 100644 --- a/packages/SystemUI/src/com/android/systemui/media/MediaCarouselController.kt +++ b/packages/SystemUI/src/com/android/systemui/media/MediaCarouselController.kt @@ -306,7 +306,7 @@ class MediaCarouselController @Inject constructor( private fun updatePageIndicator() { val numPages = mediaContent.getChildCount() - pageIndicator.setNumPages(numPages, Color.WHITE) + pageIndicator.setNumPages(numPages) if (numPages == 1) { pageIndicator.setLocation(0f) } diff --git a/packages/SystemUI/src/com/android/systemui/qs/PageIndicator.java b/packages/SystemUI/src/com/android/systemui/qs/PageIndicator.java index 2c76d70fb3cc8..f8655cc44d2ad 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/PageIndicator.java +++ b/packages/SystemUI/src/com/android/systemui/qs/PageIndicator.java @@ -10,10 +10,18 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; +import androidx.annotation.NonNull; + +import com.android.settingslib.Utils; import com.android.systemui.R; import java.util.ArrayList; +/** + * Page indicator for using with pageable layouts + * + * Supports {@code android.R.attr.tint}. If missing, it will use the current accent color. + */ public class PageIndicator extends ViewGroup { private static final String TAG = "PageIndicator"; @@ -31,12 +39,22 @@ public class PageIndicator extends ViewGroup { private final int mPageIndicatorWidth; private final int mPageIndicatorHeight; private final int mPageDotWidth; + private @NonNull ColorStateList mTint; private int mPosition = -1; private boolean mAnimating; public PageIndicator(Context context, AttributeSet attrs) { super(context, attrs); + + TypedArray array = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.tint}); + if (array.hasValue(0)) { + mTint = array.getColorStateList(0); + } else { + mTint = Utils.getColorAccent(context); + } + array.recycle(); + mPageIndicatorWidth = (int) mContext.getResources().getDimension(R.dimen.qs_page_indicator_width); mPageIndicatorHeight = @@ -45,15 +63,6 @@ public class PageIndicator extends ViewGroup { } public void setNumPages(int numPages) { - TypedArray array = getContext().obtainStyledAttributes( - new int[]{android.R.attr.colorControlActivated}); - int color = array.getColor(0, 0); - array.recycle(); - setNumPages(numPages, color); - } - - /** Overload of setNumPages that allows the indicator color to be specified.*/ - public void setNumPages(int numPages, int color) { setVisibility(numPages > 1 ? View.VISIBLE : View.GONE); if (numPages == getChildCount()) { return; @@ -67,13 +76,41 @@ public class PageIndicator extends ViewGroup { while (numPages > getChildCount()) { ImageView v = new ImageView(mContext); v.setImageResource(R.drawable.minor_a_b); - v.setImageTintList(ColorStateList.valueOf(color)); + v.setImageTintList(mTint); addView(v, new LayoutParams(mPageIndicatorWidth, mPageIndicatorHeight)); } // Refresh state. setIndex(mPosition >> 1); } + /** + * @return the current tint list for this view. + */ + @NonNull + public ColorStateList getTintList() { + return mTint; + } + + /** + * Set the color for this view. + *
+ * Calling this will change the color of the current view and any new dots that are added to it. + * @param color the new color + */ + public void setTintList(@NonNull ColorStateList color) { + if (color.equals(mTint)) { + return; + } + mTint = color; + final int N = getChildCount(); + for (int i = 0; i < N; i++) { + View v = getChildAt(i); + if (v instanceof ImageView) { + ((ImageView) v).setImageTintList(mTint); + } + } + } + public void setLocation(float location) { int index = (int) location; setContentDescription(getContext().getString(R.string.accessibility_quick_settings_page, diff --git a/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java b/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java index 3eed8ad89075f..560998b5d1d88 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java +++ b/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java @@ -508,7 +508,6 @@ public class PagedTileLayout extends ViewPager implements QSTileLayout { mPageListener.onPageChanged(isLayoutRtl() ? position == mPages.size() - 1 : position == 0); } - } @Override