• 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 "ime_state_manager.h"
17 
18 #include "global.h"
19 
20 namespace OHOS {
21 namespace MiscServices {
22 std::shared_ptr<AppExecFwk::EventHandler> ImeStateManager::eventHandler_ = nullptr;
IsIpcNeeded(RequestType type)23 bool ImeStateManager::IsIpcNeeded(RequestType type)
24 {
25     // If ime is in use, no need to request hide.
26     std::lock_guard<std::mutex> lock(mutex_);
27     return !(type == RequestType::REQUEST_HIDE && !isImeInUse_);
28 }
29 
BeforeIpc(RequestType type)30 void ImeStateManager::BeforeIpc(RequestType type)
31 {
32     std::lock_guard<std::mutex> lock(mutex_);
33     if (type == RequestType::START_INPUT || type == RequestType::REQUEST_SHOW) {
34         isImeInUse_ = true;
35     }
36     if (!isFrozen_) {
37         IMSA_HILOGD("not frozen already.");
38         return;
39     }
40     isFrozen_ = false;
41     ControlIme(false);
42 }
43 
AfterIpc(RequestType type,bool isSuccess)44 void ImeStateManager::AfterIpc(RequestType type, bool isSuccess)
45 {
46     bool shouldFreeze = false;
47     std::lock_guard<std::mutex> lock(mutex_);
48     if (type == RequestType::START_INPUT || type == RequestType::REQUEST_SHOW) {
49         isImeInUse_ = isSuccess;
50     }
51     if (type == RequestType::REQUEST_HIDE && isImeInUse_) {
52         isImeInUse_ = !isSuccess;
53     }
54     if (type == RequestType::STOP_INPUT) {
55         isImeInUse_ = false;
56     }
57     if (isFrozen_ == !isImeInUse_) {
58         IMSA_HILOGD("frozen state already: %{public}d.", isFrozen_);
59         return;
60     }
61     isFrozen_ = !isImeInUse_;
62     shouldFreeze = isFrozen_;
63     ControlIme(shouldFreeze);
64 }
65 
SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> & eventHandler)66 void ImeStateManager::SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &eventHandler)
67 {
68     eventHandler_ = eventHandler;
69 }
70 
IsImeInUse()71 bool ImeStateManager::IsImeInUse()
72 {
73     std::lock_guard<std::mutex> lock(mutex_);
74     return isImeInUse_;
75 }
76 } // namespace MiscServices
77 } // namespace OHOS