@nativue/core 0.12.0 → 0.14.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.
- package/AGENTS.md +33 -8
- package/README.md +2 -1
- package/android/build.gradle.kts +4 -0
- package/android/nativue.settings.gradle.kts +2 -0
- package/android/src/main/java/dev/nativue/Bridge.kt +118 -12
- package/android/src/main/java/dev/nativue/DownloadBanner.kt +45 -0
- package/android/src/main/java/dev/nativue/NativueActivity.kt +26 -5
- package/android/src/main/java/dev/nativue/Permissions.kt +107 -0
- package/android/src/main/java/dev/nativue/Plugins.kt +98 -15
- package/cli/android-project.mjs +377 -0
- package/cli/app-config.mjs +515 -0
- package/cli/config.d.mts +162 -3
- package/cli/hot-client.js +192 -0
- package/cli/ios-project.mjs +652 -0
- package/cli/link.mjs +66 -71
- package/cli/nativue.mjs +262 -58
- package/cli/plugins.mjs +236 -0
- package/cli/prebuild.mjs +458 -0
- package/cli/run.mjs +2 -2
- package/cli/serialize.mjs +168 -0
- package/cli/symbolicate.mjs +26 -14
- package/ios/AppLifecycle.swift +165 -0
- package/ios/Bridge.swift +110 -9
- package/ios/DownloadBanner.swift +38 -0
- package/ios/NativueView.swift +24 -1
- package/ios/Permissions.swift +19 -0
- package/ios/Plugins.swift +24 -0
- package/package.json +3 -2
- package/src/globals.ts +8 -1
- package/src/native-events.ts +99 -0
- package/src/native-functions.ts +16 -2
- package/src/permissions.ts +57 -0
- package/src/renderer.ts +6 -1
- package/src/router.ts +31 -1
- package/src/types.ts +6 -0
- package/template/android/app/src/debug/AndroidManifest.xml +4 -0
- package/template/android/app/src/main/java/MainActivity.kt +5 -0
- package/template/android/app/src/main/res/drawable-night-xxxhdpi/splash_logo.png +0 -0
- package/template/android/app/src/main/res/drawable-xxxhdpi/splash_logo.png +0 -0
- package/template/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml +6 -0
- package/template/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
- package/template/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png +0 -0
- package/template/android/app/src/main/res/values/colors.xml +4 -0
- package/template/android/app/src/main/res/values/styles.xml +15 -0
- package/template/android/app/src/main/res/values-night/styles.xml +5 -0
- package/template/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/template/android/gradle/wrapper/gradle-wrapper.properties +9 -0
- package/template/android/gradlew +248 -0
- package/template/android/gradlew.bat +112 -0
- package/template/ios/App/App.swift +18 -0
- package/template/ios/App/Assets.xcassets/AppIcon.appiconset/Contents.json +1 -0
- package/template/ios/App/Assets.xcassets/AppIcon.appiconset/icon.png +0 -0
- package/template/ios/App/Assets.xcassets/Contents.json +1 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/Contents.json +11 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo-dark@1x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo-dark@2x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo-dark@3x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo@1x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo@2x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo@3x.png +0 -0
- package/template/ios/App.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +15 -0
package/AGENTS.md
CHANGED
|
@@ -70,7 +70,18 @@ Import navigation from `@nativue/core/router`:
|
|
|
70
70
|
|
|
71
71
|
Import the runtime from `vue`: `ref`, `computed`, `StyleSheet`, `Animated`, `LayoutAnimation`,
|
|
72
72
|
`Platform`, `AsyncStorage`, `Keyboard`, `Dimensions`, `useWindowDimensions`, `useColorScheme`,
|
|
73
|
-
`AccessibilityInfo`, `requireNativeFunction
|
|
73
|
+
`AccessibilityInfo`, `requireNativeFunction`, `addNativeListener`, `onOpenURL`, `AppState`,
|
|
74
|
+
`NativeError`, `usePermission`, `openSettings`.
|
|
75
|
+
|
|
76
|
+
- Native events: `addNativeListener('camera.captured', (payload) => ...)` returns a subscription;
|
|
77
|
+
in a component it's removed on unmount. `addNativeListener('app.state', (state) => ...)` and
|
|
78
|
+
`AppState.current` say whether the app is `'active'` or `'background'`.
|
|
79
|
+
- A rejected native call is a `NativeError` with a `code`: `ERR_PERMISSION_DENIED`,
|
|
80
|
+
`ERR_UNAVAILABLE`, `ERR_CANCELED`, `ERR_INVALID_ARGUMENT`.
|
|
81
|
+
- `onOpenURL((url) => true)` takes a URL the app was opened with before the router does.
|
|
82
|
+
- Permissions: `const { permission, request } = usePermission(Camera.getPermission, Camera.requestPermission)`
|
|
83
|
+
gives `{ status, granted, canAskAgain }`, re-read when the app comes back to the front. When
|
|
84
|
+
`!granted && !canAskAgain`, only `openSettings()` can help: explain why, then offer it.
|
|
74
85
|
|
|
75
86
|
- `AsyncStorage`: `getItem`, `setItem`, `removeItem`, `getAllKeys`, `multiGet`, `multiSet`,
|
|
76
87
|
`clear`; promise-based, string values. Store objects as JSON. For large data or queries, use
|
|
@@ -84,15 +95,29 @@ Import the runtime from `vue`: `ref`, `computed`, `StyleSheet`, `Animated`, `Lay
|
|
|
84
95
|
- `@/` points at `src/`. Shared components go in `src/components`, everything else in `src/lib`.
|
|
85
96
|
- Vue plugins (Pinia...) are installed in `src/app.ts`: `export default function setup(app: App)`.
|
|
86
97
|
- Official packages: `@nativue/icons` (icon components), `@nativue/tailwind`, `@nativue/haptics`,
|
|
87
|
-
`@nativue/sqlite`. A package with native code needs the app
|
|
88
|
-
`npm run android`, not just a reload.
|
|
89
|
-
-
|
|
98
|
+
`@nativue/clipboard` (copy text), `@nativue/sqlite`. A package with native code needs the app
|
|
99
|
+
rebuilt with `npm run ios` or `npm run android`, not just a reload.
|
|
100
|
+
- Copying: `const { copied, systemConfirmed } = await Clipboard.copy(text)`; show a "copied" toast
|
|
101
|
+
only when `copied && !systemConfirmed` (Android 13+ shows its own).
|
|
102
|
+
- Commands: `npm run ios` / `npm run android` build and open the app, then update it on save
|
|
103
|
+
(template edits keep the component's state; script edits re-create the component);
|
|
90
104
|
`npm run typecheck` checks scripts and templates; `npm run build` makes the release bundle;
|
|
91
105
|
`npm run format` formats with Prettier.
|
|
92
106
|
- Native code the app needs (a platform API no package covers) goes in `native/<name>/ios` and
|
|
93
107
|
`native/<name>/android`, as `<Name>Plugin` classes registering functions JavaScript calls with
|
|
94
108
|
`requireNativeFunction`; libraries it uses (Swift packages, Maven) go in `native/<name>/nativue.json`.
|
|
95
|
-
See the docs' "Native code in your app". Rebuild after changing it.
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
109
|
+
See the docs' "Native code in your app". Rebuild after changing it. On Android, use
|
|
110
|
+
`registry.applicationContext`, and `registry.activity` only when needed, without keeping it. App
|
|
111
|
+
events (push token, notification taps, URLs, activity results) reach plugins implementing
|
|
112
|
+
`AppLifecycleListener` / `NotificationCenterListener` (iOS) or `ActivityListener` (Android).
|
|
113
|
+
- An iOS app extension (a widget, a Notification Service, a share extension) goes in
|
|
114
|
+
`native/extensions/<name>/`: plain Swift files, plus a `nativue.json` with its `ios.target` (`type`,
|
|
115
|
+
`bundleIdSuffix`). See the docs' "iOS app extensions". `ios.appGroups` in `nativue.config.ts`
|
|
116
|
+
shares data with it.
|
|
117
|
+
- Check changes with `npm run typecheck`.
|
|
118
|
+
- `ios/` and `android/` are generated from `nativue.config.ts` (name, scheme, ids, version,
|
|
119
|
+
buildNumber, icon, splash, `ios.infoPlist`, `ios.entitlements`, `android.permissions`,
|
|
120
|
+
`android.blockedPermissions`, `android.adaptiveIcon`, `android.signing`, `android.gradle`,
|
|
121
|
+
`resources`) by `npx nativue prebuild`, which `dev`, `build` and `run` also run: never edit them,
|
|
122
|
+
nor commit them. Prebuild stops with an error when they were changed by hand. An app with
|
|
123
|
+
`prebuild: false` in `nativue.config.ts` keeps them by hand instead.
|
package/README.md
CHANGED
|
@@ -17,8 +17,9 @@ This package holds:
|
|
|
17
17
|
|
|
18
18
|
```sh
|
|
19
19
|
nativue run ios # or android: builds, opens the app on a simulator or emulator, then dev
|
|
20
|
-
nativue dev #
|
|
20
|
+
nativue dev # updates the running app on save
|
|
21
21
|
nativue build # bundles the app into ios/ and android/ (--production for releases)
|
|
22
|
+
nativue prebuild # generates ios/ and android/ from nativue.config.ts (the others run it too)
|
|
22
23
|
```
|
|
23
24
|
|
|
24
25
|
## Documentation
|
package/android/build.gradle.kts
CHANGED
|
@@ -73,4 +73,8 @@ dependencies {
|
|
|
73
73
|
implementation("com.facebook.soloader:soloader:0.10.5")
|
|
74
74
|
// Insets, system bars, fonts and accessibility the same way down to minSdk.
|
|
75
75
|
implementation("androidx.core:core:1.16.0")
|
|
76
|
+
// ComponentActivity: activity-result launchers and new-intent listeners for plugins.
|
|
77
|
+
api("androidx.activity:activity:1.13.0")
|
|
78
|
+
// AppState: whether the app, not one activity, is in front.
|
|
79
|
+
implementation("androidx.lifecycle:lifecycle-process:2.11.1")
|
|
76
80
|
}
|
|
@@ -46,6 +46,8 @@ fun pascalCase(name: String): String =
|
|
|
46
46
|
val appDirectory: File = settingsDir.parentFile
|
|
47
47
|
val androidPlugins = dependencyNames(appDirectory).mapNotNull { name -> androidPlugin(name, appDirectory) }
|
|
48
48
|
val appSourceDirectories = File(appDirectory, "native").listFiles().orEmpty()
|
|
49
|
+
// native/extensions holds iOS extension targets, not modules.
|
|
50
|
+
.filter { it.name != "extensions" }
|
|
49
51
|
.sortedBy { it.name }
|
|
50
52
|
.map { File(it, "android") }
|
|
51
53
|
.filter { it.isDirectory }
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
package dev.nativue
|
|
2
2
|
|
|
3
3
|
import android.annotation.SuppressLint
|
|
4
|
-
import android.
|
|
4
|
+
import android.content.ActivityNotFoundException
|
|
5
|
+
import android.content.Intent
|
|
6
|
+
import android.net.Uri
|
|
7
|
+
import android.provider.Settings
|
|
5
8
|
import android.content.pm.ApplicationInfo
|
|
6
9
|
import android.content.res.Configuration
|
|
7
10
|
import android.content.res.ColorStateList
|
|
@@ -17,7 +20,11 @@ import android.util.TypedValue
|
|
|
17
20
|
import android.view.Gravity
|
|
18
21
|
import android.view.View
|
|
19
22
|
import android.view.ViewGroup
|
|
23
|
+
import androidx.activity.ComponentActivity
|
|
20
24
|
import androidx.core.graphics.Insets
|
|
25
|
+
import androidx.lifecycle.DefaultLifecycleObserver
|
|
26
|
+
import androidx.lifecycle.LifecycleOwner
|
|
27
|
+
import androidx.lifecycle.ProcessLifecycleOwner
|
|
21
28
|
import androidx.core.view.ViewCompat
|
|
22
29
|
import androidx.core.view.WindowInsetsAnimationCompat
|
|
23
30
|
import androidx.core.view.WindowInsetsCompat
|
|
@@ -31,6 +38,7 @@ import com.facebook.yoga.YogaDisplay
|
|
|
31
38
|
import com.facebook.yoga.YogaEdge
|
|
32
39
|
import com.facebook.yoga.YogaFlexDirection
|
|
33
40
|
import com.facebook.yoga.YogaPositionType
|
|
41
|
+
import java.io.ByteArrayOutputStream
|
|
34
42
|
import java.net.HttpURLConnection
|
|
35
43
|
import java.net.URL
|
|
36
44
|
import kotlin.concurrent.thread
|
|
@@ -39,7 +47,7 @@ import kotlin.concurrent.thread
|
|
|
39
47
|
* Runs the JS bundle on its own thread and applies the operations sent by the Vue renderer
|
|
40
48
|
* (src/renderer.ts) to Android views + Yoga in batches on the main thread. Mirrors ios/Bridge.swift.
|
|
41
49
|
*/
|
|
42
|
-
class Bridge(private val activity:
|
|
50
|
+
class Bridge(private val activity: ComponentActivity, private val rootView: RootView) {
|
|
43
51
|
private val mainHandler = Handler(Looper.getMainLooper())
|
|
44
52
|
|
|
45
53
|
/** The dev server address that answered last (set by listenForReload, read when symbolicating). */
|
|
@@ -51,11 +59,28 @@ class Bridge(private val activity: Activity, private val rootView: RootView) {
|
|
|
51
59
|
@Volatile
|
|
52
60
|
private var reloadConnection: HttpURLConnection? = null
|
|
53
61
|
|
|
62
|
+
/** The router's screens, reopened after a reload in development. Only touched on the JS thread. */
|
|
63
|
+
private var savedNavigation: String? = null
|
|
64
|
+
|
|
54
65
|
private val javaScriptHandler = startJavaScriptThread()
|
|
55
66
|
private val isDebuggable = activity.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0
|
|
56
67
|
private val density = activity.resources.displayMetrics.density
|
|
57
68
|
private val storage = KeyValueStorage(activity)
|
|
58
|
-
private val pluginRegistry = PluginRegistry(activity)
|
|
69
|
+
private val pluginRegistry = PluginRegistry.get(activity)
|
|
70
|
+
private val sendToJavaScript: (String, Any?) -> Unit = { eventName, payload ->
|
|
71
|
+
evaluate { runtime?.call("__nativeEvent", eventName, payload) }
|
|
72
|
+
}
|
|
73
|
+
/** A URL that came before the bundle (from the dev server, in debug): sent once it has run. */
|
|
74
|
+
private var pendingURL: String? = null
|
|
75
|
+
|
|
76
|
+
/** AppState.current: "active" or "background". Only touched on the JS thread. */
|
|
77
|
+
private var appState = "active"
|
|
78
|
+
/** The whole app's, not the activity's: another activity of the app in front keeps it active. */
|
|
79
|
+
private val appStateObserver = object : DefaultLifecycleObserver {
|
|
80
|
+
override fun onResume(owner: LifecycleOwner) = appStateDidChange("active")
|
|
81
|
+
|
|
82
|
+
override fun onStop(owner: LifecycleOwner) = appStateDidChange("background")
|
|
83
|
+
}
|
|
59
84
|
|
|
60
85
|
// Only touched on the JS thread.
|
|
61
86
|
private var runtime: JavaScriptRuntime? = null
|
|
@@ -84,9 +109,13 @@ class Bridge(private val activity: Activity, private val rootView: RootView) {
|
|
|
84
109
|
/** The bundle running now, for the error screen's Reload. */
|
|
85
110
|
private var bundle = ByteArray(0)
|
|
86
111
|
private val errorOverlay = ErrorOverlay(activity)
|
|
112
|
+
private val downloadBanner = DownloadBanner(activity)
|
|
87
113
|
|
|
88
114
|
fun start() {
|
|
89
115
|
NativeNode.setPixelDensity(density)
|
|
116
|
+
pluginRegistry.sendToJavaScript = sendToJavaScript
|
|
117
|
+
pluginRegistry.activityCreated(activity)
|
|
118
|
+
ProcessLifecycleOwner.get().lifecycle.addObserver(appStateObserver)
|
|
90
119
|
rootView.onLayoutPass = ::layout
|
|
91
120
|
NativeNode.onLayoutCalculated = ::reportLayoutChanges
|
|
92
121
|
ViewCompat.setOnApplyWindowInsetsListener(rootView) { view, insets ->
|
|
@@ -117,6 +146,10 @@ class Bridge(private val activity: Activity, private val rootView: RootView) {
|
|
|
117
146
|
/** On the main thread, when the activity is destroyed. */
|
|
118
147
|
fun destroy() {
|
|
119
148
|
isDestroyed = true
|
|
149
|
+
ProcessLifecycleOwner.get().lifecycle.removeObserver(appStateObserver)
|
|
150
|
+
// Another activity may have taken them over already.
|
|
151
|
+
if (pluginRegistry.sendToJavaScript === sendToJavaScript) pluginRegistry.sendToJavaScript = { _, _ -> }
|
|
152
|
+
pluginRegistry.activityDestroyed(activity)
|
|
120
153
|
reloadConnection?.disconnect()
|
|
121
154
|
animatedGraph.reset() // a looping animation would keep running on the dead views
|
|
122
155
|
javaScriptHandler.post {
|
|
@@ -130,14 +163,27 @@ class Bridge(private val activity: Activity, private val rootView: RootView) {
|
|
|
130
163
|
private fun asyncCall(function: AsyncFunction): NativeFunction = { arguments ->
|
|
131
164
|
val callId = int(arguments[0])
|
|
132
165
|
val owner = runtime
|
|
133
|
-
val promise = Promise { value, errorMessage ->
|
|
134
|
-
callIfStillRunning(owner, "__asyncFunctionFinished", callId, value, errorMessage)
|
|
166
|
+
val promise = Promise { value, errorMessage, errorCode ->
|
|
167
|
+
callIfStillRunning(owner, "__asyncFunctionFinished", callId, value, errorMessage, errorCode)
|
|
135
168
|
}
|
|
136
169
|
|
|
137
|
-
runCatching { function(arguments.drop(1), promise) }.onFailure {
|
|
170
|
+
runCatching { function(arguments.drop(1), promise) }.onFailure { error ->
|
|
171
|
+
val message = error.message ?: error.toString()
|
|
172
|
+
|
|
173
|
+
if (error is PluginException) promise.reject(error.code, message) else promise.reject(message)
|
|
174
|
+
}
|
|
138
175
|
null
|
|
139
176
|
}
|
|
140
177
|
|
|
178
|
+
private fun appStateDidChange(nextAppState: String) {
|
|
179
|
+
evaluate {
|
|
180
|
+
if (nextAppState == appState) return@evaluate
|
|
181
|
+
|
|
182
|
+
appState = nextAppState
|
|
183
|
+
runtime?.call("__nativeEvent", "app.state", nextAppState)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
141
187
|
fun onConfigurationChanged() {
|
|
142
188
|
updateFontScale()
|
|
143
189
|
updateColorScheme()
|
|
@@ -200,7 +246,11 @@ class Bridge(private val activity: Activity, private val rootView: RootView) {
|
|
|
200
246
|
|
|
201
247
|
/** A URL the app was opened with (deep link), for the router (`__openURL`); after the bundle has run. */
|
|
202
248
|
fun openURL(url: String) {
|
|
203
|
-
evaluate {
|
|
249
|
+
evaluate {
|
|
250
|
+
val runningRuntime = runtime
|
|
251
|
+
|
|
252
|
+
if (runningRuntime == null) pendingURL = url else runningRuntime.call("__openURL", url)
|
|
253
|
+
}
|
|
204
254
|
}
|
|
205
255
|
|
|
206
256
|
/**
|
|
@@ -217,6 +267,8 @@ class Bridge(private val activity: Activity, private val rootView: RootView) {
|
|
|
217
267
|
runtime?.destroy()
|
|
218
268
|
runtime = JavaScriptRuntime(nativeFunctions(), ::reportError)
|
|
219
269
|
runtime?.evaluate(bundle, "main.bundle.js")
|
|
270
|
+
pendingURL?.let { url -> runtime?.call("__openURL", url) }
|
|
271
|
+
pendingURL = null
|
|
220
272
|
}
|
|
221
273
|
}
|
|
222
274
|
|
|
@@ -329,12 +381,26 @@ class Bridge(private val activity: Activity, private val rootView: RootView) {
|
|
|
329
381
|
put("windowSize") { _ -> windowSize }
|
|
330
382
|
put("platform") { _ -> "android" }
|
|
331
383
|
put("colorScheme") { _ -> colorScheme() }
|
|
384
|
+
put("appState") { _ -> appState }
|
|
385
|
+
put("savedNavigation") { _ -> savedNavigation }
|
|
386
|
+
put("saveNavigation") { arguments -> savedNavigation = arguments[0] as String }
|
|
332
387
|
put("accessibilityInfo") { _ -> accessibilityStatus(activity) }
|
|
333
388
|
put("announce") { arguments ->
|
|
334
389
|
val message = arguments[0] as String
|
|
335
390
|
@Suppress("DEPRECATION") // its replacements (live regions, pane titles) need a view to describe
|
|
336
391
|
mainHandler.post { rootView.announceForAccessibility(message) }
|
|
337
392
|
}
|
|
393
|
+
// Google: an app details screen "may not exist" on some devices.
|
|
394
|
+
put("openSettings") { _ ->
|
|
395
|
+
val packageURI = Uri.fromParts("package", activity.packageName, null)
|
|
396
|
+
|
|
397
|
+
try {
|
|
398
|
+
activity.startActivity(Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI))
|
|
399
|
+
true
|
|
400
|
+
} catch (error: ActivityNotFoundException) {
|
|
401
|
+
false
|
|
402
|
+
}
|
|
403
|
+
}
|
|
338
404
|
// Prefixed, so a plugin can't replace nativue's own functions.
|
|
339
405
|
pluginRegistry.functions.forEach { (name, function) -> put("plugin:$name", function) }
|
|
340
406
|
pluginRegistry.asyncFunctions.forEach { (name, function) -> put("pluginAsync:$name", asyncCall(function)) }
|
|
@@ -513,7 +579,7 @@ class Bridge(private val activity: Activity, private val rootView: RootView) {
|
|
|
513
579
|
return hosts.map { "http://$it:$DEV_SERVER_PORT" }
|
|
514
580
|
}
|
|
515
581
|
|
|
516
|
-
/** The dev server sends `data: reload` on connect and
|
|
582
|
+
/** The dev server sends `data: reload` on connect and when the app must reload, `data: hot` with an update. */
|
|
517
583
|
private fun listenForReload() {
|
|
518
584
|
val candidateURLs = devServerURLs()
|
|
519
585
|
var attempt = 0
|
|
@@ -526,10 +592,13 @@ class Bridge(private val activity: Activity, private val rootView: RootView) {
|
|
|
526
592
|
reloadConnection = connection
|
|
527
593
|
|
|
528
594
|
connection.inputStream.bufferedReader().useLines { lines ->
|
|
529
|
-
lines.
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
595
|
+
lines.forEach { line ->
|
|
596
|
+
if (line == "data: reload") {
|
|
597
|
+
downloadAndRun()
|
|
598
|
+
} else if (line.startsWith("data: hot ")) {
|
|
599
|
+
val update = line.removePrefix("data: hot ")
|
|
600
|
+
evaluate { runtime?.call("__hotUpdate", update) }
|
|
601
|
+
}
|
|
533
602
|
}
|
|
534
603
|
}
|
|
535
604
|
}
|
|
@@ -538,6 +607,43 @@ class Bridge(private val activity: Activity, private val rootView: RootView) {
|
|
|
538
607
|
}
|
|
539
608
|
}
|
|
540
609
|
|
|
610
|
+
fun reload() {
|
|
611
|
+
if (isDebuggable) thread(isDaemon = true) { runCatching { downloadAndRun() } }
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
private fun downloadAndRun() {
|
|
615
|
+
val bundle = download(URL("$devServerURL/bundle.js"))
|
|
616
|
+
Log.i(TAG, "reload")
|
|
617
|
+
mainHandler.post { run(bundle) }
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
private fun download(url: URL): ByteArray {
|
|
621
|
+
val connection = url.openConnection() as HttpURLConnection
|
|
622
|
+
val length = connection.contentLengthLong
|
|
623
|
+
val bundle = ByteArrayOutputStream()
|
|
624
|
+
val buffer = ByteArray(64 * 1024)
|
|
625
|
+
|
|
626
|
+
mainHandler.post { downloadBanner.show(0.0) }
|
|
627
|
+
try {
|
|
628
|
+
connection.inputStream.use { input ->
|
|
629
|
+
while (true) {
|
|
630
|
+
val count = input.read(buffer)
|
|
631
|
+
if (count < 0) break
|
|
632
|
+
|
|
633
|
+
bundle.write(buffer, 0, count)
|
|
634
|
+
if (length > 0) {
|
|
635
|
+
val progress = bundle.size().toDouble() / length
|
|
636
|
+
mainHandler.post { downloadBanner.show(progress) }
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
} finally {
|
|
641
|
+
mainHandler.post { downloadBanner.hide() }
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
return bundle.toByteArray()
|
|
645
|
+
}
|
|
646
|
+
|
|
541
647
|
// ---------- Main thread ----------
|
|
542
648
|
|
|
543
649
|
/** Vue removes only a subtree's root: the nodes under it are forgotten with it. */
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
package dev.nativue
|
|
2
|
+
|
|
3
|
+
import android.app.Activity
|
|
4
|
+
import android.graphics.Color
|
|
5
|
+
import android.graphics.Typeface
|
|
6
|
+
import android.view.Gravity
|
|
7
|
+
import android.view.ViewGroup
|
|
8
|
+
import android.widget.FrameLayout
|
|
9
|
+
import android.widget.TextView
|
|
10
|
+
import androidx.core.view.ViewCompat
|
|
11
|
+
import androidx.core.view.WindowInsetsCompat
|
|
12
|
+
|
|
13
|
+
/** Development only: a strip at the top of the screen while the app downloads the dev server's bundle. Mirrors ios/DownloadBanner.swift. */
|
|
14
|
+
class DownloadBanner(private val activity: Activity) {
|
|
15
|
+
private var label: TextView? = null
|
|
16
|
+
|
|
17
|
+
fun show(progress: Double) {
|
|
18
|
+
val label = label ?: makeLabel().also { label = it }
|
|
19
|
+
label.text = "Downloading ${(progress * 100).toInt()}%"
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
fun hide() {
|
|
23
|
+
label?.let { (it.parent as? ViewGroup)?.removeView(it) }
|
|
24
|
+
label = null
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private fun makeLabel(): TextView {
|
|
28
|
+
val height = (24 * activity.resources.displayMetrics.density).toInt()
|
|
29
|
+
val label = TextView(activity)
|
|
30
|
+
label.setBackgroundColor(Color.rgb(0x0B, 0x12, 0x20))
|
|
31
|
+
label.setTextColor(Color.rgb(0xF1, 0xF5, 0xF9))
|
|
32
|
+
label.textSize = 12f
|
|
33
|
+
label.typeface = Typeface.DEFAULT_BOLD
|
|
34
|
+
label.gravity = Gravity.CENTER
|
|
35
|
+
ViewCompat.setOnApplyWindowInsetsListener(label) { view, insets ->
|
|
36
|
+
val top = insets.getInsets(WindowInsetsCompat.Type.statusBars()).top
|
|
37
|
+
view.layoutParams = (view.layoutParams as FrameLayout.LayoutParams).apply { topMargin = top }
|
|
38
|
+
insets
|
|
39
|
+
}
|
|
40
|
+
activity.addContentView(label, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height))
|
|
41
|
+
ViewCompat.requestApplyInsets(label)
|
|
42
|
+
|
|
43
|
+
return label
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
package dev.nativue
|
|
2
2
|
|
|
3
|
-
import android.app.Activity
|
|
4
3
|
import android.content.Intent
|
|
5
4
|
import android.content.res.Configuration
|
|
5
|
+
import android.net.Uri
|
|
6
6
|
import android.os.Build
|
|
7
7
|
import android.os.Bundle
|
|
8
|
+
import android.view.KeyEvent
|
|
8
9
|
import android.view.ViewGroup
|
|
9
10
|
import android.view.ViewTreeObserver
|
|
11
|
+
import android.widget.EditText
|
|
10
12
|
import android.window.OnBackInvokedCallback
|
|
11
13
|
import android.window.OnBackInvokedDispatcher
|
|
14
|
+
import androidx.activity.ComponentActivity
|
|
12
15
|
import androidx.annotation.RequiresApi
|
|
13
16
|
import com.facebook.soloader.SoLoader
|
|
14
17
|
|
|
15
18
|
/** The app's activity: runs assets/main.bundle.js (or, in debug builds, the dev server's bundle). */
|
|
16
|
-
open class NativueActivity :
|
|
19
|
+
open class NativueActivity : ComponentActivity() {
|
|
17
20
|
private lateinit var bridge: Bridge
|
|
21
|
+
private var lastRKeyTime = 0L
|
|
18
22
|
|
|
19
23
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
20
24
|
super.onCreate(savedInstanceState)
|
|
@@ -28,7 +32,13 @@ open class NativueActivity : Activity() {
|
|
|
28
32
|
bridge = Bridge(this, rootView)
|
|
29
33
|
bridge.onCanGoBackChange = ::setHandlesBack
|
|
30
34
|
bridge.start()
|
|
31
|
-
intent.data?.let
|
|
35
|
+
intent.data?.let(::openURL)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private fun openURL(uri: Uri) {
|
|
39
|
+
val url = uri.toString()
|
|
40
|
+
|
|
41
|
+
if (!PluginRegistry.get(this).open(url)) bridge.openURL(url)
|
|
32
42
|
}
|
|
33
43
|
|
|
34
44
|
/**
|
|
@@ -52,7 +62,7 @@ open class NativueActivity : Activity() {
|
|
|
52
62
|
/** Deep links while running: singleTask brings this activity back instead of starting another. */
|
|
53
63
|
override fun onNewIntent(intent: Intent) {
|
|
54
64
|
super.onNewIntent(intent)
|
|
55
|
-
intent.data?.let
|
|
65
|
+
intent.data?.let(::openURL)
|
|
56
66
|
}
|
|
57
67
|
|
|
58
68
|
/** The manifest's configChanges keep the activity (and the JS state) through uiMode changes. */
|
|
@@ -71,6 +81,15 @@ open class NativueActivity : Activity() {
|
|
|
71
81
|
predictiveBack.setRegistered(handlesBack)
|
|
72
82
|
}
|
|
73
83
|
|
|
84
|
+
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
|
|
85
|
+
if (keyCode != KeyEvent.KEYCODE_R || currentFocus is EditText) return super.onKeyUp(keyCode, event)
|
|
86
|
+
|
|
87
|
+
if (event.eventTime - lastRKeyTime < DOUBLE_PRESS_MS) bridge.reload()
|
|
88
|
+
lastRKeyTime = event.eventTime
|
|
89
|
+
|
|
90
|
+
return true
|
|
91
|
+
}
|
|
92
|
+
|
|
74
93
|
@Deprecated("Deprecated in Java")
|
|
75
94
|
override fun onBackPressed() {
|
|
76
95
|
if (!bridge.goBack()) super.onBackPressed()
|
|
@@ -87,7 +106,7 @@ open class NativueActivity : Activity() {
|
|
|
87
106
|
* release build crashes on launch if the activity itself refers to it.
|
|
88
107
|
*/
|
|
89
108
|
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
|
90
|
-
private class PredictiveBack(private val activity:
|
|
109
|
+
private class PredictiveBack(private val activity: ComponentActivity, onBack: () -> Unit) {
|
|
91
110
|
private val callback = OnBackInvokedCallback(onBack)
|
|
92
111
|
|
|
93
112
|
fun setRegistered(isRegistered: Boolean) {
|
|
@@ -96,3 +115,5 @@ private class PredictiveBack(private val activity: Activity, onBack: () -> Unit)
|
|
|
96
115
|
else dispatcher.unregisterOnBackInvokedCallback(callback)
|
|
97
116
|
}
|
|
98
117
|
}
|
|
118
|
+
|
|
119
|
+
private const val DOUBLE_PRESS_MS = 500
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
package dev.nativue
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.content.pm.PackageManager
|
|
5
|
+
import android.os.Handler
|
|
6
|
+
import android.os.Looper
|
|
7
|
+
import androidx.activity.ComponentActivity
|
|
8
|
+
import androidx.activity.result.ActivityResultLauncher
|
|
9
|
+
import androidx.activity.result.contract.ActivityResultContracts.RequestMultiplePermissions
|
|
10
|
+
import androidx.core.app.ActivityCompat
|
|
11
|
+
import androidx.core.content.ContextCompat
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Runtime permissions as JavaScript's PermissionResponse: `{ status, granted, canAskAgain }`, to
|
|
15
|
+
* resolve a Promise with, plus the plugin's own fields. Pass only the permissions this Android
|
|
16
|
+
* version asks for (POST_NOTIFICATIONS is Android 13+).
|
|
17
|
+
*/
|
|
18
|
+
class Permissions internal constructor(private val registry: PluginRegistry) {
|
|
19
|
+
// Android can't tell "never asked" from "denied for good": both have no rationale to show.
|
|
20
|
+
private val asked = registry.applicationContext.getSharedPreferences("dev.nativue.permissions", Context.MODE_PRIVATE)
|
|
21
|
+
private var launcher: ActivityResultLauncher<Array<String>>? = null
|
|
22
|
+
private var launcherActivity: ComponentActivity? = null
|
|
23
|
+
private val mainHandler = Handler(Looper.getMainLooper())
|
|
24
|
+
/** One system prompt at a time: the others wait their turn. */
|
|
25
|
+
private val waitingRequests = ArrayDeque<Pair<List<String>, (Map<String, Any>) -> Unit>>()
|
|
26
|
+
|
|
27
|
+
fun status(permissions: List<String>): Map<String, Any> {
|
|
28
|
+
val context = registry.applicationContext
|
|
29
|
+
val denied = permissions.filter {
|
|
30
|
+
ContextCompat.checkSelfPermission(context, it) != PackageManager.PERMISSION_GRANTED
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (denied.isEmpty()) return response("granted", canAskAgain = true)
|
|
34
|
+
// A rationale means it was denied once, even when another prompt asked.
|
|
35
|
+
if (denied.any { !asked.getBoolean(it, false) && !showsRationale(it) }) {
|
|
36
|
+
return response("undetermined", canAskAgain = true)
|
|
37
|
+
}
|
|
38
|
+
// Without an activity there's no rationale to read: assume it can still ask.
|
|
39
|
+
if (registry.activity == null) return response("denied", canAskAgain = true)
|
|
40
|
+
|
|
41
|
+
return response("denied", canAskAgain = denied.all(::showsRationale))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Shows the system prompt (unless all are granted), then calls onResult on the main thread. */
|
|
45
|
+
fun request(permissions: List<String>, onResult: (Map<String, Any>) -> Unit) {
|
|
46
|
+
mainHandler.post {
|
|
47
|
+
waitingRequests.addLast(permissions to onResult)
|
|
48
|
+
if (waitingRequests.size == 1) launchNext()
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
internal fun activityCreated(activity: ComponentActivity) {
|
|
53
|
+
// A result that comes after a recreation reaches this one: the waiting requests outlive the activity.
|
|
54
|
+
launcher = activity.registerForActivityResult(RequestMultiplePermissions(), ::finishRequest)
|
|
55
|
+
launcherActivity = activity
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Finished for good, not recreated: a prompt it showed won't answer, so the waiting requests get the status. */
|
|
59
|
+
internal fun activityDestroyed(activity: ComponentActivity) {
|
|
60
|
+
if (activity !== launcherActivity || activity.isChangingConfigurations) return
|
|
61
|
+
|
|
62
|
+
launcher = null
|
|
63
|
+
launcherActivity = null
|
|
64
|
+
while (waitingRequests.isNotEmpty()) {
|
|
65
|
+
val (permissions, onResult) = waitingRequests.removeFirst()
|
|
66
|
+
|
|
67
|
+
onResult(status(permissions))
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private fun launchNext() {
|
|
72
|
+
val (permissions, onResult) = waitingRequests.firstOrNull() ?: return
|
|
73
|
+
val launcher = launcher
|
|
74
|
+
|
|
75
|
+
if (status(permissions)["granted"] == true || launcher == null || registry.activity == null) {
|
|
76
|
+
waitingRequests.removeFirst()
|
|
77
|
+
onResult(status(permissions))
|
|
78
|
+
launchNext()
|
|
79
|
+
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
launcher.launch(permissions.toTypedArray())
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Asked only when the user answered: Android 11+ reports a prompt dismissed with back as denied,
|
|
88
|
+
* but only a real "Don't allow" shows a rationale next time.
|
|
89
|
+
*/
|
|
90
|
+
private fun finishRequest(results: Map<String, Boolean>) {
|
|
91
|
+
val (permissions, onResult) = waitingRequests.removeFirstOrNull() ?: return
|
|
92
|
+
val answered = results.filter { (permission, granted) -> granted || showsRationale(permission) }.keys
|
|
93
|
+
|
|
94
|
+
asked.edit().apply { answered.forEach { putBoolean(it, true) } }.apply()
|
|
95
|
+
onResult(status(permissions))
|
|
96
|
+
launchNext()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private fun showsRationale(permission: String): Boolean {
|
|
100
|
+
val activity = registry.activity ?: return false
|
|
101
|
+
|
|
102
|
+
return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private fun response(status: String, canAskAgain: Boolean): Map<String, Any> =
|
|
106
|
+
mapOf("status" to status, "granted" to (status == "granted"), "canAskAgain" to canAskAgain)
|
|
107
|
+
}
|