From 5ff8942a8e160269c98deefbfb16e53b812b74e9 Mon Sep 17 00:00:00 2001 From: Joshua Trask Date: Wed, 9 Nov 2022 11:28:07 -0500 Subject: Extract ChooserActivity.RoundedRectImageView This is a trivial code move (diffs show only minor formatting changes [EDIT: a later snapshot also gets rid of the `` tags in our layouts, which were only necessary to reference this as an inner class, in favor of the more common practice of using an XML element with the same fully-qualified name). As an inner class, it had already been marked `static`, so there's no correctness concern (EDIT: on the Java side...) The only "client" is the (soon-to-be-extracted) preview UI, but instead of moving this to an inner class of the extracted component, it seems reasonable for this class to stand alone -- it has a simple design that's plausibly reusable in other applications, since it doesn't really encapsulate any requirements that are specific to Sharesheet. Bug: 202167050 Test: atest IntentResolverUnitTests Change-Id: I63833626925896a0baaa06d729233cd7037ac730 --- java/res/layout/chooser_grid_preview_file.xml | 2 +- java/res/layout/chooser_grid_preview_image.xml | 8 +- java/res/layout/chooser_grid_preview_text.xml | 2 +- .../android/intentresolver/ChooserActivity.java | 110 +---------------- .../widget/RoundedRectImageView.java | 131 +++++++++++++++++++++ 5 files changed, 138 insertions(+), 115 deletions(-) create mode 100644 java/src/com/android/intentresolver/widget/RoundedRectImageView.java (limited to 'java') diff --git a/java/res/layout/chooser_grid_preview_file.xml b/java/res/layout/chooser_grid_preview_file.xml index 6d2e76a0..c3392704 100644 --- a/java/res/layout/chooser_grid_preview_file.xml +++ b/java/res/layout/chooser_grid_preview_file.xml @@ -37,7 +37,7 @@ android:layout_marginBottom="@dimen/chooser_view_spacing" android:id="@androidprv:id/content_preview_file_layout"> - - - - - - 0) { - this.mExtraImageCount = "+" + count; - } else { - this.mExtraImageCount = null; - } - } - - @Override - protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) { - super.onSizeChanged(width, height, oldWidth, oldHeight); - updatePath(width, height); - } - - @Override - protected void onDraw(Canvas canvas) { - if (mRadius != 0) { - canvas.clipPath(mPath); - } - - super.onDraw(canvas); - - int x = getPaddingLeft(); - int y = getPaddingRight(); - int width = getWidth() - getPaddingRight() - getPaddingLeft(); - int height = getHeight() - getPaddingBottom() - getPaddingTop(); - if (mExtraImageCount != null) { - canvas.drawRect(x, y, width, height, mOverlayPaint); - - int xPos = canvas.getWidth() / 2; - int yPos = (int) ((canvas.getHeight() / 2.0f) - - ((mTextPaint.descent() + mTextPaint.ascent()) / 2.0f)); - - canvas.drawText(mExtraImageCount, xPos, yPos, mTextPaint); - } - - canvas.drawRoundRect(x, y, width, height, mRadius, mRadius, mRoundRectPaint); - } - } - /** * A helper class to track app's readiness for the scene transition animation. * The app is ready when both the image is laid out and the drawer offset is calculated. diff --git a/java/src/com/android/intentresolver/widget/RoundedRectImageView.java b/java/src/com/android/intentresolver/widget/RoundedRectImageView.java new file mode 100644 index 00000000..cf7bd543 --- /dev/null +++ b/java/src/com/android/intentresolver/widget/RoundedRectImageView.java @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2008 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.intentresolver.widget; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Path; +import android.util.AttributeSet; +import android.widget.ImageView; + +import com.android.intentresolver.R; + +/** + * {@link ImageView} that rounds the corners around the presented image while obeying view padding. + */ +public class RoundedRectImageView extends ImageView { + private int mRadius = 0; + private Path mPath = new Path(); + private Paint mOverlayPaint = new Paint(0); + private Paint mRoundRectPaint = new Paint(0); + private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private String mExtraImageCount = null; + + public RoundedRectImageView(Context context) { + super(context); + } + + public RoundedRectImageView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public RoundedRectImageView(Context context, AttributeSet attrs, int defStyleAttr) { + this(context, attrs, defStyleAttr, 0); + } + + public RoundedRectImageView( + Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + mRadius = context.getResources().getDimensionPixelSize(R.dimen.chooser_corner_radius); + + mOverlayPaint.setColor(0x99000000); + mOverlayPaint.setStyle(Paint.Style.FILL); + + mRoundRectPaint.setColor(context.getResources().getColor(R.color.chooser_row_divider)); + mRoundRectPaint.setStyle(Paint.Style.STROKE); + mRoundRectPaint.setStrokeWidth(context.getResources() + .getDimensionPixelSize(R.dimen.chooser_preview_image_border)); + + mTextPaint.setColor(Color.WHITE); + mTextPaint.setTextSize(context.getResources() + .getDimensionPixelSize(R.dimen.chooser_preview_image_font_size)); + mTextPaint.setTextAlign(Paint.Align.CENTER); + } + + private void updatePath(int width, int height) { + mPath.reset(); + + int imageWidth = width - getPaddingRight() - getPaddingLeft(); + int imageHeight = height - getPaddingBottom() - getPaddingTop(); + mPath.addRoundRect(getPaddingLeft(), getPaddingTop(), imageWidth, imageHeight, mRadius, + mRadius, Path.Direction.CW); + } + + /** + * Sets the corner radius on all corners + * + * param radius 0 for no radius, > 0 for a visible corner radius + */ + public void setRadius(int radius) { + mRadius = radius; + updatePath(getWidth(), getHeight()); + } + + /** + * Display an overlay with extra image count on 3rd image + */ + public void setExtraImageCount(int count) { + if (count > 0) { + this.mExtraImageCount = "+" + count; + } else { + this.mExtraImageCount = null; + } + } + + @Override + protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) { + super.onSizeChanged(width, height, oldWidth, oldHeight); + updatePath(width, height); + } + + @Override + protected void onDraw(Canvas canvas) { + if (mRadius != 0) { + canvas.clipPath(mPath); + } + + super.onDraw(canvas); + + int x = getPaddingLeft(); + int y = getPaddingRight(); + int width = getWidth() - getPaddingRight() - getPaddingLeft(); + int height = getHeight() - getPaddingBottom() - getPaddingTop(); + if (mExtraImageCount != null) { + canvas.drawRect(x, y, width, height, mOverlayPaint); + + int xPos = canvas.getWidth() / 2; + int yPos = (int) ((canvas.getHeight() / 2.0f) + - ((mTextPaint.descent() + mTextPaint.ascent()) / 2.0f)); + + canvas.drawText(mExtraImageCount, xPos, yPos, mTextPaint); + } + + canvas.drawRoundRect(x, y, width, height, mRadius, mRadius, mRoundRectPaint); + } +} -- cgit v1.2.3-59-g8ed1b