1 /*
2 * Copyright (c) 2021-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 "context_deal.h"
17
18 #include <regex>
19
20 #include "constants.h"
21 #include "ability_manager_client.h"
22 #include "ability_manager_interface.h"
23 #include "app_context.h"
24 #include "directory_ex.h"
25 #include "file_ex.h"
26 #include "hilog_wrapper.h"
27 #include "iservice_registry.h"
28 #include "os_account_manager_wrapper.h"
29 #include "sys_mgr_client.h"
30 #include "system_ability_definition.h"
31
32 #define MODE 0771
33 namespace OHOS {
34 namespace AppExecFwk {
35 using namespace OHOS::AbilityBase::Constants;
36
37 const std::string ContextDeal::CONTEXT_DEAL_FILE_SEPARATOR("/");
38 const std::string ContextDeal::CONTEXT_DEAL_Files("files");
39 const int64_t ContextDeal::CONTEXT_CREATE_BY_SYSTEM_APP(0x00000001);
40 const std::string ContextDeal::CONTEXT_DATA_STORAGE("/data/storage/");
41 const std::string ContextDeal::CONTEXT_DEAL_DATA_APP("/data/app/");
42 const std::string ContextDeal::CONTEXT_DEAL_BASE("base");
43 const std::string ContextDeal::CONTEXT_DEAL_DATABASE("database");
44 const std::string ContextDeal::CONTEXT_DEAL_PREFERENCES("preferences");
45 const std::string ContextDeal::CONTEXT_DEAL_DATA("data");
46
ContextDeal(bool isCreateBySystemApp)47 ContextDeal::ContextDeal(bool isCreateBySystemApp) : isCreateBySystemApp_(isCreateBySystemApp)
48 {}
49
GetApplicationInfo() const50 std::shared_ptr<ApplicationInfo> ContextDeal::GetApplicationInfo() const
51 {
52 return applicationInfo_;
53 }
54
SetApplicationInfo(const std::shared_ptr<ApplicationInfo> & info)55 void ContextDeal::SetApplicationInfo(const std::shared_ptr<ApplicationInfo> &info)
56 {
57 if (info == nullptr) {
58 HILOG_ERROR("SetApplicationInfo failed, info is empty");
59 return;
60 }
61 applicationInfo_ = info;
62 }
63
GetApplicationContext() const64 std::shared_ptr<Context> ContextDeal::GetApplicationContext() const
65 {
66 return appContext_;
67 }
68
SetApplicationContext(const std::shared_ptr<Context> & context)69 void ContextDeal::SetApplicationContext(const std::shared_ptr<Context> &context)
70 {
71 if (context == nullptr) {
72 HILOG_ERROR("SetApplicationContext failed, context is empty");
73 return;
74 }
75 appContext_ = context;
76 }
77
GetBundleCodePath()78 std::string ContextDeal::GetBundleCodePath()
79 {
80 if (applicationInfo_ == nullptr) {
81 return "";
82 }
83
84 std::string dir;
85 if (isCreateBySystemApp_) {
86 dir = std::regex_replace(applicationInfo_->codePath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
87 } else {
88 dir = LOCAL_CODE_PATH;
89 }
90
91 return dir;
92 }
93
SetBundleCodePath(std::string & path)94 void ContextDeal::SetBundleCodePath(std::string &path)
95 {
96 path_ = path;
97 }
98
GetAbilityInfo()99 const std::shared_ptr<AbilityInfo> ContextDeal::GetAbilityInfo()
100 {
101 return abilityInfo_;
102 }
103
SetAbilityInfo(const std::shared_ptr<AbilityInfo> & info)104 void ContextDeal::SetAbilityInfo(const std::shared_ptr<AbilityInfo> &info)
105 {
106 if (info == nullptr) {
107 HILOG_ERROR("SetAbilityInfo failed, info is empty");
108 return;
109 }
110 abilityInfo_ = info;
111 }
112
GetContext()113 std::shared_ptr<Context> ContextDeal::GetContext()
114 {
115 return abilityContext_;
116 }
117
SetContext(const std::shared_ptr<Context> & context)118 void ContextDeal::SetContext(const std::shared_ptr<Context> &context)
119 {
120 if (context == nullptr) {
121 HILOG_ERROR("SetContext failed, context is empty");
122 return;
123 }
124 abilityContext_ = context;
125 }
126
GetBundleManager() const127 sptr<IBundleMgr> ContextDeal::GetBundleManager() const
128 {
129 auto bundleObj =
130 OHOS::DelayedSingleton<SysMrgClient>::GetInstance()->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
131 if (bundleObj == nullptr) {
132 HILOG_ERROR("failed to get bundle manager service");
133 return nullptr;
134 }
135 sptr<IBundleMgr> bms = iface_cast<IBundleMgr>(bundleObj);
136 return bms;
137 }
138
GetResourceManager() const139 std::shared_ptr<Global::Resource::ResourceManager> ContextDeal::GetResourceManager() const
140 {
141 return resourceManager_;
142 }
143
GetDatabaseDir()144 std::string ContextDeal::GetDatabaseDir()
145 {
146 std::string dir;
147 if (IsCreateBySystemApp()) {
148 dir = CONTEXT_DEAL_DATA_APP + currArea_ + CONTEXT_DEAL_FILE_SEPARATOR + std::to_string(GetCurrentAccountId())
149 + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_DATABASE + CONTEXT_DEAL_FILE_SEPARATOR + GetBundleName();
150 } else {
151 dir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_DATABASE;
152 }
153 CreateDirIfNotExist(dir);
154 HILOG_DEBUG("GetDatabaseDir:%{public}s", dir.c_str());
155 return dir;
156 }
157
GetDataDir()158 std::string ContextDeal::GetDataDir()
159 {
160 std::string dir = GetBaseDir() + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_DATA;
161 CreateDirIfNotExist(dir);
162 HILOG_DEBUG("GetDataDir dir = %{public}s", dir.c_str());
163 return dir;
164 }
165
GetDir(const std::string & name,int mode)166 std::string ContextDeal::GetDir(const std::string &name, int mode)
167 {
168 if (applicationInfo_ == nullptr) {
169 HILOG_ERROR("GetDir failed, applicationInfo_ == nullptr");
170 return "";
171 }
172 std::string dir = applicationInfo_->dataDir + CONTEXT_DEAL_FILE_SEPARATOR + name;
173 if (!OHOS::FileExists(dir)) {
174 HILOG_INFO("GetDir File is not exits");
175 OHOS::ForceCreateDirectory(dir);
176 OHOS::ChangeModeDirectory(dir, mode);
177 }
178 return dir;
179 }
180
GetFilesDir()181 std::string ContextDeal::GetFilesDir()
182 {
183 std::string dir = GetBaseDir() + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_Files;
184 CreateDirIfNotExist(dir);
185 HILOG_DEBUG("GetFilesDir dir = %{public}s", dir.c_str());
186 return dir;
187 }
188
GetBundleName() const189 std::string ContextDeal::GetBundleName() const
190 {
191 return (applicationInfo_ != nullptr) ? applicationInfo_->bundleName : "";
192 }
193
GetBundleResourcePath()194 std::string ContextDeal::GetBundleResourcePath()
195 {
196 if (abilityInfo_ == nullptr) {
197 return "";
198 }
199
200 std::string dir;
201 if (isCreateBySystemApp_) {
202 dir = std::regex_replace(abilityInfo_->resourcePath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
203 } else {
204 std::regex pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + abilityInfo_->bundleName);
205 dir = std::regex_replace(abilityInfo_->resourcePath, pattern, LOCAL_CODE_PATH);
206 }
207 return dir;
208 }
209
GetAbilityManager()210 sptr<AAFwk::IAbilityManager> ContextDeal::GetAbilityManager()
211 {
212 auto remoteObject = OHOS::DelayedSingleton<SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
213 if (remoteObject == nullptr) {
214 HILOG_ERROR("failed to get ability manager service");
215 return nullptr;
216 }
217 sptr<AAFwk::IAbilityManager> ams = iface_cast<AAFwk::IAbilityManager>(remoteObject);
218 return ams;
219 }
220
GetAppType()221 std::string ContextDeal::GetAppType()
222 {
223 sptr<IBundleMgr> ptr = GetBundleManager();
224 if (ptr == nullptr) {
225 HILOG_ERROR("GetAppType failed to get bundle manager service");
226 return "";
227 }
228 std::string retString = ptr->GetAppType(applicationInfo_->bundleName);
229 return retString;
230 }
231
IsCreateBySystemApp() const232 bool ContextDeal::IsCreateBySystemApp() const
233 {
234 return (static_cast<uint64_t>(flags_) & static_cast<uint64_t>(CONTEXT_CREATE_BY_SYSTEM_APP)) == 1;
235 }
236
GetCurrentAccountId() const237 int ContextDeal::GetCurrentAccountId() const
238 {
239 int userId = 0;
240 DelayedSingleton<OsAccountManagerWrapper>::GetInstance()->GetOsAccountLocalIdFromProcess(userId);
241 return userId;
242 }
243
CreateDirIfNotExist(const std::string & dirPath) const244 void ContextDeal::CreateDirIfNotExist(const std::string &dirPath) const
245 {
246 if (!OHOS::FileExists(dirPath)) {
247 HILOG_DEBUG("CreateDirIfNotExist File is not exits");
248 bool createDir = OHOS::ForceCreateDirectory(dirPath);
249 if (!createDir) {
250 HILOG_INFO("CreateDirIfNotExist: create dir %{public}s failed.", dirPath.c_str());
251 return;
252 }
253 }
254 }
255
SetPattern(int patternId)256 void ContextDeal::SetPattern(int patternId)
257 {
258 if (resourceManager_ != nullptr) {
259 if (!pattern_.empty()) {
260 pattern_.clear();
261 }
262 OHOS::Global::Resource::RState errval = resourceManager_->GetPatternById(patternId, pattern_);
263 if (errval != OHOS::Global::Resource::RState::SUCCESS) {
264 HILOG_ERROR("SetPattern GetPatternById(patternId:%d) retval is %u", patternId, errval);
265 }
266 } else {
267 HILOG_ERROR("SetPattern resourceManager_ is nullptr");
268 }
269 }
270
GetHapModuleInfo()271 std::shared_ptr<HapModuleInfo> ContextDeal::GetHapModuleInfo()
272 {
273 // fix set HapModuleInfoLocal data failed, request only once
274 if (hapModuleInfoLocal_ == nullptr) {
275 HapModuleInfoRequestInit();
276 if (hapModuleInfoLocal_ == nullptr) {
277 HILOG_ERROR("hapModuleInfoLocal_ is nullptr");
278 return nullptr;
279 }
280 }
281 return hapModuleInfoLocal_;
282 }
283
initResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)284 void ContextDeal::initResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
285 {
286 resourceManager_ = resourceManager;
287 }
288
GetString(int resId)289 std::string ContextDeal::GetString(int resId)
290 {
291 if (resourceManager_ == nullptr) {
292 HILOG_ERROR("GetString resourceManager_ is nullptr");
293 return "";
294 }
295
296 std::string ret;
297 OHOS::Global::Resource::RState errval = resourceManager_->GetStringById(resId, ret);
298 if (errval == OHOS::Global::Resource::RState::SUCCESS) {
299 return ret;
300 } else {
301 HILOG_ERROR("GetString GetStringById(resId:%d) retval is %u", resId, errval);
302 return "";
303 }
304 }
305
GetStringArray(int resId)306 std::vector<std::string> ContextDeal::GetStringArray(int resId)
307 {
308 if (resourceManager_ == nullptr) {
309 HILOG_ERROR("GetStringArray resourceManager_ is nullptr");
310 return std::vector<std::string>();
311 }
312
313 std::vector<std::string> retv;
314 OHOS::Global::Resource::RState errval = resourceManager_->GetStringArrayById(resId, retv);
315 if (errval == OHOS::Global::Resource::RState::SUCCESS) {
316 return retv;
317 } else {
318 HILOG_ERROR("GetStringArray GetStringArrayById(resId:%d) retval is %u", resId, errval);
319 return std::vector<std::string>();
320 }
321 }
322
GetIntArray(int resId)323 std::vector<int> ContextDeal::GetIntArray(int resId)
324 {
325 if (resourceManager_ == nullptr) {
326 HILOG_ERROR("GetIntArray resourceManager_ is nullptr");
327 return std::vector<int>();
328 }
329
330 std::vector<int> retv;
331 OHOS::Global::Resource::RState errval = resourceManager_->GetIntArrayById(resId, retv);
332 if (errval == OHOS::Global::Resource::RState::SUCCESS) {
333 return retv;
334 } else {
335 HILOG_ERROR("GetIntArray GetIntArrayById(resId:%d) retval is %u", resId, errval);
336 return std::vector<int>();
337 }
338 }
339
GetTheme()340 std::map<std::string, std::string> ContextDeal::GetTheme()
341 {
342 if (theme_.empty()) {
343 SetTheme(GetThemeId());
344 }
345 return theme_;
346 }
347
SetTheme(int themeId)348 void ContextDeal::SetTheme(int themeId)
349 {
350 if (resourceManager_ == nullptr) {
351 HILOG_ERROR("SetTheme resourceManager_ is nullptr");
352 return;
353 }
354
355 auto hapModInfo = GetHapModuleInfo();
356 if (hapModInfo == nullptr) {
357 HILOG_ERROR("SetTheme hapModInfo is nullptr");
358 return;
359 }
360
361 if (!theme_.empty()) {
362 theme_.clear();
363 }
364 OHOS::Global::Resource::RState errval = resourceManager_->GetThemeById(themeId, theme_);
365 if (errval != OHOS::Global::Resource::RState::SUCCESS) {
366 HILOG_ERROR("SetTheme GetThemeById(themeId:%d) retval is %u", themeId, errval);
367 }
368 }
369
GetPattern()370 std::map<std::string, std::string> ContextDeal::GetPattern()
371 {
372 if (!pattern_.empty()) {
373 return pattern_;
374 } else {
375 HILOG_ERROR("GetPattern pattern_ is empty");
376 return std::map<std::string, std::string>();
377 }
378 }
379
GetColor(int resId)380 int ContextDeal::GetColor(int resId)
381 {
382 if (resourceManager_ == nullptr) {
383 HILOG_ERROR("GetColor resourceManager_ is nullptr");
384 return INVALID_RESOURCE_VALUE;
385 }
386
387 uint32_t ret = INVALID_RESOURCE_VALUE;
388 OHOS::Global::Resource::RState errval = resourceManager_->GetColorById(resId, ret);
389 if (errval == OHOS::Global::Resource::RState::SUCCESS) {
390 return ret;
391 } else {
392 HILOG_ERROR("GetColor GetColorById(resId:%d) retval is %u", resId, errval);
393 return INVALID_RESOURCE_VALUE;
394 }
395 }
396
GetThemeId()397 int ContextDeal::GetThemeId()
398 {
399 auto hapModInfo = GetHapModuleInfo();
400 if (hapModInfo != nullptr) {
401 return -1;
402 } else {
403 HILOG_ERROR("GetThemeId hapModInfo is nullptr");
404 return -1;
405 }
406 }
407
GetDisplayOrientation()408 int ContextDeal::GetDisplayOrientation()
409 {
410 if (abilityInfo_ != nullptr) {
411 HILOG_DEBUG("GetDisplayOrientation end");
412 return static_cast<int>(abilityInfo_->orientation);
413 } else {
414 HILOG_ERROR("GetDisplayOrientation abilityInfo_ is nullptr");
415 return static_cast<int>(DisplayOrientation::UNSPECIFIED);
416 }
417 }
418
GetPreferencesDir()419 std::string ContextDeal::GetPreferencesDir()
420 {
421 std::string dir = GetBaseDir() + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_PREFERENCES;
422 CreateDirIfNotExist(dir);
423 HILOG_DEBUG("GetPreferencesDir:%{public}s", dir.c_str());
424 return dir;
425 }
426
SetColorMode(int mode)427 void ContextDeal::SetColorMode(int mode)
428 {
429 auto hapModInfo = GetHapModuleInfo();
430 if (hapModInfo == nullptr) {
431 HILOG_ERROR("SetColorMode hapModInfo is nullptr");
432 return;
433 }
434
435 if (mode == static_cast<int>(ModuleColorMode::DARK)) {
436 hapModInfo->colorMode = ModuleColorMode::DARK;
437 } else if (mode == static_cast<int>(ModuleColorMode::LIGHT)) {
438 hapModInfo->colorMode = ModuleColorMode::LIGHT;
439 } else { // default use AUTO
440 hapModInfo->colorMode = ModuleColorMode::AUTO;
441 }
442 }
443
GetColorMode()444 int ContextDeal::GetColorMode()
445 {
446 auto hapModInfo = GetHapModuleInfo();
447 if (hapModInfo == nullptr) {
448 HILOG_ERROR("GetColorMode hapModInfo is nullptr");
449 return -1;
450 }
451 return static_cast<int>(hapModInfo->colorMode);
452 }
453
454
HapModuleInfoRequestInit()455 bool ContextDeal::HapModuleInfoRequestInit()
456 {
457 sptr<IBundleMgr> ptr = GetBundleManager();
458 if (ptr == nullptr) {
459 HILOG_ERROR("GetHapModuleInfo failed to get bundle manager service");
460 return false;
461 }
462
463 if (abilityInfo_ == nullptr) {
464 HILOG_ERROR("GetHapModuleInfo failed for abilityInfo_ is nullptr");
465 return false;
466 }
467
468 hapModuleInfoLocal_ = std::make_shared<HapModuleInfo>();
469 if (!ptr->GetHapModuleInfo(*abilityInfo_.get(), *hapModuleInfoLocal_)) {
470 HILOG_ERROR("IBundleMgr::GetHapModuleInfo failed, will retval false value");
471 return false;
472 }
473 return true;
474 }
475
GetBaseDir() const476 std::string ContextDeal::GetBaseDir() const
477 {
478 std::string baseDir;
479 if (IsCreateBySystemApp()) {
480 baseDir = CONTEXT_DEAL_DATA_APP + currArea_ + CONTEXT_DEAL_FILE_SEPARATOR +
481 std::to_string(GetCurrentAccountId()) + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_BASE +
482 CONTEXT_DEAL_FILE_SEPARATOR + GetBundleName();
483 } else {
484 baseDir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_BASE;
485 }
486
487 HILOG_DEBUG("GetBaseDir:%{public}s", baseDir.c_str());
488 return baseDir;
489 }
490 } // namespace AppExecFwk
491 } // namespace OHOS
492