Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8c0a4d0f authored by Alan Viverette's avatar Alan Viverette Committed by Android (Google) Code Review
Browse files

Merge "Clean up ImageView"

parents bbe8ad9d 6aa92d1c
Loading
Loading
Loading
Loading
+92 −99
Original line number Original line Diff line number Diff line
@@ -72,6 +72,8 @@ import java.io.InputStream;
 */
 */
@RemoteView
@RemoteView
public class ImageView extends View {
public class ImageView extends View {
    private static final String LOG_TAG = "ImageView";

    // settable by the client
    // settable by the client
    private Uri mUri;
    private Uri mUri;
    private int mResource = 0;
    private int mResource = 0;
@@ -87,7 +89,7 @@ public class ImageView extends View {
    private boolean mHasColorFilter = false;
    private boolean mHasColorFilter = false;
    private Xfermode mXfermode;
    private Xfermode mXfermode;
    private int mAlpha = 255;
    private int mAlpha = 255;
    private int mViewAlphaScale = 256;
    private final int mViewAlphaScale = 256;
    private boolean mColorMod = false;
    private boolean mColorMod = false;


    private Drawable mDrawable = null;
    private Drawable mDrawable = null;
@@ -105,8 +107,8 @@ public class ImageView extends View {
    private Matrix mDrawMatrix = null;
    private Matrix mDrawMatrix = null;


    // Avoid allocations...
    // Avoid allocations...
    private RectF mTempSrc = new RectF();
    private final RectF mTempSrc = new RectF();
    private RectF mTempDst = new RectF();
    private final RectF mTempDst = new RectF();


    private boolean mCropToPadding;
    private boolean mCropToPadding;


@@ -147,30 +149,21 @@ public class ImageView extends View {
        initImageView();
        initImageView();


        final TypedArray a = context.obtainStyledAttributes(
        final TypedArray a = context.obtainStyledAttributes(
                attrs, com.android.internal.R.styleable.ImageView, defStyleAttr, defStyleRes);
                attrs, R.styleable.ImageView, defStyleAttr, defStyleRes);


        Drawable d = a.getDrawable(com.android.internal.R.styleable.ImageView_src);
        final Drawable d = a.getDrawable(R.styleable.ImageView_src);
        if (d != null) {
        if (d != null) {
            setImageDrawable(d);
            setImageDrawable(d);
        }
        }


        mBaselineAlignBottom = a.getBoolean(
        mBaselineAlignBottom = a.getBoolean(R.styleable.ImageView_baselineAlignBottom, false);
                com.android.internal.R.styleable.ImageView_baselineAlignBottom, false);
        mBaseline = a.getDimensionPixelSize(R.styleable.ImageView_baseline, -1);

        mBaseline = a.getDimensionPixelSize(
                com.android.internal.R.styleable.ImageView_baseline, -1);

        setAdjustViewBounds(
            a.getBoolean(com.android.internal.R.styleable.ImageView_adjustViewBounds,
            false));

        setMaxWidth(a.getDimensionPixelSize(
                com.android.internal.R.styleable.ImageView_maxWidth, Integer.MAX_VALUE));


        setMaxHeight(a.getDimensionPixelSize(
        setAdjustViewBounds(a.getBoolean(R.styleable.ImageView_adjustViewBounds, false));
                com.android.internal.R.styleable.ImageView_maxHeight, Integer.MAX_VALUE));
        setMaxWidth(a.getDimensionPixelSize(R.styleable.ImageView_maxWidth, Integer.MAX_VALUE));
        setMaxHeight(a.getDimensionPixelSize(R.styleable.ImageView_maxHeight, Integer.MAX_VALUE));


        final int index = a.getInt(com.android.internal.R.styleable.ImageView_scaleType, -1);
        final int index = a.getInt(R.styleable.ImageView_scaleType, -1);
        if (index >= 0) {
        if (index >= 0) {
            setScaleType(sScaleTypeArray[index]);
            setScaleType(sScaleTypeArray[index]);
        }
        }
@@ -193,13 +186,13 @@ public class ImageView extends View {


        applyImageTint();
        applyImageTint();


        final int alpha = a.getInt(com.android.internal.R.styleable.ImageView_drawableAlpha, 255);
        final int alpha = a.getInt(R.styleable.ImageView_drawableAlpha, 255);
        if (alpha != 255) {
        if (alpha != 255) {
            setAlpha(alpha);
            setImageAlpha(alpha);
        }
        }


        mCropToPadding = a.getBoolean(
        mCropToPadding = a.getBoolean(
                com.android.internal.R.styleable.ImageView_cropToPadding, false);
                R.styleable.ImageView_cropToPadding, false);


        a.recycle();
        a.recycle();


@@ -209,8 +202,8 @@ public class ImageView extends View {
    private void initImageView() {
    private void initImageView() {
        mMatrix = new Matrix();
        mMatrix = new Matrix();
        mScaleType = ScaleType.FIT_CENTER;
        mScaleType = ScaleType.FIT_CENTER;
        mAdjustViewBoundsCompat = mContext.getApplicationInfo().targetSdkVersion <=
        mAdjustViewBoundsCompat = mContext.getApplicationInfo().targetSdkVersion
                Build.VERSION_CODES.JELLY_BEAN_MR1;
                <= Build.VERSION_CODES.JELLY_BEAN_MR1;
    }
    }


    @Override
    @Override
@@ -258,7 +251,8 @@ public class ImageView extends View {
    @Override
    @Override
    public void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
    public void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
        super.onPopulateAccessibilityEventInternal(event);
        super.onPopulateAccessibilityEventInternal(event);
        CharSequence contentDescription = getContentDescription();

        final CharSequence contentDescription = getContentDescription();
        if (!TextUtils.isEmpty(contentDescription)) {
        if (!TextUtils.isEmpty(contentDescription)) {
            event.getText().add(contentDescription);
            event.getText().add(contentDescription);
        }
        }
@@ -269,7 +263,7 @@ public class ImageView extends View {
     * to preserve the aspect ratio of its drawable
     * to preserve the aspect ratio of its drawable
     *
     *
     * @return whether to adjust the bounds of this view
     * @return whether to adjust the bounds of this view
     * to presrve the original aspect ratio of the drawable
     * to preserve the original aspect ratio of the drawable
     *
     *
     * @see #setAdjustViewBounds(boolean)
     * @see #setAdjustViewBounds(boolean)
     *
     *
@@ -436,9 +430,7 @@ public class ImageView extends View {
     */
     */
    @android.view.RemotableViewMethod
    @android.view.RemotableViewMethod
    public void setImageURI(@Nullable Uri uri) {
    public void setImageURI(@Nullable Uri uri) {
        if (mResource != 0 ||
        if (mResource != 0 || (mUri != uri && (uri == null || mUri == null || !uri.equals(mUri)))) {
                (mUri != uri &&
                 (uri == null || mUri == null || !uri.equals(mUri)))) {
            updateDrawable(null);
            updateDrawable(null);
            mResource = 0;
            mResource = 0;
            mUri = uri;
            mUri = uri;
@@ -787,8 +779,8 @@ public class ImageView extends View {
            return;
            return;
        }
        }


        Resources rsrc = getResources();
        final Resources res = getResources();
        if (rsrc == null) {
        if (res == null) {
            return;
            return;
        }
        }


@@ -798,12 +790,12 @@ public class ImageView extends View {
            try {
            try {
                d = mContext.getDrawable(mResource);
                d = mContext.getDrawable(mResource);
            } catch (Exception e) {
            } catch (Exception e) {
                Log.w("ImageView", "Unable to find resource: " + mResource, e);
                Log.w(LOG_TAG, "Unable to find resource: " + mResource, e);
                // Don't try again.
                // Don't try again.
                mUri = null;
                mUri = null;
            }
            }
        } else if (mUri != null) {
        } else if (mUri != null) {
            String scheme = mUri.getScheme();
            final String scheme = mUri.getScheme();
            if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) {
            if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) {
                try {
                try {
                    // Load drawable through Resources, to get the source density information
                    // Load drawable through Resources, to get the source density information
@@ -811,7 +803,7 @@ public class ImageView extends View {
                            mContext.getContentResolver().getResourceId(mUri);
                            mContext.getContentResolver().getResourceId(mUri);
                    d = r.r.getDrawable(r.id, mContext.getTheme());
                    d = r.r.getDrawable(r.id, mContext.getTheme());
                } catch (Exception e) {
                } catch (Exception e) {
                    Log.w("ImageView", "Unable to open content: " + mUri, e);
                    Log.w(LOG_TAG, "Unable to open content: " + mUri, e);
                }
                }
            } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)
            } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)
                    || ContentResolver.SCHEME_FILE.equals(scheme)) {
                    || ContentResolver.SCHEME_FILE.equals(scheme)) {
@@ -820,13 +812,13 @@ public class ImageView extends View {
                    stream = mContext.getContentResolver().openInputStream(mUri);
                    stream = mContext.getContentResolver().openInputStream(mUri);
                    d = Drawable.createFromStream(stream, null);
                    d = Drawable.createFromStream(stream, null);
                } catch (Exception e) {
                } catch (Exception e) {
                    Log.w("ImageView", "Unable to open content: " + mUri, e);
                    Log.w(LOG_TAG, "Unable to open content: " + mUri, e);
                } finally {
                } finally {
                    if (stream != null) {
                    if (stream != null) {
                        try {
                        try {
                            stream.close();
                            stream.close();
                        } catch (IOException e) {
                        } catch (IOException e) {
                            Log.w("ImageView", "Unable to close content: " + mUri, e);
                            Log.w(LOG_TAG, "Unable to close content: " + mUri, e);
                        }
                        }
                    }
                    }
                }
                }
@@ -835,7 +827,7 @@ public class ImageView extends View {
            }
            }


            if (d == null) {
            if (d == null) {
                System.out.println("resolveUri failed on bad bitmap uri: " + mUri);
                Log.w(LOG_TAG, "resolveUri failed on bad bitmap uri: " + mUri);
                // Don't try again.
                // Don't try again.
                mUri = null;
                mUri = null;
            }
            }
@@ -890,7 +882,7 @@ public class ImageView extends View {
    }
    }


    private void resizeFromDrawable() {
    private void resizeFromDrawable() {
        Drawable d = mDrawable;
        final Drawable d = mDrawable;
        if (d != null) {
        if (d != null) {
            int w = d.getIntrinsicWidth();
            int w = d.getIntrinsicWidth();
            if (w < 0) w = mDrawableWidth;
            if (w < 0) w = mDrawableWidth;
@@ -964,10 +956,10 @@ public class ImageView extends View {
            }
            }
        }
        }


        int pleft = mPaddingLeft;
        final int pleft = mPaddingLeft;
        int pright = mPaddingRight;
        final int pright = mPaddingRight;
        int ptop = mPaddingTop;
        final int ptop = mPaddingTop;
        int pbottom = mPaddingBottom;
        final int pbottom = mPaddingBottom;


        int widthSize;
        int widthSize;
        int heightSize;
        int heightSize;
@@ -986,7 +978,7 @@ public class ImageView extends View {


            if (desiredAspect != 0.0f) {
            if (desiredAspect != 0.0f) {
                // See what our actual aspect ratio is
                // See what our actual aspect ratio is
                float actualAspect = (float)(widthSize - pleft - pright) /
                final float actualAspect = (float)(widthSize - pleft - pright) /
                                        (heightSize - ptop - pbottom);
                                        (heightSize - ptop - pbottom);


                if (Math.abs(actualAspect - desiredAspect) > 0.0000001) {
                if (Math.abs(actualAspect - desiredAspect) > 0.0000001) {
@@ -1047,8 +1039,8 @@ public class ImageView extends View {
    private int resolveAdjustedSize(int desiredSize, int maxSize,
    private int resolveAdjustedSize(int desiredSize, int maxSize,
                                   int measureSpec) {
                                   int measureSpec) {
        int result = desiredSize;
        int result = desiredSize;
        int specMode = MeasureSpec.getMode(measureSpec);
        final int specMode = MeasureSpec.getMode(measureSpec);
        int specSize =  MeasureSpec.getSize(measureSpec);
        final int specSize =  MeasureSpec.getSize(measureSpec);
        switch (specMode) {
        switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.UNSPECIFIED:
                /* Parent says we can be as big as we want. Just don't be larger
                /* Parent says we can be as big as we want. Just don't be larger
@@ -1072,7 +1064,7 @@ public class ImageView extends View {


    @Override
    @Override
    protected boolean setFrame(int l, int t, int r, int b) {
    protected boolean setFrame(int l, int t, int r, int b) {
        boolean changed = super.setFrame(l, t, r, b);
        final boolean changed = super.setFrame(l, t, r, b);
        mHaveFrame = true;
        mHaveFrame = true;
        configureBounds();
        configureBounds();
        return changed;
        return changed;
@@ -1083,14 +1075,14 @@ public class ImageView extends View {
            return;
            return;
        }
        }


        int dwidth = mDrawableWidth;
        final int dwidth = mDrawableWidth;
        int dheight = mDrawableHeight;
        final int dheight = mDrawableHeight;


        int vwidth = getWidth() - mPaddingLeft - mPaddingRight;
        final int vwidth = getWidth() - mPaddingLeft - mPaddingRight;
        int vheight = getHeight() - mPaddingTop - mPaddingBottom;
        final int vheight = getHeight() - mPaddingTop - mPaddingBottom;


        boolean fits = (dwidth < 0 || vwidth == dwidth) &&
        final boolean fits = (dwidth < 0 || vwidth == dwidth)
                       (dheight < 0 || vheight == dheight);
                && (dheight < 0 || vheight == dheight);


        if (dwidth <= 0 || dheight <= 0 || ScaleType.FIT_XY == mScaleType) {
        if (dwidth <= 0 || dheight <= 0 || ScaleType.FIT_XY == mScaleType) {
            /* If the drawable has no intrinsic size, or we're told to
            /* If the drawable has no intrinsic size, or we're told to
@@ -1166,7 +1158,8 @@ public class ImageView extends View {
    @Override
    @Override
    protected void drawableStateChanged() {
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        super.drawableStateChanged();
        Drawable d = mDrawable;

        final Drawable d = mDrawable;
        if (d != null && d.isStateful()) {
        if (d != null && d.isStateful()) {
            d.setState(getDrawableState());
            d.setState(getDrawableState());
        }
        }
@@ -1213,7 +1206,7 @@ public class ImageView extends View {
        if (mDrawMatrix == null && mPaddingTop == 0 && mPaddingLeft == 0) {
        if (mDrawMatrix == null && mPaddingTop == 0 && mPaddingLeft == 0) {
            mDrawable.draw(canvas);
            mDrawable.draw(canvas);
        } else {
        } else {
            int saveCount = canvas.getSaveCount();
            final int saveCount = canvas.getSaveCount();
            canvas.save();
            canvas.save();


            if (mCropToPadding) {
            if (mCropToPadding) {