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