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(const bool clearPageStack)588 void ApplicationContext::KillProcessBySelf(const bool clearPageStack)
589 {
590     if (contextImpl_ != nullptr) {
591         contextImpl_->KillProcessBySelf(clearPageStack);
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 
GetAllRunningInstanceKeys(std::vector<std::string> & instanceKeys)600 int32_t ApplicationContext::GetAllRunningInstanceKeys(std::vector<std::string> &instanceKeys)
601 {
602     return (contextImpl_ != nullptr) ? contextImpl_->GetAllRunningInstanceKeys(instanceKeys) : -1;
603 }
604 
IsUpdatingConfigurations()605 bool ApplicationContext::IsUpdatingConfigurations()
606 {
607     return (contextImpl_ != nullptr) ? contextImpl_->IsUpdatingConfigurations() : false;
608 }
609 
PrintDrawnCompleted()610 bool ApplicationContext::PrintDrawnCompleted()
611 {
612     return (contextImpl_ != nullptr) ? contextImpl_->PrintDrawnCompleted() : false;
613 }
614 
GetDatabaseDir()615 std::string ApplicationContext::GetDatabaseDir()
616 {
617     return (contextImpl_ != nullptr) ? contextImpl_->GetDatabaseDir() : "";
618 }
619 
GetPreferencesDir()620 std::string ApplicationContext::GetPreferencesDir()
621 {
622     return (contextImpl_ != nullptr) ? contextImpl_->GetPreferencesDir() : "";
623 }
624 
GetSystemDatabaseDir(const std::string & groupId,bool checkExist,std::string & databaseDir)625 int32_t ApplicationContext::GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir)
626 {
627     return contextImpl_ ?
628         contextImpl_->GetSystemDatabaseDir(groupId, checkExist, databaseDir) : ERR_INVALID_VALUE;
629 }
630 
GetSystemPreferencesDir(const std::string & groupId,bool checkExist,std::string & preferencesDir)631 int32_t ApplicationContext::GetSystemPreferencesDir(const std::string &groupId, bool checkExist,
632     std::string &preferencesDir)
633 {
634     return contextImpl_ ?
635         contextImpl_->GetSystemPreferencesDir(groupId, checkExist, preferencesDir) : ERR_INVALID_VALUE;
636 }
637 
GetGroupDir(std::string groupId)638 std::string ApplicationContext::GetGroupDir(std::string groupId)
639 {
640     return (contextImpl_ != nullptr) ? contextImpl_->GetGroupDir(groupId) : "";
641 }
642 
RestartApp(const AAFwk::Want & want)643 int32_t ApplicationContext::RestartApp(const AAFwk::Want& want)
644 {
645     std::string abilityName = want.GetElement().GetAbilityName();
646     if (abilityName == "") {
647         TAG_LOGE(AAFwkTag::APPKIT, "abilityName is empty");
648         return ERR_INVALID_VALUE;
649     }
650     std::string bundleName = GetBundleName();
651     const_cast<AAFwk::Want &>(want).SetBundle(bundleName);
652     return (contextImpl_ != nullptr) ? contextImpl_->RestartApp(want) : ERR_INVALID_VALUE;
653 }
654 
GetDistributedFilesDir()655 std::string ApplicationContext::GetDistributedFilesDir()
656 {
657     return (contextImpl_ != nullptr) ? contextImpl_->GetDistributedFilesDir() : "";
658 }
659 
GetCloudFileDir()660 std::string ApplicationContext::GetCloudFileDir()
661 {
662     return (contextImpl_ != nullptr) ? contextImpl_->GetCloudFileDir() : "";
663 }
664 
GetToken()665 sptr<IRemoteObject> ApplicationContext::GetToken()
666 {
667     return (contextImpl_ != nullptr) ? contextImpl_->GetToken() : nullptr;
668 }
669 
SetToken(const sptr<IRemoteObject> & token)670 void ApplicationContext::SetToken(const sptr<IRemoteObject> &token)
671 {
672     if (contextImpl_ != nullptr) {
673         contextImpl_->SetToken(token);
674     }
675 }
676 
SwitchArea(int mode)677 void ApplicationContext::SwitchArea(int mode)
678 {
679     if (contextImpl_ != nullptr) {
680         contextImpl_->SwitchArea(mode);
681     }
682 }
683 
SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & config)684 void ApplicationContext::SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config)
685 {
686     if (contextImpl_ == nullptr) {
687         TAG_LOGE(AAFwkTag::APPKIT, "context is null");
688         return;
689     }
690     contextImpl_->SetConfiguration(config);
691 }
692 
AppHasDarkRes(bool & darkRes)693 void ApplicationContext::AppHasDarkRes(bool &darkRes)
694 {
695     if (contextImpl_ == nullptr) {
696         TAG_LOGE(AAFwkTag::APPKIT, "context is null");
697         return;
698     }
699     contextImpl_->AppHasDarkRes(darkRes);
700 }
701 
SetColorMode(int32_t colorMode)702 void ApplicationContext::SetColorMode(int32_t colorMode)
703 {
704     TAG_LOGD(AAFwkTag::APPKIT, "colorMode:%{public}d", colorMode);
705     if (colorMode < -1 || colorMode > 1) {
706         TAG_LOGE(AAFwkTag::APPKIT, "colorMode is invalid");
707         return;
708     }
709     AppExecFwk::Configuration config;
710     config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, AppExecFwk::GetColorModeStr(colorMode));
711     if (appConfigChangeCallback_ != nullptr) {
712         appConfigChangeCallback_(config);
713     }
714 }
715 
SetLanguage(const std::string & language)716 void ApplicationContext::SetLanguage(const std::string &language)
717 {
718     TAG_LOGD(AAFwkTag::APPKIT, "language:%{public}s", language.c_str());
719     AppExecFwk::Configuration config;
720     config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, language);
721     if (appConfigChangeCallback_ != nullptr) {
722         appConfigChangeCallback_(config);
723     }
724 }
725 
SetFont(const std::string & font)726 void ApplicationContext::SetFont(const std::string &font)
727 {
728     TAG_LOGD(AAFwkTag::APPKIT, "font:%{public}s", font.c_str());
729     #ifdef SUPPORT_GRAPHICS
730     // Notify Window
731     AppExecFwk::Configuration config;
732     config.AddItem(AppExecFwk::ConfigurationInner::APPLICATION_FONT, font);
733     if (appFontCallback_ != nullptr) {
734         appFontCallback_(config);
735     }
736     #endif
737 }
738 
SetFontSizeScale(double fontSizeScale)739 bool ApplicationContext::SetFontSizeScale(double fontSizeScale)
740 {
741     if (contextImpl_ == nullptr) {
742         TAG_LOGE(AAFwkTag::APPKIT, "null context");
743         return false;
744     }
745 
746     AppExecFwk::Configuration config;
747     config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE, std::to_string(fontSizeScale));
748     if (appConfigChangeCallback_ != nullptr) {
749         appConfigChangeCallback_(config);
750     }
751     TAG_LOGD(AAFwkTag::APPKIT, "SetFontSizeScale callback ok");
752     return true;
753 }
754 
SetMcc(const std::string & mcc)755 void ApplicationContext::SetMcc(const std::string &mcc)
756 {
757     if (contextImpl_ != nullptr) {
758         contextImpl_->SetMcc(mcc);
759     }
760 }
761 
SetMnc(const std::string & mnc)762 void ApplicationContext::SetMnc(const std::string &mnc)
763 {
764     if (contextImpl_ != nullptr) {
765         contextImpl_->SetMnc(mnc);
766     }
767 }
768 
ClearUpApplicationData()769 void ApplicationContext::ClearUpApplicationData()
770 {
771     if (contextImpl_ != nullptr) {
772         contextImpl_->ClearUpApplicationData();
773     }
774 }
775 
GetArea()776 int ApplicationContext::GetArea()
777 {
778     if (contextImpl_ == nullptr) {
779         TAG_LOGE(AAFwkTag::APPKIT, "contextImpl is nullptr");
780         return ContextImpl::EL_DEFAULT;
781     }
782     return contextImpl_->GetArea();
783 }
784 
GetConfiguration() const785 std::shared_ptr<AppExecFwk::Configuration> ApplicationContext::GetConfiguration() const
786 {
787     return (contextImpl_ != nullptr) ? contextImpl_->GetConfiguration() : nullptr;
788 }
789 
GetBaseDir() const790 std::string ApplicationContext::GetBaseDir() const
791 {
792     return (contextImpl_ != nullptr) ? contextImpl_->GetBaseDir() : nullptr;
793 }
794 
GetDeviceType() const795 Global::Resource::DeviceType ApplicationContext::GetDeviceType() const
796 {
797     return (contextImpl_ != nullptr) ? contextImpl_->GetDeviceType() : Global::Resource::DeviceType::DEVICE_PHONE;
798 }
799 
RegisterAppConfigUpdateObserver(AppConfigUpdateCallback appConfigChangeCallback)800 void ApplicationContext::RegisterAppConfigUpdateObserver(AppConfigUpdateCallback appConfigChangeCallback)
801 {
802     appConfigChangeCallback_ = appConfigChangeCallback;
803 }
804 
RegisterAppFontObserver(AppConfigUpdateCallback appFontCallback)805 void ApplicationContext::RegisterAppFontObserver(AppConfigUpdateCallback appFontCallback)
806 {
807     appFontCallback_ = appFontCallback;
808 }
809 
GetAppRunningUniqueId() const810 std::string ApplicationContext::GetAppRunningUniqueId() const
811 {
812     TAG_LOGD(AAFwkTag::APPKIT, "GetAppRunningUniqueId is %{public}s", appRunningUniqueId_.c_str());
813     return appRunningUniqueId_;
814 }
815 
GetCurrentAppCloneIndex()816 int32_t ApplicationContext::GetCurrentAppCloneIndex()
817 {
818     TAG_LOGD(AAFwkTag::APPKIT, "getCurrentAppCloneIndex is %{public}d", appIndex_);
819     return appIndex_;
820 }
821 
GetCurrentAppMode()822 int32_t ApplicationContext::GetCurrentAppMode()
823 {
824     TAG_LOGD(AAFwkTag::APPKIT, "getCurrentMode is %{public}d", appMode_);
825     return appMode_;
826 }
827 
GetCurrentInstanceKey()828 std::string ApplicationContext::GetCurrentInstanceKey()
829 {
830     TAG_LOGD(AAFwkTag::APPKIT, "getCurrentInstanceKey is %{public}s", instanceKey_.c_str());
831     return instanceKey_;
832 }
833 
SetAppRunningUniqueId(const std::string & appRunningUniqueId)834 void ApplicationContext::SetAppRunningUniqueId(const std::string &appRunningUniqueId)
835 {
836     TAG_LOGD(AAFwkTag::APPKIT, "SetAppRunningUniqueId is %{public}s", appRunningUniqueId.c_str());
837     appRunningUniqueId_ = appRunningUniqueId;
838 }
839 
SetSupportedProcessCacheSelf(bool isSupport)840 int32_t ApplicationContext::SetSupportedProcessCacheSelf(bool isSupport)
841 {
842     if (contextImpl_ != nullptr) {
843         return contextImpl_->SetSupportedProcessCacheSelf(isSupport);
844     }
845     TAG_LOGE(AAFwkTag::APPKIT, "contextImpl_ is nullptr");
846     return ERR_INVALID_VALUE;
847 }
848 
SetCurrentAppCloneIndex(int32_t appIndex)849 void ApplicationContext::SetCurrentAppCloneIndex(int32_t appIndex)
850 {
851     TAG_LOGD(AAFwkTag::APPKIT, "setCurrentAppCloneIndex is %{public}d", appIndex);
852     appIndex_ = appIndex;
853 }
854 
SetCurrentAppMode(int32_t appMode)855 void ApplicationContext::SetCurrentAppMode(int32_t appMode)
856 {
857     TAG_LOGD(AAFwkTag::APPKIT, "setCurrentAppMode is %{public}d", appMode);
858     appMode_ = appMode;
859 }
860 
SetCurrentInstanceKey(const std::string & instanceKey)861 void ApplicationContext::SetCurrentInstanceKey(const std::string& instanceKey)
862 {
863     TAG_LOGD(AAFwkTag::APPKIT, "setCurrentInstanceKey is %{public}s", instanceKey.c_str());
864     instanceKey_ = instanceKey;
865 }
866 }  // namespace AbilityRuntime
867 }  // namespace OHOS
868