kopuz-player 0.8.2

A modern, lightweight music player built with Rust and Dioxus.
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use jni::objects::{GlobalRef, JClass, JObject, JString, JValue};
use jni::{JNIEnv, JavaVM};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, OnceLock};

// Set from the JNI thread when the hardware/gesture back is pressed; drained on the
// runtime by take_back_pressed(). Decoupled because dioxus signals can only be touched
// from the runtime thread, not the JNI thread.
static BACK_PENDING: AtomicBool = AtomicBool::new(false);

/// Returns true once per back press, clearing the pending flag.
pub fn take_back_pressed() -> bool {
    BACK_PENDING.swap(false, Ordering::SeqCst)
}

#[derive(Debug, Clone, Copy)]
pub enum SystemEvent {
    Play,
    Pause,
    Toggle,
    Next,
    Prev,
    Stop,
}

static JVM: OnceLock<JavaVM> = OnceLock::new();
// App classloader cached from main thread so FindClass works from any thread.
static CLASSLOADER: OnceLock<GlobalRef> = OnceLock::new();
static BACKGROUND_HANDLER: OnceLock<Arc<Mutex<Option<Box<dyn Fn(SystemEvent) + Send + Sync>>>>> =
    OnceLock::new();

fn get_bg_handler() -> Arc<Mutex<Option<Box<dyn Fn(SystemEvent) + Send + Sync>>>> {
    BACKGROUND_HANDLER
        .get_or_init(|| Arc::new(Mutex::new(None)))
        .clone()
}

pub fn set_background_handler(handler: impl Fn(SystemEvent) + Send + Sync + 'static) {
    let binding = get_bg_handler();
    let mut guard = binding.lock().unwrap_or_else(|e| e.into_inner());
    *guard = Some(Box::new(handler));
}

fn dispatch_event(event: SystemEvent) {
    if let Ok(guard) = get_bg_handler().lock() {
        if let Some(ref handler) = *guard {
            handler(event);
        }
    }
}

pub fn init() {
    static ONCE: OnceLock<()> = OnceLock::new();
    ONCE.get_or_init(|| {
        let ctx = ndk_context::android_context();
        let vm_ptr = ctx.vm();
        if vm_ptr.is_null() {
            return;
        }
        match unsafe { JavaVM::from_raw(vm_ptr.cast()) } {
            Ok(vm) => {
                let _ = JVM.set(vm);
                cache_classloader();
                init_media_session();
            }
            Err(e) => tracing::warn!(error = %e, "failed to capture JVM"),
        }
    });
}

// Cache the app classloader from the activity so FindClass works from background threads.
fn cache_classloader() {
    let vm = match JVM.get() {
        Some(v) => v,
        None => return,
    };
    let mut env = match vm.attach_current_thread() {
        Ok(e) => e,
        Err(_) => return,
    };
    let ctx = ndk_context::android_context();
    let raw = ctx.context();
    if raw.is_null() {
        tracing::warn!("null activity context; skipping classloader cache");
        return;
    }
    // Transient local only — we immediately turn the resolved classloader into a
    // GlobalRef below and never retain this raw activity pointer.
    let activity = unsafe { JObject::from_raw(raw.cast()) };
    let result: Result<(), jni::errors::Error> = (|| {
        let cl = env
            .call_method(
                &activity,
                "getClassLoader",
                "()Ljava/lang/ClassLoader;",
                &[],
            )?
            .l()?;
        let global = env.new_global_ref(&cl)?;
        let _ = CLASSLOADER.set(global);
        Ok(())
    })();
    if let Err(e) = result {
        tracing::warn!(error = %e, "failed to cache classloader");
    }
}

// Resolve an app class using the cached classloader, falling back to FindClass.
fn find_app_class<'a>(env: &mut JNIEnv<'a>, name: &str) -> Result<JClass<'a>, jni::errors::Error> {
    if let Some(cl) = CLASSLOADER.get() {
        let dot_name = env.new_string(name.replace('/', "."))?;
        let class_obj = env
            .call_method(
                cl.as_obj(),
                "loadClass",
                "(Ljava/lang/String;)Ljava/lang/Class;",
                &[JValue::Object(&dot_name)],
            )?
            .l()?;
        Ok(JClass::from(class_obj))
    } else {
        env.find_class(name)
    }
}

fn init_media_session() {
    let vm = match JVM.get() {
        Some(v) => v,
        None => return,
    };
    let mut env = match vm.attach_current_thread() {
        Ok(e) => e,
        Err(e) => {
            tracing::warn!(error = %e, "attach_current_thread failed");
            return;
        }
    };
    let ctx = ndk_context::android_context();
    let activity = unsafe { JObject::from_raw(ctx.context().cast()) };
    let result: Result<(), jni::errors::Error> = (|| {
        let class = find_app_class(&mut env, "com/temidaradev/kopuz/MediaSessionHelper")?;
        env.call_static_method(
            &class,
            "init",
            "(Landroid/content/Context;)V",
            &[JValue::Object(&activity)],
        )?
        .v()?;
        Ok(())
    })();
    if let Err(e) = result {
        tracing::warn!(error = %e, "MediaSessionHelper.init failed");
        clear_jni_exception(&mut env);
    }
}

fn dir_via_jni(method: &str) -> Option<String> {
    init();
    let vm = JVM.get()?;
    let mut env = vm.attach_current_thread().ok()?;
    let ctx = ndk_context::android_context();
    let activity = unsafe { JObject::from_raw(ctx.context().cast()) };
    let r: Result<String, jni::errors::Error> = (|| {
        let file = env
            .call_method(&activity, method, "()Ljava/io/File;", &[])?
            .l()?;
        let path = env
            .call_method(&file, "getAbsolutePath", "()Ljava/lang/String;", &[])?
            .l()?;
        Ok(env.get_string(&JString::from(path))?.into())
    })();
    r.map_err(|_| {
        if env.exception_check().unwrap_or(false) {
            let _ = env.exception_clear();
        }
    })
    .ok()
}

pub fn get_files_dir() -> Option<String> {
    dir_via_jni("getFilesDir").or_else(|| {
        std::env::var("FILES_DIR").ok().or_else(|| {
            let home = std::env::var("HOME").ok()?;
            if home.contains("com.temidaradev.kopuz") {
                Some(format!("{}/files", home))
            } else {
                None
            }
        })
    })
}

pub fn get_android_music_dir() -> Option<String> {
    init();
    let vm = JVM.get()?;
    let mut env = vm.attach_current_thread().ok()?;
    let result: Result<String, jni::errors::Error> = (|env: &mut JNIEnv| {
        let env_class = env.find_class("android/os/Environment")?;
        let dir_type = env.new_string("Music")?;
        let file = env
            .call_static_method(
                env_class,
                "getExternalStoragePublicDirectory",
                "(Ljava/lang/String;)Ljava/io/File;",
                &[JValue::Object(&dir_type)],
            )?
            .l()?;
        let path = env
            .call_method(&file, "getAbsolutePath", "()Ljava/lang/String;", &[])?
            .l()?;
        Ok(env.get_string(&JString::from(path))?.into())
    })(&mut env);
    if let Err(e) = result {
        tracing::warn!(error = %e, "get_android_music_dir failed");
        clear_jni_exception(&mut env);
        None
    } else {
        result.ok()
    }
}

/// Normalises an artwork URL to something Kotlin can consume:
/// - `artwork://local?p=…` → decoded absolute file path
/// - `http(s)://…`         → passed through as-is for Kotlin to download
/// - anything else         → None
fn normalize_artwork(url: &str) -> Option<String> {
    if url.starts_with("http://") || url.starts_with("https://") {
        return Some(url.to_string());
    }
    let query = url.strip_prefix("artwork://local?")?;
    let encoded = query.split('&').find_map(|kv| {
        let mut parts = kv.splitn(2, '=');
        if parts.next() == Some("p") {
            parts.next()
        } else {
            None
        }
    })?;
    let decoded = percent_decode(encoded);
    let path = if decoded.starts_with("/~") {
        std::env::var("HOME")
            .ok()
            .map(|h| decoded.replacen("/~", &h, 1))
            .unwrap_or(decoded)
    } else if decoded.starts_with('~') {
        std::env::var("HOME")
            .ok()
            .map(|h| decoded.replacen('~', &h, 1))
            .unwrap_or(decoded)
    } else {
        decoded
    };
    Some(path)
}

/// Cache of the last decoded `data:` artwork: (content hash, written file path).
/// The player re-sends the same artwork every position tick (~1s); without this
/// we'd base64-decode and rewrite the file each tick.
static LAST_DATA_ART: Mutex<Option<(u64, String)>> = Mutex::new(None);

/// Resolve an artwork URL to something `MediaSessionHelper` can load: an http(s)
/// URL, a local file path, or — for the base64 `data:` URLs the Android UI uses —
/// a file decoded into app storage (the notification can't render a data URL).
fn resolve_artwork(url: &str) -> Option<String> {
    let resolved = if url.starts_with("data:") {
        data_url_to_file(url)
    } else if let Some(stripped) = url.strip_prefix("file://") {
        Some(stripped.to_string())
    } else if url.starts_with('/') {
        // Bare absolute path (e.g. a downloaded server cover in the temp dir).
        Some(url.to_string())
    } else {
        normalize_artwork(url)
    };
    tracing::trace!(
        input = &url[..url.len().min(48)],
        ?resolved,
        "resolve_artwork"
    );
    resolved
}

/// Decode a `data:<mime>;base64,<payload>` URL to a file under the app's files dir
/// and return its path. Cached by content hash so repeated identical updates reuse
/// the same file instead of rewriting it.
fn data_url_to_file(url: &str) -> Option<String> {
    use base64::{Engine as _, engine::general_purpose};
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let meta = url.strip_prefix("data:")?;
    let comma = meta.find(',')?;
    let header = &meta[..comma];
    let payload = &meta[comma + 1..];
    if !header.contains("base64") {
        return None;
    }
    let ext = if header.contains("image/png") {
        "png"
    } else if header.contains("image/webp") {
        "webp"
    } else if header.contains("image/gif") {
        "gif"
    } else {
        "jpg"
    };

    let mut hasher = DefaultHasher::new();
    payload.hash(&mut hasher);
    let hash = hasher.finish();

    // Hash is part of the filename so a new track yields a new path — the Kotlin
    // side caches its decoded bitmap by path and would otherwise keep showing the
    // previous track's art when the filename stayed constant.
    if let Ok(guard) = LAST_DATA_ART.lock() {
        if let Some((last_hash, path)) = guard.as_ref() {
            if *last_hash == hash && std::path::Path::new(path).exists() {
                return Some(path.clone());
            }
        }
    }

    let files_dir = get_files_dir()?;
    let path = format!("{files_dir}/np_art_{hash}.{ext}");
    let bytes = general_purpose::STANDARD.decode(payload).ok()?;
    std::fs::write(&path, &bytes).ok()?;
    if let Ok(mut guard) = LAST_DATA_ART.lock() {
        // Remove the previously written art file so they don't accumulate.
        if let Some((_, old_path)) = guard.as_ref() {
            if old_path != &path {
                let _ = std::fs::remove_file(old_path);
            }
        }
        *guard = Some((hash, path.clone()));
    }
    Some(path)
}

fn percent_decode(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut chars = s.bytes().peekable();
    while let Some(b) = chars.next() {
        if b == b'%' {
            let h1 = chars.next().map(hex_val).unwrap_or(0);
            let h2 = chars.next().map(hex_val).unwrap_or(0);
            out.push(char::from(h1 << 4 | h2));
        } else if b == b'+' {
            out.push(' ');
        } else {
            out.push(char::from(b));
        }
    }
    out
}

fn hex_val(b: u8) -> u8 {
    match b {
        b'0'..=b'9' => b - b'0',
        b'a'..=b'f' => b - b'a' + 10,
        b'A'..=b'F' => b - b'A' + 10,
        _ => 0,
    }
}

pub fn update_now_playing(
    title: &str,
    artist: &str,
    album: &str,
    duration: f64,
    position: f64,
    playing: bool,
    artwork_path: Option<&str>,
) {
    init();
    let vm = match JVM.get() {
        Some(v) => v,
        None => return,
    };
    let mut env = match vm.attach_current_thread() {
        Ok(e) => e,
        Err(_) => return,
    };
    let ctx = ndk_context::android_context();
    let activity = unsafe { JObject::from_raw(ctx.context().cast()) };
    let duration_ms = (duration * 1000.0) as i64;
    let position_ms = (position * 1000.0) as i64;
    let resolved_art = artwork_path.and_then(resolve_artwork);
    let result: Result<(), jni::errors::Error> = (|| {
        let class = find_app_class(&mut env, "com/temidaradev/kopuz/MediaSessionHelper")?;
        let j_title = env.new_string(title)?;
        let j_artist = env.new_string(artist)?;
        let j_album = env.new_string(album)?;
        let null_obj = JObject::null();
        let j_art_owned;
        let j_art: &JObject = if let Some(ref path) = resolved_art {
            j_art_owned = env.new_string(path)?;
            &*j_art_owned
        } else {
            &null_obj
        };
        env.call_static_method(
            &class,
            "updateNowPlaying",
            "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JJZLjava/lang/String;)V",
            &[
                JValue::Object(&activity),
                JValue::Object(&j_title),
                JValue::Object(&j_artist),
                JValue::Object(&j_album),
                JValue::Long(duration_ms),
                JValue::Long(position_ms),
                JValue::Bool(playing as u8),
                JValue::Object(j_art),
            ],
        )?
        .v()?;
        Ok(())
    })();
    if let Err(e) = result {
        tracing::warn!(error = %e, "MediaSessionHelper.updateNowPlaying failed");
        clear_jni_exception(&mut env);
    }
}

pub fn wake_run_loop() {
    let vm = match JVM.get() {
        Some(v) => v,
        None => return,
    };
    let mut env = match vm.attach_current_thread() {
        Ok(e) => e,
        Err(_) => return,
    };
    let result: Result<(), jni::errors::Error> = (|| {
        let class = find_app_class(&mut env, "com/temidaradev/kopuz/MediaSessionHelper")?;
        env.call_static_method(&class, "wakeMainThread", "()V", &[])?
            .v()?;
        Ok(())
    })();
    if let Err(_) = result {
        clear_jni_exception(&mut env);
    }
}

pub fn stop_session() {
    let vm = match JVM.get() {
        Some(v) => v,
        None => return,
    };
    let mut env = match vm.attach_current_thread() {
        Ok(e) => e,
        Err(_) => return,
    };
    let ctx = ndk_context::android_context();
    let activity = unsafe { JObject::from_raw(ctx.context().cast()) };
    let result: Result<(), jni::errors::Error> = (|| {
        let class = find_app_class(&mut env, "com/temidaradev/kopuz/MediaSessionHelper")?;
        env.call_static_method(
            &class,
            "stopSession",
            "(Landroid/content/Context;)V",
            &[JValue::Object(&activity)],
        )?
        .v()?;
        Ok(())
    })();
    if let Err(e) = result {
        tracing::warn!(error = %e, "MediaSessionHelper.stopSession failed");
        clear_jni_exception(&mut env);
    }
}

pub fn request_permissions() {
    init();
    let vm = match JVM.get() {
        Some(v) => v,
        None => return,
    };
    let mut env = match vm.attach_current_thread() {
        Ok(e) => e,
        Err(_) => return,
    };
    let ctx = ndk_context::android_context();
    let activity = unsafe { JObject::from_raw(ctx.context().cast()) };
    let result: Result<(), jni::errors::Error> = (|env: &mut JNIEnv| {
        let class = find_app_class(env, "com/temidaradev/kopuz/MediaSessionHelper")?;
        env.call_static_method(
            &class,
            "requestPermissions",
            "(Landroid/app/Activity;)V",
            &[JValue::Object(&activity)],
        )?
        .v()?;
        Ok(())
    })(&mut env);
    if let Err(e) = result {
        tracing::warn!(error = %e, "MediaSessionHelper.requestPermissions failed");
        clear_jni_exception(&mut env);
    }
}

fn clear_jni_exception(env: &mut JNIEnv) {
    if env.exception_check().unwrap_or(false) {
        let _ = env.exception_describe();
        let _ = env.exception_clear();
    }
}

// Called from Kotlin: MediaReceiver.nativeOnAction(String) — routes notification button taps to Rust
#[unsafe(no_mangle)]
pub extern "system" fn Java_com_temidaradev_kopuz_MediaReceiver_nativeOnAction(
    mut env: JNIEnv,
    _class: JClass,
    action: JString,
) {
    let action_str: String = match env.get_string(&action) {
        Ok(s) => s.into(),
        Err(_) => return,
    };
    match action_str.as_str() {
        "play" => dispatch_event(SystemEvent::Play),
        "pause" => dispatch_event(SystemEvent::Pause),
        "toggle" => dispatch_event(SystemEvent::Toggle),
        "next" => dispatch_event(SystemEvent::Next),
        "prev" => dispatch_event(SystemEvent::Prev),
        "stop" => dispatch_event(SystemEvent::Stop),
        // Hardware/gesture back — handled by the app router, not a media command.
        "back" => {
            BACK_PENDING.store(true, Ordering::SeqCst);
            super::back_wake();
        }
        _ => {}
    }
}

/// Send the app to the background (like Home) instead of finishing it, so playback
/// survives. Delegates to MainActivity.moveToBack(), which marshals onto the UI thread.
pub fn move_task_to_back() {
    let vm = match JVM.get() {
        Some(v) => v,
        None => return,
    };
    let mut env = match vm.attach_current_thread() {
        Ok(e) => e,
        Err(_) => return,
    };
    let result: Result<(), jni::errors::Error> = (|| {
        let class = find_app_class(&mut env, "dev/dioxus/main/MainActivity")?;
        env.call_static_method(&class, "moveToBack", "()V", &[])?
            .v()?;
        Ok(())
    })();
    if let Err(e) = result {
        tracing::warn!(error = %e, "MainActivity.moveToBack failed");
        clear_jni_exception(&mut env);
    }
}