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 "i18n_hilog.h"
17 #include "i18n_service_ability_load_manager.h"
18 #include "iremote_object.h"
19 #include "system_ability_definition.h"
20 #include "i18n_service_ability_client.h"
21
22 namespace OHOS {
23 namespace Global {
24 namespace I18n {
GetProxy(I18nErrorCode & err)25 sptr<II18nServiceAbility> I18nServiceAbilityClient::GetProxy(I18nErrorCode &err)
26 {
27 sptr<IRemoteObject> proxy = I18nServiceAbilityLoadManager::GetInstance()->GetI18nServiceAbility(I18N_SA_ID);
28 if (proxy == nullptr) {
29 HILOG_ERROR_I18N("I18nServiceAbilityClient::GetProxy load sa failed, try again.");
30 proxy = I18nServiceAbilityLoadManager::GetInstance()->GetI18nServiceAbility(I18N_SA_ID);
31 }
32 if (proxy == nullptr) {
33 HILOG_ERROR_I18N("I18nServiceAbilityClient::GetProxy load sa failed.");
34 err = I18nErrorCode::LOAD_SA_FAILED;
35 return nullptr;
36 }
37 return iface_cast<II18nServiceAbility>(proxy);
38 }
39
SetSystemLanguage(const std::string & language)40 I18nErrorCode I18nServiceAbilityClient::SetSystemLanguage(const std::string &language)
41 {
42 I18nErrorCode err = I18nErrorCode::SUCCESS;
43 sptr<II18nServiceAbility> i18nServiceAbilityObj = GetProxy(err);
44 if (i18nServiceAbilityObj == nullptr) {
45 return err;
46 }
47 return i18nServiceAbilityObj->SetSystemLanguage(language);
48 }
49
SetSystemRegion(const std::string & region)50 I18nErrorCode I18nServiceAbilityClient::SetSystemRegion(const std::string ®ion)
51 {
52 I18nErrorCode err = I18nErrorCode::SUCCESS;
53 sptr<II18nServiceAbility> i18nServiceAbilityObj = GetProxy(err);
54 if (i18nServiceAbilityObj == nullptr) {
55 return err;
56 }
57 return i18nServiceAbilityObj->SetSystemRegion(region);
58 }
59
SetSystemLocale(const std::string & locale)60 I18nErrorCode I18nServiceAbilityClient::SetSystemLocale(const std::string &locale)
61 {
62 I18nErrorCode err = I18nErrorCode::SUCCESS;
63 sptr<II18nServiceAbility> i18nServiceAbilityObj = GetProxy(err);
64 if (i18nServiceAbilityObj == nullptr) {
65 return err;
66 }
67 return i18nServiceAbilityObj->SetSystemLocale(locale);
68 }
69
Set24HourClock(const std::string & flag)70 I18nErrorCode I18nServiceAbilityClient::Set24HourClock(const std::string &flag)
71 {
72 I18nErrorCode err = I18nErrorCode::SUCCESS;
73 sptr<II18nServiceAbility> i18nServiceAbilityObj = GetProxy(err);
74 if (i18nServiceAbilityObj == nullptr) {
75 return err;
76 }
77 return i18nServiceAbilityObj->Set24HourClock(flag);
78 }
79
SetUsingLocalDigit(bool flag)80 I18nErrorCode I18nServiceAbilityClient::SetUsingLocalDigit(bool flag)
81 {
82 I18nErrorCode err = I18nErrorCode::SUCCESS;
83 sptr<II18nServiceAbility> i18nServiceAbilityObj = GetProxy(err);
84 if (i18nServiceAbilityObj == nullptr) {
85 return err;
86 }
87 return i18nServiceAbilityObj->SetUsingLocalDigit(flag);
88 }
89
AddPreferredLanguage(const std::string & language,int32_t index)90 I18nErrorCode I18nServiceAbilityClient::AddPreferredLanguage(const std::string &language, int32_t index)
91 {
92 I18nErrorCode err = I18nErrorCode::SUCCESS;
93 sptr<II18nServiceAbility> i18nServiceAbilityObj = GetProxy(err);
94 if (i18nServiceAbilityObj == nullptr) {
95 return err;
96 }
97 return i18nServiceAbilityObj->AddPreferredLanguage(language, index);
98 }
99
RemovePreferredLanguage(int32_t index)100 I18nErrorCode I18nServiceAbilityClient::RemovePreferredLanguage(int32_t index)
101 {
102 I18nErrorCode err = I18nErrorCode::SUCCESS;
103 sptr<II18nServiceAbility> i18nServiceAbilityObj = GetProxy(err);
104 if (i18nServiceAbilityObj == nullptr) {
105 return err;
106 }
107 return i18nServiceAbilityObj->RemovePreferredLanguage(index);
108 }
109
VerifyLibphonenumberUpdate()110 std::pair<int, int> I18nServiceAbilityClient::VerifyLibphonenumberUpdate()
111 {
112 I18nErrorCode err = I18nErrorCode::SUCCESS;
113 sptr<II18nServiceAbility> i18nServiceAbilityObj = GetProxy(err);
114 if (i18nServiceAbilityObj == nullptr) {
115 return {-1, -1};
116 }
117 return i18nServiceAbilityObj->VerifyLibphonenumberUpdate();
118 }
119
VerifyLibphonenumberUpdate(int * metadataFd,int * geocodingFd)120 extern "C" void VerifyLibphonenumberUpdate(int* metadataFd, int* geocodingFd)
121 {
122 std::pair<int, int> fds = I18nServiceAbilityClient::VerifyLibphonenumberUpdate();
123 *metadataFd = fds.first;
124 *geocodingFd = fds.second;
125 }
126 } // namespace I18n
127 } // namespace Global
128 } // namespace OHOS