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 #include "ime_lifecycle_manager.h" 16 17 #include "global.h" 18 19 namespace OHOS { 20 namespace MiscServices { 21 constexpr const char *STOP_IME_TASK_NAME = "StopImeTask"; ControlIme(bool shouldApply)22void ImeLifecycleManager::ControlIme(bool shouldApply) 23 { 24 if (eventHandler_ == nullptr) { 25 IMSA_HILOGE("eventHandler_ is nullptr."); 26 return; 27 } 28 29 if (!shouldApply) { 30 // Cancel the unexecuted stop task. 31 eventHandler_->RemoveTask(STOP_IME_TASK_NAME); 32 return; 33 } 34 35 // Delay the stop report by 20s. 36 std::weak_ptr<ImeLifecycleManager> weakThis = shared_from_this(); 37 eventHandler_->PostTask( 38 [weakThis]() { 39 auto sharedThis = weakThis.lock(); 40 if (sharedThis == nullptr) { 41 IMSA_HILOGE("sharedThis is nullptr."); 42 return; 43 } 44 if (sharedThis->stopImeFunc_ == nullptr) { 45 IMSA_HILOGE("stopImeFunc_ is nullptr."); 46 return; 47 } 48 IMSA_HILOGD("Stop ime pid %{public}d", sharedThis->pid_); 49 sharedThis->stopImeFunc_(); 50 }, 51 STOP_IME_TASK_NAME, stopDelayTime_); 52 } 53 } // namespace MiscServices 54 } // namespace OHOS