122736c2fSopenharmony_ci/* 222736c2fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 322736c2fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 422736c2fSopenharmony_ci * you may not use this file except in compliance with the License. 522736c2fSopenharmony_ci * You may obtain a copy of the License at 622736c2fSopenharmony_ci * 722736c2fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 822736c2fSopenharmony_ci * 922736c2fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1022736c2fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1122736c2fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1222736c2fSopenharmony_ci * See the License for the specific language governing permissions and 1322736c2fSopenharmony_ci * limitations under the License. 1422736c2fSopenharmony_ci */ 1522736c2fSopenharmony_ci 1622736c2fSopenharmony_ci#include "freeze_manager.h" 1722736c2fSopenharmony_ci 1822736c2fSopenharmony_ci#include "ability_manager_client.h" 1922736c2fSopenharmony_ci#include "global.h" 2022736c2fSopenharmony_ci#include "res_sched_client.h" 2122736c2fSopenharmony_ci#include "res_type.h" 2222736c2fSopenharmony_ci#include "system_ability_definition.h" 2322736c2fSopenharmony_ci 2422736c2fSopenharmony_cinamespace OHOS { 2522736c2fSopenharmony_cinamespace MiscServices { 2622736c2fSopenharmony_ciconst std::string INPUT_METHOD_SERVICE_SA_NAME = "inputmethod_service"; 2722736c2fSopenharmony_ciconstexpr const char *STOP_TASK_NAME = "ReportStop"; 2822736c2fSopenharmony_ciconstexpr std::int32_t DELAY_TIME = 3000L; 2922736c2fSopenharmony_cistd::shared_ptr<AppExecFwk::EventHandler> FreezeManager::eventHandler_ = nullptr; 3022736c2fSopenharmony_cibool FreezeManager::IsIpcNeeded(RequestType type) 3122736c2fSopenharmony_ci{ 3222736c2fSopenharmony_ci // If ime is in use, no need to request hide. 3322736c2fSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 3422736c2fSopenharmony_ci return !(type == RequestType::REQUEST_HIDE && !isImeInUse_); 3522736c2fSopenharmony_ci} 3622736c2fSopenharmony_ci 3722736c2fSopenharmony_civoid FreezeManager::BeforeIpc(RequestType type) 3822736c2fSopenharmony_ci{ 3922736c2fSopenharmony_ci { 4022736c2fSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 4122736c2fSopenharmony_ci if (type == RequestType::START_INPUT || type == RequestType::REQUEST_SHOW) { 4222736c2fSopenharmony_ci isImeInUse_ = true; 4322736c2fSopenharmony_ci } 4422736c2fSopenharmony_ci if (!isFrozen_) { 4522736c2fSopenharmony_ci IMSA_HILOGD("not frozen already."); 4622736c2fSopenharmony_ci return; 4722736c2fSopenharmony_ci } 4822736c2fSopenharmony_ci isFrozen_ = false; 4922736c2fSopenharmony_ci } 5022736c2fSopenharmony_ci ControlIme(false); 5122736c2fSopenharmony_ci} 5222736c2fSopenharmony_ci 5322736c2fSopenharmony_civoid FreezeManager::AfterIpc(RequestType type, bool isSuccess) 5422736c2fSopenharmony_ci{ 5522736c2fSopenharmony_ci bool shouldFreeze = false; 5622736c2fSopenharmony_ci { 5722736c2fSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 5822736c2fSopenharmony_ci if (type == RequestType::START_INPUT || type == RequestType::REQUEST_SHOW) { 5922736c2fSopenharmony_ci isImeInUse_ = isSuccess; 6022736c2fSopenharmony_ci } 6122736c2fSopenharmony_ci if (type == RequestType::REQUEST_HIDE && isImeInUse_) { 6222736c2fSopenharmony_ci isImeInUse_ = !isSuccess; 6322736c2fSopenharmony_ci } 6422736c2fSopenharmony_ci if (type == RequestType::STOP_INPUT) { 6522736c2fSopenharmony_ci isImeInUse_ = false; 6622736c2fSopenharmony_ci } 6722736c2fSopenharmony_ci if (isFrozen_ == !isImeInUse_) { 6822736c2fSopenharmony_ci IMSA_HILOGD("frozen state already: %{public}d.", isFrozen_); 6922736c2fSopenharmony_ci return; 7022736c2fSopenharmony_ci } 7122736c2fSopenharmony_ci isFrozen_ = !isImeInUse_; 7222736c2fSopenharmony_ci shouldFreeze = isFrozen_; 7322736c2fSopenharmony_ci } 7422736c2fSopenharmony_ci ControlIme(shouldFreeze); 7522736c2fSopenharmony_ci} 7622736c2fSopenharmony_ci 7722736c2fSopenharmony_civoid FreezeManager::ControlIme(bool shouldFreeze) 7822736c2fSopenharmony_ci{ 7922736c2fSopenharmony_ci if (eventHandler_ == nullptr) { 8022736c2fSopenharmony_ci IMSA_HILOGW("eventHandler_ is nullptr."); 8122736c2fSopenharmony_ci ReportRss(shouldFreeze, pid_); 8222736c2fSopenharmony_ci return; 8322736c2fSopenharmony_ci } 8422736c2fSopenharmony_ci if (shouldFreeze) { 8522736c2fSopenharmony_ci // Delay the FREEZE report by 3s. 8622736c2fSopenharmony_ci eventHandler_->PostTask( 8722736c2fSopenharmony_ci [shouldFreeze, pid = pid_]() { ReportRss(shouldFreeze, pid); }, STOP_TASK_NAME, DELAY_TIME); 8822736c2fSopenharmony_ci } else { 8922736c2fSopenharmony_ci // Cancel the unexecuted FREEZE task. 9022736c2fSopenharmony_ci eventHandler_->RemoveTask(STOP_TASK_NAME); 9122736c2fSopenharmony_ci ReportRss(shouldFreeze, pid_); 9222736c2fSopenharmony_ci } 9322736c2fSopenharmony_ci} 9422736c2fSopenharmony_ci 9522736c2fSopenharmony_civoid FreezeManager::ReportRss(bool shouldFreeze, pid_t pid) 9622736c2fSopenharmony_ci{ 9722736c2fSopenharmony_ci auto type = ResourceSchedule::ResType::RES_TYPE_SA_CONTROL_APP_EVENT; 9822736c2fSopenharmony_ci auto status = shouldFreeze ? ResourceSchedule::ResType::SaControlAppStatus::SA_STOP_APP 9922736c2fSopenharmony_ci : ResourceSchedule::ResType::SaControlAppStatus::SA_START_APP; 10022736c2fSopenharmony_ci std::unordered_map<std::string, std::string> payload = { { "saId", std::to_string(INPUT_METHOD_SYSTEM_ABILITY_ID) }, 10122736c2fSopenharmony_ci { "saName", INPUT_METHOD_SERVICE_SA_NAME }, 10222736c2fSopenharmony_ci { "extensionType", std::to_string(static_cast<int32_t>(AppExecFwk::ExtensionAbilityType::INPUTMETHOD)) }, 10322736c2fSopenharmony_ci { "pid", std::to_string(pid) } }; 10422736c2fSopenharmony_ci IMSA_HILOGD("report RSS should freeze: %{public}d.", shouldFreeze); 10522736c2fSopenharmony_ci ResourceSchedule::ResSchedClient::GetInstance().ReportData(type, status, payload); 10622736c2fSopenharmony_ci} 10722736c2fSopenharmony_ci 10822736c2fSopenharmony_civoid FreezeManager::SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &eventHandler) 10922736c2fSopenharmony_ci{ 11022736c2fSopenharmony_ci eventHandler_ = eventHandler; 11122736c2fSopenharmony_ci} 11222736c2fSopenharmony_ci} // namespace MiscServices 11322736c2fSopenharmony_ci} // namespace OHOS