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

Migrate image picker to new Photo Picker (Android 13 compatibility) #499

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 4 additions & 14 deletions android/src/main/java/org/ligi/passandroid/ui/PassEditActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PassEditActivity : AppCompatActivity() {

private lateinit var binding: EditBinding
private lateinit var currentPass: PassImpl
private val imageEditHelper by lazy { ImageEditHelper(this, passStore) }
private lateinit var imageEditHelper: ImageEditHelper

internal val passStore: PassStore by inject()

Expand All @@ -50,7 +50,7 @@ class PassEditActivity : AppCompatActivity() {
when (i) {
0 -> showCategoryPickDialog(this@PassEditActivity, currentPass, refreshCallback)
1 -> showColorPickDialog(this@PassEditActivity, currentPass, refreshCallback)
2 -> pickWithPermissionCheck(ImageEditHelper.REQ_CODE_PICK_ICON)
2 -> imageEditHelper.startPick(ImageEditHelper.REQ_CODE_PICK_ICON)
}
}.show()
}
Expand Down Expand Up @@ -78,22 +78,12 @@ class PassEditActivity : AppCompatActivity() {
[email protected],
BarCode(PassBarCodeFormat.QR_CODE, UUID.randomUUID().toString().uppercase(Locale.ROOT)))
}
}

private fun pickWithPermissionCheck(requestCode: Int) {
constructPermissionsRequest(Manifest.permission.READ_EXTERNAL_STORAGE) {
imageEditHelper.startPick(requestCode)
}.launch()
imageEditHelper = ImageEditHelper(this, passStore)
}

val refreshCallback = { refresh(currentPass) }


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
imageEditHelper.onActivityResult(requestCode, resultCode, data)
}

private fun refresh(pass: Pass) {
val passViewHolder = EditViewHolder(binding.passCard)

Expand All @@ -118,7 +108,7 @@ class PassEditActivity : AppCompatActivity() {
addButton.visibility = if (bitmap == null) View.VISIBLE else View.GONE

val listener = View.OnClickListener {
pickWithPermissionCheck(requestCode)
imageEditHelper.startPick(requestCode)
}

val logoImage = findViewById<ImageView>(logo_img)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.ligi.passandroid.ui.edit

import android.app.Activity
import android.app.Activity.RESULT_OK
import android.content.Intent
import android.net.Uri
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import org.ligi.kaxt.loadImage
import org.ligi.passandroid.model.PassBitmapDefinitions
import org.ligi.passandroid.model.PassStore
Expand All @@ -11,27 +13,24 @@ import org.ligi.passandroid.model.pass.PassImpl
import java.io.File
import java.io.IOException

class ImageEditHelper(private val context: Activity, private val passStore: PassStore) {
class ImageEditHelper(private val context: AppCompatActivity, private val passStore: PassStore) {

fun startPick(reqCodePickLogo: Int) {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
context.startActivityForResult(Intent.createChooser(intent, "Select Picture"), reqCodePickLogo)
private val pickVisualMedia: ActivityResultLauncher<PickVisualMediaRequest> = context.registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
val imageStringByRequestCode = reqCodePickLogo?.let { getImageStringByRequestCode(it) }
if (imageStringByRequestCode != null) {
extractImage(uri, imageStringByRequestCode)
}
}

fun onActivityResult(requestCode: Int, resultCode: Int, imageReturnedIntent: Intent?) {
private var reqCodePickLogo: Int? = null

if (resultCode == RESULT_OK && imageReturnedIntent != null) {
val imageStringByRequestCode = getImageStringByRequestCode(requestCode)
if (imageStringByRequestCode != null) {
extractImage(imageReturnedIntent, imageStringByRequestCode)
}
}
fun startPick(reqCodePickLogo: Int) {
this.reqCodePickLogo = reqCodePickLogo
pickVisualMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
}

private fun extractImage(imageReturnedIntent: Intent, name: String) {
val extractedFile = imageReturnedIntent.data?.loadImage(context)
private fun extractImage(imageReturned: Uri?, name: String) {
val extractedFile = imageReturned?.loadImage(context)
val pass = passStore.currentPass
if (extractedFile != null && pass != null && extractedFile.exists()) {
try {
Expand Down
Loading