1 /*
2 * Copyright (c) 2023 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 "system_param_adapter.h"
17
18 #include "inputmethod_message_handler.h"
19 #include "parameter.h"
20 #include "parameters.h"
21 namespace OHOS {
22 namespace MiscServices {
23 const std::unordered_map<std::string, SystemParamAdapter::CbHandler> SystemParamAdapter::PARAM_CB_HANDLERS = {
24 { SystemParamAdapter::SYSTEM_LANGUAGE_KEY, SystemParamAdapter::OnLanguageChange },
25 { SystemParamAdapter::MEMORY_WATERMARK_KEY, SystemParamAdapter::OnMemoryChange },
26 };
GetInstance()27 SystemParamAdapter &SystemParamAdapter::GetInstance()
28 {
29 static SystemParamAdapter adapter;
30 return adapter;
31 }
32
WatchParam(const std::string & key)33 int32_t SystemParamAdapter::WatchParam(const std::string &key)
34 {
35 auto it = PARAM_CB_HANDLERS.find(key);
36 if (it == PARAM_CB_HANDLERS.end() || it->second == nullptr) {
37 return ErrorCode::ERROR_BAD_PARAMETERS;
38 }
39 auto errNo = WatchParameter(key.c_str(), it->second, nullptr);
40 IMSA_HILOGD("key/ret: %{public}s/%{public}d.", key.c_str(), errNo);
41 return errNo;
42 }
43
GetBoolParam(const std::string & key)44 bool SystemParamAdapter::GetBoolParam(const std::string &key)
45 {
46 return system::GetBoolParameter(key, false);
47 }
48
OnLanguageChange(const char * key,const char * value,void * context)49 void SystemParamAdapter::OnLanguageChange(const char *key, const char *value, void *context)
50 {
51 HandleSysParamChanged(key, value, SYSTEM_LANGUAGE_KEY, MessageID::MSG_ID_SYS_LANGUAGE_CHANGED);
52 }
53
OnMemoryChange(const char * key,const char * value,void * context)54 void SystemParamAdapter::OnMemoryChange(const char *key, const char *value, void *context)
55 {
56 HandleSysParamChanged(key, value, MEMORY_WATERMARK_KEY, MessageID::MSG_ID_SYS_MEMORY_CHANGED);
57 }
58
HandleSysParamChanged(const char * key,const char * value,const char * expectedKey,int32_t messageId)59 void SystemParamAdapter::HandleSysParamChanged(
60 const char *key, const char *value, const char *expectedKey, int32_t messageId)
61 {
62 if (strcmp(key, expectedKey) != 0) {
63 IMSA_HILOGE("key: %{public}s is error!", key);
64 return;
65 }
66 IMSA_HILOGI("value: %{public}s.", value);
67 Message *msg = new (std::nothrow) Message(messageId, nullptr);
68 if (msg == nullptr) {
69 return;
70 }
71 auto mesHandler = MessageHandler::Instance();
72 if (mesHandler == nullptr) {
73 delete msg;
74 return;
75 }
76 mesHandler->SendMessage(msg);
77 }
78 } // namespace MiscServices
79 } // namespace OHOS