@@ -5,46 +5,47 @@ import android.net.Uri
55import android.os.Build
66import androidx.appcompat.app.AlertDialog
77import com.foxdebug.acode.Acode
8- import com.foxdebug.acode.MainActivity
9- import com.foxdebug.acode.Utils
8+ import com.foxdebug.acode.runOnUiThread
109import com.getcapacitor.JSObject
1110import com.getcapacitor.Plugin
1211import com.getcapacitor.PluginCall
1312import com.getcapacitor.PluginMethod
1413import com.getcapacitor.annotation.CapacitorPlugin
14+ import kotlinx.coroutines.CoroutineName
15+ import kotlinx.coroutines.MainScope
16+ import kotlinx.coroutines.cancel
17+ import kotlinx.coroutines.plus
18+ import androidx.core.net.toUri
1519
1620@CapacitorPlugin(name = " NativeLayer" )
1721class NativeLayer : Plugin () {
22+ val scope = MainScope () + CoroutineName (" NativeLayer" )
23+
24+ override fun handleOnDestroy () {
25+ super .handleOnDestroy()
26+ scope.cancel()
27+ }
1828
1929 /* *
20- * Displays a dialog with the provided title and message .
30+ * Displays a simple alert dialog with a title, message, and an "OK" button .
2131 *
22- * @param call The PluginCall containing the dialog parameters. It should contain :
23- * - `title`: The title of the dialog (String) .
24- * - `message`: The message displayed in the dialog (String) .
32+ * @param call The PluginCall containing the dialog parameters. It expects the following keys :
33+ * - `title`: (String) The title of the dialog.
34+ * - `message`: (String) The message content of the dialog.
2535 *
26- * If either `title` or `message` is missing , the call will be rejected with an appropriate error message .
27- * Upon clicking "OK", the dialog will resolve the call.
36+ * The dialog will display the provided title and message. If either `title` or `message` is not provided , the call will be rejected with an error.
37+ * When the user clicks the "OK" button , the dialog will be dismissed, and the plugin call will be resolved .
2838 */
2939 @PluginMethod
3040 fun showDialog (call : PluginCall ) = with (call) {
3141 autoRejectOnError(onFailure = {}) {
32- existsNot(" title" ) {
33- return @with
34- }
35- existsNot(" message" ) {
36- return @with
37- }
42+ existsNot(" title" ) { return @with }
43+ existsNot(" message" ) { return @with }
3844
3945 val title = call.getString(" title" )
4046 val message = call.getString(" message" )
4147
42- Utils .runOnUiThread {
43- val context = MainActivity .getActivityContext()
44- if (context == null ) {
45- call.reject(" Context is null" )
46- return @runOnUiThread
47- }
48+ runOnUiThread {
4849 AlertDialog .Builder (context).apply {
4950 setTitle(title)
5051 setMessage(message)
@@ -60,27 +61,31 @@ class NativeLayer : Plugin() {
6061 /* *
6162 * Launches an intent based on the provided parameters.
6263 *
63- * @param call The PluginCall containing the intent parameters. It should contain:
64- * - `constructor_number`: The type of intent constructor to use (Int).
65- * - `launch_type`: The type of intent launch ("activity" or "service").
66- * - `activity_context`: Whether to use the activity context (Boolean).
67- * - `action`: The action for the intent (String) (for constructor 0).
68- * - `className`: The class name to launch (String) (for constructor 1).
69- * - `new_task`: Whether to launch the intent in a new task (Boolean).
70- * - `extras`: A JSObject containing key-value pairs to add as extras to the intent.
71- * - `data`: Optional URI to set as the intent data.
72- * - `type`: Optional MIME type for the intent.
73- * - `package` and `class`: Optional package and class for explicit intents.
74- *
75- * This method supports multiple constructors based on the `constructor_number`:
76- * - `0`: Uses an action string to create an intent.
77- * - `1`: Uses a class name to create an explicit intent.
78- * - `2`: Creates a generic intent.
64+ * @param call The PluginCall containing the intent parameters.
65+ * The following parameters are expected:
66+ * - `constructor_number`: (Int) The type of intent constructor to use.
67+ * - `0`: Creates an intent using an action string. Requires `action`.
68+ * - `1`: Creates an explicit intent using a class name. Requires `className`.
69+ * - `2`: Creates a generic intent.
70+ * - `launch_type`: (String) The type of intent launch.
71+ * - `"activity"`: Starts an activity.
72+ * - `"service"`: Starts a service.
73+ * - `activity_context`: (Boolean) Whether to use the activity context. If `false`, uses the application context.
74+ * - `action`: (String) The action for the intent (required for `constructor_number` 0).
75+ * - `className`: (String) The fully qualified class name to launch (required for `constructor_number` 1).
76+ * - `new_task`: (Boolean) Whether to launch the intent in a new task.
77+ * - `extras`: (JSObject) Key-value pairs to add as extras to the intent. Supported types are String, Int, Boolean, Double, and Float.
78+ * - `data`: (String, optional) URI to set as the intent data.
79+ * - `type`: (String, optional) MIME type for the intent.
80+ * - `package`: (String, optional) Package name for an explicit intent.
81+ * - `class`: (String, optional) Class name for an explicit intent. Should be used alongside `package`.
82+ * - `foreground_service`: (Boolean, optional) Indicates if the service is a foreground service. (required for service launch)
83+ * Only applies when `launch_type` is "service". Requires API 26+.
7984 *
80- * If the `launch_type` is "activity" , it starts an activity .
81- * If the `launch_type` is "service" , it starts a service, and you can specify if it's a foreground service.
85+ * If `launch_type` is "service" and `foreground_service` is `true` , it starts a foreground service .
86+ * Otherwise , it starts a regular background service.
8287 *
83- * If any required parameters are missing, the call will be rejected with an appropriate error message.
88+ * If any required parameters are missing or invalid , the call will be rejected with an appropriate error message.
8489 */
8590 @PluginMethod
8691 fun launchIntent (call : PluginCall ) {
@@ -100,11 +105,7 @@ class NativeLayer : Plugin() {
100105 return
101106 }
102107
103- val context = if (useActivityContext) MainActivity .getActivityContext() else Acode .instance
104- if (context == null ) {
105- call.reject(" Requested context is not available" )
106- return
107- }
108+ val context = if (useActivityContext) context else Acode .instance.applicationContext
108109
109110 val intent = when (constructorNumber) {
110111 0 -> {
@@ -153,13 +154,8 @@ class NativeLayer : Plugin() {
153154 }
154155 }
155156
156- call.getString(" data" )?.let {
157- intent.data = Uri .parse(it)
158- }
159-
160- call.getString(" type" )?.let {
161- intent.type = it
162- }
157+ call.getString(" data" )?.let { intent.data = it.toUri() }
158+ call.getString(" type" )?.let (intent::setType)
163159
164160 if (call.data.has(" package" ) && call.data.has(" class" )) {
165161 val pkg = call.getString(" package" )
@@ -199,6 +195,5 @@ class NativeLayer : Plugin() {
199195 }
200196 }
201197 }
202-
203198 }
204199}
0 commit comments