Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 44f873ed authored by Joe Hattori's avatar Joe Hattori
Browse files

Implement setProcessState() and scheduleTrimMemory()

This commit implements setProcessState() and scheduleTrimMemory() for
NativeApplicationThread. Respecting what ActivityThread does,
scheduleTrimMemory() calls the onTrimMemory callback when the process
is not in critical state or the specified trim level is not high enough.

Bug: 431632697
Bug: 431636312
Test: build and run cuttlefish
Flag: android.os.native_framework_prototype
Change-Id: Id1a0923ea859684cc3d47a090f49a4c11dcdf9af
parent 4667c871
Loading
Loading
Loading
Loading
+18 −11
Original line number Diff line number Diff line
@@ -609,17 +609,6 @@ cc_library {
    ],
}

cc_library {
    name: "libactivity_manager_procstate_aidl-cpp",
    host_supported: true,
    srcs: [
        ":activity_manager_procstate_aidl",
    ],
    aidl: {
        export_aidl_headers: true,
    },
}

// Build Rust bindings for PermissionController. Needed by keystore2.
aidl_interface {
    name: "android.os.permissions_aidl",
@@ -865,3 +854,21 @@ aidl_interface {
        },
    },
}

aidl_interface {
    name: "libactivity_manager_procstate_aidl",
    srcs: [
        ":activity_manager_procstate_aidl",
    ],
    host_supported: true,
    unstable: true,
    backend: {
        java: {
            // java code is generated through framework-core-sources.
            enabled: false,
        },
        rust: {
            enabled: true,
        },
    },
}
+3 −0
Original line number Diff line number Diff line
@@ -50,4 +50,7 @@ oneway interface INativeApplicationThread {

    @UnsupportedAppUsage
    void bindApplication();

    @UnsupportedAppUsage
    void setProcessState(int state);
}
+1 −0
Original line number Diff line number Diff line
@@ -73,5 +73,6 @@ rust_library {
        "liblooper_bindgen",
        "libnative_service_bindgen",
        "native_application_thread_aidl-rust",
        "libactivity_manager_procstate_aidl-rust",
    ],
}
+16 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ use binder::{
    unstable_api::{new_spibinder, AIBinder as SysAIBinder},
    SpIBinder, Strong,
};
use libactivity_manager_procstate_aidl::aidl::android::app::ProcessStateEnum::ProcessStateEnum;
use native_service_bindgen::{
    ANativeService, ANativeServiceCallbacks,
    ANativeServiceTrimMemoryLevel_ANATIVE_SERVICE_TRIM_MEMORY_BACKGROUND,
@@ -52,6 +53,7 @@ pub struct NativeActivityThread {
    start_seq: i64,
    services: BTreeMap<SpIBinder, NativeService>,
    namespace_factory: NamespaceFactory,
    process_state: i32,
}

impl NativeActivityThread {
@@ -61,6 +63,7 @@ impl NativeActivityThread {
            start_seq,
            services: BTreeMap::new(),
            namespace_factory: NamespaceFactory::new(format!("native_app_{}", start_seq)),
            process_state: ProcessStateEnum::UNKNOWN.0,
        }
    }

@@ -199,6 +202,11 @@ impl NativeActivityThread {
        {
            bail!("Received an unexpected level: {}", level);
        }
        if self.process_state <= ProcessStateEnum::IMPORTANT_FOREGROUND.0
            && level == ANativeServiceTrimMemoryLevel_ANATIVE_SERVICE_TRIM_MEMORY_BACKGROUND
        {
            return Ok(());
        }
        for service in self.services.values_mut() {
            if let Some(on_trim_memory) = service.service.callbacks.onTrimMemory {
                let native_service = service.service.as_mut();
@@ -215,6 +223,11 @@ impl NativeActivityThread {
            .finishAttachApplication(self.start_seq, 0)
            .context("Failed to call finishAttachApplication")
    }

    fn handle_set_process_state(&mut self, state: i32) -> Result<()> {
        self.process_state = state;
        Ok(())
    }
}

impl HandlerCallback<NativeApplicationThreadRequest> for NativeActivityThread {
@@ -238,6 +251,9 @@ impl HandlerCallback<NativeApplicationThreadRequest> for NativeActivityThread {
            NativeApplicationThreadRequest::BindApplication => {
                self.handle_bind_application_request()
            }
            NativeApplicationThreadRequest::SetProcessState(state) => {
                self.handle_set_process_state(state)
            }
        }
    }
}
+12 −0
Original line number Diff line number Diff line
@@ -84,6 +84,7 @@ pub enum NativeApplicationThreadRequest {
    UnbindService(UnbindServiceRequest),
    TrimMemory(i32),
    BindApplication,
    SetProcessState(i32),
}

/// NativeApplicationThread is used as a "Binder node" to accept requests for managing the process
@@ -227,4 +228,15 @@ impl INativeApplicationThread for NativeApplicationThread {
        })?;
        Ok(())
    }

    fn setProcessState(&self, state: i32) -> binder::Result<()> {
        info!("setProcessState thread id={:?}", thread::current().id());
        self.sender.send(NativeApplicationThreadRequest::SetProcessState(state)).map_err(|e| {
            binder::Status::new_exception_str(
                binder::ExceptionCode::SERVICE_SPECIFIC,
                Some(format!("Failed to send a task: {:?}", e)),
            )
        })?;
        Ok(())
    }
}
Loading