1 /*
2 * Copyright (c) 2021-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
16 #include "context_deal.h"
17
18 #include <regex>
19
20 #include "ability_manager_client.h"
21 #include "ability_manager_interface.h"
22 #include "app_context.h"
23 #include "bundle_mgr_helper.h"
24 #include "constants.h"
25 #include "directory_ex.h"
26 #include "file_ex.h"
27 #include "hilog_tag_wrapper.h"
28 #include "iservice_registry.h"
29 #include "os_account_manager_wrapper.h"
30 #include "sys_mgr_client.h"
31 #include "system_ability_definition.h"
32
33 #define MODE 0771
34 namespace OHOS {
35 namespace AppExecFwk {
36 using namespace OHOS::AbilityBase::Constants;
37
38 const std::string ContextDeal::CONTEXT_DEAL_FILE_SEPARATOR("/");
39 const std::string ContextDeal::CONTEXT_DEAL_Files("files");
40 const int64_t ContextDeal::CONTEXT_CREATE_BY_SYSTEM_APP(0x00000001);
41 const std::string ContextDeal::CONTEXT_DATA_STORAGE("/data/storage/");
42 const std::string ContextDeal::CONTEXT_DEAL_DATA_APP("/data/app/");
43 const std::string ContextDeal::CONTEXT_DEAL_BASE("base");
44 const std::string ContextDeal::CONTEXT_DEAL_DATABASE("database");
45 const std::string ContextDeal::CONTEXT_DEAL_PREFERENCES("preferences");
46 const std::string ContextDeal::CONTEXT_DEAL_DATA("data");
47
ContextDeal(bool isCreateBySystemApp)48 ContextDeal::ContextDeal(bool isCreateBySystemApp) : isCreateBySystemApp_(isCreateBySystemApp)
49 {}
50
GetApplicationInfo() const51 std::shared_ptr<ApplicationInfo> ContextDeal::GetApplicationInfo() const
52 {
53 return applicationInfo_;
54 }
55
SetApplicationInfo(const std::shared_ptr<ApplicationInfo> & info)56 void ContextDeal::SetApplicationInfo(const std::shared_ptr<ApplicationInfo> &info)
57 {
58 if (info == nullptr) {
59 TAG_LOGE(AAFwkTag::APPKIT, "null info");
60 return;
61 }
62 applicationInfo_ = info;
63 }
64
GetApplicationContext() const65 std::shared_ptr<Context> ContextDeal::GetApplicationContext() const
66 {
67 return appContext_;
68 }
69
SetApplicationContext(const std::shared_ptr<Context> & context)70 void ContextDeal::SetApplicationContext(const std::shared_ptr<Context> &context)
71 {
72 if (context == nullptr) {
73 TAG_LOGE(AAFwkTag::APPKIT, "null context");
74 return;
75 }
76 appContext_ = context;
77 }
78
GetBundleCodePath()79 std::string ContextDeal::GetBundleCodePath()
80 {
81 if (applicationInfo_ == nullptr) {
82 return "";
83 }
84
85 std::string dir;
86 if (isCreateBySystemApp_) {
87 dir = std::regex_replace(applicationInfo_->codePath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
88 } else {
89 dir = LOCAL_CODE_PATH;
90 }
91
92 return dir;
93 }
94
SetBundleCodePath(std::string & path)95 void ContextDeal::SetBundleCodePath(std::string &path)
96 {
97 path_ = path;
98 }
99
GetAbilityInfo()100 const std::shared_ptr<AbilityInfo> ContextDeal::GetAbilityInfo()
101 {
102 return abilityInfo_;
103 }
104
SetAbilityInfo(const std::shared_ptr<AbilityInfo> & info)105 void ContextDeal::SetAbilityInfo(const std::shared_ptr<AbilityInfo> &info)
106 {
107 if (info == nullptr) {
108 TAG_LOGE(AAFwkTag::APPKIT, "null info");
109 return;
110 }
111 abilityInfo_ = info;
112 }
113
GetContext()114 std::shared_ptr<Context> ContextDeal::GetContext()
115 {
116 return abilityContext_;
117 }
118
SetContext(const std::shared_ptr<Context> & context)119 void ContextDeal::SetContext(const std::shared_ptr<Context> &context)
120 {
121 if (context == nullptr) {
122 TAG_LOGE(AAFwkTag::APPKIT, "null context");
123 return;
124 }
125 abilityContext_ = context;
126 }
127
GetBundleManager() const128 std::shared_ptr<BundleMgrHelper> ContextDeal::GetBundleManager() const
129 {
130 auto bundleMgrHelper = DelayedSingleton<BundleMgrHelper>::GetInstance();
131 if (bundleMgrHelper == nullptr) {
132 TAG_LOGE(AAFwkTag::APPKIT, "null bundleMgrHelper");
133 return nullptr;
134 }
135 return bundleMgrHelper;
136 }
137
GetResourceManager() const138 std::shared_ptr<Global::Resource::ResourceManager> ContextDeal::GetResourceManager() const
139 {
140 return resourceManager_;
141 }
142
GetDatabaseDir()143 std::string ContextDeal::GetDatabaseDir()
144 {
145 std::string dir;
146 if (IsCreateBySystemApp()) {
147 dir = CONTEXT_DEAL_DATA_APP + currArea_ + CONTEXT_DEAL_FILE_SEPARATOR + std::to_string(GetCurrentAccountId())
148 + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_DATABASE + CONTEXT_DEAL_FILE_SEPARATOR + GetBundleName();
149 } else {
150 dir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_DATABASE;
151 }
152 CreateDirIfNotExist(dir);
153 TAG_LOGD(AAFwkTag::APPKIT, "GetDatabaseDir:%{public}s", dir.c_str());
154 return dir;
155 }
156
GetDataDir()157 std::string ContextDeal::GetDataDir()
158 {
159 std::string dir = GetBaseDir() + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_DATA;
160 CreateDirIfNotExist(dir);
161 TAG_LOGD(AAFwkTag::APPKIT, "GetDataDir dir = %{public}s", dir.c_str());
162 return dir;
163 }
164
GetDir(const std::string & name,int mode)165 std::string ContextDeal::GetDir(const std::string &name, int mode)
166 {
167 if (applicationInfo_ == nullptr) {
168 TAG_LOGE(AAFwkTag::APPKIT, "null applicationInfo_");
169 return "";
170 }
171 std::string dir = applicationInfo_->dataDir + CONTEXT_DEAL_FILE_SEPARATOR + name;
172 if (!OHOS::FileExists(dir)) {
173 TAG_LOGI(AAFwkTag::APPKIT, "GetDir File not exits");
174 OHOS::ForceCreateDirectory(dir);
175 OHOS::ChangeModeDirectory(dir, mode);
176 }
177 return dir;
178 }
179
GetFilesDir()180 std::string ContextDeal::GetFilesDir()
181 {
182 std::string dir = GetBaseDir() + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_Files;
183 CreateDirIfNotExist(dir);
184 TAG_LOGD(AAFwkTag::APPKIT, "GetFilesDir dir = %{public}s", dir.c_str());
185 return dir;
186 }
187
GetBundleName() const188 std::string ContextDeal::GetBundleName() const
189 {
190 return (applicationInfo_ != nullptr) ? applicationInfo_->bundleName : "";
191 }
192
GetBundleResourcePath()193 std::string ContextDeal::GetBundleResourcePath()
194 {
195 if (abilityInfo_ == nullptr) {
196 return "";
197 }
198
199 std::string dir;
200 if (isCreateBySystemApp_) {
201 dir = std::regex_replace(abilityInfo_->resourcePath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
202 } else {
203 std::regex pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + abilityInfo_->bundleName);
204 dir = std::regex_replace(abilityInfo_->resourcePath, pattern, LOCAL_CODE_PATH);
205 }
206 return dir;
207 }
208
GetAbilityManager()209 sptr<AAFwk::IAbilityManager> ContextDeal::GetAbilityManager()
210 {
211 auto remoteObject = OHOS::DelayedSingleton<SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
212 if (remoteObject == nullptr) {
213 TAG_LOGE(AAFwkTag::APPKIT, "null remoteObject");
214 return nullptr;
215 }
216 sptr<AAFwk::IAbilityManager> ams = iface_cast<AAFwk::IAbilityManager>(remoteObject);
217 return ams;
218 }
219
GetAppType()220 std::string ContextDeal::GetAppType()
221 {
222 auto ptr = GetBundleManager();
223 if (ptr == nullptr) {
224 TAG_LOGE(AAFwkTag::APPKIT, "null ptr");
225 return "";
226 }
227 std::string retString = ptr->GetAppType(applicationInfo_->bundleName);
228 return retString;
229 }
230
IsCreateBySystemApp() const231 bool ContextDeal::IsCreateBySystemApp() const
232 {
233 return (static_cast<uint64_t>(flags_) & static_cast<uint64_t>(CONTEXT_CREATE_BY_SYSTEM_APP)) == 1;
234 }
235
GetCurrentAccountId() const236 int ContextDeal::GetCurrentAccountId() const
237 {
238 int userId = 0;
239 DelayedSingleton<OsAccountManagerWrapper>::GetInstance()->GetOsAccountLocalIdFromProcess(userId);
240 TAG_LOGD(AAFwkTag::APPKIT, "userId: %{public}d", 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 TAG_LOGD(AAFwkTag::APPKIT, "CreateDirIfNotExist File is not exits");
248 bool createDir = OHOS::ForceCreateDirectory(dirPath);
249 if (!createDir) {
250 TAG_LOGI(AAFwkTag::APPKIT, "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 TAG_LOGE(AAFwkTag::APPKIT, "SetPattern GetPatternById(patternId:%d) retval: %u", patternId, errval);
265 }
266 } else {
267 TAG_LOGE(AAFwkTag::APPKIT, "null resourceManager_");
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 TAG_LOGE(AAFwkTag::APPKIT, "null hapModuleInfoLocal_");
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 TAG_LOGE(AAFwkTag::APPKIT, "null resourceManager_");
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 TAG_LOGE(AAFwkTag::APPKIT, "GetStringById(resId:%d) retval: %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 TAG_LOGE(AAFwkTag::APPKIT, "null resourceManager_");
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 TAG_LOGE(AAFwkTag::APPKIT, "GetStringArrayById(resId:%d) retval: %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 TAG_LOGE(AAFwkTag::APPKIT, "null resourceManager_");
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 TAG_LOGE(AAFwkTag::APPKIT, "GetIntArrayById(resId:%d) retval: %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 TAG_LOGE(AAFwkTag::APPKIT, "null resourceManager_");
352 return;
353 }
354
355 auto hapModInfo = GetHapModuleInfo();
356 if (hapModInfo == nullptr) {
357 TAG_LOGE(AAFwkTag::APPKIT, "null hapModInfo");
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 TAG_LOGE(AAFwkTag::APPKIT, "GetThemeById(themeId:%d) retval: %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 TAG_LOGE(AAFwkTag::APPKIT, "pattern_ 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 TAG_LOGE(AAFwkTag::APPKIT, "null resourceManager_");
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 TAG_LOGE(AAFwkTag::APPKIT, "GetColorById(resId:%d) retval: %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 TAG_LOGE(AAFwkTag::APPKIT, "null hapModInfo");
404 return -1;
405 }
406 }
407
GetDisplayOrientation()408 int ContextDeal::GetDisplayOrientation()
409 {
410 if (abilityInfo_ != nullptr) {
411 return static_cast<int>(abilityInfo_->orientation);
412 } else {
413 TAG_LOGE(AAFwkTag::APPKIT, "null abilityInfo_");
414 return static_cast<int>(DisplayOrientation::UNSPECIFIED);
415 }
416 }
417
GetPreferencesDir()418 std::string ContextDeal::GetPreferencesDir()
419 {
420 std::string dir = GetBaseDir() + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_PREFERENCES;
421 CreateDirIfNotExist(dir);
422 TAG_LOGD(AAFwkTag::APPKIT, "GetPreferencesDir:%{public}s", dir.c_str());
423 return dir;
424 }
425
SetColorMode(int mode)426 void ContextDeal::SetColorMode(int mode)
427 {
428 auto hapModInfo = GetHapModuleInfo();
429 if (hapModInfo == nullptr) {
430 TAG_LOGE(AAFwkTag::APPKIT, "null hapModInfo");
431 return;
432 }
433
434 if (mode == static_cast<int>(ModuleColorMode::DARK)) {
435 hapModInfo->colorMode = ModuleColorMode::DARK;
436 } else if (mode == static_cast<int>(ModuleColorMode::LIGHT)) {
437 hapModInfo->colorMode = ModuleColorMode::LIGHT;
438 } else { // default use AUTO
439 hapModInfo->colorMode = ModuleColorMode::AUTO;
440 }
441 }
442
GetColorMode()443 int ContextDeal::GetColorMode()
444 {
445 auto hapModInfo = GetHapModuleInfo();
446 if (hapModInfo == nullptr) {
447 TAG_LOGE(AAFwkTag::APPKIT, "null hapModInfo");
448 return -1;
449 }
450 return static_cast<int>(hapModInfo->colorMode);
451 }
452
453
HapModuleInfoRequestInit()454 bool ContextDeal::HapModuleInfoRequestInit()
455 {
456 auto ptr = GetBundleManager();
457 if (ptr == nullptr) {
458 TAG_LOGE(AAFwkTag::APPKIT, "null ptr");
459 return false;
460 }
461
462 if (abilityInfo_ == nullptr) {
463 TAG_LOGE(AAFwkTag::APPKIT, "null abilityInfo_");
464 return false;
465 }
466
467 hapModuleInfoLocal_ = std::make_shared<HapModuleInfo>();
468 if (!ptr->GetHapModuleInfo(*abilityInfo_.get(), *hapModuleInfoLocal_)) {
469 TAG_LOGE(AAFwkTag::APPKIT, "will retval false");
470 return false;
471 }
472 return true;
473 }
474
GetBaseDir() const475 std::string ContextDeal::GetBaseDir() const
476 {
477 std::string baseDir;
478 if (IsCreateBySystemApp()) {
479 baseDir = CONTEXT_DEAL_DATA_APP + currArea_ + CONTEXT_DEAL_FILE_SEPARATOR +
480 std::to_string(GetCurrentAccountId()) + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_BASE +
481 CONTEXT_DEAL_FILE_SEPARATOR + GetBundleName();
482 } else {
483 baseDir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_DEAL_FILE_SEPARATOR + CONTEXT_DEAL_BASE;
484 }
485
486 TAG_LOGD(AAFwkTag::APPKIT, "GetBaseDir:%{public}s", baseDir.c_str());
487 return baseDir;
488 }
489 } // namespace AppExecFwk
490 } // namespace OHOS
491