sinomo-ui-platform 0.7.0

Platform shell and event normalization boundary for SUI.
Documentation
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
use std::{
    collections::BTreeMap,
    fs::File,
    io::{Read, Write},
    os::fd::FromRawFd,
    path::Path,
    sync::{
        Arc, Mutex, MutexGuard, OnceLock,
        atomic::{AtomicI64, Ordering},
    },
    task::{Poll, Waker},
};

use jni::{
    Env, EnvUnowned, NativeMethod, Outcome,
    objects::{JClass, JObject, JObjectArray, JString, JValue},
    refs::Global,
};
use sui_core::{Error, Result};
use winit::platform::android::activity::AndroidApp;

use crate::file_dialog::{
    FileDialogFuture, FileDialogMode, FileDialogRequest, FileDialogSelection, FolderEntry,
    PlatformFile, PlatformFolder,
};

/// `net.sinomo.ui.platform.FilePicker`, compiled by `cargo xtask android dex`.
const FILE_PICKER_DEX: &[u8] = include_bytes!("../java/classes.dex");

// Matches the mode numbers in FilePicker.java.
const OPEN_FILE: i32 = 0;
const OPEN_FILES: i32 = 1;
const SAVE_FILE: i32 = 2;
const OPEN_FOLDER: i32 = 3;

/// The MIME type that `DocumentsContract` gives folders.
const FOLDER_MIME_TYPE: &str = "vnd.android.document/directory";

/// What a picker answered: the chosen document URIs, `None` when cancelled.
type PickerAnswer = std::result::Result<Option<Vec<String>>, String>;

#[derive(Default)]
struct PendingPicker {
    answer: Option<PickerAnswer>,
    waker: Option<Waker>,
}

static ANDROID_APP: Mutex<Option<AndroidApp>> = Mutex::new(None);
static PENDING_PICKERS: Mutex<BTreeMap<i64, Arc<Mutex<PendingPicker>>>> =
    Mutex::new(BTreeMap::new());
static NEXT_TOKEN: AtomicI64 = AtomicI64::new(1);
static FILE_PICKER_CLASS: OnceLock<Global<JClass<'static>>> = OnceLock::new();
static FILE_PICKER_CLASS_LOADING: Mutex<()> = Mutex::new(());

/// Lets file dialogs reach the activity the platform runs in.
pub(crate) fn set_android_app(app: AndroidApp) {
    *lock(&ANDROID_APP) = Some(app);
}

/// Shows the system document picker. Android has no dialog titles, parent
/// windows, or starting directories, and its pickers filter by MIME type, so
/// the request's extensions become the types Android knows for them. Its
/// folder picker chooses one folder, even for `OpenFolders`.
pub(crate) fn show_file_dialog(request: FileDialogRequest) -> FileDialogFuture {
    let mode = match request.mode {
        FileDialogMode::OpenFile => OPEN_FILE,
        FileDialogMode::OpenFiles => OPEN_FILES,
        FileDialogMode::SaveFile => SAVE_FILE,
        FileDialogMode::OpenFolder | FileDialogMode::OpenFolders => OPEN_FOLDER,
    };
    let app = match android_app() {
        Ok(app) => app,
        Err(error) => return Box::pin(async move { Err(error) }),
    };
    let extensions = request
        .filters
        .into_iter()
        .flat_map(|filter| filter.extensions)
        .collect::<Vec<_>>();
    let name = request.suggested_name;

    let token = NEXT_TOKEN.fetch_add(1, Ordering::Relaxed);
    let pending = Arc::new(Mutex::new(PendingPicker::default()));
    lock(&PENDING_PICKERS).insert(token, pending.clone());
    let main_thread_app = app.clone();
    app.run_on_java_main_thread(Box::new(move || {
        let shown = with_activity(&main_thread_app, |env, activity| {
            let class = file_picker_class(env, activity)?;
            let extensions = string_array(env, &extensions)?;
            let name = match &name {
                Some(name) => env.new_string(name)?.into(),
                None => JObject::null(),
            };
            env.call_static_method(
                class,
                jni::jni_str!("show"),
                jni::jni_sig!("(Landroid/app/Activity;JI[Ljava/lang/String;Ljava/lang/String;)V"),
                &[
                    activity.into(),
                    JValue::Long(token),
                    JValue::Int(mode),
                    (&extensions).into(),
                    (&name).into(),
                ],
            )?;
            Ok(())
        });
        if let Err(error) = shown {
            answer(
                token,
                Err(format!("could not show the file dialog: {error}")),
            );
        }
    }));

    Box::pin(async move {
        let answer = std::future::poll_fn(|context| {
            let mut pending = lock(&pending);
            match pending.answer.take() {
                Some(answer) => Poll::Ready(answer),
                None => {
                    pending.waker = Some(context.waker().clone());
                    Poll::Pending
                }
            }
        })
        .await
        .map_err(Error::new)?;
        let Some(uris) = answer.filter(|uris| !uris.is_empty()) else {
            return Ok(None);
        };
        let mut selection = FileDialogSelection::default();
        for uri in uris {
            let name = display_name(&app, &uri);
            if mode == OPEN_FOLDER {
                selection.folders.push(AndroidFolder::at(uri, name));
            } else {
                selection.files.push(AndroidFile::at(uri, name));
            }
        }
        Ok(Some(selection))
    })
}

/// A document the user chose in an Android picker, or found in a chosen
/// folder, named by a content URI.
#[derive(Clone, Debug)]
pub(crate) struct AndroidFile {
    uri: String,
    name: String,
}

impl AndroidFile {
    fn at(uri: String, name: String) -> PlatformFile {
        PlatformFile::from_inner(Self { uri, name })
    }

    pub(crate) fn file_name(&self) -> String {
        self.name.clone()
    }

    pub(crate) fn path(&self) -> Option<&Path> {
        None
    }

    pub(crate) async fn read(&self) -> Result<Vec<u8>> {
        let mut bytes = Vec::new();
        self.open("r")?
            .read_to_end(&mut bytes)
            .map_err(|error| Error::new(format!("file read failed: {error}")))?;
        Ok(bytes)
    }

    pub(crate) async fn write(&self, data: &[u8]) -> Result<()> {
        self.open("wt")?
            .write_all(data)
            .map_err(|error| Error::new(format!("file write failed: {error}")))
    }

    /// Opens the document through its provider in a `ContentResolver` mode.
    fn open(&self, mode: &str) -> Result<File> {
        let app = android_app()?;
        let descriptor = with_activity(&app, |env, activity| {
            let class = file_picker_class(env, activity)?;
            let uri = env.new_string(&self.uri)?;
            let mode = env.new_string(mode)?;
            env.call_static_method(
                class,
                jni::jni_str!("open"),
                jni::jni_sig!("(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)I"),
                &[activity.into(), (&uri).into(), (&mode).into()],
            )?
            .i()
        })
        .map_err(|error| Error::new(format!("could not open {}: {error}", self.name)))?;
        // SAFETY: `FilePicker.open` detached the descriptor from its Java
        // owner, so the file is its only owner.
        Ok(unsafe { File::from_raw_fd(descriptor) })
    }
}

/// A folder in a document tree the user chose in Android's folder picker,
/// named by the content URI of its document within that tree. The tree's
/// permission covers everything inside it.
#[derive(Clone, Debug)]
pub(crate) struct AndroidFolder {
    uri: String,
    name: String,
}

/// A document directly inside an [`AndroidFolder`].
struct Child {
    uri: String,
    name: String,
    folder: bool,
}

impl Child {
    fn into_entry(self) -> FolderEntry {
        if self.folder {
            FolderEntry::Folder(AndroidFolder::at(self.uri, self.name))
        } else {
            FolderEntry::File(AndroidFile::at(self.uri, self.name))
        }
    }
}

impl AndroidFolder {
    fn at(uri: String, name: String) -> PlatformFolder {
        PlatformFolder::from_inner(Self { uri, name })
    }

    pub(crate) fn name(&self) -> String {
        self.name.clone()
    }

    pub(crate) fn path(&self) -> Option<&Path> {
        None
    }

    pub(crate) async fn entries(&self) -> Result<Vec<FolderEntry>> {
        Ok(self
            .children()?
            .into_iter()
            .map(Child::into_entry)
            .collect())
    }

    pub(crate) async fn file(&self, name: &str) -> Result<Option<PlatformFile>> {
        Ok(self
            .child(name)?
            .filter(|child| !child.folder)
            .map(|child| AndroidFile::at(child.uri, child.name)))
    }

    pub(crate) async fn folder(&self, name: &str) -> Result<Option<PlatformFolder>> {
        Ok(self
            .child(name)?
            .filter(|child| child.folder)
            .map(|child| Self::at(child.uri, child.name)))
    }

    pub(crate) async fn create_file(&self, name: &str) -> Result<PlatformFile> {
        let child = self.child_or_create(name, false)?;
        Ok(AndroidFile::at(child.uri, child.name))
    }

    pub(crate) async fn create_folder(&self, name: &str) -> Result<PlatformFolder> {
        let child = self.child_or_create(name, true)?;
        Ok(Self::at(child.uri, child.name))
    }

    fn children(&self) -> Result<Vec<Child>> {
        let app = android_app()?;
        let listing = with_activity(&app, |env, activity| {
            let class = file_picker_class(env, activity)?;
            let uri = env.new_string(&self.uri)?;
            let listing = env
                .call_static_method(
                    class,
                    jni::jni_str!("children"),
                    jni::jni_sig!(
                        "(Landroid/content/Context;Ljava/lang/String;)[Ljava/lang/String;"
                    ),
                    &[activity.into(), (&uri).into()],
                )?
                .l()?;
            let listing = env.cast_local::<JObjectArray<JString>>(listing)?;
            let mut strings = Vec::new();
            for index in 0..listing.len(env)? {
                let string = listing.get_element(env, index)?;
                strings.push(if string.is_null() {
                    String::new()
                } else {
                    string.try_to_string(env)?
                });
            }
            Ok(strings)
        })
        .map_err(|error| Error::new(format!("could not list {}: {error}", self.name)))?;
        let (children, _) = listing.as_chunks::<3>();
        Ok(children
            .iter()
            .map(|[uri, name, mime_type]| Child {
                uri: uri.clone(),
                name: name.clone(),
                folder: mime_type == FOLDER_MIME_TYPE,
            })
            .collect())
    }

    /// The child called `name`. Document providers do not look documents up
    /// by name, so this lists the folder.
    fn child(&self, name: &str) -> Result<Option<Child>> {
        Ok(self
            .children()?
            .into_iter()
            .find(|child| child.name == name))
    }

    /// The child called `name` if it is the requested kind, else a new one.
    /// Providers give a new document a unique name rather than replace one,
    /// so this checks for an existing child first.
    fn child_or_create(&self, name: &str, folder: bool) -> Result<Child> {
        match self.child(name)? {
            Some(child) if child.folder == folder => return Ok(child),
            Some(_) => {
                return Err(Error::new(format!(
                    "could not create {name} in {}: a {} has that name",
                    self.name,
                    if folder { "file" } else { "folder" }
                )));
            }
            None => {}
        }
        let app = android_app()?;
        let uri = with_activity(&app, |env, activity| {
            let class = file_picker_class(env, activity)?;
            let folder_uri = env.new_string(&self.uri)?;
            let child_name = env.new_string(name)?;
            let uri = env
                .call_static_method(
                    class,
                    jni::jni_str!("create"),
                    jni::jni_sig!(
                        "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/String;"
                    ),
                    &[
                        activity.into(),
                        (&folder_uri).into(),
                        (&child_name).into(),
                        JValue::Bool(folder),
                    ],
                )?
                .l()?;
            env.cast_local::<JString>(uri)?.try_to_string(env)
        })
        .map_err(|error| {
            Error::new(format!("could not create {name} in {}: {error}", self.name))
        })?;
        // The provider may adjust the name, so ask for the one it chose.
        let name = display_name(&app, &uri);
        Ok(Child { uri, name, folder })
    }
}

/// The name a document's provider shows for it, or its URI if that fails.
fn display_name(app: &AndroidApp, uri: &str) -> String {
    with_activity(app, |env, activity| {
        let class = file_picker_class(env, activity)?;
        let uri = env.new_string(uri)?;
        let name = env
            .call_static_method(
                class,
                jni::jni_str!("displayName"),
                jni::jni_sig!("(Landroid/content/Context;Ljava/lang/String;)Ljava/lang/String;"),
                &[activity.into(), (&uri).into()],
            )?
            .l()?;
        env.cast_local::<JString>(name)?.try_to_string(env)
    })
    .unwrap_or_else(|_| uri.to_string())
}

fn android_app() -> Result<AndroidApp> {
    lock(&ANDROID_APP)
        .clone()
        .ok_or_else(|| Error::new("file dialogs need the Android platform to be running"))
}

fn answer(token: i64, answer: PickerAnswer) {
    let Some(pending) = lock(&PENDING_PICKERS).remove(&token) else {
        return;
    };
    let mut pending = lock(&pending);
    pending.answer = Some(answer);
    if let Some(waker) = pending.waker.take() {
        waker.wake();
    }
}

/// `FilePicker.onResult`, which the Java main thread calls once a picker
/// answers.
extern "system" fn on_picker_result<'local>(
    mut env: EnvUnowned<'local>,
    _class: JClass<'local>,
    token: jni::sys::jlong,
    uris: JObjectArray<'local, JString<'local>>,
    error: JString<'local>,
) {
    let outcome = env
        .with_env(|env| -> jni::errors::Result<PickerAnswer> {
            if !error.is_null() {
                return Ok(Err(error.try_to_string(env)?));
            }
            if uris.is_null() {
                return Ok(Ok(None));
            }
            let mut chosen = Vec::new();
            for index in 0..uris.len(env)? {
                chosen.push(uris.get_element(env, index)?.try_to_string(env)?);
            }
            Ok(Ok(Some(chosen)))
        })
        .into_outcome();
    answer(
        token,
        match outcome {
            Outcome::Ok(picked) => picked,
            Outcome::Err(error) => Err(format!("could not read the file dialog's answer: {error}")),
            Outcome::Panic(_) => Err("reading the file dialog's answer panicked".to_string()),
        },
    );
}

/// The `FilePicker` class, loaded from the embedded DEX the first time it is
/// needed. The activity's class loader cannot see it, so callers must use this
/// class reference rather than look the class up by name.
fn file_picker_class<'a>(
    env: &mut Env<'_>,
    activity: &JObject<'_>,
) -> jni::errors::Result<&'a Global<JClass<'static>>> {
    if let Some(class) = FILE_PICKER_CLASS.get() {
        return Ok(class);
    }
    let _loading = lock(&FILE_PICKER_CLASS_LOADING);
    if let Some(class) = FILE_PICKER_CLASS.get() {
        return Ok(class);
    }
    let dex = env.byte_array_from_slice(FILE_PICKER_DEX)?;
    let buffer = env
        .call_static_method(
            jni::jni_str!("java/nio/ByteBuffer"),
            jni::jni_str!("wrap"),
            jni::jni_sig!("([B)Ljava/nio/ByteBuffer;"),
            &[(&dex).into()],
        )?
        .l()?;
    let parent = env
        .call_method(
            activity,
            jni::jni_str!("getClassLoader"),
            jni::jni_sig!("()Ljava/lang/ClassLoader;"),
            &[],
        )?
        .l()?;
    let loader = env.new_object(
        jni::jni_str!("dalvik/system/InMemoryDexClassLoader"),
        jni::jni_sig!("(Ljava/nio/ByteBuffer;Ljava/lang/ClassLoader;)V"),
        &[(&buffer).into(), (&parent).into()],
    )?;
    let class_name = env.new_string("net.sinomo.ui.platform.FilePicker")?;
    let class = env
        .call_method(
            &loader,
            jni::jni_str!("loadClass"),
            jni::jni_sig!("(Ljava/lang/String;)Ljava/lang/Class;"),
            &[(&class_name).into()],
        )?
        .l()?;
    let class = env.cast_local::<JClass>(class)?;
    let natives = [unsafe {
        NativeMethod::from_raw_parts(
            jni::jni_str!("onResult"),
            jni::jni_str!("(J[Ljava/lang/String;Ljava/lang/String;)V"),
            on_picker_result as *mut _,
        )
    }];
    unsafe { env.register_native_methods(&class, &natives)? };
    let class = env.new_global_ref(class)?;
    Ok(FILE_PICKER_CLASS.get_or_init(|| class))
}

fn string_array<'local>(
    env: &mut Env<'local>,
    strings: &[String],
) -> jni::errors::Result<JObjectArray<'local, JString<'local>>> {
    let array = JObjectArray::<JString>::new(env, strings.len(), JString::null())?;
    for (index, string) in strings.iter().enumerate() {
        let string = env.new_string(string)?;
        array.set_element(env, index, &string)?;
    }
    Ok(array)
}

/// Runs `call` with the activity on the current thread, attaching it to the
/// Java VM if needed, and reports a Java exception it throws as an error.
fn with_activity<T>(
    app: &AndroidApp,
    call: impl FnOnce(&mut Env<'_>, &JObject<'_>) -> jni::errors::Result<T>,
) -> std::result::Result<T, String> {
    let vm = unsafe { jni::JavaVM::from_raw(app.vm_as_ptr().cast()) };
    let raw_activity = app.activity_as_ptr() as jni::sys::jobject;
    vm.attach_current_thread(|env| -> jni::errors::Result<_> {
        let activity = unsafe { env.as_cast_raw::<Global<JObject>>(&raw_activity)? };
        Ok(match call(env, &activity) {
            Ok(value) => Ok(value),
            Err(jni::errors::Error::JavaException) => Err(match env.exception_catch() {
                Err(jni::errors::Error::CaughtJavaException { name, msg, .. }) => {
                    format!("{name}: {msg}")
                }
                Err(error) => error.to_string(),
                Ok(()) => "a Java exception was thrown".to_string(),
            }),
            Err(error) => Err(error.to_string()),
        })
    })
    .unwrap_or_else(|error| Err(error.to_string()))
}

fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    mutex
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
}