1/*
2 * Copyright (c) 2022 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 "ui_appearance_ability_stub.h"
17
18#include <string>
19#include "ui_appearance_ipc_interface_code.h"
20#include "ui_appearance_log.h"
21
22namespace OHOS {
23namespace ArkUi::UiAppearance {
24int32_t UiAppearanceAbilityStub::OnRemoteRequest(
25    uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
26{
27    bool ret = false;
28    std::u16string interfaceToken = data.ReadInterfaceToken();
29    if (interfaceToken != GetDescriptor()) {
30        LOGE("error permission denied.");
31        return -1;
32    }
33    switch (code) {
34        case static_cast<uint32_t>(UiAppearanceInterfaceCode::SET_DARK_MODE): {
35            UiAppearanceAbilityInterface::DarkMode mode =
36                static_cast<UiAppearanceAbilityInterface::DarkMode>(data.ReadInt32());
37            ret = reply.WriteInt32(SetDarkMode(mode));
38            return (ret ? 0 : -1);
39        }
40        case static_cast<uint32_t>(UiAppearanceInterfaceCode::GET_DARK_MODE): {
41            ret = reply.WriteInt32(GetDarkMode());
42            return (ret ? 0 : -1);
43        }
44        case static_cast<uint32_t>(UiAppearanceInterfaceCode::GET_FONT_SCALE): {
45            return OnGetFontScaleInner(data, reply, option);
46        }
47        case static_cast<uint32_t>(UiAppearanceInterfaceCode::SET_FONT_SCALE): {
48            return OnSetFontScaleInner(data, reply, option);
49        }
50        case static_cast<uint32_t>(UiAppearanceInterfaceCode::GET_FONT_Weight_SCALE): {
51            return OnGetFontWeightScaleInner(data, reply, option);
52        }
53        case static_cast<uint32_t>(UiAppearanceInterfaceCode::SET_FONT_Weight_SCALE): {
54            return OnSetFontWeightScaleInner(data, reply, option);
55        }
56        default:
57            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
58    }
59}
60
61int32_t UiAppearanceAbilityStub::OnGetFontScaleInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
62{
63    std::string fontScale;
64    auto state = GetFontScale(fontScale);
65    auto ret = reply.WriteInt32(state);
66    if (!ret) {
67        LOGE("ui_appearance GetFontScale write state fail");
68        return -1;
69    }
70    ret = reply.WriteString(fontScale);
71    if (!ret) {
72        LOGE("ui_appearance GetFontScale write fontScale fail");
73        return -1;
74    }
75    return 0;
76}
77
78int32_t UiAppearanceAbilityStub::OnSetFontScaleInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
79{
80    std::string fontScale;
81    auto ret = data.ReadString(fontScale);
82    if (!ret) {
83        LOGE("ui_appearance SetFontScale Read fontScale fail");
84        return -1;
85    }
86    ret = reply.WriteInt32(SetFontScale(fontScale));
87    return (ret ? 0 : -1);
88}
89
90int32_t UiAppearanceAbilityStub::OnGetFontWeightScaleInner(
91    MessageParcel& data, MessageParcel& reply, MessageOption& option)
92{
93    std::string fontWeightScale;
94    auto state = GetFontWeightScale(fontWeightScale);
95    auto ret = reply.WriteInt32(state);
96    if (!ret) {
97        LOGE("ui_appearance GetFontWeightScale write state fail");
98        return -1;
99    }
100    ret = reply.WriteString(fontWeightScale);
101    if (!ret) {
102        LOGE("ui_appearance GetFontWeightScale write fontScale fail");
103        return -1;
104    }
105    return 0;
106}
107
108int32_t UiAppearanceAbilityStub::OnSetFontWeightScaleInner(
109    MessageParcel& data, MessageParcel& reply, MessageOption& option)
110{
111    std::string fontWeightScale;
112    auto ret = data.ReadString(fontWeightScale);
113    if (!ret) {
114        LOGE("ui_appearance SetFontWeightScale Read fontScale fail");
115        return -1;
116    }
117    ret = reply.WriteInt32(SetFontWeightScale(fontWeightScale));
118    return (ret ? 0 : -1);
119}
120
121} // namespace ArkUi::UiAppearance
122} // namespace OHOS
123