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_language_observer.h"
17 #include "inputmethod_message_handler.h"
18 #include "parameter.h"
19
20 namespace OHOS {
21 namespace MiscServices {
GetInstance()22 SystemLanguageObserver &SystemLanguageObserver::GetInstance()
23 {
24 static SystemLanguageObserver observer;
25 return observer;
26 }
27
Watch()28 void SystemLanguageObserver::Watch()
29 {
30 auto errNo = WatchParameter(SYSTEM_LANGUAGE_KEY, OnChange, nullptr);
31 IMSA_HILOGD("ret: %{public}d.", errNo);
32 }
33
OnChange(const char * key,const char * value,void * context)34 void SystemLanguageObserver::OnChange(const char *key, const char *value, void *context)
35 {
36 if (strncmp(key, SYSTEM_LANGUAGE_KEY, strlen(SYSTEM_LANGUAGE_KEY)) != 0) {
37 IMSA_HILOGE("key: %{public}s is error!", key);
38 return;
39 }
40 IMSA_HILOGD("value: %{public}s.", value);
41 Message *msg = new (std::nothrow) Message(MessageID::MSG_ID_SYS_LANGUAGE_CHANGED, nullptr);
42 if (msg == nullptr) {
43 return;
44 }
45 MessageHandler::Instance()->SendMessage(msg);
46 }
47 } // namespace MiscServices
48 } // namespace OHOS