android.rs (4476B)
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 = Notedeck::new(ctx, path, &app_args); 70 notedeck.set_android_context(android_app); 71 notedeck.setup(ctx); 72 let chrome = Chrome::new_with_apps(cc, &app_args, &mut notedeck)?; 73 notedeck.set_app(chrome); 74 75 Ok(Box::new(notedeck)) 76 }), 77 ); 78 } 79 /* 80 Read args from a config file: 81 - allows use of more interesting args w/o risk of checking them in by mistake 82 - allows use of different args w/o rebuilding the app 83 - uses compiled in defaults if config file missing or broken 84 85 Example android-config.json: 86 ``` 87 { 88 "args": [ 89 "argv0-placeholder", 90 "--npub", 91 "npub1h50pnxqw9jg7dhr906fvy4mze2yzawf895jhnc3p7qmljdugm6gsrurqev", 92 "-c", 93 "contacts", 94 "-c", 95 "notifications" 96 ] 97 } 98 ``` 99 100 Install/update android-config.json with: 101 ``` 102 adb push android-config.json /sdcard/Android/data/com.damus.notedeck/files/android-config.json 103 ``` 104 105 Using internal storage would be better but it seems hard to get the config file onto 106 the device ... 107 */ 108 109 fn get_app_args() -> Vec<String> { 110 vec!["argv0-placeholder".to_string()] 111 /* 112 use serde_json::value; 113 use std::fs; 114 use std::path::PathBuf; 115 116 let external_data_path: pathbuf = app 117 .external_data_path() 118 .expect("external data path") 119 .to_path_buf(); 120 let config_file = external_data_path.join("android-config.json"); 121 122 let initial_user = hex::encode(notedeck::FALLBACK_PUBKEY().bytes()); 123 let default_args = vec![ 124 "argv0-placeholder", 125 "--no-tmp-columns", 126 "--pub", 127 &initial_user, 128 "-c", 129 "contacts", 130 "-c", 131 "notifications", 132 ] 133 .into_iter() 134 .map(|s| s.to_string()) 135 .collect(); 136 137 if config_file.exists() { 138 if let Ok(config_contents) = fs::read_to_string(config_file) { 139 if let Ok(json) = serde_json::from_str::<Value>(&config_contents) { 140 if let Some(args_array) = json.get("args").and_then(|v| v.as_array()) { 141 let config_args = args_array 142 .iter() 143 .filter_map(|v| v.as_str().map(String::from)) 144 .collect(); 145 146 return config_args; 147 } 148 } 149 } 150 } 151 152 default_args // Return the default args if config is missing or invalid 153 */ 154 }