//! The network this phone is on, as its shell last said (MEDIA DRAIN 1 item 2, `media.network`). One phone is on one //! network, so it is one value for the process. A file queued for Wi-Fi only goes up only while the shell has said //! Wi-Fi; until a shell says anything (an older shell, a launch before its first word) it waits, because "Use mobile //! data" off is strict. A file queued for any network (every app's default, what they did before this rule) always goes. use std::sync::atomic::{AtomicU8, Ordering}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Path { /// No word from the shell yet. Unknown, Wifi, /// Mobile data. Mobile, /// Low Data Mode, or a hotspot the phone counts as expensive. Constrained, /// No network at all. None, } static PATH: AtomicU8 = AtomicU8::new(0); impl Path { fn code(self) -> u8 { match self { Path::Unknown => 0, Path::Wifi => 1, Path::Mobile => 2, Path::Constrained => 3, Path::None => 4, } } /// The shell's word: `wifi`, `mobile`, `constrained` or `none`. #[must_use] pub fn parse(word: &str) -> Option { Some(match word { "wifi" => Path::Wifi, "mobile" => Path::Mobile, "constrained" => Path::Constrained, "none" => Path::None, _ => return None, }) } #[must_use] pub fn word(self) -> &'static str { match self { Path::Unknown => "unknown", Path::Wifi => "wifi", Path::Mobile => "mobile", Path::Constrained => "constrained", Path::None => "none", } } } /// The shell says where the phone is now. Answers whether Wi-Fi-only files may go now and could not before (the /// caller wakes the drain). pub fn set(path: Path) -> bool { let before = now(); PATH.store(path.code(), Ordering::SeqCst); path == Path::Wifi && before != Path::Wifi } #[must_use] pub fn now() -> Path { match PATH.load(Ordering::SeqCst) { 1 => Path::Wifi, 2 => Path::Mobile, 3 => Path::Constrained, 4 => Path::None, _ => Path::Unknown, } } /// Whether a file queued for Wi-Fi only may go up now. #[must_use] pub fn wifi_only_may_go() -> bool { now() == Path::Wifi }