15ba71b47Sopenharmony_ci// Copyright (C) 2024 Huawei Device Co., Ltd.
25ba71b47Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
35ba71b47Sopenharmony_ci// you may not use this file except in compliance with the License.
45ba71b47Sopenharmony_ci// You may obtain a copy of the License at
55ba71b47Sopenharmony_ci//
65ba71b47Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
75ba71b47Sopenharmony_ci//
85ba71b47Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
95ba71b47Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
105ba71b47Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
115ba71b47Sopenharmony_ci// See the License for the specific language governing permissions and
125ba71b47Sopenharmony_ci// limitations under the License.
135ba71b47Sopenharmony_ci
145ba71b47Sopenharmony_ciuse cxx::{let_cxx_string, UniquePtr};
155ba71b47Sopenharmony_ciuse ipc::parcel::MsgParcel;
165ba71b47Sopenharmony_ciuse ipc::remote::{RemoteObj, RemoteStub};
175ba71b47Sopenharmony_ci
185ba71b47Sopenharmony_ciuse crate::wrapper::{
195ba71b47Sopenharmony_ci    GetCommonEventExtraDataIdlist,
205ba71b47Sopenharmony_ci    AbilityStub, AddOnDemandSystemAbilityInfo, AddSystemAbility, AddSystemAbilityConfig,
215ba71b47Sopenharmony_ci    CancelUnloadSystemAbility, CheckSystemAbility, CheckSystemAbilityWithDeviceId,
225ba71b47Sopenharmony_ci    GetContextManager, GetOnDemandReasonExtraData, GetRunningSystemProcess, GetSystemAbility,
235ba71b47Sopenharmony_ci    GetSystemAbilityWithDeviceId, GetSystemProcessInfo, ListSystemAbilities,
245ba71b47Sopenharmony_ci    ListSystemAbilitiesWithDumpFlag, LoadSystemAbility, LoadSystemAbilityWithCallback,
255ba71b47Sopenharmony_ci    RemoveSystemAbility, SendStrategy, SubscribeSystemAbility, SubscribeSystemProcess,
265ba71b47Sopenharmony_ci    SystemProcessInfo, UnSubscribeSystemAbilityHandler, UnSubscribeSystemProcessHandler,
275ba71b47Sopenharmony_ci    UnloadAllIdleSystemAbility, UnloadSystemAbility,
285ba71b47Sopenharmony_ci};
295ba71b47Sopenharmony_ciuse crate::DumpFlagPriority;
305ba71b47Sopenharmony_ci
315ba71b47Sopenharmony_cipub struct SystemAbilityManager;
325ba71b47Sopenharmony_ci
335ba71b47Sopenharmony_ciunsafe impl Sync for SystemAbilityManager {}
345ba71b47Sopenharmony_ciunsafe impl Send for SystemAbilityManager {}
355ba71b47Sopenharmony_ci
365ba71b47Sopenharmony_ciimpl SystemAbilityManager {
375ba71b47Sopenharmony_ci    /// # Example
385ba71b47Sopenharmony_ci    /// ```rust
395ba71b47Sopenharmony_ci    /// use samgr::manage::SystemAbilityManager;
405ba71b47Sopenharmony_ci    ///
415ba71b47Sopenharmony_ci    /// let context = SystemAbilityManager::get_context_manager();
425ba71b47Sopenharmony_ci    /// ```
435ba71b47Sopenharmony_ci    pub fn get_context_manager() -> Option<RemoteObj> {
445ba71b47Sopenharmony_ci        info!("get context manager");
455ba71b47Sopenharmony_ci        RemoteObj::from_sptr(GetContextManager())
465ba71b47Sopenharmony_ci    }
475ba71b47Sopenharmony_ci
485ba71b47Sopenharmony_ci    /// let abilities = sysm.list_system_ability();
495ba71b47Sopenharmony_ci    ///
505ba71b47Sopenharmony_ci    ///
515ba71b47Sopenharmony_ci    /// # Example
525ba71b47Sopenharmony_ci    /// ```rust
535ba71b47Sopenharmony_ci    /// ```
545ba71b47Sopenharmony_ci    pub fn list_system_abilities() -> Vec<String> {
555ba71b47Sopenharmony_ci        info!("list system ability");
565ba71b47Sopenharmony_ci        ListSystemAbilities()
575ba71b47Sopenharmony_ci    }
585ba71b47Sopenharmony_ci    /// let abilities = sysm.list_system_ability();
595ba71b47Sopenharmony_ci    ///
605ba71b47Sopenharmony_ci    ///
615ba71b47Sopenharmony_ci    /// # Example
625ba71b47Sopenharmony_ci    /// ```rust
635ba71b47Sopenharmony_ci    /// ```
645ba71b47Sopenharmony_ci    pub fn list_system_abilities_with_dump_flag(dump_flag: DumpFlagPriority) -> Vec<String> {
655ba71b47Sopenharmony_ci        info!("list system ability");
665ba71b47Sopenharmony_ci        ListSystemAbilitiesWithDumpFlag(dump_flag as u32)
675ba71b47Sopenharmony_ci    }
685ba71b47Sopenharmony_ci
695ba71b47Sopenharmony_ci    /// # Example
705ba71b47Sopenharmony_ci    /// ```rust
715ba71b47Sopenharmony_ci    /// use samgr::manage::SystemAbilityManager;
725ba71b47Sopenharmony_ci    ///
735ba71b47Sopenharmony_ci    /// let request_abilty = SystemAbilityManager::get_system_ability(3706, None);
745ba71b47Sopenharmony_ci    /// ```
755ba71b47Sopenharmony_ci    pub fn get_system_ability(said: i32) -> Option<RemoteObj> {
765ba71b47Sopenharmony_ci        info!("get system ability {}", said);
775ba71b47Sopenharmony_ci        RemoteObj::from_sptr(GetSystemAbility(said))
785ba71b47Sopenharmony_ci    }
795ba71b47Sopenharmony_ci
805ba71b47Sopenharmony_ci    /// # Example
815ba71b47Sopenharmony_ci    /// ```rust
825ba71b47Sopenharmony_ci    /// use samgr::manage::SystemAbilityManager;
835ba71b47Sopenharmony_ci    /// ```
845ba71b47Sopenharmony_ci    pub fn get_system_ability_with_device_id(said: i32, device_id: &str) -> Option<RemoteObj> {
855ba71b47Sopenharmony_ci        info!("get system ability {} with device id", said);
865ba71b47Sopenharmony_ci        let_cxx_string!(id = device_id);
875ba71b47Sopenharmony_ci        RemoteObj::from_sptr(GetSystemAbilityWithDeviceId(said, &id))
885ba71b47Sopenharmony_ci    }
895ba71b47Sopenharmony_ci
905ba71b47Sopenharmony_ci    /// # Example
915ba71b47Sopenharmony_ci    /// ```rust
925ba71b47Sopenharmony_ci    /// use samgr::definition::DOWNLOAD_SERVICE_ID;
935ba71b47Sopenharmony_ci    /// use samgr::manage::SystemAbilityManager;
945ba71b47Sopenharmony_ci    ///
955ba71b47Sopenharmony_ci    /// let download_service = SystemAbilityManager::check_system_ability(DOWNLOAD_SERVICE_ID).unwrap();
965ba71b47Sopenharmony_ci    /// ```
975ba71b47Sopenharmony_ci    pub fn check_system_ability(said: i32) -> Option<RemoteObj> {
985ba71b47Sopenharmony_ci        info!("check system ability {}", said);
995ba71b47Sopenharmony_ci
1005ba71b47Sopenharmony_ci        RemoteObj::from_sptr(CheckSystemAbility(said))
1015ba71b47Sopenharmony_ci    }
1025ba71b47Sopenharmony_ci
1035ba71b47Sopenharmony_ci    pub fn check_system_ability_with_ability(said: i32, device_id: &str) -> Option<RemoteObj> {
1045ba71b47Sopenharmony_ci        info!("check system ability {} with device id", said);
1055ba71b47Sopenharmony_ci        let_cxx_string!(id = device_id);
1065ba71b47Sopenharmony_ci        RemoteObj::from_sptr(CheckSystemAbilityWithDeviceId(said, &id))
1075ba71b47Sopenharmony_ci    }
1085ba71b47Sopenharmony_ci
1095ba71b47Sopenharmony_ci    /// # Example
1105ba71b47Sopenharmony_ci    /// ```rust
1115ba71b47Sopenharmony_ci    /// use samgr::definition::DOWNLOAD_SERVICE_ID;
1125ba71b47Sopenharmony_ci    /// use samgr::manage::SystemAbilityManager;
1135ba71b47Sopenharmony_ci    ///
1145ba71b47Sopenharmony_ci    /// SystemAbilityManager::remove_system_ability(DOWNLOAD_SERVICE_IDD);
1155ba71b47Sopenharmony_ci    /// ```
1165ba71b47Sopenharmony_ci    pub fn remove_system_ability(said: i32) -> i32 {
1175ba71b47Sopenharmony_ci        info!("remove system ability {}", said);
1185ba71b47Sopenharmony_ci        RemoveSystemAbility(said)
1195ba71b47Sopenharmony_ci    }
1205ba71b47Sopenharmony_ci
1215ba71b47Sopenharmony_ci    /// Adds a new system ability
1225ba71b47Sopenharmony_ci    pub fn add_systemability<A: RemoteStub + 'static>(said: i32, ability: A) -> i32 {
1235ba71b47Sopenharmony_ci        info!("add system ability {}", said);
1245ba71b47Sopenharmony_ci        let is_distributed = false;
1255ba71b47Sopenharmony_ci        let dump_flags = DumpFlagPriority::Default;
1265ba71b47Sopenharmony_ci        let capability = "";
1275ba71b47Sopenharmony_ci        let permission = "";
1285ba71b47Sopenharmony_ci        let stub = AbilityStub::new(ability);
1295ba71b47Sopenharmony_ci        AddSystemAbility(
1305ba71b47Sopenharmony_ci            said,
1315ba71b47Sopenharmony_ci            Box::new(stub),
1325ba71b47Sopenharmony_ci            AddSystemAbilityConfig {
1335ba71b47Sopenharmony_ci                is_distributed,
1345ba71b47Sopenharmony_ci                dump_flags: dump_flags as u32,
1355ba71b47Sopenharmony_ci                capability: capability.to_string(),
1365ba71b47Sopenharmony_ci                permission: permission.to_string(),
1375ba71b47Sopenharmony_ci            },
1385ba71b47Sopenharmony_ci        )
1395ba71b47Sopenharmony_ci    }
1405ba71b47Sopenharmony_ci
1415ba71b47Sopenharmony_ci    pub fn add_systemability_with_extra<A: RemoteStub + 'static>(
1425ba71b47Sopenharmony_ci        said: i32,
1435ba71b47Sopenharmony_ci        ability: A,
1445ba71b47Sopenharmony_ci        is_distributed: bool,
1455ba71b47Sopenharmony_ci        dump_flags: DumpFlagPriority,
1465ba71b47Sopenharmony_ci        capability: &str,
1475ba71b47Sopenharmony_ci        permission: &str,
1485ba71b47Sopenharmony_ci    ) -> i32 {
1495ba71b47Sopenharmony_ci        info!("add system ability {}", said);
1505ba71b47Sopenharmony_ci        let stub = AbilityStub::new(ability);
1515ba71b47Sopenharmony_ci        AddSystemAbility(
1525ba71b47Sopenharmony_ci            said,
1535ba71b47Sopenharmony_ci            Box::new(stub),
1545ba71b47Sopenharmony_ci            AddSystemAbilityConfig {
1555ba71b47Sopenharmony_ci                is_distributed,
1565ba71b47Sopenharmony_ci                dump_flags: dump_flags as u32,
1575ba71b47Sopenharmony_ci                capability: capability.to_string(),
1585ba71b47Sopenharmony_ci                permission: permission.to_string(),
1595ba71b47Sopenharmony_ci            },
1605ba71b47Sopenharmony_ci        )
1615ba71b47Sopenharmony_ci    }
1625ba71b47Sopenharmony_ci
1635ba71b47Sopenharmony_ci    pub fn load_system_ability(said: i32, timeout: i32) -> Option<RemoteObj> {
1645ba71b47Sopenharmony_ci        info!("load system ability {}", said);
1655ba71b47Sopenharmony_ci        RemoteObj::from_sptr(LoadSystemAbility(said, timeout))
1665ba71b47Sopenharmony_ci    }
1675ba71b47Sopenharmony_ci
1685ba71b47Sopenharmony_ci    pub fn load_system_ability_with_callback(said: i32, on_success: fn(), on_fail: fn()) -> i32 {
1695ba71b47Sopenharmony_ci        info!("load system ability {}", said);
1705ba71b47Sopenharmony_ci        LoadSystemAbilityWithCallback(said, on_success, on_fail)
1715ba71b47Sopenharmony_ci    }
1725ba71b47Sopenharmony_ci
1735ba71b47Sopenharmony_ci    pub fn subscribe_system_ability(
1745ba71b47Sopenharmony_ci        said: i32,
1755ba71b47Sopenharmony_ci        on_add: fn(i32, &str),
1765ba71b47Sopenharmony_ci        on_remove: fn(i32, &str),
1775ba71b47Sopenharmony_ci    ) -> UnsubscribeHandler {
1785ba71b47Sopenharmony_ci        info!("subscribe system ability {}", said);
1795ba71b47Sopenharmony_ci        UnsubscribeHandler::new(Unsubscribe::Ability(SubscribeSystemAbility(
1805ba71b47Sopenharmony_ci            said, on_add, on_remove,
1815ba71b47Sopenharmony_ci        )))
1825ba71b47Sopenharmony_ci    }
1835ba71b47Sopenharmony_ci
1845ba71b47Sopenharmony_ci    /// # Example
1855ba71b47Sopenharmony_ci    /// ```rust
1865ba71b47Sopenharmony_ci    /// use samgr::definition::DOWNLOAD_SERVICE_ID;
1875ba71b47Sopenharmony_ci    /// use samgr::manage::SystemAbilityManager;
1885ba71b47Sopenharmony_ci    ///
1895ba71b47Sopenharmony_ci    /// SystemAbilityManager::remove_system_ability(DOWNLOAD_SERVICE_IDD);
1905ba71b47Sopenharmony_ci    /// ```
1915ba71b47Sopenharmony_ci    pub fn add_ondemand_system_ability_info(said: i32, local_ability_manager_name: &str) -> i32 {
1925ba71b47Sopenharmony_ci        info!("add ondemand system ability {} info", said);
1935ba71b47Sopenharmony_ci
1945ba71b47Sopenharmony_ci        AddOnDemandSystemAbilityInfo(said, local_ability_manager_name)
1955ba71b47Sopenharmony_ci    }
1965ba71b47Sopenharmony_ci
1975ba71b47Sopenharmony_ci    pub fn unload_system_ability(said: i32) -> i32 {
1985ba71b47Sopenharmony_ci        info!("unload system ability {}", said);
1995ba71b47Sopenharmony_ci        UnloadSystemAbility(said)
2005ba71b47Sopenharmony_ci    }
2015ba71b47Sopenharmony_ci
2025ba71b47Sopenharmony_ci    pub fn cancel_unload_system_ability(said: i32) -> i32 {
2035ba71b47Sopenharmony_ci        info!("cancel unload system ability {}", said);
2045ba71b47Sopenharmony_ci        CancelUnloadSystemAbility(said)
2055ba71b47Sopenharmony_ci    }
2065ba71b47Sopenharmony_ci
2075ba71b47Sopenharmony_ci    /// # Example
2085ba71b47Sopenharmony_ci    /// ```rust
2095ba71b47Sopenharmony_ci    /// use samgr::definition::DOWNLOAD_SERVICE_ID;
2105ba71b47Sopenharmony_ci    ///
2115ba71b47Sopenharmony_ci    /// SystemAbilityManager::unload_all_idle_system_ability();
2125ba71b47Sopenharmony_ci    /// ```
2135ba71b47Sopenharmony_ci    pub fn unload_all_idle_system_ability(&self) -> i32 {
2145ba71b47Sopenharmony_ci        info!("unload all idle system ability");
2155ba71b47Sopenharmony_ci        UnloadAllIdleSystemAbility()
2165ba71b47Sopenharmony_ci    }
2175ba71b47Sopenharmony_ci
2185ba71b47Sopenharmony_ci    pub fn get_system_process_info(said: i32) -> SystemProcessInfo {
2195ba71b47Sopenharmony_ci        info!("get system ability {} process info", said);
2205ba71b47Sopenharmony_ci        GetSystemProcessInfo(said)
2215ba71b47Sopenharmony_ci    }
2225ba71b47Sopenharmony_ci
2235ba71b47Sopenharmony_ci    pub fn get_running_system_process() -> Vec<SystemProcessInfo> {
2245ba71b47Sopenharmony_ci        info!("get running system ability process info");
2255ba71b47Sopenharmony_ci        GetRunningSystemProcess()
2265ba71b47Sopenharmony_ci    }
2275ba71b47Sopenharmony_ci
2285ba71b47Sopenharmony_ci    ///
2295ba71b47Sopenharmony_ci    pub fn send_strategy(s_type: i32, saids: Vec<i32>, level: i32, action: &str) -> i32 {
2305ba71b47Sopenharmony_ci        let_cxx_string!(action = action);
2315ba71b47Sopenharmony_ci        SendStrategy(s_type, saids, level, action)
2325ba71b47Sopenharmony_ci    }
2335ba71b47Sopenharmony_ci
2345ba71b47Sopenharmony_ci    pub fn get_common_event_extra_data_id_list(said: i32, extraids: &mut Vec<i64>, event_name: &str) -> i32 {
2355ba71b47Sopenharmony_ci        let_cxx_string!(event_name = event_name);
2365ba71b47Sopenharmony_ci        GetCommonEventExtraDataIdlist(said, extraids, &event_name)
2375ba71b47Sopenharmony_ci    }
2385ba71b47Sopenharmony_ci
2395ba71b47Sopenharmony_ci    pub fn subscribe_system_process(
2405ba71b47Sopenharmony_ci        on_start: fn(&SystemProcessInfo),
2415ba71b47Sopenharmony_ci        on_stop: fn(&SystemProcessInfo),
2425ba71b47Sopenharmony_ci    ) -> UnsubscribeHandler {
2435ba71b47Sopenharmony_ci        UnsubscribeHandler::new(Unsubscribe::Process(SubscribeSystemProcess(
2445ba71b47Sopenharmony_ci            on_start, on_stop,
2455ba71b47Sopenharmony_ci        )))
2465ba71b47Sopenharmony_ci    }
2475ba71b47Sopenharmony_ci
2485ba71b47Sopenharmony_ci    pub fn get_on_demand_reason_extra_date(extra_data_id: i64, parcel: &mut MsgParcel) -> i32 {
2495ba71b47Sopenharmony_ci        GetOnDemandReasonExtraData(extra_data_id, parcel.pin_mut().unwrap())
2505ba71b47Sopenharmony_ci    }
2515ba71b47Sopenharmony_ci}
2525ba71b47Sopenharmony_ci
2535ba71b47Sopenharmony_cienum Unsubscribe {
2545ba71b47Sopenharmony_ci    Ability(UniquePtr<UnSubscribeSystemAbilityHandler>),
2555ba71b47Sopenharmony_ci    Process(UniquePtr<UnSubscribeSystemProcessHandler>),
2565ba71b47Sopenharmony_ci}
2575ba71b47Sopenharmony_ci
2585ba71b47Sopenharmony_cipub struct UnsubscribeHandler {
2595ba71b47Sopenharmony_ci    inner: Unsubscribe,
2605ba71b47Sopenharmony_ci}
2615ba71b47Sopenharmony_ci
2625ba71b47Sopenharmony_ciimpl UnsubscribeHandler {
2635ba71b47Sopenharmony_ci    fn new(inner: Unsubscribe) -> Self {
2645ba71b47Sopenharmony_ci        Self { inner }
2655ba71b47Sopenharmony_ci    }
2665ba71b47Sopenharmony_ci
2675ba71b47Sopenharmony_ci    pub fn unsubscribe(self) {
2685ba71b47Sopenharmony_ci        match self.inner {
2695ba71b47Sopenharmony_ci            Unsubscribe::Ability(mut p) => p.pin_mut().UnSubscribe(),
2705ba71b47Sopenharmony_ci            Unsubscribe::Process(mut p) => p.pin_mut().UnSubscribe(),
2715ba71b47Sopenharmony_ci        }
2725ba71b47Sopenharmony_ci    }
2735ba71b47Sopenharmony_ci}
274