1 /*
2 * Copyright (c) 2022-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 "application_context.h"
17
18 #include <algorithm>
19
20 #include "ability_manager_errors.h"
21 #include "configuration_convertor.h"
22 #include "hilog_tag_wrapper.h"
23 #include "hitrace_meter.h"
24 #include "running_process_info.h"
25
26 namespace OHOS {
27 namespace AbilityRuntime {
28 const size_t ApplicationContext::CONTEXT_TYPE_ID(std::hash<const char*> {} ("ApplicationContext"));
29 std::vector<std::shared_ptr<AbilityLifecycleCallback>> ApplicationContext::callbacks_;
30 std::vector<std::shared_ptr<EnvironmentCallback>> ApplicationContext::envCallbacks_;
31 std::vector<std::weak_ptr<ApplicationStateChangeCallback>> ApplicationContext::applicationStateCallback_;
32
GetInstance()33 std::shared_ptr<ApplicationContext> ApplicationContext::GetInstance()
34 {
35 if (applicationContext_ == nullptr) {
36 std::lock_guard<std::mutex> lock_l(Context::contextMutex_);
37 if (applicationContext_ == nullptr) {
38 applicationContext_ = std::make_shared<ApplicationContext>();
39 }
40 }
41 return applicationContext_;
42 }
43
AttachContextImpl(const std::shared_ptr<ContextImpl> & contextImpl)44 void ApplicationContext::AttachContextImpl(const std::shared_ptr<ContextImpl> &contextImpl)
45 {
46 contextImpl_ = contextImpl;
47 }
48
RegisterAbilityLifecycleCallback(const std::shared_ptr<AbilityLifecycleCallback> & abilityLifecycleCallback)49 void ApplicationContext::RegisterAbilityLifecycleCallback(
50 const std::shared_ptr<AbilityLifecycleCallback> &abilityLifecycleCallback)
51 {
52 TAG_LOGD(AAFwkTag::APPKIT, "called");
53 if (abilityLifecycleCallback == nullptr) {
54 return;
55 }
56 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
57 callbacks_.push_back(abilityLifecycleCallback);
58 }
59
UnregisterAbilityLifecycleCallback(const std::shared_ptr<AbilityLifecycleCallback> & abilityLifecycleCallback)60 void ApplicationContext::UnregisterAbilityLifecycleCallback(
61 const std::shared_ptr<AbilityLifecycleCallback> &abilityLifecycleCallback)
62 {
63 TAG_LOGD(AAFwkTag::APPKIT, "called");
64 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
65 auto it = std::find(callbacks_.begin(), callbacks_.end(), abilityLifecycleCallback);
66 if (it != callbacks_.end()) {
67 callbacks_.erase(it);
68 }
69 }
70
IsAbilityLifecycleCallbackEmpty()71 bool ApplicationContext::IsAbilityLifecycleCallbackEmpty()
72 {
73 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
74 return callbacks_.empty();
75 }
76
RegisterEnvironmentCallback(const std::shared_ptr<EnvironmentCallback> & environmentCallback)77 void ApplicationContext::RegisterEnvironmentCallback(
78 const std::shared_ptr<EnvironmentCallback> &environmentCallback)
79 {
80 TAG_LOGD(AAFwkTag::APPKIT, "called");
81 if (environmentCallback == nullptr) {
82 return;
83 }
84 std::lock_guard<std::recursive_mutex> lock(envCallbacksLock_);
85 envCallbacks_.push_back(environmentCallback);
86 }
87
UnregisterEnvironmentCallback(const std::shared_ptr<EnvironmentCallback> & environmentCallback)88 void ApplicationContext::UnregisterEnvironmentCallback(
89 const std::shared_ptr<EnvironmentCallback> &environmentCallback)
90 {
91 TAG_LOGD(AAFwkTag::APPKIT, "called");
92 std::lock_guard<std::recursive_mutex> lock(envCallbacksLock_);
93 auto it = std::find(envCallbacks_.begin(), envCallbacks_.end(), environmentCallback);
94 if (it != envCallbacks_.end()) {
95 envCallbacks_.erase(it);
96 }
97 }
98
RegisterApplicationStateChangeCallback(const std::weak_ptr<ApplicationStateChangeCallback> & applicationStateChangeCallback)99 void ApplicationContext::RegisterApplicationStateChangeCallback(
100 const std::weak_ptr<ApplicationStateChangeCallback> &applicationStateChangeCallback)
101 {
102 std::lock_guard<std::recursive_mutex> lock(applicationStateCallbackLock_);
103 applicationStateCallback_.push_back(applicationStateChangeCallback);
104 }
105
DispatchOnAbilityCreate(const std::shared_ptr<NativeReference> & ability)106 void ApplicationContext::DispatchOnAbilityCreate(const std::shared_ptr<NativeReference> &ability)
107 {
108 if (!ability) {
109 TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
110 return;
111 }
112 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
113 for (auto callback : callbacks_) {
114 if (callback != nullptr) {
115 callback->OnAbilityCreate(ability);
116 }
117 }
118 }
119
DispatchOnWindowStageCreate(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)120 void ApplicationContext::DispatchOnWindowStageCreate(const std::shared_ptr<NativeReference> &ability,
121 const std::shared_ptr<NativeReference> &windowStage)
122 {
123 if (!ability || !windowStage) {
124 TAG_LOGE(AAFwkTag::APPKIT, "ability or windowStage is nullptr");
125 return;
126 }
127 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
128 for (auto callback : callbacks_) {
129 if (callback != nullptr) {
130 callback->OnWindowStageCreate(ability, windowStage);
131 }
132 }
133 }
134
DispatchOnWindowStageDestroy(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)135 void ApplicationContext::DispatchOnWindowStageDestroy(const std::shared_ptr<NativeReference> &ability,
136 const std::shared_ptr<NativeReference> &windowStage)
137 {
138 if (!ability || !windowStage) {
139 TAG_LOGE(AAFwkTag::APPKIT, "ability or windowStage is nullptr");
140 return;
141 }
142 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
143 for (auto callback : callbacks_) {
144 if (callback != nullptr) {
145 callback->OnWindowStageDestroy(ability, windowStage);
146 }
147 }
148 }
149
DispatchWindowStageFocus(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)150 void ApplicationContext::DispatchWindowStageFocus(const std::shared_ptr<NativeReference> &ability,
151 const std::shared_ptr<NativeReference> &windowStage)
152 {
153 TAG_LOGD(AAFwkTag::APPKIT, "called");
154 if (!ability || !windowStage) {
155 TAG_LOGE(AAFwkTag::APPKIT, "ability or windowStage is null");
156 return;
157 }
158 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
159 for (auto callback : callbacks_) {
160 if (callback != nullptr) {
161 callback->OnWindowStageActive(ability, windowStage);
162 }
163 }
164 }
165
DispatchWindowStageUnfocus(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)166 void ApplicationContext::DispatchWindowStageUnfocus(const std::shared_ptr<NativeReference> &ability,
167 const std::shared_ptr<NativeReference> &windowStage)
168 {
169 TAG_LOGD(AAFwkTag::APPKIT, "called");
170 if (!ability || !windowStage) {
171 TAG_LOGE(AAFwkTag::APPKIT, "ability or windowStage is nullptr");
172 return;
173 }
174 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
175 for (auto callback : callbacks_) {
176 if (callback != nullptr) {
177 callback->OnWindowStageInactive(ability, windowStage);
178 }
179 }
180 }
181
DispatchOnAbilityDestroy(const std::shared_ptr<NativeReference> & ability)182 void ApplicationContext::DispatchOnAbilityDestroy(const std::shared_ptr<NativeReference> &ability)
183 {
184 if (!ability) {
185 TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
186 return;
187 }
188 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
189 for (auto callback : callbacks_) {
190 if (callback != nullptr) {
191 callback->OnAbilityDestroy(ability);
192 }
193 }
194 }
195
DispatchOnAbilityForeground(const std::shared_ptr<NativeReference> & ability)196 void ApplicationContext::DispatchOnAbilityForeground(const std::shared_ptr<NativeReference> &ability)
197 {
198 if (!ability) {
199 TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
200 return;
201 }
202 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
203 for (auto callback : callbacks_) {
204 if (callback != nullptr) {
205 callback->OnAbilityForeground(ability);
206 }
207 }
208 }
209
DispatchOnAbilityBackground(const std::shared_ptr<NativeReference> & ability)210 void ApplicationContext::DispatchOnAbilityBackground(const std::shared_ptr<NativeReference> &ability)
211 {
212 if (!ability) {
213 TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
214 return;
215 }
216 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
217 for (auto callback : callbacks_) {
218 if (callback != nullptr) {
219 callback->OnAbilityBackground(ability);
220 }
221 }
222 }
223
DispatchOnAbilityContinue(const std::shared_ptr<NativeReference> & ability)224 void ApplicationContext::DispatchOnAbilityContinue(const std::shared_ptr<NativeReference> &ability)
225 {
226 if (!ability) {
227 TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
228 return;
229 }
230 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
231 for (auto callback : callbacks_) {
232 if (callback != nullptr) {
233 callback->OnAbilityContinue(ability);
234 }
235 }
236 }
237
DispatchOnAbilityWillContinue(const std::shared_ptr<NativeReference> & ability)238 void ApplicationContext::DispatchOnAbilityWillContinue(const std::shared_ptr<NativeReference> &ability)
239 {
240 TAG_LOGD(AAFwkTag::APPKIT, "Dispatch onAbilityWillContinue");
241 if (ability == nullptr) {
242 TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
243 return;
244 }
245
246 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
247 for (auto callback : callbacks_) {
248 if (callback != nullptr) {
249 callback->OnAbilityWillContinue(ability);
250 }
251 }
252 }
253
DispatchOnWindowStageWillRestore(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)254 void ApplicationContext::DispatchOnWindowStageWillRestore(const std::shared_ptr<NativeReference> &ability,
255 const std::shared_ptr<NativeReference> &windowStage)
256 {
257 TAG_LOGD(AAFwkTag::APPKIT, "Dispatch onWindowStageWillRestore");
258 if (ability == nullptr || windowStage == nullptr) {
259 TAG_LOGE(AAFwkTag::APPKIT, "ability or windowStage is null");
260 return;
261 }
262
263 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
264 for (auto callback : callbacks_) {
265 if (callback != nullptr) {
266 callback->OnWindowStageWillRestore(ability, windowStage);
267 }
268 }
269 }
270
DispatchOnWindowStageRestore(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)271 void ApplicationContext::DispatchOnWindowStageRestore(const std::shared_ptr<NativeReference> &ability,
272 const std::shared_ptr<NativeReference> &windowStage)
273 {
274 TAG_LOGD(AAFwkTag::APPKIT, "Dispatch onWindowStageRestore");
275 if (ability == nullptr || windowStage == nullptr) {
276 TAG_LOGE(AAFwkTag::APPKIT, "ability or windowStage is null");
277 return;
278 }
279
280 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
281 for (auto callback : callbacks_) {
282 if (callback != nullptr) {
283 callback->OnWindowStageRestore(ability, windowStage);
284 }
285 }
286 }
287
DispatchOnAbilityWillSaveState(const std::shared_ptr<NativeReference> & ability)288 void ApplicationContext::DispatchOnAbilityWillSaveState(const std::shared_ptr<NativeReference> &ability)
289 {
290 TAG_LOGD(AAFwkTag::APPKIT, "Dispatch onAbilityWillSaveState");
291 if (ability == nullptr) {
292 TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
293 return;
294 }
295
296 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
297 for (auto callback : callbacks_) {
298 if (callback != nullptr) {
299 callback->OnAbilityWillSaveState(ability);
300 }
301 }
302 }
303
DispatchOnAbilitySaveState(const std::shared_ptr<NativeReference> & ability)304 void ApplicationContext::DispatchOnAbilitySaveState(const std::shared_ptr<NativeReference> &ability)
305 {
306 TAG_LOGD(AAFwkTag::APPKIT, "called");
307 if (ability == nullptr) {
308 TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
309 return;
310 }
311
312 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
313 for (auto callback : callbacks_) {
314 if (callback != nullptr) {
315 callback->OnAbilitySaveState(ability);
316 }
317 }
318 }
319
DispatchConfigurationUpdated(const AppExecFwk::Configuration & config)320 void ApplicationContext::DispatchConfigurationUpdated(const AppExecFwk::Configuration &config)
321 {
322 std::lock_guard<std::recursive_mutex> lock(envCallbacksLock_);
323 for (auto envCallback : envCallbacks_) {
324 if (envCallback != nullptr) {
325 envCallback->OnConfigurationUpdated(config);
326 }
327 }
328 }
329
DispatchMemoryLevel(const int level)330 void ApplicationContext::DispatchMemoryLevel(const int level)
331 {
332 std::lock_guard<std::recursive_mutex> lock(envCallbacksLock_);
333 for (auto envCallback : envCallbacks_) {
334 if (envCallback != nullptr) {
335 envCallback->OnMemoryLevel(level);
336 }
337 }
338 }
339
NotifyApplicationForeground()340 void ApplicationContext::NotifyApplicationForeground()
341 {
342 std::lock_guard<std::recursive_mutex> lock(applicationStateCallbackLock_);
343 for (auto callback : applicationStateCallback_) {
344 auto callbackSptr = callback.lock();
345 if (callbackSptr != nullptr) {
346 callbackSptr->NotifyApplicationForeground();
347 }
348 }
349 }
350
NotifyApplicationBackground()351 void ApplicationContext::NotifyApplicationBackground()
352 {
353 std::lock_guard<std::recursive_mutex> lock(applicationStateCallbackLock_);
354 for (auto callback : applicationStateCallback_) {
355 auto callbackSptr = callback.lock();
356 if (callbackSptr != nullptr) {
357 callbackSptr->NotifyApplicationBackground();
358 }
359 }
360 }
361
DispatchOnWillNewWant(const std::shared_ptr<NativeReference> & ability)362 void ApplicationContext::DispatchOnWillNewWant(const std::shared_ptr<NativeReference> &ability)
363 {
364 if (!ability) {
365 TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
366 return;
367 }
368 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
369 for (auto callback : callbacks_) {
370 if (callback != nullptr) {
371 callback->OnWillNewWant(ability);
372 }
373 }
374 }
375
DispatchOnNewWant(const std::shared_ptr<NativeReference> & ability)376 void ApplicationContext::DispatchOnNewWant(const std::shared_ptr<NativeReference> &ability)
377 {
378 if (!ability) {
379 TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
380 return;
381 }
382 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
383 for (auto callback : callbacks_) {
384 if (callback != nullptr) {
385 callback->OnNewWant(ability);
386 }
387 }
388 }
389
DispatchOnAbilityWillCreate(const std::shared_ptr<NativeReference> & ability)390 void ApplicationContext::DispatchOnAbilityWillCreate(const std::shared_ptr<NativeReference> &ability)
391 {
392 TAG_LOGD(AAFwkTag::APPKIT, "called");
393 if (!ability) {
394 TAG_LOGE(AAFwkTag::APPKIT, "ability is null");
395 return;
396 }
397 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
398 for (auto callback : callbacks_) {
399 if (callback != nullptr) {
400 callback->OnAbilityWillCreate(ability);
401 }
402 }
403 }
404
DispatchOnWindowStageWillCreate(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)405 void ApplicationContext::DispatchOnWindowStageWillCreate(const std::shared_ptr<NativeReference> &ability,
406 const std::shared_ptr<NativeReference> &windowStage)
407 {
408 TAG_LOGD(AAFwkTag::APPKIT, "called");
409 if (!ability || !windowStage) {
410 TAG_LOGE(AAFwkTag::APPKIT, "ability or windowStage is null");
411 return;
412 }
413 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
414 for (auto callback : callbacks_) {
415 if (callback != nullptr) {
416 callback->OnWindowStageWillCreate(ability, windowStage);
417 }
418 }
419 }
420
DispatchOnWindowStageWillDestroy(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)421 void ApplicationContext::DispatchOnWindowStageWillDestroy(const std::shared_ptr<NativeReference> &ability,
422 const std::shared_ptr<NativeReference> &windowStage)
423 {
424 TAG_LOGD(AAFwkTag::APPKIT, "called");
425 if (!ability || !windowStage) {
426 TAG_LOGE(AAFwkTag::APPKIT, "ability or windowStage is null");
427 return;
428 }
429 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
430 for (auto callback : callbacks_) {
431 if (callback != nullptr) {
432 callback->OnWindowStageWillDestroy(ability, windowStage);
433 }
434 }
435 }
436
DispatchOnAbilityWillDestroy(const std::shared_ptr<NativeReference> & ability)437 void ApplicationContext::DispatchOnAbilityWillDestroy(const std::shared_ptr<NativeReference> &ability)
438 {
439 TAG_LOGD(AAFwkTag::APPKIT, "called");
440 if (!ability) {
441 TAG_LOGE(AAFwkTag::APPKIT, "ability is null");
442 return;
443 }
444 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
445 for (auto callback : callbacks_) {
446 if (callback != nullptr) {
447 callback->OnAbilityWillDestroy(ability);
448 }
449 }
450 }
451
DispatchOnAbilityWillForeground(const std::shared_ptr<NativeReference> & ability)452 void ApplicationContext::DispatchOnAbilityWillForeground(const std::shared_ptr<NativeReference> &ability)
453 {
454 TAG_LOGD(AAFwkTag::APPKIT, "called");
455 if (!ability) {
456 TAG_LOGE(AAFwkTag::APPKIT, "ability is null");
457 return;
458 }
459 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
460 for (auto callback : callbacks_) {
461 if (callback != nullptr) {
462 callback->OnAbilityWillForeground(ability);
463 }
464 }
465 }
466
DispatchOnAbilityWillBackground(const std::shared_ptr<NativeReference> & ability)467 void ApplicationContext::DispatchOnAbilityWillBackground(const std::shared_ptr<NativeReference> &ability)
468 {
469 TAG_LOGD(AAFwkTag::APPKIT, "called");
470 if (!ability) {
471 TAG_LOGE(AAFwkTag::APPKIT, "ability is null");
472 return;
473 }
474 std::lock_guard<std::recursive_mutex> lock(callbackLock_);
475 for (auto callback : callbacks_) {
476 if (callback != nullptr) {
477 callback->OnAbilityWillBackground(ability);
478 }
479 }
480 }
481
GetBundleName() const482 std::string ApplicationContext::GetBundleName() const
483 {
484 return (contextImpl_ != nullptr) ? contextImpl_->GetBundleName() : "";
485 }
486
CreateBundleContext(const std::string & bundleName)487 std::shared_ptr<Context> ApplicationContext::CreateBundleContext(const std::string &bundleName)
488 {
489 return (contextImpl_ != nullptr) ? contextImpl_->CreateBundleContext(bundleName) : nullptr;
490 }
491
CreateModuleContext(const std::string & moduleName)492 std::shared_ptr<Context> ApplicationContext::CreateModuleContext(const std::string &moduleName)
493 {
494 return contextImpl_ ? contextImpl_->CreateModuleContext(moduleName) : nullptr;
495 }
496
CreateModuleContext(const std::string & bundleName,const std::string & moduleName)497 std::shared_ptr<Context> ApplicationContext::CreateModuleContext(const std::string &bundleName,
498 const std::string &moduleName)
499 {
500 return contextImpl_ ? contextImpl_->CreateModuleContext(bundleName, moduleName) : nullptr;
501 }
502
CreateModuleResourceManager(const std::string & bundleName,const std::string & moduleName)503 std::shared_ptr<Global::Resource::ResourceManager> ApplicationContext::CreateModuleResourceManager(
504 const std::string &bundleName, const std::string &moduleName)
505 {
506 return contextImpl_ ? contextImpl_->CreateModuleResourceManager(bundleName, moduleName) : nullptr;
507 }
508
CreateSystemHspModuleResourceManager(const std::string & bundleName,const std::string & moduleName,std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)509 int32_t ApplicationContext::CreateSystemHspModuleResourceManager(const std::string &bundleName,
510 const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
511 {
512 return contextImpl_ ?
513 contextImpl_->CreateSystemHspModuleResourceManager(bundleName, moduleName, resourceManager) : ERR_INVALID_VALUE;
514 }
515
GetApplicationInfo() const516 std::shared_ptr<AppExecFwk::ApplicationInfo> ApplicationContext::GetApplicationInfo() const
517 {
518 return (contextImpl_ != nullptr) ? contextImpl_->GetApplicationInfo() : nullptr;
519 }
520
SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> & info)521 void ApplicationContext::SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> &info)
522 {
523 if (contextImpl_ != nullptr) {
524 contextImpl_->SetApplicationInfo(info);
525 }
526 applicationInfoUpdateFlag_ = true;
527 }
528
GetApplicationInfoUpdateFlag() const529 bool ApplicationContext::GetApplicationInfoUpdateFlag() const
530 {
531 return applicationInfoUpdateFlag_;
532 }
533
SetApplicationInfoUpdateFlag(bool flag)534 void ApplicationContext::SetApplicationInfoUpdateFlag(bool flag)
535 {
536 applicationInfoUpdateFlag_ = flag;
537 }
538
GetResourceManager() const539 std::shared_ptr<Global::Resource::ResourceManager> ApplicationContext::GetResourceManager() const
540 {
541 return (contextImpl_ != nullptr) ? contextImpl_->GetResourceManager() : nullptr;
542 }
543
GetBundleCodePath() const544 std::string ApplicationContext::GetBundleCodePath() const
545 {
546 return (contextImpl_ != nullptr) ? contextImpl_->GetBundleCodePath() : "";
547 }
548
GetHapModuleInfo() const549 std::shared_ptr<AppExecFwk::HapModuleInfo> ApplicationContext::GetHapModuleInfo() const
550 {
551 return nullptr;
552 }
553
GetBundleCodeDir()554 std::string ApplicationContext::GetBundleCodeDir()
555 {
556 return (contextImpl_ != nullptr) ? contextImpl_->GetBundleCodeDir() : "";
557 }
558
GetCacheDir()559 std::string ApplicationContext::GetCacheDir()
560 {
561 return (contextImpl_ != nullptr) ? contextImpl_->GetCacheDir() : "";
562 }
563
GetTempDir()564 std::string ApplicationContext::GetTempDir()
565 {
566 return (contextImpl_ != nullptr) ? contextImpl_->GetTempDir() : "";
567 }
568
GetResourceDir()569 std::string ApplicationContext::GetResourceDir()
570 {
571 return (contextImpl_ != nullptr) ? contextImpl_->GetResourceDir() : "";
572 }
573
GetAllTempDir(std::vector<std::string> & tempPaths)574 void ApplicationContext::GetAllTempDir(std::vector<std::string> &tempPaths)
575 {
576 if (contextImpl_ == nullptr) {
577 TAG_LOGE(AAFwkTag::APPKIT, "The contextimpl is nullptr");
578 return;
579 }
580 contextImpl_->GetAllTempDir(tempPaths);
581 }
582
GetFilesDir()583 std::string ApplicationContext::GetFilesDir()
584 {
585 return (contextImpl_ != nullptr) ? contextImpl_->GetFilesDir() : "";
586 }
587
KillProcessBySelf()588 void ApplicationContext::KillProcessBySelf()
589 {
590 if (contextImpl_ != nullptr) {
591 contextImpl_->KillProcessBySelf();
592 }
593 }
594
GetProcessRunningInformation(AppExecFwk::RunningProcessInfo & info)595 int32_t ApplicationContext::GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info)
596 {
597 return (contextImpl_ != nullptr) ? contextImpl_->GetProcessRunningInformation(info) : -1;
598 }
599
IsUpdatingConfigurations()600 bool ApplicationContext::IsUpdatingConfigurations()
601 {
602 return (contextImpl_ != nullptr) ? contextImpl_->IsUpdatingConfigurations() : false;
603 }
604
PrintDrawnCompleted()605 bool ApplicationContext::PrintDrawnCompleted()
606 {
607 return (contextImpl_ != nullptr) ? contextImpl_->PrintDrawnCompleted() : false;
608 }
609
GetDatabaseDir()610 std::string ApplicationContext::GetDatabaseDir()
611 {
612 return (contextImpl_ != nullptr) ? contextImpl_->GetDatabaseDir() : "";
613 }
614
GetPreferencesDir()615 std::string ApplicationContext::GetPreferencesDir()
616 {
617 return (contextImpl_ != nullptr) ? contextImpl_->GetPreferencesDir() : "";
618 }
619
GetSystemDatabaseDir(const std::string & groupId,bool checkExist,std::string & databaseDir)620 int32_t ApplicationContext::GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir)
621 {
622 return contextImpl_ ?
623 contextImpl_->GetSystemDatabaseDir(groupId, checkExist, databaseDir) : ERR_INVALID_VALUE;
624 }
625
GetSystemPreferencesDir(const std::string & groupId,bool checkExist,std::string & preferencesDir)626 int32_t ApplicationContext::GetSystemPreferencesDir(const std::string &groupId, bool checkExist,
627 std::string &preferencesDir)
628 {
629 return contextImpl_ ?
630 contextImpl_->GetSystemPreferencesDir(groupId, checkExist, preferencesDir) : ERR_INVALID_VALUE;
631 }
632
GetGroupDir(std::string groupId)633 std::string ApplicationContext::GetGroupDir(std::string groupId)
634 {
635 return (contextImpl_ != nullptr) ? contextImpl_->GetGroupDir(groupId) : "";
636 }
637
RestartApp(const AAFwk::Want & want)638 int32_t ApplicationContext::RestartApp(const AAFwk::Want& want)
639 {
640 std::string abilityName = want.GetElement().GetAbilityName();
641 if (abilityName == "") {
642 TAG_LOGE(AAFwkTag::APPKIT, "abilityName is empty");
643 return ERR_INVALID_VALUE;
644 }
645 std::string bundleName = GetBundleName();
646 const_cast<AAFwk::Want &>(want).SetBundle(bundleName);
647 return (contextImpl_ != nullptr) ? contextImpl_->RestartApp(want) : ERR_INVALID_VALUE;
648 }
649
GetDistributedFilesDir()650 std::string ApplicationContext::GetDistributedFilesDir()
651 {
652 return (contextImpl_ != nullptr) ? contextImpl_->GetDistributedFilesDir() : "";
653 }
654
GetCloudFileDir()655 std::string ApplicationContext::GetCloudFileDir()
656 {
657 return (contextImpl_ != nullptr) ? contextImpl_->GetCloudFileDir() : "";
658 }
659
GetToken()660 sptr<IRemoteObject> ApplicationContext::GetToken()
661 {
662 return (contextImpl_ != nullptr) ? contextImpl_->GetToken() : nullptr;
663 }
664
SetToken(const sptr<IRemoteObject> & token)665 void ApplicationContext::SetToken(const sptr<IRemoteObject> &token)
666 {
667 if (contextImpl_ != nullptr) {
668 contextImpl_->SetToken(token);
669 }
670 }
671
SwitchArea(int mode)672 void ApplicationContext::SwitchArea(int mode)
673 {
674 if (contextImpl_ != nullptr) {
675 contextImpl_->SwitchArea(mode);
676 }
677 }
678
SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & config)679 void ApplicationContext::SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config)
680 {
681 if (contextImpl_ == nullptr) {
682 TAG_LOGE(AAFwkTag::APPKIT, "context is null");
683 return;
684 }
685 contextImpl_->SetConfiguration(config);
686 }
687
AppHasDarkRes(bool & darkRes)688 void ApplicationContext::AppHasDarkRes(bool &darkRes)
689 {
690 if (contextImpl_ == nullptr) {
691 TAG_LOGE(AAFwkTag::APPKIT, "context is null");
692 return;
693 }
694 contextImpl_->AppHasDarkRes(darkRes);
695 }
696
SetColorMode(int32_t colorMode)697 void ApplicationContext::SetColorMode(int32_t colorMode)
698 {
699 TAG_LOGD(AAFwkTag::APPKIT, "colorMode:%{public}d", colorMode);
700 if (colorMode < -1 || colorMode > 1) {
701 TAG_LOGE(AAFwkTag::APPKIT, "colorMode is invalid");
702 return;
703 }
704 AppExecFwk::Configuration config;
705 config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, AppExecFwk::GetColorModeStr(colorMode));
706 if (appConfigChangeCallback_ != nullptr) {
707 appConfigChangeCallback_(config);
708 }
709 }
710
SetLanguage(const std::string & language)711 void ApplicationContext::SetLanguage(const std::string &language)
712 {
713 TAG_LOGD(AAFwkTag::APPKIT, "language:%{public}s", language.c_str());
714 AppExecFwk::Configuration config;
715 config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, language);
716 if (appConfigChangeCallback_ != nullptr) {
717 appConfigChangeCallback_(config);
718 }
719 }
720
SetFont(const std::string & font)721 void ApplicationContext::SetFont(const std::string &font)
722 {
723 TAG_LOGD(AAFwkTag::APPKIT, "font:%{public}s", font.c_str());
724 #ifdef SUPPORT_GRAPHICS
725 // Notify Window
726 AppExecFwk::Configuration config;
727 config.AddItem(AppExecFwk::ConfigurationInner::APPLICATION_FONT, font);
728 if (appFontCallback_ != nullptr) {
729 appFontCallback_(config);
730 }
731 #endif
732 }
733
SetFontSizeScale(double fontSizeScale)734 bool ApplicationContext::SetFontSizeScale(double fontSizeScale)
735 {
736 if (contextImpl_ == nullptr) {
737 TAG_LOGE(AAFwkTag::APPKIT, "null context");
738 return false;
739 }
740
741 AppExecFwk::Configuration config;
742 config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE, std::to_string(fontSizeScale));
743 if (appConfigChangeCallback_ != nullptr) {
744 appConfigChangeCallback_(config);
745 }
746 TAG_LOGD(AAFwkTag::APPKIT, "SetFontSizeScale callback ok");
747 return true;
748 }
749
SetMcc(const std::string & mcc)750 void ApplicationContext::SetMcc(const std::string &mcc)
751 {
752 if (contextImpl_ != nullptr) {
753 contextImpl_->SetMcc(mcc);
754 }
755 }
756
SetMnc(const std::string & mnc)757 void ApplicationContext::SetMnc(const std::string &mnc)
758 {
759 if (contextImpl_ != nullptr) {
760 contextImpl_->SetMnc(mnc);
761 }
762 }
763
ClearUpApplicationData()764 void ApplicationContext::ClearUpApplicationData()
765 {
766 if (contextImpl_ != nullptr) {
767 contextImpl_->ClearUpApplicationData();
768 }
769 }
770
GetArea()771 int ApplicationContext::GetArea()
772 {
773 if (contextImpl_ == nullptr) {
774 TAG_LOGE(AAFwkTag::APPKIT, "contextImpl is nullptr");
775 return ContextImpl::EL_DEFAULT;
776 }
777 return contextImpl_->GetArea();
778 }
779
GetConfiguration() const780 std::shared_ptr<AppExecFwk::Configuration> ApplicationContext::GetConfiguration() const
781 {
782 return (contextImpl_ != nullptr) ? contextImpl_->GetConfiguration() : nullptr;
783 }
784
GetBaseDir() const785 std::string ApplicationContext::GetBaseDir() const
786 {
787 return (contextImpl_ != nullptr) ? contextImpl_->GetBaseDir() : nullptr;
788 }
789
GetDeviceType() const790 Global::Resource::DeviceType ApplicationContext::GetDeviceType() const
791 {
792 return (contextImpl_ != nullptr) ? contextImpl_->GetDeviceType() : Global::Resource::DeviceType::DEVICE_PHONE;
793 }
794
RegisterAppConfigUpdateObserver(AppConfigUpdateCallback appConfigChangeCallback)795 void ApplicationContext::RegisterAppConfigUpdateObserver(AppConfigUpdateCallback appConfigChangeCallback)
796 {
797 appConfigChangeCallback_ = appConfigChangeCallback;
798 }
799
RegisterAppFontObserver(AppConfigUpdateCallback appFontCallback)800 void ApplicationContext::RegisterAppFontObserver(AppConfigUpdateCallback appFontCallback)
801 {
802 appFontCallback_ = appFontCallback;
803 }
804
GetAppRunningUniqueId() const805 std::string ApplicationContext::GetAppRunningUniqueId() const
806 {
807 TAG_LOGD(AAFwkTag::APPKIT, "GetAppRunningUniqueId is %{public}s", appRunningUniqueId_.c_str());
808 return appRunningUniqueId_;
809 }
810
GetCurrentAppCloneIndex()811 int32_t ApplicationContext::GetCurrentAppCloneIndex()
812 {
813 TAG_LOGD(AAFwkTag::APPKIT, "getCurrentAppCloneIndex is %{public}d", appIndex_);
814 return appIndex_;
815 }
816
GetCurrentAppMode()817 int32_t ApplicationContext::GetCurrentAppMode()
818 {
819 TAG_LOGD(AAFwkTag::APPKIT, "getCurrentMode is %{public}d", appMode_);
820 return appMode_;
821 }
822
823
SetAppRunningUniqueId(const std::string & appRunningUniqueId)824 void ApplicationContext::SetAppRunningUniqueId(const std::string &appRunningUniqueId)
825 {
826 TAG_LOGD(AAFwkTag::APPKIT, "SetAppRunningUniqueId is %{public}s", appRunningUniqueId.c_str());
827 appRunningUniqueId_ = appRunningUniqueId;
828 }
829
SetSupportedProcessCacheSelf(bool isSupport)830 int32_t ApplicationContext::SetSupportedProcessCacheSelf(bool isSupport)
831 {
832 if (contextImpl_ != nullptr) {
833 return contextImpl_->SetSupportedProcessCacheSelf(isSupport);
834 }
835 TAG_LOGE(AAFwkTag::APPKIT, "contextImpl_ is nullptr");
836 return ERR_INVALID_VALUE;
837 }
838
SetCurrentAppCloneIndex(int32_t appIndex)839 void ApplicationContext::SetCurrentAppCloneIndex(int32_t appIndex)
840 {
841 TAG_LOGD(AAFwkTag::APPKIT, "setCurrentAppCloneIndex is %{public}d", appIndex);
842 appIndex_ = appIndex;
843 }
844
SetCurrentAppMode(int32_t appMode)845 void ApplicationContext::SetCurrentAppMode(int32_t appMode)
846 {
847 TAG_LOGD(AAFwkTag::APPKIT, "setCurrentAppMode is %{public}d", appMode);
848 appMode_ = appMode;
849 }
850 } // namespace AbilityRuntime
851 } // namespace OHOS
852