1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci#include <vector>
16f857971dSopenharmony_ci
17f857971dSopenharmony_ci#include "devicestatus_common.h"
18f857971dSopenharmony_ci#include "devicestatus_define.h"
19f857971dSopenharmony_ci#include "devicestatus_errors.h"
20f857971dSopenharmony_ci#include "preview_style_packer.h"
21f857971dSopenharmony_ci
22f857971dSopenharmony_ci#undef LOG_TAG
23f857971dSopenharmony_ci#define LOG_TAG "PreviewStylePacker"
24f857971dSopenharmony_ci
25f857971dSopenharmony_cinamespace OHOS {
26f857971dSopenharmony_cinamespace Msdp {
27f857971dSopenharmony_cinamespace DeviceStatus {
28f857971dSopenharmony_ci
29f857971dSopenharmony_ciint32_t PreviewStylePacker::Marshalling(const PreviewStyle &previewStyle, Parcel &data)
30f857971dSopenharmony_ci{
31f857971dSopenharmony_ci    std::vector<int32_t> types;
32f857971dSopenharmony_ci    for (const auto &elem : previewStyle.types) {
33f857971dSopenharmony_ci        types.push_back(static_cast<int32_t>(elem));
34f857971dSopenharmony_ci    }
35f857971dSopenharmony_ci    WRITEINT32VECTOR(data, types, ERR_INVALID_VALUE);
36f857971dSopenharmony_ci    WRITEUINT32(data, previewStyle.foregroundColor, ERR_INVALID_VALUE);
37f857971dSopenharmony_ci    WRITEINT32(data, previewStyle.opacity, ERR_INVALID_VALUE);
38f857971dSopenharmony_ci    WRITEFLOAT(data, previewStyle.radius, ERR_INVALID_VALUE);
39f857971dSopenharmony_ci    WRITEFLOAT(data, previewStyle.scale, ERR_INVALID_VALUE);
40f857971dSopenharmony_ci    return RET_OK;
41f857971dSopenharmony_ci}
42f857971dSopenharmony_ci
43f857971dSopenharmony_ciint32_t PreviewStylePacker::UnMarshalling(Parcel &data, PreviewStyle &previewStyle)
44f857971dSopenharmony_ci{
45f857971dSopenharmony_ci    std::vector<int32_t> types;
46f857971dSopenharmony_ci    READINT32VECTOR(data, types, ERR_INVALID_VALUE);
47f857971dSopenharmony_ci    for (const auto &elem : types) {
48f857971dSopenharmony_ci        previewStyle.types.push_back(static_cast<PreviewType>(elem));
49f857971dSopenharmony_ci    }
50f857971dSopenharmony_ci    READUINT32(data, previewStyle.foregroundColor, ERR_INVALID_VALUE);
51f857971dSopenharmony_ci    READINT32(data, previewStyle.opacity, ERR_INVALID_VALUE);
52f857971dSopenharmony_ci    READFLOAT(data, previewStyle.radius, ERR_INVALID_VALUE);
53f857971dSopenharmony_ci    READFLOAT(data, previewStyle.scale, ERR_INVALID_VALUE);
54f857971dSopenharmony_ci    return RET_OK;
55f857971dSopenharmony_ci}
56f857971dSopenharmony_ci
57f857971dSopenharmony_ciint32_t PreviewAnimationPacker::Marshalling(const PreviewAnimation &previewAnimation, Parcel &data)
58f857971dSopenharmony_ci{
59f857971dSopenharmony_ci    WRITEINT32(data, previewAnimation.duration, ERR_INVALID_VALUE);
60f857971dSopenharmony_ci    WRITESTRING(data, previewAnimation.curveName, ERR_INVALID_VALUE);
61f857971dSopenharmony_ci    WRITEFLOATVECTOR(data, previewAnimation.curve, ERR_INVALID_VALUE);
62f857971dSopenharmony_ci    return RET_OK;
63f857971dSopenharmony_ci}
64f857971dSopenharmony_ci
65f857971dSopenharmony_ciint32_t PreviewAnimationPacker::UnMarshalling(Parcel &data, PreviewAnimation &previewAnimation)
66f857971dSopenharmony_ci{
67f857971dSopenharmony_ci    READINT32(data, previewAnimation.duration, ERR_INVALID_VALUE);
68f857971dSopenharmony_ci    if (previewAnimation.duration <= 0) {
69f857971dSopenharmony_ci        FI_HILOGE("Invalid paramater duration:%{public}d", previewAnimation.duration);
70f857971dSopenharmony_ci        return ERR_INVALID_VALUE;
71f857971dSopenharmony_ci    }
72f857971dSopenharmony_ci    if (previewAnimation.duration > MAX_ANIMATION_DURATION_MS) {
73f857971dSopenharmony_ci        FI_HILOGW("Duration:%{public}d too long, use default value", previewAnimation.duration);
74f857971dSopenharmony_ci        previewAnimation.duration = MAX_ANIMATION_DURATION_MS;
75f857971dSopenharmony_ci    }
76f857971dSopenharmony_ci    READSTRING(data, previewAnimation.curveName, ERR_INVALID_VALUE);
77f857971dSopenharmony_ci    READFLOATVECTOR(data, previewAnimation.curve, ERR_INVALID_VALUE);
78f857971dSopenharmony_ci    return RET_OK;
79f857971dSopenharmony_ci}
80f857971dSopenharmony_ci
81f857971dSopenharmony_ci} // namespace DeviceStatus
82f857971dSopenharmony_ci} // namespace Msdp
83f857971dSopenharmony_ci} // namespace OHOS