notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

android.rs (4654B)


      1 //#[cfg(target_os = "android")]
      2 //use egui_android::run_android;
      3 
      4 use egui_winit::winit::platform::android::activity::AndroidApp;
      5 
      6 use crate::chrome::Chrome;
      7 use notedeck::Notedeck;
      8 
      9 #[no_mangle]
     10 #[tokio::main]
     11 pub async fn android_main(android_app: AndroidApp) {
     12     //use tracing_logcat::{LogcatMakeWriter, LogcatTag};
     13     use tracing_subscriber::{prelude::*, EnvFilter};
     14 
     15     std::env::set_var("RUST_BACKTRACE", "full");
     16     //std::env::set_var("DAVE_MODEL", "hhao/qwen2.5-coder-tools:latest");
     17     std::env::set_var(
     18         "RUST_LOG",
     19         "egui=debug,egui-winit=debug,winit=debug,notedeck=debug,notedeck_columns=debug,notedeck_chrome=debug,enostr=debug,android_activity=debug",
     20     );
     21 
     22     //std::env::set_var(
     23     //    "RUST_LOG",
     24     //    "enostr=debug,notedeck_columns=debug,notedeck_chrome=debug",
     25     //);
     26 
     27     //let writer =
     28     //LogcatMakeWriter::new(LogcatTag::Target).expect("Failed to initialize logcat writer");
     29 
     30     let fmt_layer = tracing_subscriber::fmt::layer()
     31         .with_level(false)
     32         .with_target(false)
     33         .without_time();
     34 
     35     let filter_layer = EnvFilter::try_from_default_env()
     36         .or_else(|_| EnvFilter::try_new("info"))
     37         .unwrap();
     38 
     39     tracing_subscriber::registry()
     40         .with(filter_layer)
     41         .with(fmt_layer)
     42         .init();
     43 
     44     let _ = android_keyring::set_android_keyring_credential_builder();
     45 
     46     let path = android_app.internal_data_path().expect("data path");
     47     let mut options = eframe::NativeOptions {
     48         depth_buffer: 24,
     49         ..eframe::NativeOptions::default()
     50     };
     51 
     52     options.renderer = eframe::Renderer::Wgpu;
     53     // Clone `app` to use it both in the closure and later in the function
     54     //let app_clone_for_event_loop = app.clone();
     55     //options.event_loop_builder = Some(Box::new(move |builder| {
     56     //    builder.with_android_app(app_clone_for_event_loop);
     57     //}));
     58 
     59     options.android_app = Some(android_app.clone());
     60 
     61     let app_args = get_app_args();
     62 
     63     let _res = eframe::run_native(
     64         "Damus Notedeck",
     65         options,
     66         Box::new(move |cc| {
     67             let ctx = &cc.egui_ctx;
     68 
     69             let mut notedeck_ctx = Notedeck::init(ctx, path, &app_args);
     70             notedeck_ctx.notedeck.set_android_context(android_app);
     71             notedeck_ctx.notedeck.setup(ctx);
     72             let chrome = Chrome::new_with_apps(
     73                 cc,
     74                 &app_args,
     75                 &mut notedeck_ctx.notedeck,
     76                 notedeck_ctx.outbox_session,
     77             )?;
     78             notedeck_ctx.notedeck.set_app(chrome);
     79 
     80             Ok(Box::new(notedeck_ctx.notedeck))
     81         }),
     82     );
     83 }
     84 /*
     85 Read args from a config file:
     86 - allows use of more interesting args w/o risk of checking them in by mistake
     87 - allows use of different args w/o rebuilding the app
     88 - uses compiled in defaults if config file missing or broken
     89 
     90 Example android-config.json:
     91 ```
     92 {
     93   "args": [
     94     "argv0-placeholder",
     95     "--npub",
     96     "npub1h50pnxqw9jg7dhr906fvy4mze2yzawf895jhnc3p7qmljdugm6gsrurqev",
     97     "-c",
     98     "contacts",
     99     "-c",
    100     "notifications"
    101   ]
    102 }
    103 ```
    104 
    105 Install/update android-config.json with:
    106 ```
    107 adb push android-config.json /sdcard/Android/data/com.damus.notedeck/files/android-config.json
    108 ```
    109 
    110 Using internal storage would be better but it seems hard to get the config file onto
    111 the device ...
    112 */
    113 
    114 fn get_app_args() -> Vec<String> {
    115     vec!["argv0-placeholder".to_string()]
    116     /*
    117     use serde_json::value;
    118     use std::fs;
    119     use std::path::PathBuf;
    120 
    121     let external_data_path: pathbuf = app
    122         .external_data_path()
    123         .expect("external data path")
    124         .to_path_buf();
    125     let config_file = external_data_path.join("android-config.json");
    126 
    127     let initial_user = hex::encode(notedeck::FALLBACK_PUBKEY().bytes());
    128     let default_args = vec![
    129         "argv0-placeholder",
    130         "--no-tmp-columns",
    131         "--pub",
    132         &initial_user,
    133         "-c",
    134         "contacts",
    135         "-c",
    136         "notifications",
    137     ]
    138     .into_iter()
    139     .map(|s| s.to_string())
    140     .collect();
    141 
    142     if config_file.exists() {
    143         if let Ok(config_contents) = fs::read_to_string(config_file) {
    144             if let Ok(json) = serde_json::from_str::<Value>(&config_contents) {
    145                 if let Some(args_array) = json.get("args").and_then(|v| v.as_array()) {
    146                     let config_args = args_array
    147                         .iter()
    148                         .filter_map(|v| v.as_str().map(String::from))
    149                         .collect();
    150 
    151                     return config_args;
    152                 }
    153             }
    154         }
    155     }
    156 
    157     default_args // Return the default args if config is missing or invalid
    158     */
    159 }