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 "message_parcel.h"
18#include "i18n_service_ability_proxy.h"
19
20namespace OHOS {
21namespace Global {
22namespace I18n {
23static const std::u16string DESCRIPTOR = u"OHOS.global.II18nServiceAbility";
24
25I18nServiceAbilityProxy::I18nServiceAbilityProxy(const sptr<IRemoteObject> &impl)
26    : IRemoteProxy<II18nServiceAbility>(impl)
27{
28}
29
30I18nErrorCode I18nServiceAbilityProxy::SetSystemLanguage(const std::string &language)
31{
32    MessageParcel data;
33    MessageParcel reply;
34    MessageOption option;
35    data.WriteInterfaceToken(DESCRIPTOR);
36    data.WriteString(language);
37    Remote()->SendRequest(static_cast<uint32_t>(ILocaleConfigAbilityCode::SET_SYSTEM_LANGUAGE), data,
38        reply, option);
39    return ProcessReply(reply.ReadInt32());
40}
41
42I18nErrorCode I18nServiceAbilityProxy::SetSystemRegion(const std::string &region)
43{
44    MessageParcel data;
45    MessageParcel reply;
46    MessageOption option;
47    data.WriteInterfaceToken(DESCRIPTOR);
48    data.WriteString(region);
49    Remote()->SendRequest(static_cast<uint32_t>(ILocaleConfigAbilityCode::SET_SYSTEM_REGION), data,
50        reply, option);
51    return ProcessReply(reply.ReadInt32());
52}
53
54I18nErrorCode I18nServiceAbilityProxy::SetSystemLocale(const std::string &locale)
55{
56    MessageParcel data;
57    MessageParcel reply;
58    MessageOption option;
59    data.WriteInterfaceToken(DESCRIPTOR);
60    data.WriteString(locale);
61    Remote()->SendRequest(static_cast<uint32_t>(ILocaleConfigAbilityCode::SET_SYSTEM_LOCALE), data,
62        reply, option);
63    return ProcessReply(reply.ReadInt32());
64}
65
66I18nErrorCode I18nServiceAbilityProxy::Set24HourClock(const std::string &flag)
67{
68    MessageParcel data;
69    MessageParcel reply;
70    MessageOption option;
71    data.WriteInterfaceToken(DESCRIPTOR);
72    data.WriteString(flag);
73    Remote()->SendRequest(static_cast<uint32_t>(ILocaleConfigAbilityCode::SET_24_HOUR_CLOCK), data,
74        reply, option);
75    return ProcessReply(reply.ReadInt32());
76}
77
78I18nErrorCode I18nServiceAbilityProxy::SetUsingLocalDigit(bool flag)
79{
80    MessageParcel data;
81    MessageParcel reply;
82    MessageOption option;
83    data.WriteInterfaceToken(DESCRIPTOR);
84    data.WriteBool(flag);
85    Remote()->SendRequest(static_cast<uint32_t>(ILocaleConfigAbilityCode::SET_USING_LOCAL_DIGIT), data,
86        reply, option);
87    return ProcessReply(reply.ReadInt32());
88}
89
90I18nErrorCode I18nServiceAbilityProxy::AddPreferredLanguage(const std::string &language, int32_t index)
91{
92    MessageParcel data;
93    MessageParcel reply;
94    MessageOption option;
95    data.WriteInterfaceToken(DESCRIPTOR);
96    data.WriteString(language);
97    data.WriteInt32(index);
98    Remote()->SendRequest(static_cast<uint32_t>(ILocaleConfigAbilityCode::ADD_PREFERRED_LANGUAGE), data,
99        reply, option);
100    return ProcessReply(reply.ReadInt32());
101}
102
103I18nErrorCode I18nServiceAbilityProxy::RemovePreferredLanguage(int32_t index)
104{
105    MessageParcel data;
106    MessageParcel reply;
107    MessageOption option;
108    data.WriteInterfaceToken(DESCRIPTOR);
109    data.WriteInt32(index);
110    Remote()->SendRequest(static_cast<uint32_t>(ILocaleConfigAbilityCode::REMOVE_PREFERRED_LANGUAGE),
111        data, reply, option);
112    return ProcessReply(reply.ReadInt32());
113}
114
115I18nErrorCode I18nServiceAbilityProxy::ProcessReply(int32_t reply)
116{
117    I18nErrorCode err = static_cast<I18nErrorCode>(reply);
118    if (err != I18nErrorCode::SUCCESS) {
119        HILOG_ERROR_I18N(
120            "I18nServiceAbilityProxy::ProcessReply failed with errorcode=%{public}d", reply);
121    }
122    return err;
123}
124} // namespace I18n
125} // namespace Global
126} // namespace OHOS