craft-native 0.0.90 → 0.0.92

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 (31) hide show
  1. package/dist/android/src/index.d.ts +36 -1
  2. package/dist/android/src/index.js +332 -44
  3. package/dist/android/src/promise-runtime.d.ts +3 -0
  4. package/dist/android/templates/CraftBridge.kt.template +2167 -1177
  5. package/dist/android/templates/CraftHealthConnect.kt.template +45 -6
  6. package/dist/android/templates/CraftHealthConnectStub.kt.template +7 -1
  7. package/dist/android/templates/CraftNative.kt.template +2250 -0
  8. package/dist/android/templates/LocationRecordingService.kt.template +34 -9
  9. package/dist/android/templates/MainActivity.kt.template +42 -8
  10. package/dist/android/templates/proguard-rules.pro.template +4 -1
  11. package/dist/android/templates/test-bridges.html +10 -33
  12. package/dist/api/index.d.ts +1 -1
  13. package/dist/api/ios-advanced.d.ts +8 -5
  14. package/dist/api/live-activity-handle.d.ts +6 -0
  15. package/dist/api/mobile.d.ts +25 -7
  16. package/dist/api/window.d.ts +2 -0
  17. package/dist/cli.js +452 -129
  18. package/dist/index.cjs +77 -22
  19. package/dist/index.js +77 -22
  20. package/dist/ios/src/index.d.ts +1 -1
  21. package/dist/ios/src/index.js +23 -5
  22. package/dist/ios/templates/CraftApp.swift +706 -92
  23. package/dist/ios/templates/CraftWatchApp.swift.template +8 -0
  24. package/dist/ios/templates/WatchApp.Info.plist.template +2 -0
  25. package/dist/ios/templates/WatchExtension.Info.plist.template +34 -0
  26. package/dist/ios/templates/project.yml.template +10 -4
  27. package/dist/mobile.js +52 -21
  28. package/dist/scaffold-version.d.ts +5 -0
  29. package/package.json +1 -1
  30. package/dist/android/templates/CraftBridgeExtensions.kt.template +0 -383
  31. package/dist/android/templates/CraftWidgetProvider.kt.template +0 -246
@@ -1,4 +1,4 @@
1
- package {{PACKAGE_NAME}}
1
+ package com.craft.runtime
2
2
 
3
3
  import android.Manifest
4
4
  import android.app.NotificationChannel
@@ -105,7 +105,7 @@ class LocationRecordingService : Service() {
105
105
  val notification = NotificationCompat.Builder(this, CHANNEL)
106
106
  .setSmallIcon(android.R.drawable.ic_menu_mylocation)
107
107
  .setContentTitle("Recording your activity")
108
- .setContentText("WildLoop is securely saving your route")
108
+ .setContentText("${applicationInfo.loadLabel(packageManager)} is securely saving your route")
109
109
  .setOngoing(true)
110
110
  .setSilent(true)
111
111
  .build()
@@ -119,24 +119,49 @@ class LocationRecordingService : Service() {
119
119
  stopSelf()
120
120
  return START_NOT_STICKY
121
121
  }
122
- else -> startUpdates()
122
+ else -> if (!startUpdates()) {
123
+ stopSelf()
124
+ return START_NOT_STICKY
125
+ }
123
126
  }
124
- return if (CraftLocationRecordingStore.isActive(this)) START_STICKY else START_NOT_STICKY
127
+ return START_STICKY
125
128
  }
126
129
 
127
- private fun startUpdates() {
128
- if (callback != null || !CraftLocationRecordingStore.isActive(this)) return
129
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) return
130
+ private fun startUpdates(): Boolean {
131
+ if (callback != null) return true
132
+ if (!CraftLocationRecordingStore.isActive(this)) return false
133
+ val hasForegroundLocation =
134
+ ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
135
+ ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
136
+ if (!hasForegroundLocation) {
137
+ CraftLocationRecordingStore.stop(this)
138
+ return false
139
+ }
130
140
  val request = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 5_000)
131
141
  .setMinUpdateIntervalMillis(2_000)
132
142
  .setMaxUpdateDelayMillis(10_000)
133
143
  .build()
134
- callback = object : LocationCallback() {
144
+ val nextCallback = object : LocationCallback() {
135
145
  override fun onLocationResult(result: LocationResult) {
136
146
  for (location in result.locations) CraftLocationRecordingStore.append(this@LocationRecordingService, location)
137
147
  }
138
148
  }
139
- client.requestLocationUpdates(request, callback!!, Looper.getMainLooper())
149
+ callback = nextCallback
150
+ return try {
151
+ client.requestLocationUpdates(request, nextCallback, Looper.getMainLooper())
152
+ .addOnFailureListener {
153
+ if (callback === nextCallback) {
154
+ CraftLocationRecordingStore.stop(this)
155
+ stopUpdates()
156
+ stopSelf()
157
+ }
158
+ }
159
+ true
160
+ } catch (error: Exception) {
161
+ callback = null
162
+ CraftLocationRecordingStore.stop(this)
163
+ false
164
+ }
140
165
  }
141
166
 
142
167
  private fun stopUpdates() {
@@ -3,6 +3,7 @@ package {{PACKAGE_NAME}}
3
3
  import android.annotation.SuppressLint
4
4
  import android.os.Bundle
5
5
  import android.content.Intent
6
+ import android.graphics.Bitmap
6
7
  import android.net.Uri
7
8
  import android.webkit.WebResourceRequest
8
9
  import android.webkit.WebResourceError
@@ -43,7 +44,20 @@ class MainActivity : AppCompatActivity() {
43
44
 
44
45
  webView = findViewById(R.id.webview)
45
46
  craftBridge = CraftBridge(this, webView, CraftHealthConnect(this, webView))
46
- handleIncomingDeepLink(intent, dispatch = false)
47
+ // The link that launched the app (#215). Queued until the page's bridge
48
+ // is injected, then dispatched once with `initial: true`, so a page that
49
+ // subscribes rather than calling getInitialURL still hears it. A
50
+ // relaunch from Recents carries the old intent, and was not opened by it.
51
+ // A recreated activity restores what the bridge saved: its intent may
52
+ // be a later warm link that onNewIntent set.
53
+ if (savedInstanceState == null) {
54
+ val launchLink = intent.data?.toString()
55
+ val launchedFromHistory = intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY != 0
56
+ if (launchLink != null && !launchedFromHistory) craftBridge.receiveDeepLink(launchLink)
57
+ } else {
58
+ craftBridge.restoreDeepLinks(savedInstanceState)
59
+ }
60
+ if (savedInstanceState == null) handleIncomingShortcut(intent)
47
61
 
48
62
  setupWebView()
49
63
  loadContent()
@@ -67,6 +81,11 @@ class MainActivity : AppCompatActivity() {
67
81
  }
68
82
 
69
83
  webView.webViewClient = object : WebViewClient() {
84
+ override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
85
+ super.onPageStarted(view, url, favicon)
86
+ craftBridge.markBridgeLoading()
87
+ }
88
+
70
89
  override fun onPageFinished(view: WebView?, url: String?) {
71
90
  super.onPageFinished(view, url)
72
91
  if (url != null && isTrustedUrl(url)) craftBridge.injectBridge()
@@ -143,10 +162,8 @@ class MainActivity : AppCompatActivity() {
143
162
  }
144
163
  }
145
164
 
146
- private fun handleIncomingDeepLink(source: Intent?, dispatch: Boolean) {
147
- val url = source?.data?.toString() ?: return
148
- craftBridge.setInitialURL(url)
149
- if (dispatch) craftBridge.dispatchDeepLink(url)
165
+ private fun handleIncomingShortcut(source: Intent?) {
166
+ source?.getStringExtra(SHORTCUT_TYPE_EXTRA)?.let(craftBridge::dispatchShortcut)
150
167
  }
151
168
 
152
169
  private fun configJson(): JSONObject? {
@@ -181,16 +198,22 @@ class MainActivity : AppCompatActivity() {
181
198
 
182
199
  private fun isTrustedUrl(value: String): Boolean {
183
200
  val uri = Uri.parse(value)
184
- if (uri.scheme == "https" && uri.host == BUNDLED_APP_HOST) return true
185
201
  val origin = "${uri.scheme}://${uri.authority}"
202
+ if (origin == BUNDLED_APP_ORIGIN) return true
186
203
  val localDevelopment = uri.scheme == "http" && uri.host in setOf("localhost", "127.0.0.1", "10.0.2.2")
187
204
  return (uri.scheme == "https" || localDevelopment) && origin in trustedOrigins
188
205
  }
189
206
 
207
+ override fun onSaveInstanceState(outState: Bundle) {
208
+ super.onSaveInstanceState(outState)
209
+ craftBridge.saveDeepLinks(outState)
210
+ }
211
+
190
212
  override fun onNewIntent(intent: Intent) {
191
213
  super.onNewIntent(intent)
192
214
  setIntent(intent)
193
- handleIncomingDeepLink(intent, dispatch = true)
215
+ intent.data?.toString()?.let(craftBridge::receiveDeepLink)
216
+ handleIncomingShortcut(intent)
194
217
  }
195
218
 
196
219
  override fun onBackPressed() {
@@ -223,8 +246,19 @@ class MainActivity : AppCompatActivity() {
223
246
  super.onActivityResult(requestCode, resultCode, data)
224
247
  }
225
248
 
249
+ override fun onRequestPermissionsResult(
250
+ requestCode: Int,
251
+ permissions: Array<out String>,
252
+ grantResults: IntArray
253
+ ) {
254
+ if (craftBridge.onRequestPermissionsResult(requestCode)) return
255
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults)
256
+ }
257
+
226
258
  companion object {
227
259
  private const val BUNDLED_APP_HOST = "appassets.androidplatform.net"
228
- private const val BUNDLED_APP_URL = "https://appassets.androidplatform.net/"
260
+ private const val BUNDLED_APP_ORIGIN = "https://appassets.androidplatform.net"
261
+ private const val BUNDLED_APP_URL = "$BUNDLED_APP_ORIGIN/"
262
+ private const val SHORTCUT_TYPE_EXTRA = "shortcut_type"
229
263
  }
230
264
  }
@@ -6,4 +6,7 @@
6
6
  }
7
7
  -keep class {{PACKAGE_NAME}}.CraftBridge { *; }
8
8
  -keep class {{PACKAGE_NAME}}.CraftHealthConnect { *; }
9
- -keep class {{PACKAGE_NAME}}.LocationRecordingService { *; }
9
+ # libcraft.so finds this exact class and registers its private external methods
10
+ # by their source names during JNI_OnLoad.
11
+ -keep class com.craft.runtime.CraftNative { *; }
12
+ -keep class com.craft.runtime.LocationRecordingService { *; }
@@ -307,9 +307,8 @@
307
307
 
308
308
  <!-- Voice Assistant -->
309
309
  <div class="section">
310
- <h2>Siri / Google Assistant</h2>
311
- <button class="btn" onclick="testRegisterSiri()">Register Shortcut</button>
312
- <button class="btn" onclick="testRemoveSiri()">Remove Shortcut</button>
310
+ <h2>Google Assistant</h2>
311
+ <p>App Actions are declared statically in <code>shortcuts.xml</code>.</p>
313
312
  <div id="siri-result" class="result">Results will appear here...</div>
314
313
  </div>
315
314
 
@@ -766,6 +765,9 @@
766
765
 
767
766
  function testBadge() {
768
767
  if (!window.craft) return showResult('system-result', 'Craft not ready');
768
+ if (!window.craft.capabilities.appBadge) {
769
+ return showResult('system-result', 'App badges are unavailable on Android');
770
+ }
769
771
  window.craft.setBadge(5);
770
772
  setTimeout(() => window.craft.clearBadge(), 3000);
771
773
  showResult('system-result', 'Badge set to 5 (will clear in 3s)');
@@ -1255,36 +1257,11 @@
1255
1257
  }
1256
1258
  }
1257
1259
 
1258
- // Siri / Voice Assistant tests
1259
- async function testRegisterSiri() {
1260
- if (!window.craft?.siri) return showResult('siri-result', 'Siri API not available');
1261
- try {
1262
- const result = await window.craft.siri.register('Open my app', 'open_app');
1263
- showResult('siri-result', 'Siri shortcut registered: ' + JSON.stringify(result));
1264
- log('Siri shortcut registered');
1265
- } catch (e) {
1266
- showResult('siri-result', 'Error: ' + e.message);
1267
- }
1268
- }
1269
-
1270
- async function testRemoveSiri() {
1271
- if (!window.craft?.siri) return showResult('siri-result', 'Siri API not available');
1272
- try {
1273
- const result = await window.craft.siri.remove('open_app');
1274
- showResult('siri-result', 'Siri shortcut removed: ' + JSON.stringify(result));
1275
- log('Siri shortcut removed');
1276
- } catch (e) {
1277
- showResult('siri-result', 'Error: ' + e.message);
1278
- }
1279
- }
1280
-
1281
- // Listen for Siri invocations
1282
- if (window.craft?.siri) {
1283
- window.craft.siri.onInvoke((detail) => {
1284
- log('Siri shortcut invoked: ' + JSON.stringify(detail));
1285
- showResult('siri-result', 'Siri invoked: ' + JSON.stringify(detail));
1286
- });
1287
- }
1260
+ // Listen for Google Assistant invocations declared in shortcuts.xml.
1261
+ window.addEventListener('craftVoiceAction', (event) => {
1262
+ log('Voice action invoked: ' + JSON.stringify(event.detail));
1263
+ showResult('siri-result', 'Voice action invoked: ' + JSON.stringify(event.detail));
1264
+ });
1288
1265
 
1289
1266
  // Watch Connectivity tests
1290
1267
  async function testWatchReachable() {
@@ -20,7 +20,7 @@ export type { Platform, SystemInfo, ExecOptions, ExecResult, SpawnOptions } from
20
20
  export { device, haptics, permissions, camera, biometrics, secureStorage, location, share, lifecycle, notifications, notifications as notification } from './mobile.js';
21
21
  export type { DeviceInfo, DeviceCapabilities, HapticStyle, HapticNotificationType, PermissionType, PermissionStatus, CameraOptions as MobileCameraOptions, PhotoResult, BiometricType, Location, LocationOptions, ShareOptions, AppState, NotificationOptions as MobileNotificationOptions } from './mobile.js';
22
22
  export { carplay, appClips, liveActivities, sharePlay, storeKit, appIntents, tipKit, focusFilters } from './ios-advanced.js';
23
- export type { CarPlayTemplateType, CarPlayListItem, CarPlayGridItem, CarPlayTemplate, AppClipInvocation, LiveActivityContentState, LiveActivityAttributes, LiveActivityConfig, SharePlaySessionState, SharePlayParticipant, SharePlayActivity, ProductType, Product, Transaction, IntentParameterType, IntentParameter, AppIntent, TipDisplayFrequency, Tip, FocusStatus, FocusFilter } from './ios-advanced.js';
23
+ export type { CarPlayTemplateType, CarPlayListItem, CarPlayGridItem, CarPlayTemplate, AppClipInvocation, LiveActivityContentState, LiveActivityAttributes, LiveActivityConfig, LiveActivityHandle, SharePlaySessionState, SharePlayParticipant, SharePlayActivity, ProductType, Product, Transaction, IntentParameterType, IntentParameter, AppIntent, TipDisplayFrequency, Tip, FocusStatus, FocusFilter } from './ios-advanced.js';
24
24
  export { materialYou, photoPicker, workManager, foregroundService, predictiveBack, appLanguage, widgets as androidWidgets, playBilling } from './android-advanced.js';
25
25
  export type { MaterialYouColors, PhotoPickerMediaType, PhotoPickerResult, WorkConstraints, WorkRequest, WorkInfo, ForegroundServiceType, ForegroundNotification, BackEvent, WidgetSizeClass, WidgetConfig, WidgetData, PlayProduct, PlayPurchase } from './android-advanced.js';
26
26
  export { touchBar, desktopWidgets, stageManager, handoff, sidecar, spotlight, quickActions, shareExtension, windowManagement } from './macos-advanced.js';
@@ -224,6 +224,11 @@ export interface LiveActivityConfig {
224
224
  /** Relevance score (0-100) */
225
225
  relevanceScore?: number;
226
226
  }
227
+ export interface LiveActivityHandle {
228
+ readonly id: string;
229
+ update(state: LiveActivityContentState): Promise<void>;
230
+ end(finalState?: LiveActivityContentState): Promise<void>;
231
+ }
227
232
  /**
228
233
  * Live Activities API for Dynamic Island and Lock Screen.
229
234
  *
@@ -236,14 +241,14 @@ export interface LiveActivityConfig {
236
241
  * })
237
242
  *
238
243
  * // Update the activity
239
- * await liveActivities.update(activity.id, {
244
+ * await activity.update({
240
245
  * status: 'on-the-way',
241
246
  * eta: '5 min',
242
247
  * driverName: 'John'
243
248
  * })
244
249
  *
245
250
  * // End the activity
246
- * await liveActivities.end(activity.id, { status: 'delivered' })
251
+ * await activity.end({ status: 'delivered' })
247
252
  */
248
253
  export declare const liveActivities: {
249
254
  /**
@@ -257,9 +262,7 @@ export declare const liveActivities: {
257
262
  /**
258
263
  * Start a new Live Activity.
259
264
  */
260
- start(config: LiveActivityConfig): Promise<{
261
- id: string;
262
- }>;
265
+ start(config: LiveActivityConfig): Promise<LiveActivityHandle>;
263
266
  /**
264
267
  * Update an existing Live Activity.
265
268
  */
@@ -0,0 +1,6 @@
1
+ export interface TypedLiveActivityHandle<State> {
2
+ readonly id: string;
3
+ update(state: State): Promise<void>;
4
+ end(finalState?: State): Promise<void>;
5
+ }
6
+ export declare function createLiveActivityHandle<State>(id: string, update: (state: State) => Promise<void>, end: (finalState?: State) => Promise<void>): TypedLiveActivityHandle<State>;
@@ -550,9 +550,12 @@ export declare const share: {
550
550
  /**
551
551
  * Open native share dialog.
552
552
  *
553
+ * Resolves `true` when the person shared and `false` when they dismissed the
554
+ * dialog, on the native bridges and in the browser alike.
555
+ *
553
556
  * @param options - Share options
554
557
  */
555
- share(options: ShareOptions): Promise<void>;
558
+ share(options: ShareOptions): Promise<boolean>;
556
559
  /**
557
560
  * Check if sharing is available.
558
561
  */
@@ -670,7 +673,14 @@ export declare const keepAwake: {
670
673
  };
671
674
  export declare const deepLinks: {
672
675
  getInitialURL(): Promise<string | null>;
673
- onLink(callback: (url: string) => void): () => void;
676
+ /**
677
+ * The second argument says whether this is the link that launched the app.
678
+ * A page that also calls `getInitialURL()` after an await gets that link
679
+ * from both, and can skip it here when `initial` is true.
680
+ */
681
+ onLink(callback: (url: string, link: {
682
+ initial: boolean;
683
+ }) => void): () => void;
674
684
  };
675
685
  export declare function normalizeDeepLinkURL(value: unknown): string | null;
676
686
  export declare const network: {
@@ -729,13 +739,21 @@ export interface LiveActivityOptions extends LiveActivityState {
729
739
  activityId: string;
730
740
  title: string;
731
741
  }
732
- export declare const liveActivities: {
733
- start(options: LiveActivityOptions): Promise<{
734
- id: string;
735
- }>;
742
+ export interface LiveActivityHandle {
743
+ readonly id: string;
736
744
  update(state: LiveActivityState): Promise<void>;
745
+ end(finalState?: LiveActivityState): Promise<void>;
746
+ }
747
+ export interface LiveActivitiesApi {
748
+ start(options: LiveActivityOptions): Promise<LiveActivityHandle>;
749
+ update(id: string, state: LiveActivityState): Promise<void>;
750
+ /** @deprecated Pass the activity id returned by start, or use handle.update(state). */
751
+ update(state: LiveActivityState): Promise<void>;
752
+ end(id: string, finalState?: LiveActivityState): Promise<void>;
753
+ /** @deprecated Pass the activity id returned by start, or use handle.end(finalState). */
737
754
  end(): Promise<void>;
738
- };
755
+ }
756
+ export declare const liveActivities: LiveActivitiesApi;
739
757
  export declare const watchConnectivity: {
740
758
  send(message: Record<string, unknown>): Promise<Record<string, unknown>>;
741
759
  updateContext(context: Record<string, unknown>): Promise<void>;
@@ -198,6 +198,8 @@ export declare class Window {
198
198
  get isClosed(): boolean;
199
199
  private _setupEventListeners;
200
200
  private _cleanupEventListeners;
201
+ /** Restore a retained handle after native code has shown it again. */
202
+ private _markOpen;
201
203
  private _emit;
202
204
  /**
203
205
  * Register an event handler