Moved to the new Cropping library v4.3.3

This commit is contained in:
ThomasSession 2024-08-09 10:38:10 +10:00
parent 2028a8419e
commit 4bb583e7d9
3 changed files with 62 additions and 34 deletions

View File

@ -284,7 +284,7 @@ dependencies {
implementation 'com.pnikosis:materialish-progress:1.5'
implementation 'org.greenrobot:eventbus:3.0.0'
implementation 'pl.tajchert:waitingdots:0.1.0'
implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0'
implementation 'com.vanniktech:android-image-cropper:4.3.3'
implementation 'com.melnykov:floatingactionbutton:1.3.0'
implementation 'com.google.zxing:android-integration:3.1.0'
implementation 'mobi.upod:time-duration-picker:1.1.3'

View File

@ -8,10 +8,14 @@ import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import androidx.activity.result.ActivityResultLauncher
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import com.theartofdev.edmodo.cropper.CropImage
import com.theartofdev.edmodo.cropper.CropImageView
import com.canhub.cropper.CropImage
import com.canhub.cropper.CropImageContract
import com.canhub.cropper.CropImageContractOptions
import com.canhub.cropper.CropImageView
import com.canhub.cropper.options
import network.loki.messenger.R
import org.session.libsignal.utilities.ExternalStorageUtil.getImageDir
import org.session.libsignal.utilities.Log
@ -25,32 +29,30 @@ import java.util.LinkedList
object AvatarSelection {
private val TAG: String = AvatarSelection::class.java.simpleName
const val REQUEST_CODE_CROP_IMAGE: Int = CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE
const val REQUEST_CODE_AVATAR: Int = REQUEST_CODE_CROP_IMAGE + 1
const val REQUEST_CODE_IMAGE_PICK: Int = 888
/**
* Returns result on [.REQUEST_CODE_CROP_IMAGE]
*/
fun circularCropImage(
activity: Activity,
launcher: ActivityResultLauncher<CropImageContractOptions>,
inputFile: Uri?,
outputFile: Uri?,
@StringRes title: Int
) {
CropImage.activity(inputFile)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.setCropShape(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) CropImageView.CropShape.RECTANGLE else CropImageView.CropShape.OVAL)
.setOutputUri(outputFile)
.setAllowRotation(true)
.setAllowFlipping(true)
.setBackgroundColor(ContextCompat.getColor(activity, R.color.avatar_background))
.setActivityTitle(activity.getString(title))
.start(activity)
}
fun getResultUri(data: Intent?): Uri {
return CropImage.getActivityResult(data).uri
launcher.launch(
options(inputFile) {
setGuidelines(CropImageView.Guidelines.ON)
setAspectRatio(1, 1)
setCropShape(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) CropImageView.CropShape.RECTANGLE else CropImageView.CropShape.OVAL)
setOutputUri(outputFile)
setAllowRotation(true)
setAllowFlipping(true)
setBackgroundColor(ContextCompat.getColor(activity, R.color.avatar_background))
setActivityTitle(activity.getString(title))
}
)
}
/**
@ -80,7 +82,7 @@ object AvatarSelection {
}
val chooserIntent = createAvatarSelectionIntent(activity, captureFile, includeClear)
activity.startActivityForResult(chooserIntent, REQUEST_CODE_AVATAR)
activity.startActivityForResult(chooserIntent, REQUEST_CODE_IMAGE_PICK)
return captureFile
}

View File

@ -34,6 +34,8 @@ import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.canhub.cropper.CropImage
import com.canhub.cropper.CropImageContract
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
@ -109,6 +111,36 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
private val hexEncodedPublicKey: String get() = TextSecurePreferences.getLocalNumber(this)!!
private val onAvatarCropped = registerForActivityResult(CropImageContract()) { result ->
when {
result.isSuccessful -> {
Log.i(TAG, result.getUriFilePath(this).toString())
lifecycleScope.launch(Dispatchers.IO) {
try {
val profilePictureToBeUploaded =
BitmapUtil.createScaledBytes(
this@SettingsActivity,
result.getUriFilePath(this@SettingsActivity).toString(),
ProfileMediaConstraints()
).bitmap
launch(Dispatchers.Main) {
updateProfilePicture(profilePictureToBeUploaded)
}
} catch (e: BitmapDecodingException) {
Log.e(TAG, e)
}
}
}
result is CropImage.CancelledResult -> {
Log.i(TAG, "Cropping image was cancelled by the user")
}
else -> {
Log.e(TAG, "Cropping image failed")
}
}
}
companion object {
private const val SCROLL_STATE = "SCROLL_STATE"
}
@ -186,22 +218,16 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode != Activity.RESULT_OK) return
when (requestCode) {
AvatarSelection.REQUEST_CODE_AVATAR -> {
AvatarSelection.REQUEST_CODE_IMAGE_PICK -> {
val outputFile = Uri.fromFile(File(cacheDir, "cropped"))
val inputFile: Uri? = data?.data ?: tempFile?.let(Uri::fromFile)
AvatarSelection.circularCropImage(this, inputFile, outputFile, R.string.CropImageActivity_profile_avatar)
}
AvatarSelection.REQUEST_CODE_CROP_IMAGE -> {
lifecycleScope.launch(Dispatchers.IO) {
try {
val profilePictureToBeUploaded = BitmapUtil.createScaledBytes(this@SettingsActivity, AvatarSelection.getResultUri(data), ProfileMediaConstraints()).bitmap
launch(Dispatchers.Main) {
updateProfilePicture(profilePictureToBeUploaded)
}
} catch (e: BitmapDecodingException) {
Log.e(TAG, e)
}
}
AvatarSelection.circularCropImage(
activity = this,
launcher = onAvatarCropped,
inputFile = inputFile,
outputFile = outputFile,
title = R.string.CropImageActivity_profile_avatar
)
}
}
}