wake lock for all connection type

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages
2023-11-24 16:40:18 +08:00
parent bd81e4d0fb
commit 2de1c62daf
9 changed files with 102 additions and 87 deletions

View File

@@ -82,35 +82,28 @@ pub const PA_SAMPLE_RATE: u32 = 48000;
#[cfg(target_os = "android")]
#[derive(Default)]
pub struct WakeLock {
lock: Option<android_wakelock::WakeLock>,
}
pub struct WakeLock(Option<android_wakelock::WakeLock>);
#[cfg(target_os = "android")]
impl WakeLock {
pub fn new(tag: &str) -> Self {
let tag = format!("{}:{tag}", crate::get_app_name());
match android_wakelock::partial(tag) {
Ok(lock) => Self { lock: Some(lock) },
Ok(lock) => Self(Some(lock)),
Err(e) => {
hbb_common::log::error!("Failed to get wakelock: {e:?}");
Self::default()
}
}
}
}
pub fn acquire(&self) -> Option<android_wakelock::Guard> {
match self.lock.as_ref() {
Some(lock) => match lock.acquire() {
Ok(guard) => Some(guard),
Err(e) => {
hbb_common::log::error!("Failed to acquire wakelock guard: {e:?}");
None
}
},
None => None,
}
}
pub fn get_wake_lock(_display: bool) -> WakeLock {
hbb_common::log::info!("new wakelock, require display on: {_display}");
#[cfg(target_os = "android")]
return crate::platform::WakeLock::new("server");
#[cfg(not(target_os = "android"))]
return crate::platform::WakeLock::new(_display, true, true);
}
pub(crate) struct InstallingService; // please use new