Skip to content

Commit

Permalink
回退为调用dumpsys修复兼容问题
Browse files Browse the repository at this point in the history
  • Loading branch information
shadow3aaa committed Nov 25, 2023
1 parent 2eb3bee commit 96b0d4c
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions fas-rs-fw/src/scheduler/topapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
* See the License for the specific language governing permissions and
* limitations under the License. */
use std::{
fs::{self, OpenOptions},
process::Command,
time::{Duration, Instant},
};

use binder::binder_impl::IBinderInternal;

const TOPAPP_TEMP: &str = "/dev/fas_rs_topapp_temp";
const REFRESH_TIME: Duration = Duration::from_secs(2);

pub struct TimedWatcher {
Expand All @@ -28,50 +25,39 @@ pub struct TimedWatcher {

impl TimedWatcher {
pub fn new() -> Self {
let cache = Vec::new();
let cache = Self::get_top_pids().unwrap_or_default();

let mut result = Self {
Self {
cache,
last_refresh: Instant::now(),
};

result.get_top_pids();
result
}
}

pub fn is_topapp(&mut self, pid: i32) -> bool {
if self.last_refresh.elapsed() > REFRESH_TIME {
self.get_top_pids();
self.cache = Self::get_top_pids().unwrap_or_default();
self.last_refresh = Instant::now();
}

self.cache.contains(&pid)
}

fn get_top_pids(&mut self) {
let temp = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(TOPAPP_TEMP)
.unwrap();

let Some(mut service) = binder::get_service("window") else {
return;
};
fn get_top_pids() -> Option<Vec<i32>> {
let dump = Command::new("dumpsys")
.args(["window", "visible-apps"])
.output()
.ok()?;
let dump = String::from_utf8_lossy(&dump.stdout).into_owned();

if service.dump(&temp, &["visible-apps"]).is_err() {
return;
}

let dump = fs::read_to_string(TOPAPP_TEMP).unwrap();
Some(Self::parse_top_app(&dump))
}

self.cache = dump
.lines()
fn parse_top_app(dump: &str) -> Vec<i32> {
dump.lines()
.filter(|l| l.contains("Session{"))
.filter_map(|l| l.split_whitespace().nth(3))
.filter_map(|s| s.split(':').next())
.map(|p| p.trim().parse().unwrap())
.collect();
.collect()
}
}

0 comments on commit 96b0d4c

Please sign in to comment.