• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
16 #include "freeze_manager.h"
17 
18 #include "ability_manager_client.h"
19 #include "global.h"
20 #include "res_sched_client.h"
21 #include "system_ability_definition.h"
22 
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 std::int32_t DELAY_TIME = 3000L;
28 std::shared_ptr<AppExecFwk::EventHandler> FreezeManager::eventHandler_ = nullptr;
IsIpcNeeded(RequestType type)29 bool FreezeManager::IsIpcNeeded(RequestType type)
30 {
31     // If ime is in use, no need to request hide.
32     std::lock_guard<std::mutex> lock(mutex_);
33     return !(type == RequestType::REQUEST_HIDE && !isImeInUse_);
34 }
35 
BeforeIpc(RequestType type)36 void FreezeManager::BeforeIpc(RequestType type)
37 {
38     {
39         std::lock_guard<std::mutex> lock(mutex_);
40         if (type == RequestType::START_INPUT || type == RequestType::REQUEST_SHOW) {
41             isImeInUse_ = true;
42         }
43         if (!isFrozen_) {
44             IMSA_HILOGD("not frozen already.");
45             return;
46         }
47         isFrozen_ = false;
48     }
49     ControlIme(false);
50 }
51 
AfterIpc(RequestType type,bool isSuccess)52 void FreezeManager::AfterIpc(RequestType type, bool isSuccess)
53 {
54     bool shouldFreeze = false;
55     {
56         std::lock_guard<std::mutex> lock(mutex_);
57         if (type == RequestType::START_INPUT || type == RequestType::REQUEST_SHOW) {
58             isImeInUse_ = isSuccess;
59         }
60         if (type == RequestType::REQUEST_HIDE && isImeInUse_) {
61             isImeInUse_ = !isSuccess;
62         }
63         if (type == RequestType::STOP_INPUT) {
64             isImeInUse_ = false;
65         }
66         if (isFrozen_ == !isImeInUse_) {
67             IMSA_HILOGD("frozen state already: %{public}d.", isFrozen_);
68             return;
69         }
70         isFrozen_ = !isImeInUse_;
71         shouldFreeze = isFrozen_;
72     }
73     ControlIme(shouldFreeze);
74 }
75 
ControlIme(bool shouldFreeze)76 void FreezeManager::ControlIme(bool shouldFreeze)
77 {
78     if (eventHandler_ == nullptr) {
79         IMSA_HILOGW("eventHandler_ is nullptr.");
80         ReportRss(shouldFreeze, pid_);
81         return;
82     }
83     if (shouldFreeze) {
84         // Delay the FREEZE report by 3s.
85         eventHandler_->PostTask(
86             [shouldFreeze, pid = pid_]() { ReportRss(shouldFreeze, pid); }, STOP_TASK_NAME, DELAY_TIME);
87     } else {
88         // Cancel the unexecuted FREEZE task.
89         eventHandler_->RemoveTask(STOP_TASK_NAME);
90         ReportRss(shouldFreeze, pid_);
91     }
92 }
93 
ReportRss(bool shouldFreeze,pid_t pid)94 void FreezeManager::ReportRss(bool shouldFreeze, pid_t pid)
95 {
96     auto type = ResourceSchedule::ResType::RES_TYPE_SA_CONTROL_APP_EVENT;
97     auto status = shouldFreeze ? ResourceSchedule::ResType::SaControlAppStatus::SA_STOP_APP
98                                : ResourceSchedule::ResType::SaControlAppStatus::SA_START_APP;
99     std::unordered_map<std::string, std::string> payload = { { "saId", std::to_string(INPUT_METHOD_SYSTEM_ABILITY_ID) },
100         { "saName", std::string(INPUT_METHOD_SERVICE_SA_NAME) },
101         { "extensionType", std::to_string(static_cast<int32_t>(AppExecFwk::ExtensionAbilityType::INPUTMETHOD)) },
102         { "pid", std::to_string(pid) } };
103     IMSA_HILOGD("report RSS should freeze: %{public}d.", shouldFreeze);
104     ResourceSchedule::ResSchedClient::GetInstance().ReportData(type, status, payload);
105 }
106 
SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> & eventHandler)107 void FreezeManager::SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &eventHandler)
108 {
109     eventHandler_ = eventHandler;
110 }
111 
IsImeInUse()112 bool FreezeManager::IsImeInUse()
113 {
114     std::lock_guard<std::mutex> lock(mutex_);
115     return isImeInUse_;
116 }
117 } // namespace MiscServices
118 } // namespace OHOS