Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changes/startIntentSenderForResult.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:enhance
---

Add `Plugin#startIntentSenderForResult` Android API for mobile plugins.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.webkit.WebView
import androidx.activity.result.IntentSenderRequest
import androidx.core.app.ActivityCompat
import app.tauri.FsUtils
import app.tauri.Logger
Expand Down Expand Up @@ -86,6 +87,21 @@ abstract class Plugin(private val activity: Activity) {
handle!!.startActivityForResult(invoke, intent, callbackName)
}

/**
* Like startActivityForResult() but taking an IntentSender to describe the activity to be started.
*
* If there is no registered activity callback for the method name passed in, the call will
* be rejected. Make sure a valid activity result callback method is registered using the
* [ActivityCallback] annotation.
*
* @param invoke the invoke object
* @param intentSender the intent used to start an activity
* @param callbackName the name of the callback to run when the launched activity is finished
*/
fun startIntentSenderForResult(invoke: Invoke, intentSender: IntentSenderRequest, callbackName: String) {
handle!!.startIntentSenderForResult(invoke, intentSender, callbackName)
}

/**
* Get the plugin log tags.
* @param subTags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.app.Activity
import android.content.Intent
import android.content.SharedPreferences
import android.webkit.WebView
import androidx.activity.result.IntentSenderRequest
import androidx.core.app.ActivityCompat
import app.tauri.PermissionHelper
import app.tauri.PermissionState
Expand Down Expand Up @@ -46,6 +47,16 @@ class PluginHandle(private val manager: PluginManager, val name: String, val ins
}
}

fun startIntentSenderForResult(invoke: Invoke, intentSender: IntentSenderRequest, callbackName: String) {
manager.startIntentSenderForResult(intentSender) { result ->
val method = startActivityCallbackMethods[callbackName]
if (method != null) {
method.isAccessible = true
method(instance, invoke, result)
}
}
}

fun requestPermissions(
invoke: Invoke,
permissions: Array<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

package app.tauri.plugin

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.webkit.WebView
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.IntentSenderRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import app.tauri.annotation.InvokeArg
Expand All @@ -33,9 +34,11 @@ class PluginManager(val activity: AppCompatActivity) {

private val plugins: HashMap<String, PluginHandle> = HashMap()
private val startActivityForResultLauncher: ActivityResultLauncher<Intent>
private val startIntentSenderForResultLauncher: ActivityResultLauncher<IntentSenderRequest>
private val requestPermissionsLauncher: ActivityResultLauncher<Array<String>>
private var requestPermissionsCallback: RequestPermissionsCallback? = null
private var startActivityForResultCallback: ActivityResultCallback? = null
private var startIntentSenderForResultCallback: ActivityResultCallback? = null
private var jsonMapper: ObjectMapper

init {
Expand All @@ -47,6 +50,14 @@ class PluginManager(val activity: AppCompatActivity) {
}
}

startIntentSenderForResultLauncher =
activity.registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()
) { result ->
if (startIntentSenderForResultCallback != null) {
startIntentSenderForResultCallback!!.onResult(result)
}
}

requestPermissionsLauncher =
activity.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()
) { result ->
Expand Down Expand Up @@ -89,6 +100,11 @@ class PluginManager(val activity: AppCompatActivity) {
startActivityForResultLauncher.launch(intent)
}

fun startIntentSenderForResult(intent: IntentSenderRequest, callback: ActivityResultCallback) {
startIntentSenderForResultCallback = callback
startIntentSenderForResultLauncher.launch(intent)
}

fun requestPermissions(
permissionStrings: Array<String>,
callback: RequestPermissionsCallback
Expand Down