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 "bundle_system_state.h"
17 
18 #include "nlohmann/json.hpp"
19 #include "json_util.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 const char* JSON_KEY_COLOR_MODE = "colorMode";
25 const char* JSON_KEY_LANGUAGE = "language";
26 }
27 
BundleSystemState()28 BundleSystemState::BundleSystemState()
29 {}
30 
~BundleSystemState()31 BundleSystemState::~BundleSystemState()
32 {}
33 
GetInstance()34 BundleSystemState &BundleSystemState::GetInstance()
35 {
36     static BundleSystemState bundleSystemState;
37     return bundleSystemState;
38 }
39 
SetSystemLanguage(const std::string & language)40 void BundleSystemState::SetSystemLanguage(const std::string &language)
41 {
42     std::unique_lock<std::shared_mutex> stateLock(stateMutex_);
43     language_ = language;
44 }
45 
GetSystemLanguage()46 std::string BundleSystemState::GetSystemLanguage()
47 {
48     std::shared_lock<std::shared_mutex> stateLock(stateMutex_);
49     return language_;
50 }
51 
SetSystemColorMode(const std::string & colorMode)52 void BundleSystemState::SetSystemColorMode(const std::string &colorMode)
53 {
54     std::unique_lock<std::shared_mutex> stateLock(stateMutex_);
55     colorMode_ = colorMode;
56 }
57 
GetSystemColorMode()58 std::string BundleSystemState::GetSystemColorMode()
59 {
60     std::shared_lock<std::shared_mutex> stateLock(stateMutex_);
61     return colorMode_;
62 }
63 
ToString()64 std::string BundleSystemState::ToString()
65 {
66     std::shared_lock<std::shared_mutex> stateLock(stateMutex_);
67     nlohmann::json jsonObject = nlohmann::json {
68         {JSON_KEY_COLOR_MODE, colorMode_},
69         {JSON_KEY_LANGUAGE, language_}
70     };
71     return jsonObject.dump();
72 }
73 
FromString(const std::string & systemState)74 bool BundleSystemState::FromString(const std::string &systemState)
75 {
76     nlohmann::json jsonObject = nlohmann::json::parse(systemState, nullptr, false);
77     if (jsonObject.is_discarded()) {
78         APP_LOGE("failed parse SystemState: %{public}s", systemState.c_str());
79         return false;
80     }
81     const auto &jsonObjectEnd = jsonObject.end();
82     int32_t parseResult = ERR_OK;
83     std::unique_lock<std::shared_mutex> stateLock(stateMutex_);
84     GetValueIfFindKey<std::string>(jsonObject,
85         jsonObjectEnd,
86         JSON_KEY_COLOR_MODE,
87         colorMode_,
88         JsonType::STRING,
89         false,
90         parseResult,
91         ArrayType::NOT_ARRAY);
92     GetValueIfFindKey<std::string>(jsonObject,
93         jsonObjectEnd,
94         JSON_KEY_LANGUAGE,
95         language_,
96         JsonType::STRING,
97         false,
98         parseResult,
99         ArrayType::NOT_ARRAY);
100     if (parseResult != ERR_OK) {
101         APP_LOGE("read systemState jsonObject error : %{public}d", parseResult);
102         return false;
103     }
104     return true;
105 }
106 } // AppExecFwk
107 } // OHOS
108