1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef FREEZE_MANAGER_H
16 #define FREEZE_MANAGER_H
17 #include "freeze_manager.h"
18
19 #include "ability_manager_client.h"
20 #include "global.h"
21 #include "res_sched_client.h"
22 #include "system_ability_definition.h"
23 namespace OHOS {
24 namespace MiscServices {
25 constexpr const char *INPUT_METHOD_SERVICE_SA_NAME = "inputmethod_service";
26 constexpr const char *STOP_TASK_NAME = "ReportStop";
27 constexpr int32_t DELAY_TIME = 3000; // 3s
ControlIme(bool shouldApply)28 void FreezeManager::ControlIme(bool shouldApply)
29 {
30 if (eventHandler_ == nullptr) {
31 IMSA_HILOGW("eventHandler_ is nullptr.");
32 ReportRss(shouldApply, pid_);
33 return;
34 }
35 if (shouldApply) {
36 // Delay the FREEZE report by 3s.
37 eventHandler_->PostTask(
38 [shouldApply, pid = pid_]() {
39 ReportRss(shouldApply, pid);
40 },
41 STOP_TASK_NAME, DELAY_TIME);
42 } else {
43 // Cancel the unexecuted FREEZE task.
44 eventHandler_->RemoveTask(STOP_TASK_NAME);
45 ReportRss(shouldApply, pid_);
46 }
47 }
48
ReportRss(bool shouldFreeze,pid_t pid)49 void FreezeManager::ReportRss(bool shouldFreeze, pid_t pid)
50 {
51 auto type = ResourceSchedule::ResType::RES_TYPE_SA_CONTROL_APP_EVENT;
52 auto status = shouldFreeze ? ResourceSchedule::ResType::SaControlAppStatus::SA_STOP_APP :
53 ResourceSchedule::ResType::SaControlAppStatus::SA_START_APP;
54 std::unordered_map<std::string, std::string> payload = {
55 { "saId", std::to_string(INPUT_METHOD_SYSTEM_ABILITY_ID) },
56 { "saName", std::string(INPUT_METHOD_SERVICE_SA_NAME) },
57 { "extensionType", std::to_string(static_cast<int32_t>(AppExecFwk::ExtensionAbilityType::INPUTMETHOD)) },
58 { "pid", std::to_string(pid) },
59 };
60 IMSA_HILOGD("report RSS should freeze: %{public}d.", shouldFreeze);
61 ResourceSchedule::ResSchedClient::GetInstance().ReportData(type, status, payload);
62 }
63 // LCOV_EXCL_START
TemporaryActiveIme()64 void FreezeManager::TemporaryActiveIme()
65 {
66 std::lock_guard<std::mutex> lock(mutex_);
67 if (!isFrozen_) {
68 IMSA_HILOGI("IME is not frozen, no need active");
69 return;
70 }
71
72 IMSA_HILOGI("temporary active IME");
73 ReportRss(false, pid_);
74 ReportRss(true, pid_);
75 }
76 // LCOV_EXCL_STOP
77 } // namespace MiscServices
78 } // namespace OHOS
79 #endif // FREEZE_MANAGER_H