1e0857b17Sopenharmony_ci/*
2e0857b17Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3e0857b17Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0857b17Sopenharmony_ci * you may not use this file except in compliance with the License.
5e0857b17Sopenharmony_ci * You may obtain a copy of the License at
6e0857b17Sopenharmony_ci *
7e0857b17Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0857b17Sopenharmony_ci *
9e0857b17Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0857b17Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0857b17Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0857b17Sopenharmony_ci * See the License for the specific language governing permissions and
13e0857b17Sopenharmony_ci * limitations under the License.
14e0857b17Sopenharmony_ci */
15e0857b17Sopenharmony_ci
16e0857b17Sopenharmony_ci#ifndef OHOS_ABILITY_BASE_CONFIGURATION_H
17e0857b17Sopenharmony_ci#define OHOS_ABILITY_BASE_CONFIGURATION_H
18e0857b17Sopenharmony_ci
19e0857b17Sopenharmony_ci#include <mutex>
20e0857b17Sopenharmony_ci#include <set>
21e0857b17Sopenharmony_ci#include <string>
22e0857b17Sopenharmony_ci#include <unordered_map>
23e0857b17Sopenharmony_ci#include <vector>
24e0857b17Sopenharmony_ci
25e0857b17Sopenharmony_ci#include "parcel.h"
26e0857b17Sopenharmony_ci#include "global_configuration_key.h"
27e0857b17Sopenharmony_ci
28e0857b17Sopenharmony_cinamespace OHOS {
29e0857b17Sopenharmony_cinamespace AppExecFwk {
30e0857b17Sopenharmony_cinamespace ConfigurationInner {
31e0857b17Sopenharmony_ci    constexpr const char* CONNECTION_SYMBOL = "#";
32e0857b17Sopenharmony_ci    constexpr const char* EMPTY_STRING = "";
33e0857b17Sopenharmony_ci    constexpr const char* APPLICATION_DIRECTION = "ohos.application.direction";
34e0857b17Sopenharmony_ci    constexpr const char* APPLICATION_DENSITYDPI = "ohos.application.densitydpi";
35e0857b17Sopenharmony_ci    constexpr const char* APPLICATION_DISPLAYID = "ohos.application.displayid";
36e0857b17Sopenharmony_ci    constexpr const char* APPLICATION_FONT = "ohos.application.font";
37e0857b17Sopenharmony_ci
38e0857b17Sopenharmony_ci    constexpr const char* COLOR_MODE_LIGHT = "light";
39e0857b17Sopenharmony_ci    constexpr const char* COLOR_MODE_DARK = "dark";
40e0857b17Sopenharmony_ci    constexpr const char* COLOR_MODE_AUTO = "auto";
41e0857b17Sopenharmony_ci    constexpr const char* DEVICE_TYPE_DEFAULT = "default";
42e0857b17Sopenharmony_ci    constexpr const char* DIRECTION_VERTICAL = "vertical";
43e0857b17Sopenharmony_ci    constexpr const char* DIRECTION_HORIZONTAL = "horizontal";
44e0857b17Sopenharmony_ci    constexpr const char* IS_SET_BY_APP = "isSetByApp";
45e0857b17Sopenharmony_ci    constexpr const char* NEED_REMOVE_SET_BY_SA = "needRemoveSetBySa";
46e0857b17Sopenharmony_ci    constexpr const char* IS_SET_BY_SA = "isSetBySa";
47e0857b17Sopenharmony_ci    constexpr const char* IS_APP_FONT_FOLLOW_SYSTEM = "followSystem";
48e0857b17Sopenharmony_ci    constexpr const char* SYSTEM_DEFAULT_FONTSIZE_SCALE = "1.0";
49e0857b17Sopenharmony_ci};
50e0857b17Sopenharmony_ci
51e0857b17Sopenharmony_ciclass Configuration final: public Parcelable {
52e0857b17Sopenharmony_cipublic:
53e0857b17Sopenharmony_ci    Configuration();
54e0857b17Sopenharmony_ci
55e0857b17Sopenharmony_ci    Configuration(const Configuration &other);
56e0857b17Sopenharmony_ci
57e0857b17Sopenharmony_ci    Configuration& operator=(const Configuration &other);
58e0857b17Sopenharmony_ci
59e0857b17Sopenharmony_ci    ~Configuration();
60e0857b17Sopenharmony_ci
61e0857b17Sopenharmony_ci    /**
62e0857b17Sopenharmony_ci     * @brief Compare the difference between the current and the passed in object.
63e0857b17Sopenharmony_ci     *
64e0857b17Sopenharmony_ci     * @param diffKeyV Out Ginseng. get the current difference item keys.
65e0857b17Sopenharmony_ci     * @param other Comparisons obj
66e0857b17Sopenharmony_ci     *
67e0857b17Sopenharmony_ci     * @return void
68e0857b17Sopenharmony_ci     */
69e0857b17Sopenharmony_ci    void CompareDifferent(std::vector<std::string> &diffKeyV, const Configuration &other);
70e0857b17Sopenharmony_ci
71e0857b17Sopenharmony_ci    /**
72e0857b17Sopenharmony_ci     * @brief Update the content according to the key.
73e0857b17Sopenharmony_ci     *
74e0857b17Sopenharmony_ci     * @param mergeItemKey The key of the element currently to be updated.
75e0857b17Sopenharmony_ci     * @param other Provide updated content obj
76e0857b17Sopenharmony_ci     *
77e0857b17Sopenharmony_ci     * @return void
78e0857b17Sopenharmony_ci     */
79e0857b17Sopenharmony_ci    void Merge(const std::vector<std::string> &diffKeyV, const Configuration &other);
80e0857b17Sopenharmony_ci
81e0857b17Sopenharmony_ci    /**
82e0857b17Sopenharmony_ci     * @brief obtain the value according to the display number and storage key.
83e0857b17Sopenharmony_ci     *
84e0857b17Sopenharmony_ci     * @param displayId Currently displayed id.
85e0857b17Sopenharmony_ci     * @param key The key of the item to access configura. ej : key = GlobalConfigurationKey::SYSTEM_LANGUAGE
86e0857b17Sopenharmony_ci     * Means you want to change the language part
87e0857b17Sopenharmony_ci     * @param value Changed value
88e0857b17Sopenharmony_ci     * @return return true if the deposit is successful, otherwise return false
89e0857b17Sopenharmony_ci     */
90e0857b17Sopenharmony_ci    bool AddItem(int displayId, const std::string &key, const std::string &value);
91e0857b17Sopenharmony_ci
92e0857b17Sopenharmony_ci    /**
93e0857b17Sopenharmony_ci     * @brief obtain the value according to the display number and storage key.
94e0857b17Sopenharmony_ci     *
95e0857b17Sopenharmony_ci     * @param displayId Currently displayed id.
96e0857b17Sopenharmony_ci     * @param key The key of the item to access configura. ej : key = GlobalConfigurationKey::SYSTEM_LANGUAGE
97e0857b17Sopenharmony_ci     * Means you want to change the language part
98e0857b17Sopenharmony_ci     *
99e0857b17Sopenharmony_ci     * @return return empty string if not found | return val if found
100e0857b17Sopenharmony_ci     */
101e0857b17Sopenharmony_ci    std::string GetItem(int displayId, const std::string &key) const;
102e0857b17Sopenharmony_ci
103e0857b17Sopenharmony_ci    /**
104e0857b17Sopenharmony_ci     * @brief Delete element.
105e0857b17Sopenharmony_ci     *
106e0857b17Sopenharmony_ci     * @param displayId Currently displayed id.
107e0857b17Sopenharmony_ci     * @param key The key of the item to access configura. ej : key = GlobalConfigurationKey::SYSTEM_LANGUAGE
108e0857b17Sopenharmony_ci     * Means you want to change the language part
109e0857b17Sopenharmony_ci     *
110e0857b17Sopenharmony_ci     * @return Return an integer greater than 0 if the deletion succeeds, otherwise it returns 0.
111e0857b17Sopenharmony_ci     */
112e0857b17Sopenharmony_ci    int RemoveItem(int displayId, const std::string &key);
113e0857b17Sopenharmony_ci
114e0857b17Sopenharmony_ci    /**
115e0857b17Sopenharmony_ci     * @brief obtain the value according to the display number and storage key.
116e0857b17Sopenharmony_ci     *
117e0857b17Sopenharmony_ci     * @param key The key of the item to access configura. ej : key = GlobalConfigurationKey::SYSTEM_LANGUAGE
118e0857b17Sopenharmony_ci     * Means you want to change the language part
119e0857b17Sopenharmony_ci     * @param value Changed value
120e0857b17Sopenharmony_ci     * @return return true if the deposit is successful, otherwise return false
121e0857b17Sopenharmony_ci     */
122e0857b17Sopenharmony_ci    bool AddItem(const std::string &key, const std::string &value);
123e0857b17Sopenharmony_ci
124e0857b17Sopenharmony_ci    /**
125e0857b17Sopenharmony_ci     * @brief obtain the value according to the display number and storage key.
126e0857b17Sopenharmony_ci     *
127e0857b17Sopenharmony_ci     * @param key The key of the item to access configura. ej : key = GlobalConfigurationKey::SYSTEM_LANGUAGE
128e0857b17Sopenharmony_ci     * Means you want to change the language part
129e0857b17Sopenharmony_ci     *
130e0857b17Sopenharmony_ci     * @return return empty string if not found | return val if found
131e0857b17Sopenharmony_ci     */
132e0857b17Sopenharmony_ci    std::string GetItem(const std::string &key) const;
133e0857b17Sopenharmony_ci
134e0857b17Sopenharmony_ci    /**
135e0857b17Sopenharmony_ci     * @brief Delete element.
136e0857b17Sopenharmony_ci     *
137e0857b17Sopenharmony_ci     * @param key The key of the item to access configura. ej : key = GlobalConfigurationKey::SYSTEM_LANGUAGE
138e0857b17Sopenharmony_ci     * Means you want to change the language part
139e0857b17Sopenharmony_ci     *
140e0857b17Sopenharmony_ci     * @return Return an integer greater than 0 if the deletion succeeds, otherwise it returns 0.
141e0857b17Sopenharmony_ci     */
142e0857b17Sopenharmony_ci    int RemoveItem(const std::string &key);
143e0857b17Sopenharmony_ci
144e0857b17Sopenharmony_ci    /**
145e0857b17Sopenharmony_ci     * @brief Get the currently existing key-value pairs.
146e0857b17Sopenharmony_ci     *
147e0857b17Sopenharmony_ci     * @return return currently item size.
148e0857b17Sopenharmony_ci     */
149e0857b17Sopenharmony_ci    int GetItemSize() const;
150e0857b17Sopenharmony_ci
151e0857b17Sopenharmony_ci    /**
152e0857b17Sopenharmony_ci     * @brief Return all current key-value pairs.
153e0857b17Sopenharmony_ci     *
154e0857b17Sopenharmony_ci     */
155e0857b17Sopenharmony_ci    const std::string GetName() const;
156e0857b17Sopenharmony_ci
157e0857b17Sopenharmony_ci    /**
158e0857b17Sopenharmony_ci     * @brief read this Sequenceable object from a Parcel.
159e0857b17Sopenharmony_ci     *
160e0857b17Sopenharmony_ci     * @param inParcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
161e0857b17Sopenharmony_ci     * @return Returns true if read successed; returns false otherwise.
162e0857b17Sopenharmony_ci     */
163e0857b17Sopenharmony_ci    bool ReadFromParcel(Parcel &parcel);
164e0857b17Sopenharmony_ci
165e0857b17Sopenharmony_ci    /**
166e0857b17Sopenharmony_ci     * @brief Marshals this Sequenceable object into a Parcel.
167e0857b17Sopenharmony_ci     *
168e0857b17Sopenharmony_ci     * @param outParcel Indicates the Parcel object to which the Sequenceable object will be marshaled.
169e0857b17Sopenharmony_ci     */
170e0857b17Sopenharmony_ci    virtual bool Marshalling(Parcel &parcel) const override;
171e0857b17Sopenharmony_ci
172e0857b17Sopenharmony_ci    /**
173e0857b17Sopenharmony_ci     * @brief Unmarshals this Sequenceable object from a Parcel.
174e0857b17Sopenharmony_ci     *
175e0857b17Sopenharmony_ci     * @param inParcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
176e0857b17Sopenharmony_ci     */
177e0857b17Sopenharmony_ci    static Configuration *Unmarshalling(Parcel &parcel);
178e0857b17Sopenharmony_ci
179e0857b17Sopenharmony_ciprivate:
180e0857b17Sopenharmony_ci
181e0857b17Sopenharmony_ci    /**
182e0857b17Sopenharmony_ci     * @brief Make the key by id and param
183e0857b17Sopenharmony_ci     *
184e0857b17Sopenharmony_ci     * @param getKey Key made.
185e0857b17Sopenharmony_ci     * @param id displayId.
186e0857b17Sopenharmony_ci     * @param param The key of the item to access configura.
187e0857b17Sopenharmony_ci     *
188e0857b17Sopenharmony_ci     */
189e0857b17Sopenharmony_ci    bool MakeTheKey(std::string &getKey, int id, const std::string &param) const;
190e0857b17Sopenharmony_ci
191e0857b17Sopenharmony_ci    /**
192e0857b17Sopenharmony_ci     * @brief Get all current keys.
193e0857b17Sopenharmony_ci     *
194e0857b17Sopenharmony_ci     * @param keychain Out Ginseng. Contains all current keys.
195e0857b17Sopenharmony_ci     */
196e0857b17Sopenharmony_ci    void GetAllKey(std::vector<std::string> &keychain) const;
197e0857b17Sopenharmony_ci
198e0857b17Sopenharmony_ci    /**
199e0857b17Sopenharmony_ci     * @brief Get value by key.
200e0857b17Sopenharmony_ci     *
201e0857b17Sopenharmony_ci     * @param key the key to get value.
202e0857b17Sopenharmony_ci     */
203e0857b17Sopenharmony_ci    std::string GetValue(const std::string &key) const;
204e0857b17Sopenharmony_ci
205e0857b17Sopenharmony_ciprivate:
206e0857b17Sopenharmony_ci    int defaultDisplayId_ {0};
207e0857b17Sopenharmony_ci
208e0857b17Sopenharmony_ci    mutable std::recursive_mutex configParameterMutex_;
209e0857b17Sopenharmony_ci    std::unordered_map<std::string, std::string> configParameter_;
210e0857b17Sopenharmony_ci};
211e0857b17Sopenharmony_ci}  // namespace AppExecFwk
212e0857b17Sopenharmony_ci}  // namespace OHOS
213e0857b17Sopenharmony_ci#endif  // OHOS_ABILITY_BASE_CONFIGURATION_H
214