Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#423 Fixed accidentally swiping back on newer Android devices when tr… #425

Merged
merged 3 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Fixed the mistake in hindi conversion of "Crop" [#402](https://github.com/CanHub/Android-Image-Cropper/issues/402)
- Added the option to set custom color to toolbar of CropImageActivity [#421](https://github.com/CanHub/Android-Image-Cropper/issues/421)
- Added the option to set custom background color to activity of CropImageActivity [#421](https://github.com/CanHub/Android-Image-Cropper/issues/421)
- Fixed accidentally swiping back on newer Android devices when trying to resize the crop window [#423](https://github.com/CanHub/Android-Image-Cropper/issues/423)

## [4.3.1] - 20/07/2022
### Fix
Expand Down
41 changes: 41 additions & 0 deletions cropper/src/main/java/com/canhub/cropper/CropOverlayView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.canhub.cropper

import android.annotation.TargetApi
import android.content.Context
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
Expand All @@ -12,10 +13,12 @@ import android.graphics.Region
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.MotionEvent
import android.view.ScaleGestureDetector
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener
import android.view.View
import androidx.annotation.RequiresApi
import com.canhub.cropper.CropImageView.CropShape
import com.canhub.cropper.CropImageView.Guidelines
import com.canhub.cropper.common.CommonVersionCheck
Expand Down Expand Up @@ -193,6 +196,9 @@ class CropOverlayView
/** Used to set back LayerType after changing to software. */
private var mOriginalLayerType: Int? = null

/** The maximum vertical gesture exclusion allowed by Android (200dp) in px. **/
private val maxVerticalGestureExclusion = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200f, Resources.getSystem().displayMetrics)

Check warning

Code scanning / detekt

Line detected that is longer than the defined maximum line length in the code style.

Line detected that is longer than the defined maximum line length in the code style.

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers.

This expression contains a magic number. Consider defining it to a well named constant.

/** Set the crop window change listener. */
fun setCropWindowChangeListener(listener: CropWindowChangeListener?) {
mCropWindowChangeListener = listener
Expand Down Expand Up @@ -628,6 +634,41 @@ class CropOverlayView
drawCropLabelText(canvas)
drawBorders(canvas)
drawCorners(canvas)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
setSystemGestureExclusionRects()
}
}

/**
* Newer Android phones let you go back by swiping from the left or right edge of the screen inwards.
* When the crop window is near the edge it's easy to accidentally swipe back when trying to resize it.
* This can be prevented by setting systemGestureExclusionRects. However Android lets you only exclude max 200dp in total vertically.

Check warning

Code scanning / detekt

Line detected that is longer than the defined maximum line length in the code style.

Line detected that is longer than the defined maximum line length in the code style.
* Therefore a top, middle and bottom strip are used so at least the corners and the vertical middle of the crop window are covered.

Check warning

Code scanning / detekt

Line detected that is longer than the defined maximum line length in the code style.

Line detected that is longer than the defined maximum line length in the code style.
* **/
@RequiresApi(Build.VERSION_CODES.Q)
private fun setSystemGestureExclusionRects() {
val cropWindowRect = mCropWindowHandler.getRect()
val rectTop = systemGestureExclusionRects.getOrElse(0) { Rect() }
val rectMiddle = systemGestureExclusionRects.getOrElse(1) { Rect() }
val rectBottom = systemGestureExclusionRects.getOrElse(2) { Rect() }

rectTop.left = (cropWindowRect.left - mTouchRadius).toInt()
rectTop.right = (cropWindowRect.right + mTouchRadius).toInt()
rectTop.top = (cropWindowRect.top - mTouchRadius).toInt()
rectTop.bottom = (rectTop.top + (maxVerticalGestureExclusion * 0.3f)).toInt()

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers.

This expression contains a magic number. Consider defining it to a well named constant.

rectMiddle.left = rectTop.left
rectMiddle.right = rectTop.right
rectMiddle.top = ((cropWindowRect.top + cropWindowRect.bottom)/2.0f - (maxVerticalGestureExclusion * 0.2f)).toInt()
Fixed Show fixed Hide fixed
rectMiddle.bottom = (rectMiddle.top + (maxVerticalGestureExclusion * 0.4f)).toInt()

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers.

This expression contains a magic number. Consider defining it to a well named constant.

rectBottom.left = rectTop.left
rectBottom.right = rectTop.right
rectBottom.bottom = (cropWindowRect.bottom + mTouchRadius).toInt()
rectBottom.top = (rectBottom.bottom - (maxVerticalGestureExclusion * 0.3f)).toInt()

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers.

This expression contains a magic number. Consider defining it to a well named constant.

systemGestureExclusionRects = listOf(rectTop, rectMiddle, rectBottom)
}

/** Draws a text label (which can acts an helper text) on top of crop overlay **/
Expand Down