• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_mirror_manager.h"
16 
17 #include "global.h"
18 #include "system_ability_definition.h"
19 namespace OHOS {
20 namespace MiscServices {
IsImeMirrorEnable()21 bool ImeMirrorManager::IsImeMirrorEnable()
22 {
23     return isEnable_;
24 }
25 
SetImeMirrorEnable(bool isRegistered)26 void ImeMirrorManager::SetImeMirrorEnable(bool isRegistered)
27 {
28     isEnable_ = isRegistered;
29 }
30 
SubscribeSaStart(std::function<void ()> handler,int32_t saId)31 bool ImeMirrorManager::SubscribeSaStart(std::function<void()> handler, int32_t saId)
32 {
33     if (handler == nullptr) {
34         IMSA_HILOGE("[ImeMirrorTag] handler is nullptr, saId:%{public}d", saId);
35         return false;
36     }
37     {
38         std::lock_guard<std::mutex> lockGuard(listenerMapMutex_);
39         if (saMgrListenerMap_.find(saId) != saMgrListenerMap_.end()) {
40             IMSA_HILOGW("[ImeMirrorTag]saId:%{public}d is already registered", saId);
41             return true;
42         }
43     }
44 
45     auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
46     if (abilityManager == nullptr) {
47         IMSA_HILOGE("[ImeMirrorTag]abilityManager is nullptr, saId: %{public}d", saId);
48         return false;
49     }
50     sptr<ISystemAbilityStatusChange> listener = new (std::nothrow) SaMgrListener([handler]() {
51         if (handler != nullptr) {
52             handler();
53         }
54     });
55     if (listener == nullptr) {
56         IMSA_HILOGE("[ImeMirrorTag]failed to create listener, saId: %{public}d", saId);
57         return false;
58     }
59     int32_t ret = abilityManager->SubscribeSystemAbility(saId, listener);
60     if (ret != ErrorCode::NO_ERROR) {
61         IMSA_HILOGE("[ImeMirrorTag]subscribe system ability failed, ret: %{public}d, saId: %{public}d", ret, saId);
62         return false;
63     }
64     {
65         std::lock_guard<std::mutex> lockGuard(listenerMapMutex_);
66         saMgrListenerMap_[saId] = listener;
67     }
68     IMSA_HILOGD("[ImeMirrorTag]subscribe system ability success, saId: %{public}d", saId);
69     return true;
70 }
71 
UnSubscribeSaStart(int32_t saId)72 bool ImeMirrorManager::UnSubscribeSaStart(int32_t saId)
73 {
74     sptr<ISystemAbilityStatusChange> listener = nullptr;
75     {
76         std::lock_guard<std::mutex> lockGuard(listenerMapMutex_);
77         auto itr = saMgrListenerMap_.find(saId);
78         if (itr == saMgrListenerMap_.end()) {
79             IMSA_HILOGE("[ImeMirrorTag]saId:%{public}d is not registered", saId);
80             return false;
81         }
82 
83         listener = itr->second;
84     }
85     auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
86     if (abilityManager == nullptr) {
87         IMSA_HILOGE("[ImeMirrorTag]abilityManager is nullptr, saId: %{public}d", saId);
88         return false;
89     }
90 
91     int32_t ret = abilityManager->UnSubscribeSystemAbility(saId, listener);
92     if (ret != ErrorCode::NO_ERROR) {
93         IMSA_HILOGE("[ImeMirrorTag]UnSubscribe system ability failed, ret: %{public}d, saId: %{public}d", ret, saId);
94         return false;
95     }
96     {
97         std::lock_guard<std::mutex> lockGuard(listenerMapMutex_);
98         auto itr = saMgrListenerMap_.find(saId);
99         if (itr == saMgrListenerMap_.end()) {
100             IMSA_HILOGW("[ImeMirrorTag]saId:%{public}d is not found", saId);
101             return true;
102         }
103         saMgrListenerMap_.erase(itr);
104     }
105     IMSA_HILOGD("[ImeMirrorTag]unsubscribe system ability success, saId: %{public}d", saId);
106     return true;
107 }
108 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)109 void ImeMirrorManager::SaMgrListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
110 {
111     IMSA_HILOGD("[ImeMirrorTag]systemAbilityId: %{public}d.", systemAbilityId);
112     if (systemAbilityId != INPUT_METHOD_SYSTEM_ABILITY_ID) {
113         return;
114     }
115     if (func_ != nullptr) {
116         func_();
117     }
118 }
119 } // namespace MiscServices
120 } // namespace OHOS
121