android.rs (3245B)
1 //#[cfg(target_os = "android")] 2 //use egui_android::run_android; 3 4 use crate::app::Notedeck; 5 use notedeck_columns::Damus; 6 use winit::platform::android::activity::AndroidApp; 7 use winit::platform::android::EventLoopBuilderExtAndroid; 8 9 #[no_mangle] 10 #[tokio::main] 11 pub async fn android_main(app: AndroidApp) { 12 std::env::set_var("RUST_BACKTRACE", "full"); 13 android_logger::init_once(android_logger::Config::default().with_min_level(log::Level::Info)); 14 15 let path = app.internal_data_path().expect("data path"); 16 let mut options = eframe::NativeOptions::default(); 17 options.renderer = eframe::Renderer::Wgpu; 18 // Clone `app` to use it both in the closure and later in the function 19 let app_clone_for_event_loop = app.clone(); 20 options.event_loop_builder = Some(Box::new(move |builder| { 21 builder.with_android_app(app_clone_for_event_loop); 22 })); 23 24 let app_args = get_app_args(app); 25 26 let _res = eframe::run_native( 27 "Damus Notedeck", 28 options, 29 Box::new(move |cc| { 30 let mut notedeck = Notedeck::new(&cc.egui_ctx, path, &app_args); 31 let damus = Damus::new(&mut notedeck.app_context(), &app_args); 32 notedeck.add_app(damus); 33 Ok(Box::new(notedeck)) 34 }), 35 ); 36 } 37 38 use serde_json::Value; 39 use std::fs; 40 use std::path::PathBuf; 41 42 /* 43 Read args from a config file: 44 - allows use of more interesting args w/o risk of checking them in by mistake 45 - allows use of different args w/o rebuilding the app 46 - uses compiled in defaults if config file missing or broken 47 48 Example android-config.json: 49 ``` 50 { 51 "args": [ 52 "--npub", 53 "npub1h50pnxqw9jg7dhr906fvy4mze2yzawf895jhnc3p7qmljdugm6gsrurqev", 54 "-c", 55 "contacts", 56 "-c", 57 "notifications" 58 ] 59 } 60 ``` 61 62 Install/update android-config.json with: 63 ``` 64 adb push android-config.json /sdcard/Android/data/com.damus.app/files/android-config.json 65 ``` 66 67 Using internal storage would be better but it seems hard to get the config file onto 68 the device ... 69 */ 70 71 fn get_app_args(app: AndroidApp) -> Vec<String> { 72 let external_data_path: PathBuf = app 73 .external_data_path() 74 .expect("external data path") 75 .to_path_buf(); 76 let config_file = external_data_path.join("android-config.json"); 77 78 let default_args = vec![ 79 "--pub", 80 "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245", 81 "-c", 82 "contacts", 83 "-c", 84 "notifications", 85 "-c", 86 "notifications:3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681", 87 ] 88 .into_iter() 89 .map(|s| s.to_string()) 90 .collect(); 91 92 if config_file.exists() { 93 if let Ok(config_contents) = fs::read_to_string(config_file) { 94 if let Ok(json) = serde_json::from_str::<Value>(&config_contents) { 95 if let Some(args_array) = json.get("args").and_then(|v| v.as_array()) { 96 let config_args = args_array 97 .iter() 98 .filter_map(|v| v.as_str().map(String::from)) 99 .collect(); 100 101 return config_args; 102 } 103 } 104 } 105 } 106 107 default_args // Return the default args if config is missing or invalid 108 }