@cap-kit/settings 8.0.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/CapKitSettings.podspec +17 -0
  2. package/LICENSE +21 -0
  3. package/Package.swift +29 -0
  4. package/README.md +428 -0
  5. package/android/build.gradle +103 -0
  6. package/android/src/main/AndroidManifest.xml +2 -0
  7. package/android/src/main/java/io/capkit/settings/SettingsConfig.kt +39 -0
  8. package/android/src/main/java/io/capkit/settings/SettingsImpl.kt +95 -0
  9. package/android/src/main/java/io/capkit/settings/SettingsPlugin.kt +91 -0
  10. package/android/src/main/java/io/capkit/settings/utils/SettingsLogger.kt +85 -0
  11. package/android/src/main/java/io/capkit/settings/utils/SettingsUtils.kt +75 -0
  12. package/android/src/main/res/.gitkeep +0 -0
  13. package/dist/docs.json +877 -0
  14. package/dist/esm/definitions.d.ts +686 -0
  15. package/dist/esm/definitions.js +423 -0
  16. package/dist/esm/definitions.js.map +1 -0
  17. package/dist/esm/index.d.ts +16 -0
  18. package/dist/esm/index.js +19 -0
  19. package/dist/esm/index.js.map +1 -0
  20. package/dist/esm/web.d.ts +43 -0
  21. package/dist/esm/web.js +68 -0
  22. package/dist/esm/web.js.map +1 -0
  23. package/dist/plugin.cjs.js +516 -0
  24. package/dist/plugin.cjs.js.map +1 -0
  25. package/dist/plugin.js +519 -0
  26. package/dist/plugin.js.map +1 -0
  27. package/ios/Sources/SettingsPlugin/SettingsConfig.swift +54 -0
  28. package/ios/Sources/SettingsPlugin/SettingsImpl.swift +84 -0
  29. package/ios/Sources/SettingsPlugin/SettingsPlugin.swift +103 -0
  30. package/ios/Sources/SettingsPlugin/Utils/SettingsLogger.swift +57 -0
  31. package/ios/Sources/SettingsPlugin/Utils/SettingsUtils.swift +70 -0
  32. package/ios/Sources/SettingsPlugin/Version.swift +16 -0
  33. package/ios/Tests/SettingsPluginTests/SettingsPluginTests.swift +21 -0
  34. package/package.json +98 -0
@@ -0,0 +1,95 @@
1
+ package io.capkit.settings
2
+
3
+ import android.content.Context
4
+ import android.content.Intent
5
+ import io.capkit.settings.utils.SettingsLogger
6
+ import io.capkit.settings.utils.SettingsUtils
7
+
8
+ /**
9
+ * Platform-specific native implementation for the Settings plugin (Android).
10
+ *
11
+ * This class contains ONLY Android platform logic and MUST NOT:
12
+ * - depend on Capacitor APIs
13
+ * - reference PluginCall
14
+ * - perform JS-related validation
15
+ *
16
+ * The Capacitor plugin (SettingsPlugin) is responsible for:
17
+ * - reading configuration
18
+ * - extracting call parameters
19
+ * - resolving results to JavaScript
20
+ */
21
+ class SettingsImpl(
22
+ private val context: Context,
23
+ ) {
24
+ /**
25
+ * Cached plugin configuration.
26
+ * Injected once during plugin initialization.
27
+ */
28
+ private lateinit var config: SettingsConfig
29
+
30
+ /**
31
+ * Applies plugin configuration.
32
+ *
33
+ * This method MUST be called exactly once from the plugin's load() method.
34
+ * It translates static configuration into runtime behavior
35
+ * (e.g. enabling verbose logging).
36
+ */
37
+ fun updateConfig(newConfig: SettingsConfig) {
38
+ this.config = newConfig
39
+ SettingsLogger.verbose = newConfig.verboseLogging
40
+ SettingsLogger.debug(
41
+ "Configuration applied. Verbose logging:",
42
+ newConfig.verboseLogging.toString(),
43
+ )
44
+ }
45
+
46
+ /**
47
+ * Opens an Android system settings screen.
48
+ *
49
+ * The operation is considered successful if:
50
+ * - the option maps to a valid Intent
51
+ * - an Activity exists to handle the Intent
52
+ *
53
+ * No Activity result is awaited, by design.
54
+ *
55
+ * @param option JavaScript-facing settings key
56
+ * @return standardized Result object (state-based)
57
+ */
58
+ fun open(option: String): Result {
59
+ SettingsLogger.debug("Requested Android settings option:", option)
60
+
61
+ val intent =
62
+ SettingsUtils.resolveIntent(option, context.packageName)
63
+ ?: return Result(
64
+ success = false,
65
+ error = "Requested setting is not available on Android",
66
+ code = "UNAVAILABLE",
67
+ )
68
+
69
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
70
+
71
+ return if (intent.resolveActivity(context.packageManager) != null) {
72
+ context.startActivity(intent)
73
+ SettingsLogger.debug("Android settings activity started:", intent.action ?: "unknown")
74
+ Result(success = true)
75
+ } else {
76
+ SettingsLogger.error("No activity found to handle intent for option: $option")
77
+ Result(
78
+ success = false,
79
+ error = "Cannot open settings URL",
80
+ code = "UNAVAILABLE",
81
+ )
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Standardized result returned by Settings operations.
87
+ *
88
+ * This mirrors the iOS result model and the public TypeScript API.
89
+ */
90
+ data class Result(
91
+ val success: Boolean,
92
+ val error: String? = null,
93
+ val code: String? = null,
94
+ )
95
+ }
@@ -0,0 +1,91 @@
1
+ package io.capkit.settings
2
+
3
+ import com.getcapacitor.JSObject
4
+ import com.getcapacitor.Plugin
5
+ import com.getcapacitor.PluginCall
6
+ import com.getcapacitor.PluginMethod
7
+ import com.getcapacitor.annotation.CapacitorPlugin
8
+
9
+ /**
10
+ * Capacitor bridge for the Settings plugin (Android).
11
+ *
12
+ * Responsibilities:
13
+ * - read configuration
14
+ * - extract PluginCall input
15
+ * - delegate logic to SettingsImpl
16
+ * - resolve calls with standardized results
17
+ *
18
+ * This class MUST NOT contain platform logic.
19
+ */
20
+ @CapacitorPlugin(
21
+ name = "Settings",
22
+ )
23
+ class SettingsPlugin : Plugin() {
24
+ /**
25
+ * Plugin configuration parsed from capacitor.config.ts.
26
+ */
27
+ private lateinit var config: SettingsConfig
28
+
29
+ /**
30
+ * Native implementation containing Android-specific logic.
31
+ */
32
+ private lateinit var implementation: SettingsImpl
33
+
34
+ /**
35
+ * Plugin lifecycle entry point.
36
+ *
37
+ * Called exactly once when the plugin is loaded.
38
+ */
39
+ override fun load() {
40
+ super.load()
41
+
42
+ config = SettingsConfig(this)
43
+ implementation = SettingsImpl(context)
44
+ implementation.updateConfig(config)
45
+ }
46
+
47
+ // --- Open Settings ---
48
+
49
+ /**
50
+ * Opens a platform-specific settings screen (legacy key).
51
+ */
52
+ @PluginMethod
53
+ fun open(call: PluginCall) {
54
+ val option = call.getString("optionAndroid") ?: ""
55
+ val result = implementation.open(option)
56
+ call.resolve(result.toJS())
57
+ }
58
+
59
+ /**
60
+ * Opens an Android settings screen.
61
+ */
62
+ @PluginMethod
63
+ fun openAndroid(call: PluginCall) {
64
+ val option = call.getString("option") ?: ""
65
+ val result = implementation.open(option)
66
+ call.resolve(result.toJS())
67
+ }
68
+
69
+ // --- Version ---
70
+
71
+ /**
72
+ * Returns the native plugin version.
73
+ */
74
+ @PluginMethod
75
+ fun getPluginVersion(call: PluginCall) {
76
+ val ret = JSObject()
77
+ ret.put("version", BuildConfig.PLUGIN_VERSION)
78
+ call.resolve(ret)
79
+ }
80
+
81
+ /**
82
+ * Converts a native Result into a JSObject.
83
+ */
84
+ private fun SettingsImpl.Result.toJS(): JSObject {
85
+ val obj = JSObject()
86
+ obj.put("success", success)
87
+ if (error != null) obj.put("error", error)
88
+ if (code != null) obj.put("code", code)
89
+ return obj
90
+ }
91
+ }
@@ -0,0 +1,85 @@
1
+ package io.capkit.settings.utils
2
+
3
+ import android.util.Log
4
+
5
+ /**
6
+ * Centralized logging utility for the Settings plugin.
7
+ *
8
+ * This logging provides a single entry point for all native logs
9
+ * and supports runtime-controlled verbose logging.
10
+ *
11
+ * The goal is to avoid scattering `if (verbose)` checks across
12
+ * business logic and keep logging behavior consistent.
13
+ */
14
+ object SettingsLogger {
15
+ /**
16
+ * Logcat tag used for all plugin logs.
17
+ * Helps filtering logs during debugging.
18
+ */
19
+ private const val TAG = "⚡️ Settings"
20
+
21
+ /**
22
+ * Controls whether debug logs are printed.
23
+ *
24
+ * This flag should be set once during plugin initialization
25
+ * based on configuration values.
26
+ */
27
+ var verbose: Boolean = false
28
+
29
+ /**
30
+ * Prints a debug / verbose log message.
31
+ *
32
+ * This method should be used for development-time diagnostics
33
+ * and is automatically silenced when [verbose] is false.
34
+ *
35
+ * @param messages One or more message fragments to be concatenated.
36
+ */
37
+ fun debug(vararg messages: String) {
38
+ if (verbose) {
39
+ log(TAG, Log.DEBUG, *messages)
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Prints an error log message.
45
+ *
46
+ * Error logs are always printed regardless of [verbose] state.
47
+ *
48
+ * @param message Human-readable error description.
49
+ * @param e Optional exception for stack trace logging.
50
+ */
51
+ fun error(
52
+ message: String,
53
+ e: Throwable? = null,
54
+ ) {
55
+ val sb = StringBuilder(message)
56
+ if (e != null) {
57
+ sb.append(" | Error: ").append(e.message)
58
+ }
59
+ Log.e(TAG, sb.toString(), e)
60
+ }
61
+
62
+ /**
63
+ * Internal low-level log dispatcher.
64
+ *
65
+ * Joins message fragments and forwards them to Android's Log API
66
+ * using the specified priority.
67
+ */
68
+ fun log(
69
+ tag: String,
70
+ level: Int,
71
+ vararg messages: String,
72
+ ) {
73
+ val sb = StringBuilder()
74
+ for (msg in messages) {
75
+ sb.append(msg).append(" ")
76
+ }
77
+ when (level) {
78
+ Log.DEBUG -> Log.d(tag, sb.toString())
79
+ Log.INFO -> Log.i(tag, sb.toString())
80
+ Log.WARN -> Log.w(tag, sb.toString())
81
+ Log.ERROR -> Log.e(tag, sb.toString())
82
+ else -> Log.v(tag, sb.toString())
83
+ }
84
+ }
85
+ }
@@ -0,0 +1,75 @@
1
+ package io.capkit.settings.utils
2
+
3
+ import android.content.Intent
4
+ import android.net.Uri
5
+ import android.provider.Settings
6
+
7
+ /**
8
+ * Utility helpers for resolving Android system settings Intents.
9
+ *
10
+ * This object contains ONLY mapping logic and MUST NOT:
11
+ * - start activities
12
+ * - access Context or PackageManager
13
+ * - depend on Capacitor APIs
14
+ *
15
+ * It maps JavaScript-facing option strings to Android Intent instances.
16
+ */
17
+ object SettingsUtils {
18
+ /**
19
+ * Resolves a JavaScript settings option into an Android Intent.
20
+ *
21
+ * @param option JavaScript-facing option key
22
+ * @param packageName Application package name (used where required)
23
+ * @return A configured Intent, or null if the option is unsupported
24
+ */
25
+ fun resolveIntent(
26
+ option: String,
27
+ packageName: String,
28
+ ): Intent? {
29
+ return when (option) {
30
+ // --- App-specific ---
31
+ "application_details" ->
32
+ Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
33
+ data = Uri.parse("package:$packageName")
34
+ }
35
+
36
+ "app_notification" ->
37
+ Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
38
+ putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
39
+ }
40
+
41
+ // --- Core system settings ---
42
+ "accessibility" -> Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
43
+ "airplane_mode" -> Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS)
44
+ "apn" -> Intent(Settings.ACTION_APN_SETTINGS)
45
+ "application" -> Intent(Settings.ACTION_APPLICATION_SETTINGS)
46
+ "battery_optimization" -> Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
47
+ "bluetooth" -> Intent(Settings.ACTION_BLUETOOTH_SETTINGS)
48
+ "cast" -> Intent(Settings.ACTION_CAST_SETTINGS)
49
+ "data_roaming" -> Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)
50
+ "date" -> Intent(Settings.ACTION_DATE_SETTINGS)
51
+ "display" -> Intent(Settings.ACTION_DISPLAY_SETTINGS)
52
+ "home" -> Intent(Settings.ACTION_HOME_SETTINGS)
53
+ "location" -> Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
54
+ "nfc" -> Intent(Settings.ACTION_NFC_SETTINGS)
55
+ "nfcsharing" -> Intent(Settings.ACTION_NFCSHARING_SETTINGS)
56
+ "nfc_payment" -> Intent(Settings.ACTION_NFC_PAYMENT_SETTINGS)
57
+ "print" -> Intent(Settings.ACTION_PRINT_SETTINGS)
58
+ "security" -> Intent(Settings.ACTION_SECURITY_SETTINGS)
59
+ "settings" -> Intent(Settings.ACTION_SETTINGS)
60
+ "sound" -> Intent(Settings.ACTION_SOUND_SETTINGS)
61
+ "storage" -> Intent(Settings.ACTION_INTERNAL_STORAGE_SETTINGS)
62
+ "usage" -> Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
63
+ "vpn" -> Intent(Settings.ACTION_VPN_SETTINGS)
64
+ "wifi" -> Intent(Settings.ACTION_WIFI_SETTINGS)
65
+
66
+ // --- Advanced / device-dependent ---
67
+ "zen_mode" -> Intent("android.settings.ZEN_MODE_SETTINGS")
68
+ "zen_mode_priority" -> Intent(Settings.ACTION_ZEN_MODE_PRIORITY_SETTINGS)
69
+ "zen_mode_blocked_effects" -> Intent("android.settings.ZEN_MODE_BLOCKED_EFFECTS_SETTINGS")
70
+ "text_to_speech" -> Intent("com.android.settings.TTS_SETTINGS")
71
+
72
+ else -> null
73
+ }
74
+ }
75
+ }
File without changes