• 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 "res_type.h"
22 #include "system_ability_definition.h"
23 
24 namespace OHOS {
25 namespace MiscServices {
26 const std::string INPUT_METHOD_SERVICE_SA_NAME = "inputmethod_service";
IsIpcNeeded(RequestType type)27 bool FreezeManager::IsIpcNeeded(RequestType type)
28 {
29     // If ime is in use, no need to request hide.
30     std::lock_guard<std::mutex> lock(mutex_);
31     return !(type == RequestType::REQUEST_HIDE && !isImeInUse_);
32 }
33 
BeforeIpc(RequestType type)34 void FreezeManager::BeforeIpc(RequestType type)
35 {
36     {
37         std::lock_guard<std::mutex> lock(mutex_);
38         if (type == RequestType::START_INPUT || type == RequestType::REQUEST_SHOW) {
39             isImeInUse_ = true;
40         }
41         if (!isFrozen_) {
42             IMSA_HILOGD("already not frozen");
43             return;
44         }
45         isFrozen_ = false;
46     }
47     ControlIme(false);
48 }
49 
AfterIpc(RequestType type,bool isSuccess)50 void FreezeManager::AfterIpc(RequestType type, bool isSuccess)
51 {
52     bool shouldFreeze = false;
53     {
54         std::lock_guard<std::mutex> lock(mutex_);
55         if (type == RequestType::START_INPUT || type == RequestType::REQUEST_SHOW) {
56             isImeInUse_ = isSuccess;
57         }
58         if (type == RequestType::REQUEST_HIDE && isImeInUse_) {
59             isImeInUse_ = !isSuccess;
60         }
61         if (type == RequestType::STOP_INPUT) {
62             isImeInUse_ = false;
63         }
64         if (isFrozen_ == !isImeInUse_) {
65             IMSA_HILOGD("frozen state already: %{public}d", isFrozen_);
66             return;
67         }
68         isFrozen_ = !isImeInUse_;
69         shouldFreeze = isFrozen_;
70     }
71     ControlIme(shouldFreeze);
72 }
73 
ControlIme(bool shouldFreeze)74 void FreezeManager::ControlIme(bool shouldFreeze)
75 {
76     auto type = ResourceSchedule::ResType::RES_TYPE_SA_CONTROL_APP_EVENT;
77     auto status = shouldFreeze ? ResourceSchedule::ResType::SaControlAppStatus::SA_STOP_APP
78                                : ResourceSchedule::ResType::SaControlAppStatus::SA_START_APP;
79     std::unordered_map<std::string, std::string> payload = {
80         { "saId", std::to_string(INPUT_METHOD_SYSTEM_ABILITY_ID) },
81         { "saName", INPUT_METHOD_SERVICE_SA_NAME },
82         { "extensionType", std::to_string(static_cast<int32_t>(AppExecFwk::ExtensionAbilityType::INPUTMETHOD)) },
83         { "pid", std::to_string(pid_) } };
84     IMSA_HILOGI("report RSS should freeze: %{public}d", shouldFreeze);
85     ResourceSchedule::ResSchedClient::GetInstance().ReportData(type, status, payload);
86 }
87 } // namespace MiscServices
88 } // namespace OHOS