• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef LOG_TAG
16 #define LOG_TAG "ResourceManagerAdapter"
17 #endif
18 
19 #include "resource_manager_adapter.h"
20 
21 #include "locale_config.h"
22 #include "audio_service_log.h"
23 
24 namespace OHOS {
25 namespace AudioStandard {
26 using namespace Global::Resource;
27 
ResourceManagerAdapter()28 ResourceManagerAdapter::ResourceManagerAdapter()
29 {
30     AUDIO_INFO_LOG("construct");
31 }
32 
~ResourceManagerAdapter()33 ResourceManagerAdapter::~ResourceManagerAdapter()
34 {
35     AUDIO_INFO_LOG("destroy");
36 }
37 
GetInstance()38 ResourceManagerAdapter *ResourceManagerAdapter::GetInstance()
39 {
40     static ResourceManagerAdapter resourceManagerAdapter;
41     return &resourceManagerAdapter;
42 }
43 
44 /**
45  * InitResourceManager must be called in resourceManagerMutex_ lock scope
46  */
InitResourceManager()47 void ResourceManagerAdapter::InitResourceManager()
48 {
49     if (!resourceManager_) {
50         AUDIO_INFO_LOG("Init");
51         resourceManager_ = Global::Resource::GetSystemResourceManagerNoSandBox();
52     }
53     if (!resConfig_) {
54         resConfig_ = Global::Resource::CreateResConfig();
55     }
56     RefreshResConfig();
57 }
58 
RefreshResConfig()59 void ResourceManagerAdapter::RefreshResConfig()
60 {
61     std::string language = Global::I18n::LocaleConfig::GetSystemLanguage();
62     if (language.empty()) {
63         AUDIO_ERR_LOG("Get system language failed, skip RefreshResConfig");
64         return;
65     }
66     UErrorCode status = U_ZERO_ERROR;
67     icu::Locale locale = icu::Locale::forLanguageTag(language, status);
68     if (status != U_ZERO_ERROR || locale == nullptr) {
69         AUDIO_ERR_LOG("forLanguageTag failed, errCode:%{public}d", status);
70         return;
71     }
72     if (!resConfig_) {
73         AUDIO_ERR_LOG("resConfig_ is nullptr");
74         return;
75     }
76     resConfig_->SetLocaleInfo(locale.getLanguage(), locale.getScript(), locale.getCountry());
77     if (!resourceManager_) {
78         AUDIO_ERR_LOG("resourceManager_ is nullptr");
79         return;
80     }
81     resourceManager_->UpdateResConfig(*resConfig_);
82     AUDIO_INFO_LOG("Refresh success");
83 }
84 
ReleaseSystemResourceManager()85 void ResourceManagerAdapter::ReleaseSystemResourceManager()
86 {
87     std::lock_guard<std::mutex> lock(resourceManagerMutex_);
88     if (resourceManager_) {
89         Global::Resource::ReleaseSystemResourceManager();
90         resourceManager_ = nullptr;
91     }
92 
93     if (resConfig_) {
94         delete resConfig_;
95         resConfig_ = nullptr;
96     }
97     AUDIO_INFO_LOG("Release success");
98 }
99 
GetSystemStringByName(std::string name)100 std::string ResourceManagerAdapter::GetSystemStringByName(std::string name)
101 {
102     std::lock_guard<std::mutex> lock(resourceManagerMutex_);
103     InitResourceManager();
104 
105     std::string result;
106     if (!resourceManager_) {
107         AUDIO_ERR_LOG("resourceManager_ is nullptr");
108         return result;
109     }
110 
111     Global::Resource::RState rstate = resourceManager_->GetStringByName(name.c_str(), result);
112     AUDIO_INFO_LOG("name: %{public}s, rstate: %{public}d", name.c_str(), static_cast<int32_t>(rstate));
113     return result;
114 }
115 
GetMediaDataByName(std::string name,size_t & len,std::unique_ptr<uint8_t[]> & outValue,uint32_t density)116 Global::Resource::RState ResourceManagerAdapter::GetMediaDataByName(std::string name, size_t &len,
117     std::unique_ptr<uint8_t[]> &outValue, uint32_t density)
118 {
119     std::lock_guard<std::mutex> lock(resourceManagerMutex_);
120     InitResourceManager();
121 
122     if (!resourceManager_) {
123         AUDIO_ERR_LOG("resourceManager_ is nullptr");
124         return Global::Resource::RState::ERROR;
125     }
126 
127     Global::Resource::RState rstate = resourceManager_->GetMediaDataByName(name.c_str(), len, outValue);
128     AUDIO_INFO_LOG("rstate: %{public}d", static_cast<int32_t>(rstate));
129     return rstate;
130 }
131 
132 } // namespace AudioStandard
133 } // namespace OHOS
134