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 'com.pnikosis:materialish-progress:1.5'
implementation 'org.greenrobot:eventbus:3.0.0' implementation 'org.greenrobot:eventbus:3.0.0'
implementation 'pl.tajchert:waitingdots:0.1.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.melnykov:floatingactionbutton:1.3.0'
implementation 'com.google.zxing:android-integration:3.1.0' implementation 'com.google.zxing:android-integration:3.1.0'
implementation 'mobi.upod:time-duration-picker:1.1.3' 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.net.Uri
import android.os.Build import android.os.Build
import android.provider.MediaStore import android.provider.MediaStore
import androidx.activity.result.ActivityResultLauncher
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import com.theartofdev.edmodo.cropper.CropImage import com.canhub.cropper.CropImage
import com.theartofdev.edmodo.cropper.CropImageView 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 network.loki.messenger.R
import org.session.libsignal.utilities.ExternalStorageUtil.getImageDir import org.session.libsignal.utilities.ExternalStorageUtil.getImageDir
import org.session.libsignal.utilities.Log import org.session.libsignal.utilities.Log
@ -25,32 +29,30 @@ import java.util.LinkedList
object AvatarSelection { object AvatarSelection {
private val TAG: String = AvatarSelection::class.java.simpleName 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_IMAGE_PICK: Int = 888
const val REQUEST_CODE_AVATAR: Int = REQUEST_CODE_CROP_IMAGE + 1
/** /**
* Returns result on [.REQUEST_CODE_CROP_IMAGE] * Returns result on [.REQUEST_CODE_CROP_IMAGE]
*/ */
fun circularCropImage( fun circularCropImage(
activity: Activity, activity: Activity,
launcher: ActivityResultLauncher<CropImageContractOptions>,
inputFile: Uri?, inputFile: Uri?,
outputFile: Uri?, outputFile: Uri?,
@StringRes title: Int @StringRes title: Int
) { ) {
CropImage.activity(inputFile) launcher.launch(
.setGuidelines(CropImageView.Guidelines.ON) options(inputFile) {
.setAspectRatio(1, 1) setGuidelines(CropImageView.Guidelines.ON)
.setCropShape(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) CropImageView.CropShape.RECTANGLE else CropImageView.CropShape.OVAL) setAspectRatio(1, 1)
.setOutputUri(outputFile) setCropShape(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) CropImageView.CropShape.RECTANGLE else CropImageView.CropShape.OVAL)
.setAllowRotation(true) setOutputUri(outputFile)
.setAllowFlipping(true) setAllowRotation(true)
.setBackgroundColor(ContextCompat.getColor(activity, R.color.avatar_background)) setAllowFlipping(true)
.setActivityTitle(activity.getString(title)) setBackgroundColor(ContextCompat.getColor(activity, R.color.avatar_background))
.start(activity) setActivityTitle(activity.getString(title))
} }
)
fun getResultUri(data: Intent?): Uri {
return CropImage.getActivityResult(data).uri
} }
/** /**
@ -80,7 +82,7 @@ object AvatarSelection {
} }
val chooserIntent = createAvatarSelectionIntent(activity, captureFile, includeClear) val chooserIntent = createAvatarSelectionIntent(activity, captureFile, includeClear)
activity.startActivityForResult(chooserIntent, REQUEST_CODE_AVATAR) activity.startActivityForResult(chooserIntent, REQUEST_CODE_IMAGE_PICK)
return captureFile return captureFile
} }

View File

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