1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//! The Android alert arm: an `android.app.AlertDialog` built, shown and
//! resolved by `FrustNativePresenter.kt`, over `super::android_host`'s
//! shared JNI plumbing (`with_presenter`, the generation-keyed live-entry
//! guard, `nativeOnOutcome`).
//!
//! # The contract
//!
//! Two more members alongside `android_host`'s module doc's table, both
//! called Rust → Kotlin — plain `@JvmStatic fun`s, not `external fun`s, so
//! neither adds to the frozen export set `tests/kotlin_conformance.rs`
//! pins:
//!
//! | Kotlin | Contract |
//! |---|---|
//! | `@JvmStatic fun showAlert(generation, title, message, labels, roles, cancelable)` | Builds and shows the dialog; `labels`/`roles` are parallel, spec-ordered arrays (`roles`' wire codes below) |
//! | `@JvmStatic fun dismiss(generation)` | Takes the live dialog down, if it is still `generation`'s |
//!
//! # Mapping
//!
//! | [`AlertSpec`] | `android.app.AlertDialog` |
//! |---|---|
//! | `title` / `message` | `setTitle` / `setMessage` — always set, even empty: like `NSAlert`'s setters (`appkit_alert`'s module doc), `AlertDialog.Builder`'s take a plain `CharSequence`, never `null` |
//! | up to [`super::MAX_ALERT_ACTIONS`] `actions` | one of `setPositiveButton`/`setNegativeButton`/`setNeutralButton` each — see *Button mapping* |
//! | `cancelable` | `setCancelable` — gates both the back key and an outside tap |
//! | `style` / `anchor` | ignored — `android.app.AlertDialog` has no action-sheet idiom (framework-only, no Material/appcompat), so [`super::AlertStyle::ActionSheet`] presents the same dialog as [`super::AlertStyle::Alert`] and [`AlertSpec::anchor`] has nothing to point at, mirroring `appkit_alert`'s macOS arm |
//!
//! # Button mapping
//!
//! `AlertDialog` has exactly three button slots — positive, negative,
//! neutral — matching [`super::MAX_ALERT_ACTIONS`]. Each action claims its
//! preferred slot in spec order, falling back down its preference list when
//! that slot is already taken (Kotlin's `assignSlots`):
//!
//! - [`ActionRole::Cancel`] prefers negative, then neutral, then positive.
//! - [`ActionRole::Default`]/[`ActionRole::Destructive`] both prefer
//! positive, then neutral, then negative — the framework dialog has no
//! severity styling for `Destructive` (no color role to reach for, unlike
//! `appkit_alert`'s `hasDestructiveAction`), so the two roles differ only
//! in which slot they end up sharing when a spec mixes them.
//!
//! With at most one `Cancel` action ([`AlertSpec::validate`]) and at most
//! [`super::MAX_ALERT_ACTIONS`] actions total, every action always finds a
//! free slot: a one-action alert always lands on positive, and a
//! `[Default, Cancel]` pair lands positive/negative — the platform's own
//! "OK" / "Cancel" convention.
//!
//! # Resolution
//!
//! Every path resolves through the ONE callback
//! `android_host::Java_dev_frust_nativewidgets_FrustNativePresenter_nativeOnOutcome`,
//! guarded exactly once **on the Kotlin side** (`FrustNativePresenter`'s own
//! `liveDialog`-null check — independent of, and in addition to,
//! `android_host`'s `LIVE` generation-keyed parking on the Rust side):
//!
//! - **An action** (button tap) → `DialogInterface.OnClickListener` →
//! [`wire::OUTCOME_ACTION`] with the button's spec index.
//! - **Cancel** (back key, outside tap — only when `cancelable`) →
//! `setOnCancelListener` → [`wire::OUTCOME_CANCELLED`]. A `Dialog`
//! window receives the back key before the hosting Activity's
//! `OnBackPressedDispatcher`, so no dispatcher wiring is needed.
//! - **[`super::dismiss`]** — `FrustNativePresenter.dismiss(generation)`
//! reports [`wire::OUTCOME_DISMISSED`] itself, then closes the dialog.
//! - **No resumed Activity** — checked twice: synchronously in
//! [`Host::show_alert`] via `android_host::has_resumed_activity` (skips
//! marshaling and the JNI round-trip into `showAlert` entirely, the same
//! check the pre-arm stub this module replaces already made), and again
//! inside `showAlert` itself for the narrow race window between the two
//! calls (the resumed Activity finishing in between) → the latter reports
//! [`wire::OUTCOME_NO_HOST`] (maps to [`PresentError::NoHost`]); the
//! former returns the same error synchronously, without ever touching
//! `LIVE`.
//! - **An exception** building or showing the dialog →
//! [`wire::OUTCOME_FAILED`].
//! - **The hosting Activity destroyed** while showing (a configuration
//! change, e.g. rotation, or the user leaving the app) →
//! `Application.ActivityLifecycleCallbacks.onActivityDestroyed` →
//! [`wire::OUTCOME_HOST_LOST`], and the dialog is dismissed with it
//! (module doc's *Retention*).
//!
//! # A displaced presentation
//!
//! A caller that drops its [`super::Presentation`] frees the process-wide
//! Busy slot but not the dialog on screen, so a newer `show_alert` can find
//! the older one still parked. Its Rust-side resolver is resolved at once
//! as `android_host::install_live` displaces it — discarded, since its
//! receiver is already gone (the module doc's own contract: dropping the
//! future frees only Rust-side bookkeeping). The stale `AlertDialog` itself
//! is taken down on Kotlin's side, inside `showAlert`, before the new one is
//! built, so the two never show stacked.
//!
//! # Retention
//!
//! Unlike the Apple arms, which retain the platform objects themselves
//! (`Retained<UIAlertController>`, a delegate) in Rust-owned storage
//! (`apple_host`'s `LIVE`), the `AlertDialog` and its listeners are retained
//! entirely on the JVM side: `FrustNativePresenter`'s own `liveDialog`/
//! `liveActivity` fields, reachable from the `object` singleton (a GC root)
//! for as long as the presentation is live. `android_host`'s `LIVE` holds
//! only what Rust itself needs to resolve the presentation later — the
//! boxed [`Sender`] and the spec's action ids
//! (`android_host::alert_resolver`).
//!
//! # Main-thread dispatch
//!
//! Unlike the Apple arms, which always bounce onto the main queue
//! (`apple_host::on_main`) because a request can arrive from any thread,
//! this arm calls `FrustNativePresenter.showAlert`/`dismiss` **directly**,
//! synchronously, with no Handler post: the whole frust Android runtime is
//! thread-confined to the platform main `Looper` (`android/mod.rs`'s
//! *Threading*; `docs/PLUGINS_CODE_STANDARDS.md`'s events-as-signals
//! convention makes the same claim for a native listener callback), so a
//! [`show_alert`](AlertHost::show_alert)/[`dismiss`](AlertHost::dismiss)
//! request is itself already running there — the same assumption
//! `android_host::has_resumed_activity` already made before this arm
//! existed. There is no debug-only main-thread assertion here (unlike
//! `android/mod.rs`'s exports): that helper is private to `crate::android`,
//! and duplicating it was judged not worth the extra surface for one more
//! call site sharing the same, already-documented assumption.
//!
//! # `unsafe`
//!
//! None — every JNI call here goes through the `jni` crate's safe wrapper
//! API. The raw FFI boundary is `android_host`'s
//! `#[unsafe(no_mangle)]`-exported `nativeOnOutcome`.
compile_error!;
use ;
use Global;
use jlong;
use ;
use ;
use ;
/// The button-role wire codes `showAlert`'s `roles` parameter carries, in
/// [`ActionRole`]'s own declaration order — mirrored verbatim by
/// `FrustNativePresenter.kt`'s `ROLE_*` constants (edited together, the same
/// discipline `super::wire`'s `OUTCOME_*` table documents for the callback
/// direction). No automated drift test pins this table the way
/// `super`'s own tests pin `OUTCOME_*`: this whole module is
/// `#[cfg(target_os = "android")]`, so a test living here never runs under
/// the host-targeted `cargo test` the existing Kotlin-conformance checks
/// rely on to catch drift without an Android toolchain.
const ROLE_DEFAULT: i32 = 0;
const ROLE_CANCEL: i32 = 1;
const ROLE_DESTRUCTIVE: i32 = 2;
/// [`ROLE_DEFAULT`]/[`ROLE_CANCEL`]/[`ROLE_DESTRUCTIVE`] for `role`.
/// The Android host: `super::AlertHost` over `android.app.AlertDialog`.
pub ;
/// `FrustNativePresenter.showAlert(generation, title, message, labels,
/// roles, cancelable)` — see the module doc's contract table.
/// `FrustNativePresenter.dismiss(generation)`.
/// Build a `String[]` of each action's label, in spec order — `showAlert`'s
/// `labels` parameter.
/// Build an `int[]` of each action's [`role_code`], in spec order —
/// `showAlert`'s `roles` parameter.
/// Run JNI calls through the crate's exception-clearing helper
/// (`crate::android::run_jni`), as a [`PresentError::Platform`] — this
/// module's own copy of `android_host`'s private helper of the same shape.
/// Not shared: that one stays private to `android_host`, and widening it was
/// outside this module's own two-file write scope.