This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0bb0b17
commit c374669
Showing
9 changed files
with
131 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
plukke/src/main/java/eu/acolombo/plukke/ExternalContent.kt
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package eu.acolombo.plukke | ||
|
||
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE | ||
import android.app.Activity | ||
import android.content.ContentResolver | ||
import android.content.ContentValues | ||
import android.net.Uri | ||
import android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.result.contract.ActivityResultContracts.TakePicture | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleObserver | ||
import androidx.lifecycle.OnLifecycleEvent | ||
import eu.acolombo.plukke.PlukkeActivityResultContracts.Choose | ||
import java.io.Closeable | ||
|
||
class Plukke( | ||
private val componentActivity: ComponentActivity, | ||
clearOnDestroy: Boolean = false | ||
) : LifecycleObserver, Closeable { | ||
|
||
private class ExternalContent(private val resolver: ContentResolver) : Closeable { | ||
|
||
var uri = resolver.insert(EXTERNAL_CONTENT_URI, ContentValues()) | ||
|
||
override fun close() { | ||
uri?.let { resolver.delete(it, null, null) } | ||
} | ||
|
||
fun clear(): Nothing? = close().let { return null } | ||
|
||
} | ||
|
||
private val exc = ExternalContent(componentActivity.contentResolver) | ||
|
||
val uri = exc.uri | ||
|
||
fun pickImage(onResult: (Uri) -> Unit) = componentActivity.doWithPermission { | ||
val photo = uri?.let { PlukkeActivityResultContracts.TakePhoto(it) } | ||
val pick = PlukkeActivityResultContracts.PickImage() | ||
|
||
componentActivity.registerForActivityResult(Choose()) { result -> | ||
if (result.resultCode == Activity.RESULT_OK) onResult( | ||
photo?.uri | ||
?: exc.clear() | ||
?: result?.data?.data | ||
?: return@registerForActivityResult | ||
) else exc.close() | ||
}.launch(listOfNotNull(photo, pick)) | ||
} | ||
|
||
fun takePicture(onResult: (Uri) -> Unit) = componentActivity.doWithPermission { | ||
uri?.let { uri -> | ||
registerForActivityResult(TakePicture()) { ok -> | ||
if (ok) onResult(uri) else exc.close() | ||
}.launch(uri) | ||
} | ||
} | ||
|
||
var autoClear: Boolean = false | ||
set(value) { | ||
field = value | ||
componentActivity.lifecycle.removeObserver(this) | ||
if (value) componentActivity.lifecycle.addObserver(this) | ||
} | ||
|
||
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) | ||
override fun close() = exc.close() | ||
|
||
init { | ||
autoClear = clearOnDestroy | ||
} | ||
|
||
private fun ComponentActivity.doWithPermission(action: ComponentActivity.() -> Unit) { | ||
// Some devices don't need WRITE_EXTERNAL_STORAGE permission to be granted, so we'll just try to do what we want | ||
// If we can't (catch) we do it the proper way, requesting permission if needed | ||
// The try & catch is here to improve first time experience on some devices | ||
try { | ||
action() | ||
} catch (e: SecurityException) { | ||
doWithPermission(WRITE_EXTERNAL_STORAGE, action) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package eu.acolombo.plukke | ||
|
||
import android.content.pm.PackageManager.PERMISSION_GRANTED | ||
import android.net.Uri | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission | ||
import androidx.core.content.ContextCompat.checkSelfPermission | ||
import androidx.fragment.app.Fragment | ||
|
||
fun ComponentActivity.pickImage(onPick: (Uri) -> Unit) = Plukke(this).also { it.pickImage(onPick) } | ||
|
||
fun ComponentActivity.takePicture(onPick: (Uri) -> Unit) = Plukke(this).also { it.takePicture(onPick) } | ||
|
||
fun Fragment.pickImage(onPick: (Uri) -> Unit) = activity?.pickImage(onPick) | ||
|
||
fun Fragment.takePicture(onTake: (Uri) -> Unit) = activity?.takePicture(onTake) | ||
|
||
fun ComponentActivity.doWithPermission( | ||
permission: String, | ||
action: ComponentActivity.() -> Unit | ||
) = if (checkSelfPermission(this, permission) != PERMISSION_GRANTED) { | ||
registerForActivityResult(RequestPermission()) { | ||
if (it) action() | ||
}.launch(permission) | ||
} else { | ||
action() | ||
} | ||
|
||
fun Fragment.doWithPermission( | ||
permission: String, | ||
action: ComponentActivity.() -> Unit | ||
) = activity?.doWithPermission(permission, action) |