Skip to content

Commit

Permalink
#40: Get result from the image editor (#209)
Browse files Browse the repository at this point in the history
Receive result path from the image editor. Using ACTION_CHOOSER instead of ACTION_PICK_ACTIVITY.
  • Loading branch information
sdex authored Feb 11, 2022
1 parent beb5e0f commit 583c1f9
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 42 deletions.
4 changes: 1 addition & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
xmlns:tools="http://schemas.android.com/tools"
package="space.taran.arknavigator">

<!-- todo: are these permissions really necessary? -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" />

<!-- todo: see "requestLegacyExternalStorage"-->
<application
android:name="space.taran.arknavigator.ui.App"
android:allowBackup="true"
Expand Down Expand Up @@ -39,4 +37,4 @@
</provider>
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ interface ResourceDao {

@Query("SELECT * FROM Resource where root = :root")
suspend fun query(root: StringPath): List<ResourceWithExtra>
// todo: can be optimized with `root in (:roots)`
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package space.taran.arknavigator.mvp.model.repo.index
import java.nio.file.Path

interface ResourcesIndex {

// todo: with async indexing we must emit ids of not-indexed-yet resources too

// we pass all known resource ids to a storage because
// 1) any storage exists globally
// 2) we maintain only 1 storage per root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class GalleryPresenter(

fun onEditFabClick() {
Log.d(GALLERY_SCREEN, "[edit_resource] clicked at position $currentPos")
viewState.editResource(index.getPath(currentResource.id))
viewState.selectImageEditor(index.getPath(currentResource.id))
}

fun onRemoveFabClick() = presenterScope.launch(NonCancellable) {
Expand Down Expand Up @@ -168,4 +168,8 @@ class GalleryPresenter(
viewState.exitFullscreen()
router.exit()
}

fun onImageEditorSelected(componentName: String) {
viewState.editResource(componentName, index.getPath(currentResource.id))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ interface GalleryView : MvpView {
@StateStrategyType(SkipStrategy::class)
fun viewInExternalApp(resourcePath: Path)
@StateStrategyType(SkipStrategy::class)
fun editResource(resourcePath: Path)
fun selectImageEditor(resourcePath: Path)
@StateStrategyType(SkipStrategy::class)
fun editResource(editor: String, resourcePath: Path)
@StateStrategyType(SkipStrategy::class)
fun shareResource(resourcePath: Path)
@StateStrategyType(SkipStrategy::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package space.taran.arknavigator.ui.fragments

import android.annotation.SuppressLint
import android.app.Activity
import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import android.util.Log
Expand All @@ -9,6 +11,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.FileProvider
import androidx.core.os.bundleOf
import androidx.core.view.doOnNextLayout
Expand Down Expand Up @@ -60,6 +63,26 @@ class GalleryFragment : MvpAppCompatFragment(), GalleryView, NotifiableView {
}
}

private val pickImageEditor =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
val componentName = it.data?.component?.flattenToString()
Log.d(GALLERY_SCREEN, "image editor: $componentName")
componentName?.let { presenter.onImageEditorSelected(componentName) }
}

private val imageEditor =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
it.data?.let { intent ->
Log.d(GALLERY_SCREEN, "Edit image result: $intent")
if (it.resultCode == Activity.RESULT_OK) {
val originFileUri = intent.getStringExtra("RESULT_ORIGINAL_URI")
val saveFileUri = intent.getStringExtra("RESULT_SAVE_URI")
Log.d(GALLERY_SCREEN, "RESULT_ORIGINAL_URI: $originFileUri")
Log.d(GALLERY_SCREEN, "RESULT_SAVE_URI: $saveFileUri")
}
}
}

private lateinit var pagerAdapter: PreviewsPager

override fun onCreateView(
Expand Down Expand Up @@ -150,12 +173,27 @@ class GalleryFragment : MvpAppCompatFragment(), GalleryView, NotifiableView {
Notifications.notifyUser(context, messageID, moreTime)
}

override fun editResource(resourcePath: Path) =
openIntentChooser(
override fun selectImageEditor(resourcePath: Path) {
val intent = getExternalAppIntent(resourcePath, Intent.ACTION_EDIT, false)
val intentPick = Intent().apply {
action = Intent.ACTION_PICK_ACTIVITY
putExtra(Intent.EXTRA_TITLE, "Edit the resource with:")
putExtra(Intent.EXTRA_INTENT, intent)
}
pickImageEditor.launch(intentPick)
}

override fun editResource(editor: String, resourcePath: Path) {
val detachProcess = !editor.startsWith("space.taran.arkretouch")
val intent = getExternalAppIntent(
resourcePath,
Intent.ACTION_EDIT,
detachProcess = true
detachProcess
)
intent.component = ComponentName.unflattenFromString(editor)
intent.putExtra("SAVE_FOLDER_PATH", resourcePath.toFile().parent)
imageEditor.launch(intent)
}

override fun shareResource(resourcePath: Path) =
openIntentChooser(
Expand Down Expand Up @@ -316,47 +354,51 @@ class GalleryFragment : MvpAppCompatFragment(), GalleryView, NotifiableView {
actionType: String,
detachProcess: Boolean
) {

Log.i(GALLERY_SCREEN, "Opening resource in an external application")
Log.i(GALLERY_SCREEN, "path: $resourcePath")
Log.i(GALLERY_SCREEN, "action: $actionType")

val intent = getExternalAppIntent(resourcePath, actionType, detachProcess)
val title = when (actionType) {
Intent.ACTION_VIEW -> "View the resource with:"
Intent.ACTION_EDIT -> "Edit the resource with:"
Intent.ACTION_SEND -> "Share the resource with:"
else -> "Open the resource with:"
}
startActivity(Intent.createChooser(intent, title))
}

private fun getExternalAppIntent(
resourcePath: Path,
actionType: String,
detachProcess: Boolean
): Intent {
val file = resourcePath.toFile()
val extension: String = extension(resourcePath)

val context = requireContext()

val uri = FileProvider.getUriForFile(
context,
BuildConfig.APPLICATION_ID + ".provider", file
requireContext(),
BuildConfig.APPLICATION_ID + ".provider",
file
)

val intent = Intent()
intent.setDataAndType(uri, context.contentResolver.getType(uri))
Log.d(GALLERY_SCREEN, "URI: ${intent.data}")
Log.d(GALLERY_SCREEN, "MIME: ${intent.type}")

intent.action = actionType
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val actionString = when (actionType) {
Intent.ACTION_VIEW -> "View the resource with:"
Intent.ACTION_EDIT -> {
intent.putExtra("SAVE_FOLDER_PATH", file.parent)
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
"Edit the resource with:"
val intent = Intent().apply {
setDataAndType(uri, requireContext().contentResolver.getType(uri))
action = actionType
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
when (actionType) {
Intent.ACTION_EDIT -> {
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
}
Intent.ACTION_SEND -> {
putExtra(Intent.EXTRA_STREAM, uri)
}
}
Intent.ACTION_SEND -> {
intent.putExtra(Intent.EXTRA_STREAM, uri)
"Share the resource with:"
if (detachProcess) {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
else -> "Open the resource with:"
}
if (detachProcess) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}

val chooser = Intent.createChooser(intent, actionString)
context.startActivity(chooser)
Log.d(GALLERY_SCREEN, "URI: ${intent.data}")
Log.d(GALLERY_SCREEN, "MIME: ${intent.type}")
return intent
}

private fun getPXFromDP(dpValue: Float): Float {
Expand Down

0 comments on commit 583c1f9

Please sign in to comment.