/// Android clipboard bridge via JNI.
///
/// Writes text to the system clipboard by calling into `ClipboardManager`
/// through the safe [`solitaire_data::android_jni`] bridge. Only compiled and
/// linked on `target_os = "android"`.
#[cfg(target_os = "android")]
pub fn set_text(text: &str) -> Result<(), String> {
    use jni::objects::JValueOwned;
    use solitaire_data::android_jni;

    android_jni::with_activity_env(|env, activity| {
        // ClipboardManager cm = activity.getSystemService("clipboard")
        let svc_name = JValueOwned::from(env.new_string("clipboard")?);
        let cm = env
            .call_method(
                activity,
                "getSystemService",
                "(Ljava/lang/String;)Ljava/lang/Object;",
                &[svc_name.borrow()],
            )?
            .l()?;

        // ClipData clip = ClipData.newPlainText("link", text)
        let label = JValueOwned::from(env.new_string("link")?);
        let java_text = JValueOwned::from(env.new_string(text)?);
        let clip_class = env.find_class("android/content/ClipData")?;
        let clip = env
            .call_static_method(
                &clip_class,
                "newPlainText",
                "(Ljava/lang/CharSequence;Ljava/lang/CharSequence;)Landroid/content/ClipData;",
                &[label.borrow(), java_text.borrow()],
            )?
            .l()?;

        // cm.setPrimaryClip(clip)
        let clip_val = JValueOwned::Object(clip);
        env.call_method(
            &cm,
            "setPrimaryClip",
            "(Landroid/content/ClipData;)V",
            &[clip_val.borrow()],
        )?
        .v()
    })
}
