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 <cstdio>
17 #include <cstring>
18 #include <fcntl.h>
19 
20 #include <sys/stat.h>
21 
22 #include "ohos_application.h"
23 
24 #include "ability.h"
25 #include "ability_record_mgr.h"
26 #include "ability_thread.h"
27 #include "app_loader.h"
28 #include "application_context.h"
29 #include "application_cleaner.h"
30 #include "application_impl.h"
31 #include "bundle_mgr_helper.h"
32 #include "configuration_utils.h"
33 #include "context_impl.h"
34 #include "hilog_tag_wrapper.h"
35 #include "hitrace_meter.h"
36 #include "iservice_registry.h"
37 #include "runtime.h"
38 #include "system_ability_definition.h"
39 #include "syspara/parameter.h"
40 #include "ui_ability.h"
41 #include "application_configuration_manager.h"
42 #ifdef SUPPORT_GRAPHICS
43 #include "window.h"
44 #endif
45 
46 namespace OHOS {
47 namespace AppExecFwk {
48 namespace {
49     constexpr const char* PERSIST_DARKMODE_KEY = "persist.ace.darkmode";
50 }
51 REGISTER_APPLICATION(OHOSApplication, OHOSApplication)
52 constexpr int32_t APP_ENVIRONMENT_OVERWRITE = 1;
53 using ApplicationConfigurationManager = AbilityRuntime::ApplicationConfigurationManager;
OHOSApplication()54 OHOSApplication::OHOSApplication()
55 {
56     TAG_LOGD(AAFwkTag::APPKIT, "called");
57     abilityLifecycleCallbacks_.clear();
58     elementsCallbacks_.clear();
59 }
60 
~OHOSApplication()61 OHOSApplication::~OHOSApplication()
62 {
63 }
64 
65 /**
66  *
67  * @brief Called when Ability#onSaveAbilityState(PacMap) was called on an ability.
68  *
69  * @param outState Indicates the PacMap object passed to Ability#onSaveAbilityState(PacMap)
70  * for storing user data and states. This parameter cannot be null.
71  */
72 
DispatchAbilitySavedState(const PacMap & outState)73 void OHOSApplication::DispatchAbilitySavedState(const PacMap &outState)
74 {
75     TAG_LOGD(AAFwkTag::APPKIT, "called");
76     for (auto callback : abilityLifecycleCallbacks_) {
77         if (callback != nullptr) {
78             callback->OnAbilitySaveState(outState);
79         }
80     }
81 }
82 
83 /**
84  *
85  * @brief Will be called the application foregrounds
86  *
87  */
OnForeground()88 void OHOSApplication::OnForeground()
89 {
90     TAG_LOGD(AAFwkTag::APPKIT, "begin");
91     if (abilityRuntimeContext_) {
92         abilityRuntimeContext_->NotifyApplicationForeground();
93     }
94 
95     if (runtime_ == nullptr) {
96         TAG_LOGD(AAFwkTag::APPKIT, "NotifyApplicationState, runtime_ is nullptr");
97         return;
98     }
99     runtime_->NotifyApplicationState(false);
100     TAG_LOGD(AAFwkTag::APPKIT, "NotifyApplicationState::OnForeground end");
101 }
102 
103 /**
104  *
105  * @brief Will be called the application backgrounds
106  *
107  */
OnBackground()108 void OHOSApplication::OnBackground()
109 {
110     TAG_LOGD(AAFwkTag::APPKIT, "begin");
111     if (abilityRuntimeContext_) {
112         abilityRuntimeContext_->NotifyApplicationBackground();
113     }
114 
115     if (runtime_ == nullptr) {
116         TAG_LOGD(AAFwkTag::APPKIT, "runtime_ is nullptr");
117         return;
118     }
119     runtime_->NotifyApplicationState(true);
120 }
121 
DumpApplication()122 void OHOSApplication::DumpApplication()
123 {
124     TAG_LOGD(AAFwkTag::APPKIT, "called");
125     // create and initialize abilityInfos
126     std::shared_ptr<AbilityInfo> abilityInfo = nullptr;
127     std::shared_ptr<AbilityLocalRecord> record = nullptr;
128 
129     if (abilityRecordMgr_) {
130         record = abilityRecordMgr_->GetAbilityItem(abilityRecordMgr_->GetToken());
131     }
132 
133     if (record) {
134         abilityInfo = record->GetAbilityInfo();
135     }
136 
137     if (abilityInfo) {
138         TAG_LOGD(AAFwkTag::APPKIT, "==============AbilityInfo==============");
139         TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: package: %{public}s", abilityInfo->package.c_str());
140         TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: name: %{public}s", abilityInfo->name.c_str());
141         TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: label: %{public}s", abilityInfo->label.c_str());
142         TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: description: %{public}s", abilityInfo->description.c_str());
143         TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: iconPath: %{public}s", abilityInfo->iconPath.c_str());
144         TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: visible: %{public}d", abilityInfo->visible);
145         TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: kind: %{public}s", abilityInfo->kind.c_str());
146         TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: type: %{public}d", abilityInfo->type);
147         TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: orientation: %{public}d", abilityInfo->orientation);
148         TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: launchMode: %{public}d", abilityInfo->launchMode);
149         for (auto permission : abilityInfo->permissions) {
150             TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: permission: %{public}s", permission.c_str());
151         }
152         TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: bundleName: %{public}s", abilityInfo->bundleName.c_str());
153         TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: applicationName: %{public}s", abilityInfo->applicationName.c_str());
154     }
155 
156     // create and initialize applicationInfo
157     std::shared_ptr<ApplicationInfo> applicationInfoPtr = GetApplicationInfo();
158     if (applicationInfoPtr != nullptr) {
159         TAG_LOGD(AAFwkTag::APPKIT, "applicationInfo: name: %{public}s", applicationInfoPtr->name.c_str());
160         TAG_LOGD(AAFwkTag::APPKIT, "applicationInfo: bundleName: %{public}s", applicationInfoPtr->bundleName.c_str());
161         TAG_LOGD(
162             AAFwkTag::APPKIT, "applicationInfo: signatureKey: %{public}s", applicationInfoPtr->signatureKey.c_str());
163     }
164 }
165 
166 /**
167  * @brief Set Runtime
168  *
169  * @param runtime Runtime instance.
170  */
SetRuntime(std::unique_ptr<AbilityRuntime::Runtime> && runtime)171 void OHOSApplication::SetRuntime(std::unique_ptr<AbilityRuntime::Runtime>&& runtime)
172 {
173     TAG_LOGD(AAFwkTag::APPKIT, "begin");
174     if (runtime == nullptr) {
175         TAG_LOGE(AAFwkTag::APPKIT, "runtime is empty");
176         return;
177     }
178     runtime_ = std::move(runtime);
179 }
180 
181 /**
182  * @brief Set ApplicationContext
183  *
184  * @param abilityRuntimeContext ApplicationContext instance.
185  */
SetApplicationContext(const std::shared_ptr<AbilityRuntime::ApplicationContext> & abilityRuntimeContext)186 void OHOSApplication::SetApplicationContext(
187     const std::shared_ptr<AbilityRuntime::ApplicationContext> &abilityRuntimeContext)
188 {
189     TAG_LOGD(AAFwkTag::APPKIT, "called");
190     if (abilityRuntimeContext == nullptr) {
191         TAG_LOGE(AAFwkTag::APPKIT, "context is empty");
192         return;
193     }
194     abilityRuntimeContext_ = abilityRuntimeContext;
195     auto application = std::static_pointer_cast<OHOSApplication>(shared_from_this());
196     std::weak_ptr<OHOSApplication> applicationWptr = application;
197     abilityRuntimeContext_->RegisterAppConfigUpdateObserver([applicationWptr](const Configuration &config) {
198         std::shared_ptr<OHOSApplication> applicationSptr = applicationWptr.lock();
199         if (applicationSptr == nullptr) {
200             TAG_LOGE(AAFwkTag::APPKIT, "application is nullptr.");
201             return;
202         }
203         applicationSptr->OnConfigurationUpdated(config, AbilityRuntime::SetLevel::Application);
204     });
205     abilityRuntimeContext_->RegisterAppFontObserver([applicationWptr](const Configuration &config) {
206         std::shared_ptr<OHOSApplication> applicationSptr = applicationWptr.lock();
207         if (applicationSptr == nullptr) {
208             TAG_LOGE(AAFwkTag::APPKIT, "application is nullptr.");
209             return;
210         }
211         applicationSptr->OnFontUpdated(config);
212     });
213 }
214 
215 /**
216  *
217  * @brief Set the abilityRecordMgr to the OHOSApplication.
218  *
219  * @param abilityRecordMgr
220  */
SetAbilityRecordMgr(const std::shared_ptr<AbilityRecordMgr> & abilityRecordMgr)221 void OHOSApplication::SetAbilityRecordMgr(const std::shared_ptr<AbilityRecordMgr> &abilityRecordMgr)
222 {
223     TAG_LOGD(AAFwkTag::APPKIT, "called");
224     if (abilityRecordMgr == nullptr) {
225         TAG_LOGE(AAFwkTag::APPKIT, "abilityRecordMgr is nullptr");
226         return;
227     }
228     abilityRecordMgr_ = abilityRecordMgr;
229 }
230 
231 /**
232  *
233  * Register AbilityLifecycleCallbacks with OHOSApplication
234  *
235  * @param callBack callBack When the life cycle of the ability in the application changes,
236  */
RegisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> & callBack)237 void OHOSApplication::RegisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> &callBack)
238 {
239     TAG_LOGD(AAFwkTag::APPKIT, "called");
240 
241     if (callBack == nullptr) {
242         TAG_LOGD(AAFwkTag::APPKIT, "observer is null");
243         return;
244     }
245 
246     abilityLifecycleCallbacks_.emplace_back(callBack);
247 }
248 
249 /**
250  *
251  * Unregister AbilityLifecycleCallbacks with OHOSApplication
252  *
253  * @param callBack RegisterAbilityLifecycleCallbacks`s callBack
254  */
UnregisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> & callBack)255 void OHOSApplication::UnregisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> &callBack)
256 {
257     TAG_LOGD(AAFwkTag::APPKIT, "called");
258 
259     if (callBack == nullptr) {
260         TAG_LOGD(AAFwkTag::APPKIT, "observer is null");
261         return;
262     }
263 
264     abilityLifecycleCallbacks_.remove(callBack);
265 }
266 
267 /**
268  *
269  * Will be called when the given ability calls Ability->onStart
270  *
271  * @param Ability Indicates the ability object that calls the onStart() method.
272  */
OnAbilityStart(const std::shared_ptr<AbilityRuntime::UIAbility> & ability)273 void OHOSApplication::OnAbilityStart(const std::shared_ptr<AbilityRuntime::UIAbility> &ability)
274 {
275     if (ability == nullptr) {
276         TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
277         return;
278     }
279 
280     TAG_LOGD(AAFwkTag::APPKIT, "called");
281     for (auto callback : abilityLifecycleCallbacks_) {
282         if (callback != nullptr) {
283             callback->OnAbilityStart(ability);
284         }
285     }
286 }
287 
288 /**
289  *
290  * Will be called when the given ability calls Ability->onInactive
291  *
292  * @param Ability Indicates the Ability object that calls the onInactive() method.
293  */
OnAbilityInactive(const std::shared_ptr<AbilityRuntime::UIAbility> & ability)294 void OHOSApplication::OnAbilityInactive(const std::shared_ptr<AbilityRuntime::UIAbility> &ability)
295 {
296     if (ability == nullptr) {
297         TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
298         return;
299     }
300 
301     TAG_LOGD(AAFwkTag::APPKIT, "called");
302     for (auto callback : abilityLifecycleCallbacks_) {
303         if (callback != nullptr) {
304             callback->OnAbilityInactive(ability);
305         }
306     }
307 }
308 
309 /**
310  *
311  * Will be called when the given ability calls Ability->onBackground
312  *
313  * @param Ability Indicates the Ability object that calls the onBackground() method.
314  */
OnAbilityBackground(const std::shared_ptr<AbilityRuntime::UIAbility> & ability)315 void OHOSApplication::OnAbilityBackground(const std::shared_ptr<AbilityRuntime::UIAbility> &ability)
316 {
317     if (ability == nullptr) {
318         TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
319         return;
320     }
321 
322     TAG_LOGD(AAFwkTag::APPKIT, "called");
323     for (auto callback : abilityLifecycleCallbacks_) {
324         if (callback != nullptr) {
325             callback->OnAbilityBackground(ability);
326         }
327     }
328 }
329 
330 /**
331  *
332  * Will be called when the given ability calls Ability->onForeground
333  *
334  * @param Ability Indicates the Ability object that calls the onForeground() method.
335  */
OnAbilityForeground(const std::shared_ptr<AbilityRuntime::UIAbility> & ability)336 void OHOSApplication::OnAbilityForeground(const std::shared_ptr<AbilityRuntime::UIAbility> &ability)
337 {
338     if (ability == nullptr) {
339         TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
340         return;
341     }
342 
343     TAG_LOGD(AAFwkTag::APPKIT, "called");
344     for (auto callback : abilityLifecycleCallbacks_) {
345         if (callback != nullptr) {
346             callback->OnAbilityForeground(ability);
347         }
348     }
349 }
350 
351 /**
352  *
353  * Will be called when the given ability calls Ability->onActive
354  *
355  * @param Ability Indicates the Ability object that calls the onActive() method.
356  */
OnAbilityActive(const std::shared_ptr<AbilityRuntime::UIAbility> & ability)357 void OHOSApplication::OnAbilityActive(const std::shared_ptr<AbilityRuntime::UIAbility> &ability)
358 {
359     if (ability == nullptr) {
360         TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
361         return;
362     }
363 
364     TAG_LOGD(AAFwkTag::APPKIT, "called");
365     for (auto callback : abilityLifecycleCallbacks_) {
366         if (callback != nullptr) {
367             callback->OnAbilityActive(ability);
368         }
369     }
370 }
371 
372 /**
373  *
374  * Will be called when the given ability calls Ability->onStop
375  *
376  * @param Ability Indicates the Ability object that calls the onStop() method.
377  */
OnAbilityStop(const std::shared_ptr<AbilityRuntime::UIAbility> & ability)378 void OHOSApplication::OnAbilityStop(const std::shared_ptr<AbilityRuntime::UIAbility> &ability)
379 {
380     if (ability == nullptr) {
381         TAG_LOGE(AAFwkTag::APPKIT, "ability is nullptr");
382         return;
383     }
384 
385     TAG_LOGD(AAFwkTag::APPKIT, "called");
386     for (auto callback : abilityLifecycleCallbacks_) {
387         if (callback != nullptr) {
388             callback->OnAbilityStop(ability);
389         }
390     }
391 }
392 
393 /**
394  *
395  * @brief Register ElementsCallback with OHOSApplication
396  *
397  * @param callBack callBack when the system configuration of the device changes.
398  */
RegisterElementsCallbacks(const std::shared_ptr<ElementsCallback> & callback)399 void OHOSApplication::RegisterElementsCallbacks(const std::shared_ptr<ElementsCallback> &callback)
400 {
401     TAG_LOGD(AAFwkTag::APPKIT, "called");
402 
403     if (callback == nullptr) {
404         TAG_LOGD(AAFwkTag::APPKIT, "observer is null");
405         return;
406     }
407 
408     elementsCallbacks_.emplace_back(callback);
409 }
410 
411 /**
412  *
413  * @brief Unregister ElementsCallback with OHOSApplication
414  *
415  * @param callback RegisterElementsCallbacks`s callback
416  */
UnregisterElementsCallbacks(const std::shared_ptr<ElementsCallback> & callback)417 void OHOSApplication::UnregisterElementsCallbacks(const std::shared_ptr<ElementsCallback> &callback)
418 {
419     TAG_LOGD(AAFwkTag::APPKIT, "called");
420 
421     if (callback == nullptr) {
422         TAG_LOGD(AAFwkTag::APPKIT, "observer is null");
423         return;
424     }
425 
426     elementsCallbacks_.remove(callback);
427 }
428 
429 /**
430  *
431  * @brief Will be Called when the system configuration of the device changes.
432  *
433  * @param config Indicates the new Configuration object.
434  */
OnConfigurationUpdated(Configuration config,AbilityRuntime::SetLevel level)435 void OHOSApplication::OnConfigurationUpdated(Configuration config, AbilityRuntime::SetLevel level)
436 {
437     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
438     if (!abilityRecordMgr_ || !configuration_) {
439         TAG_LOGD(AAFwkTag::APPKIT, "abilityRecordMgr_ or configuration_ is null");
440         return;
441     }
442     bool isUpdateAppColor = IsUpdateColorNeeded(config, level);
443     bool isUpdateAppFontSize = isUpdateFontSize(config, level);
444     bool isUpdateAppLanguage = IsUpdateLanguageNeeded(config, level);
445     if (!isUpdateAppColor && !isUpdateAppFontSize && !isUpdateAppLanguage && config.GetItemSize() == 0) {
446         TAG_LOGD(AAFwkTag::APPKIT, "configuration need not updated");
447         return;
448     }
449     std::vector<std::string> changeKeyV;
450     {
451         HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "configuration_->CompareDifferent");
452         configuration_->CompareDifferent(changeKeyV, config);
453         configuration_->Merge(changeKeyV, config);
454     }
455     TAG_LOGI(AAFwkTag::APPKIT, "configuration_: %{public}s, config: %{public}s",
456         configuration_->GetName().c_str(), config.GetName().c_str());
457     // Update resConfig of resource manager, which belongs to application context.
458     UpdateAppContextResMgr(config);
459     #ifdef SUPPORT_GRAPHICS
460         auto diffSyncConfiguration = std::make_shared<AppExecFwk::Configuration>(config);
461         Rosen::Window::UpdateConfigurationSyncForAll(diffSyncConfiguration);
462     #endif
463     // Notify all abilities
464     for (const auto &abilityToken : abilityRecordMgr_->GetAllTokens()) {
465         auto abilityRecord = abilityRecordMgr_->GetAbilityItem(abilityToken);
466         if (abilityRecord && abilityRecord->GetAbilityThread()) {
467             abilityRecord->GetAbilityThread()->ScheduleUpdateConfiguration(config);
468         }
469     }
470     for (auto it = abilityStages_.begin(); it != abilityStages_.end(); it++) {
471         auto abilityStage = it->second;
472         if (abilityStage) {
473             abilityStage->OnConfigurationUpdated(config);
474         }
475     }
476 #ifdef SUPPORT_GRAPHICS
477     auto diffConfiguration = std::make_shared<AppExecFwk::Configuration>(config);
478     Rosen::Window::UpdateConfigurationForAll(diffConfiguration);
479 #endif
480 
481     for (auto callback : elementsCallbacks_) {
482         if (callback != nullptr) {
483             callback->OnConfigurationUpdated(nullptr, config);
484         }
485     }
486     abilityRuntimeContext_->DispatchConfigurationUpdated(*configuration_);
487     abilityRuntimeContext_->SetMcc(configuration_->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_MCC));
488     abilityRuntimeContext_->SetMnc(configuration_->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_MNC));
489     abilityRuntimeContext_->SetConfiguration(configuration_);
490 }
491 
492 /**
493  *
494  * @brief Will be Called when the application font of the device changes.
495  *
496  * @param config Indicates the new Configuration object.
497  */
OnFontUpdated(Configuration config)498 void OHOSApplication::OnFontUpdated(Configuration config)
499 {
500     #ifdef SUPPORT_GRAPHICS
501     // Notify Window
502     auto diffConfiguration = std::make_shared<AppExecFwk::Configuration>(config);
503     Rosen::Window::UpdateConfigurationForAll(diffConfiguration);
504     #endif
505 }
506 
507 /**
508  *
509  * @brief Called when the system has determined to trim the memory, for example,
510  * when the ability is running in the background and there is no enough memory for
511  * running as many background processes as possible.
512  *
513  * @param level Indicates the memory trim level, which shows the current memory usage status.
514  */
OnMemoryLevel(int level)515 void OHOSApplication::OnMemoryLevel(int level)
516 {
517     TAG_LOGD(AAFwkTag::APPKIT, "called");
518 
519     if (abilityRecordMgr_) {
520         TAG_LOGD(
521             AAFwkTag::APPKIT, "Number of ability to be notified : [%{public}d]", abilityRecordMgr_->GetRecordCount());
522         for (const auto &abilityToken : abilityRecordMgr_->GetAllTokens()) {
523             auto abilityRecord = abilityRecordMgr_->GetAbilityItem(abilityToken);
524             if (abilityRecord && abilityRecord->GetAbilityThread()) {
525                 abilityRecord->GetAbilityThread()->NotifyMemoryLevel(level);
526             }
527         }
528     }
529 
530     TAG_LOGD(AAFwkTag::APPKIT, "Number of abilityStage to be notified : [%{public}zu]", abilityStages_.size());
531     for (auto it = abilityStages_.begin(); it != abilityStages_.end(); it++) {
532         auto abilityStage = it->second;
533         if (abilityStage) {
534             abilityStage->OnMemoryLevel(level);
535         }
536     }
537 
538     TAG_LOGD(AAFwkTag::APPKIT, "called");
539     for (auto callback : elementsCallbacks_) {
540         if (callback != nullptr) {
541             callback->OnMemoryLevel(level);
542         }
543     }
544 
545     abilityRuntimeContext_->DispatchMemoryLevel(level);
546 }
547 
548 /**
549  *
550  * @brief Will be called the application starts
551  *
552  */
OnStart()553 void OHOSApplication::OnStart()
554 {}
555 
556 /**
557  *
558  * @brief Will be called the application ends
559  */
OnTerminate()560 void OHOSApplication::OnTerminate()
561 {}
562 
563 /**
564  *
565  * @brief Called when an ability calls Ability#onSaveAbilityState(PacMap).
566  * You can implement your own logic in this method.
567  * @param outState IIndicates the {@link PacMap} object passed to the onSaveAbilityState() callback.
568  *
569  */
OnAbilitySaveState(const PacMap & outState)570 void OHOSApplication::OnAbilitySaveState(const PacMap &outState)
571 {
572     DispatchAbilitySavedState(outState);
573 }
574 
SetAppEnv(const std::vector<AppEnvironment> & appEnvironments)575 void OHOSApplication::SetAppEnv(const std::vector<AppEnvironment>& appEnvironments)
576 {
577     if (!appEnvironments.size()) {
578         return;
579     }
580 
581     for (const auto &appEnvironment : appEnvironments) {
582         if (setenv(appEnvironment.name.c_str(), appEnvironment.value.c_str(), APP_ENVIRONMENT_OVERWRITE)) {
583             TAG_LOGE(AAFwkTag::APPKIT, "appEnvironment: %{public}s set failed", appEnvironment.name.c_str());
584             return;
585         }
586         TAG_LOGI(AAFwkTag::APPKIT, "appEnvironment set successfully: %{public}s = %{public}s",
587             appEnvironment.name.c_str(), appEnvironment.value.c_str());
588     }
589     return;
590 }
591 
AddAbilityStage(const std::shared_ptr<AbilityLocalRecord> & abilityRecord,const std::function<void (const std::shared_ptr<AbilityRuntime::Context> &)> & callback,bool & isAsyncCallback)592 std::shared_ptr<AbilityRuntime::Context> OHOSApplication::AddAbilityStage(
593     const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
594     const std::function<void(const std::shared_ptr<AbilityRuntime::Context> &)> &callback, bool &isAsyncCallback)
595 {
596     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
597     if (abilityRecord == nullptr) {
598         TAG_LOGE(AAFwkTag::APPKIT, "abilityRecord is nullptr");
599         return nullptr;
600     }
601     const std::shared_ptr<AbilityInfo> &abilityInfo = abilityRecord->GetAbilityInfo();
602     if (abilityInfo == nullptr) {
603         TAG_LOGE(AAFwkTag::APPKIT, "abilityInfo is nullptr");
604         return nullptr;
605     }
606     std::string moduleName = abilityInfo->moduleName;
607     std::shared_ptr<AbilityRuntime::AbilityStage> abilityStage;
608     auto iterator = abilityStages_.find(moduleName);
609     if (iterator == abilityStages_.end()) {
610         auto stageContext = std::make_shared<AbilityRuntime::AbilityStageContext>();
611         stageContext->SetParentContext(abilityRuntimeContext_);
612         stageContext->InitHapModuleInfo(abilityInfo);
613         stageContext->SetConfiguration(GetConfiguration());
614         std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfo = stageContext->GetHapModuleInfo();
615         if (hapModuleInfo == nullptr) {
616             TAG_LOGE(AAFwkTag::APPKIT, "hapModuleInfo is nullptr");
617             return nullptr;
618         }
619         if (runtime_) {
620             runtime_->UpdatePkgContextInfoJson(
621                 hapModuleInfo->moduleName, hapModuleInfo->hapPath, hapModuleInfo->packageName);
622         }
623         SetAppEnv(hapModuleInfo->appEnvironments);
624 
625         if (abilityInfo->applicationInfo.multiProjects) {
626             auto moduleContext = stageContext->CreateModuleContext(hapModuleInfo->moduleName);
627             auto rm = moduleContext != nullptr ? moduleContext->GetResourceManager() : nullptr;
628             stageContext->SetResourceManager(rm);
629         }
630 
631         abilityStage = AbilityRuntime::AbilityStage::Create(runtime_, *hapModuleInfo);
632         auto application = std::static_pointer_cast<OHOSApplication>(shared_from_this());
633         std::weak_ptr<OHOSApplication> weak = application;
634         abilityStage->Init(stageContext, weak);
635         auto autoStartupCallback = CreateAutoStartupCallback(abilityStage, abilityRecord, callback);
636         if (autoStartupCallback != nullptr) {
637             abilityStage->RunAutoStartupTask(autoStartupCallback, isAsyncCallback, stageContext);
638             if (isAsyncCallback) {
639                 TAG_LOGI(AAFwkTag::APPKIT, "waiting for startup");
640                 return nullptr;
641             }
642         }
643 
644         Want want;
645         if (abilityRecord->GetWant()) {
646             TAG_LOGD(AAFwkTag::APPKIT, "want is ok, transport to abilityStage");
647             want = *(abilityRecord->GetWant());
648         }
649         abilityStage->OnCreate(want);
650         abilityStages_[moduleName] = abilityStage;
651     } else {
652         abilityStage = iterator->second;
653     }
654     const sptr<IRemoteObject> &token = abilityRecord->GetToken();
655     if (token == nullptr) {
656         TAG_LOGE(AAFwkTag::APPKIT, "token is null");
657         return nullptr;
658     }
659     abilityStage->AddAbility(token, abilityRecord);
660     return abilityStage->GetContext();
661 }
662 
CreateAutoStartupCallback(const std::shared_ptr<AbilityRuntime::AbilityStage> abilityStage,const std::shared_ptr<AbilityLocalRecord> abilityRecord,const std::function<void (const std::shared_ptr<AbilityRuntime::Context> &)> & callback)663 const std::function<void()> OHOSApplication::CreateAutoStartupCallback(
664     const std::shared_ptr<AbilityRuntime::AbilityStage> abilityStage,
665     const std::shared_ptr<AbilityLocalRecord> abilityRecord,
666     const std::function<void(const std::shared_ptr<AbilityRuntime::Context>&)>& callback)
667 {
668     const std::shared_ptr<AbilityInfo> &abilityInfo = abilityRecord->GetAbilityInfo();
669     if (abilityInfo == nullptr) {
670         TAG_LOGE(AAFwkTag::APPKIT, "null abilityInfo");
671         return nullptr;
672     }
673     if (!IsMainProcess(abilityInfo->bundleName, abilityInfo->applicationInfo.process)) {
674         return nullptr;
675     }
676     std::string moduleName = abilityInfo->moduleName;
677     auto application = std::static_pointer_cast<OHOSApplication>(shared_from_this());
678     std::weak_ptr<OHOSApplication> weak = application;
679 
680     auto autoStartupCallback = [weak, abilityStage, abilityRecord, moduleName, callback]() {
681         auto ohosApplication = weak.lock();
682         if (ohosApplication == nullptr) {
683             TAG_LOGE(AAFwkTag::APPKIT, "null ohosApplication");
684             return;
685         }
686         ohosApplication->AutoStartupDone(abilityRecord, abilityStage, moduleName);
687         if (callback == nullptr) {
688             TAG_LOGE(AAFwkTag::APPKIT, "null callback");
689             return;
690         }
691         callback(abilityStage->GetContext());
692     };
693 
694     return autoStartupCallback;
695 }
696 
CreateAutoStartupCallback(const std::shared_ptr<AbilityRuntime::AbilityStage> & abilityStage,const AppExecFwk::HapModuleInfo & hapModuleInfo,const std::function<void ()> & callback)697 const std::function<void()> OHOSApplication::CreateAutoStartupCallback(
698     const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage,
699     const AppExecFwk::HapModuleInfo &hapModuleInfo,
700     const std::function<void()>& callback)
701 {
702     auto applicationInfo = abilityRuntimeContext_->GetApplicationInfo();
703     if (applicationInfo == nullptr) {
704         TAG_LOGE(AAFwkTag::APPKIT, "null applicationInfo");
705         return nullptr;
706     }
707     if (!IsMainProcess(hapModuleInfo.bundleName, applicationInfo->process)) {
708         return nullptr;
709     }
710     auto application = std::static_pointer_cast<OHOSApplication>(shared_from_this());
711     std::weak_ptr<OHOSApplication> weak = application;
712 
713     auto autoStartupCallback = [weak, abilityStage, hapModuleInfo, callback]() {
714         auto ohosApplication = weak.lock();
715         if (ohosApplication == nullptr) {
716             TAG_LOGE(AAFwkTag::APPKIT, "null ohosApplication");
717             return;
718         }
719         ohosApplication->AutoStartupDone(abilityStage, hapModuleInfo);
720         if (callback == nullptr) {
721             TAG_LOGE(AAFwkTag::APPKIT, "null callback");
722             return;
723         }
724         callback();
725     };
726 
727     return autoStartupCallback;
728 }
729 
AutoStartupDone(const std::shared_ptr<AbilityLocalRecord> & abilityRecord,const std::shared_ptr<AbilityRuntime::AbilityStage> & abilityStage,const std::string & moduleName)730 void OHOSApplication::AutoStartupDone(const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
731     const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage, const std::string &moduleName)
732 {
733     Want want;
734     if (abilityRecord->GetWant()) {
735         TAG_LOGD(AAFwkTag::APPKIT, "want is ok, transport to abilityStage");
736         want = *(abilityRecord->GetWant());
737     }
738     if (abilityStage == nullptr) {
739         TAG_LOGE(AAFwkTag::APPKIT, "abilityStage is nullptr");
740         return;
741     }
742     abilityStage->OnCreate(want);
743     abilityStages_[moduleName] = abilityStage;
744     const sptr<IRemoteObject> &token = abilityRecord->GetToken();
745     if (token == nullptr) {
746         TAG_LOGE(AAFwkTag::APPKIT, "token is null");
747         return;
748     }
749     abilityStage->AddAbility(token, abilityRecord);
750 }
751 
AutoStartupDone(const std::shared_ptr<AbilityRuntime::AbilityStage> & abilityStage,const AppExecFwk::HapModuleInfo & hapModuleInfo)752 void OHOSApplication::AutoStartupDone(
753     const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage,
754     const AppExecFwk::HapModuleInfo &hapModuleInfo)
755 {
756     if (abilityStage == nullptr) {
757         TAG_LOGE(AAFwkTag::APPKIT, "abilityStage is nullptr");
758         return;
759     }
760 
761     Want want;
762     abilityStage->OnCreate(want);
763     abilityStages_[hapModuleInfo.moduleName] = abilityStage;
764     TAG_LOGI(AAFwkTag::APPKIT, "abilityStage insert and initialization");
765     return;
766 }
767 
768 /**
769  *
770  * @brief update the application info after new module installed.
771  *
772  * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext.
773  *
774  */
UpdateApplicationInfoInstalled(const AppExecFwk::ApplicationInfo & appInfo)775 void OHOSApplication::UpdateApplicationInfoInstalled(const AppExecFwk::ApplicationInfo &appInfo)
776 {
777     TAG_LOGD(AAFwkTag::APPKIT, "called");
778 
779     if (abilityRuntimeContext_ == nullptr) {
780         TAG_LOGE(AAFwkTag::APPKIT, "abilityRuntimeContext_ is nullptr");
781         return;
782     }
783 
784     abilityRuntimeContext_->SetApplicationInfo(std::make_shared<AppExecFwk::ApplicationInfo>(appInfo));
785 }
786 
AddAbilityStage(const AppExecFwk::HapModuleInfo & hapModuleInfo,const std::function<void ()> & callback,bool & isAsyncCallback)787 bool OHOSApplication::AddAbilityStage(
788     const AppExecFwk::HapModuleInfo &hapModuleInfo,
789     const std::function<void()> &callback, bool &isAsyncCallback)
790 {
791     TAG_LOGD(AAFwkTag::APPKIT, "called");
792     if (abilityRuntimeContext_ == nullptr) {
793         TAG_LOGE(AAFwkTag::APPKIT, "abilityRuntimeContext_ is nullptr");
794         return false;
795     }
796 
797     if (runtime_ == nullptr) {
798         TAG_LOGE(AAFwkTag::APPKIT, "runtime_ is nullptr");
799         return false;
800     }
801 
802     if (abilityStages_.find(hapModuleInfo.moduleName) != abilityStages_.end()) {
803         TAG_LOGE(AAFwkTag::APPKIT, "object exist");
804         return false;
805     }
806 
807     auto stageContext = std::make_shared<AbilityRuntime::AbilityStageContext>();
808     stageContext->SetParentContext(abilityRuntimeContext_);
809     stageContext->InitHapModuleInfo(hapModuleInfo);
810     stageContext->SetConfiguration(GetConfiguration());
811     auto moduleInfo = stageContext->GetHapModuleInfo();
812     if (moduleInfo == nullptr) {
813         TAG_LOGE(AAFwkTag::APPKIT, "moduleInfo is nullptr");
814         return false;
815     }
816 
817     if (abilityRuntimeContext_->GetApplicationInfo() && abilityRuntimeContext_->GetApplicationInfo()->multiProjects) {
818         auto rm = stageContext->CreateModuleContext(hapModuleInfo.moduleName)->GetResourceManager();
819         stageContext->SetResourceManager(rm);
820     }
821 
822     auto abilityStage = AbilityRuntime::AbilityStage::Create(runtime_, *moduleInfo);
823     auto application = std::static_pointer_cast<OHOSApplication>(shared_from_this());
824     std::weak_ptr<OHOSApplication> weak = application;
825     abilityStage->Init(stageContext, weak);
826     auto autoStartupCallback = CreateAutoStartupCallback(abilityStage, hapModuleInfo, callback);
827     if (autoStartupCallback != nullptr) {
828         abilityStage->RunAutoStartupTask(autoStartupCallback, isAsyncCallback, stageContext);
829         if (isAsyncCallback) {
830             TAG_LOGI(AAFwkTag::APPKIT, "waiting for startup");
831             return false;
832         }
833     }
834     Want want;
835     abilityStage->OnCreate(want);
836     abilityStages_[hapModuleInfo.moduleName] = abilityStage;
837     TAG_LOGI(AAFwkTag::APPKIT, "abilityStage insert and initialization");
838     return true;
839 }
840 
CleanAbilityStage(const sptr<IRemoteObject> & token,const std::shared_ptr<AbilityInfo> & abilityInfo,bool isCacheProcess)841 void OHOSApplication::CleanAbilityStage(const sptr<IRemoteObject> &token,
842     const std::shared_ptr<AbilityInfo> &abilityInfo, bool isCacheProcess)
843 {
844     if (abilityInfo == nullptr) {
845         TAG_LOGE(AAFwkTag::APPKIT, "abilityInfo is nullptr");
846         return;
847     }
848     if (token == nullptr) {
849         TAG_LOGE(AAFwkTag::APPKIT, "token is nullptr");
850         return;
851     }
852     std::string moduleName = abilityInfo->moduleName;
853     auto iterator = abilityStages_.find(moduleName);
854     if (iterator != abilityStages_.end()) {
855         auto abilityStage = iterator->second;
856         abilityStage->RemoveAbility(token);
857         if (!abilityStage->ContainsAbility() && !isCacheProcess) {
858             abilityStage->OnDestroy();
859             abilityStages_.erase(moduleName);
860         }
861     }
862 }
863 
GetAppContext() const864 std::shared_ptr<AbilityRuntime::Context> OHOSApplication::GetAppContext() const
865 {
866     return abilityRuntimeContext_;
867 }
868 
GetRuntime()869 const std::unique_ptr<AbilityRuntime::Runtime>& OHOSApplication::GetRuntime()
870 {
871     return runtime_;
872 }
873 
SetConfiguration(const Configuration & config)874 void OHOSApplication::SetConfiguration(const Configuration &config)
875 {
876     if (!configuration_) {
877         configuration_ = std::make_shared<Configuration>(config);
878     }
879     auto colorMode = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
880     AbilityRuntime::ApplicationConfigurationManager::GetInstance().
881         SetColorModeSetLevel(AbilityRuntime::SetLevel::System, colorMode);
882 
883     if (abilityRuntimeContext_ && configuration_) {
884         abilityRuntimeContext_->SetConfiguration(configuration_);
885     }
886 }
887 
ScheduleAcceptWant(const AAFwk::Want & want,const std::string & moduleName,std::string & flag)888 void OHOSApplication::ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName, std::string &flag)
889 {
890     TAG_LOGD(AAFwkTag::APPKIT, "called");
891     auto iter = abilityStages_.find(moduleName);
892     if (iter != abilityStages_.end()) {
893         auto abilityStage = iter->second;
894         if (abilityStage) {
895             flag = abilityStage->OnAcceptWant(want);
896         }
897     }
898 }
899 
ScheduleNewProcessRequest(const AAFwk::Want & want,const std::string & moduleName,std::string & flag)900 void OHOSApplication::ScheduleNewProcessRequest(const AAFwk::Want &want, const std::string &moduleName,
901     std::string &flag)
902 {
903     TAG_LOGD(AAFwkTag::APPKIT, "call");
904     if (abilityStages_.empty()) {
905         TAG_LOGE(AAFwkTag::APPKIT, "abilityStages_ is empty");
906         return;
907     }
908     auto iter = abilityStages_.find(moduleName);
909     if (iter == abilityStages_.end()) {
910         TAG_LOGE(AAFwkTag::APPKIT, "%{public}s is not in abilityStage", moduleName.c_str());
911         return;
912     }
913     auto abilityStage = iter->second;
914     if (abilityStage) {
915         flag = abilityStage->OnNewProcessRequest(want);
916     }
917 }
918 
GetConfiguration()919 std::shared_ptr<Configuration> OHOSApplication::GetConfiguration()
920 {
921     return configuration_;
922 }
923 
SetExtensionTypeMap(std::map<int32_t,std::string> map)924 void OHOSApplication::SetExtensionTypeMap(std::map<int32_t, std::string> map)
925 {
926     extensionTypeMap_ = map;
927 }
928 
NotifyLoadRepairPatch(const std::string & hqfFile,const std::string & hapPath)929 bool OHOSApplication::NotifyLoadRepairPatch(const std::string &hqfFile, const std::string &hapPath)
930 {
931     if (runtime_ == nullptr) {
932         TAG_LOGD(AAFwkTag::APPKIT, "runtime is nullptr");
933         return true;
934     }
935 
936     return runtime_->LoadRepairPatch(hqfFile, hapPath);
937 }
938 
NotifyHotReloadPage()939 bool OHOSApplication::NotifyHotReloadPage()
940 {
941     if (runtime_ == nullptr) {
942         TAG_LOGD(AAFwkTag::APPKIT, "runtime is nullptr");
943         return true;
944     }
945 
946     return runtime_->NotifyHotReloadPage();
947 }
948 
NotifyUnLoadRepairPatch(const std::string & hqfFile)949 bool OHOSApplication::NotifyUnLoadRepairPatch(const std::string &hqfFile)
950 {
951     if (runtime_ == nullptr) {
952         TAG_LOGD(AAFwkTag::APPKIT, "runtime is nullptr");
953         return true;
954     }
955 
956     return runtime_->UnLoadRepairPatch(hqfFile);
957 }
958 
CleanAppTempData(bool isLastProcess)959 void OHOSApplication::CleanAppTempData(bool isLastProcess)
960 {
961     if (!isLastProcess) {
962         TAG_LOGE(AAFwkTag::APPKIT, "failed");
963         return;
964     }
965     if (abilityRuntimeContext_ == nullptr) {
966         TAG_LOGE(AAFwkTag::APPKIT, "Context is nullptr");
967         return;
968     }
969 
970     auto cleaner = ApplicationCleaner::GetInstance();
971     if (cleaner) {
972         cleaner->SetRuntimeContext(abilityRuntimeContext_);
973         cleaner->RenameTempData();
974     }
975 }
976 
CleanUselessTempData()977 void OHOSApplication::CleanUselessTempData()
978 {
979     if (abilityRuntimeContext_ == nullptr) {
980         TAG_LOGE(AAFwkTag::APPKIT, "Context is nullptr");
981         return;
982     }
983 
984     auto cleaner = ApplicationCleaner::GetInstance();
985     if (cleaner) {
986         cleaner->SetRuntimeContext(abilityRuntimeContext_);
987         cleaner->ClearTempData();
988     }
989 }
990 
UpdateAppContextResMgr(const Configuration & config)991 void OHOSApplication::UpdateAppContextResMgr(const Configuration &config)
992 {
993     auto context = GetAppContext();
994     if (context == nullptr) {
995         TAG_LOGE(AAFwkTag::APPKIT, "Application context is nullptr");
996         return;
997     }
998 
999     auto configUtils = std::make_shared<AbilityRuntime::ConfigurationUtils>();
1000     configUtils->UpdateGlobalConfig(config, context->GetResourceManager());
1001 }
1002 
CleanEmptyAbilityStage()1003 void OHOSApplication::CleanEmptyAbilityStage()
1004 {
1005     bool containsNonEmpty = false;
1006     for (auto it = abilityStages_.begin(); it != abilityStages_.end();) {
1007         auto abilityStage = it->second;
1008         if (abilityStage == nullptr) {
1009             it++;
1010             continue;
1011         }
1012         if (!abilityStage->ContainsAbility()) {
1013             abilityStage->OnDestroy();
1014             it = abilityStages_.erase(it);
1015         } else {
1016             containsNonEmpty = true;
1017             it++;
1018         }
1019     }
1020     if (containsNonEmpty) {
1021         TAG_LOGI(AAFwkTag::APPKIT, "Application contains none empty abilityStage");
1022     }
1023 }
1024 
IsUpdateColorNeeded(Configuration & config,AbilityRuntime::SetLevel level)1025 bool OHOSApplication::IsUpdateColorNeeded(Configuration &config, AbilityRuntime::SetLevel level)
1026 {
1027     std::string colorMode = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
1028     std::string colorModeIsSetBySa =
1029         config.GetItem(AAFwk::GlobalConfigurationKey::COLORMODE_IS_SET_BY_SA);
1030     if (level < AbilityRuntime::SetLevel::SA && !colorModeIsSetBySa.empty()) {
1031         level = AbilityRuntime::SetLevel::SA;
1032     }
1033 
1034     TAG_LOGI(AAFwkTag::APPKIT, "current %{public}d, pre %{public}d",
1035         static_cast<uint8_t>(level),
1036         static_cast<uint8_t>(AbilityRuntime::ApplicationConfigurationManager::GetInstance().GetColorModeSetLevel()));
1037 
1038     bool needUpdate = true;
1039 
1040     if (level < AbilityRuntime::ApplicationConfigurationManager::GetInstance().GetColorModeSetLevel() ||
1041         colorMode.empty()) {
1042         config.RemoveItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
1043         config.RemoveItem(AAFwk::GlobalConfigurationKey::COLORMODE_IS_SET_BY_SA);
1044         TAG_LOGI(AAFwkTag::APPKIT, "color remove");
1045         needUpdate = false;
1046     }
1047 
1048     if (!colorMode.empty()) {
1049         config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE,
1050             AbilityRuntime::ApplicationConfigurationManager::GetInstance().SetColorModeSetLevel(level, colorMode));
1051 
1052         if (level > AbilityRuntime::SetLevel::System) {
1053             config.AddItem(AAFwk::GlobalConfigurationKey::COLORMODE_IS_SET_BY_APP,
1054                 AppExecFwk::ConfigurationInner::IS_SET_BY_APP);
1055         }
1056     }
1057 
1058     return needUpdate;
1059 }
1060 
isUpdateFontSize(Configuration & config,AbilityRuntime::SetLevel level)1061 bool OHOSApplication::isUpdateFontSize(Configuration &config, AbilityRuntime::SetLevel level)
1062 {
1063     std::string fontSize = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE);
1064     if (fontSize.empty()) {
1065         TAG_LOGW(AAFwkTag::APPKIT, "fontSize is empty");
1066         return false;
1067     }
1068 
1069     auto preLevel = ApplicationConfigurationManager::GetInstance().GetFontSetLevel();
1070     if (level < preLevel) {
1071         TAG_LOGW(AAFwkTag::APPKIT, "low level");
1072         config.RemoveItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE);
1073         return false;
1074     }
1075 
1076     std::string globalFontFollowSystem = configuration_->GetItem(AAFwk::GlobalConfigurationKey::APP_FONT_SIZE_SCALE);
1077     if (level == preLevel && !globalFontFollowSystem.empty()) {
1078         TAG_LOGW(AAFwkTag::APPKIT, "same level");
1079         if (globalFontFollowSystem.compare(ConfigurationInner::IS_APP_FONT_FOLLOW_SYSTEM) == 0) {
1080             return true;
1081         }
1082         config.RemoveItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE);
1083         return false;
1084     }
1085 
1086     // level > preLevel
1087     TAG_LOGW(AAFwkTag::APPKIT, "high level");
1088     configuration_->RemoveItem(AAFwk::GlobalConfigurationKey::APP_FONT_SIZE_SCALE);
1089     ApplicationConfigurationManager::GetInstance().SetfontSetLevel(level);
1090     return true;
1091 }
1092 
IsUpdateLanguageNeeded(Configuration & config,AbilityRuntime::SetLevel level)1093 bool OHOSApplication::IsUpdateLanguageNeeded(Configuration &config, AbilityRuntime::SetLevel level)
1094 {
1095     TAG_LOGI(AAFwkTag::APPKIT, "current %{public}d, pre %{public}d",
1096         static_cast<uint8_t>(level),
1097         static_cast<uint8_t>(AbilityRuntime::ApplicationConfigurationManager::GetInstance().GetLanguageSetLevel()));
1098 
1099     std::string language = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE);
1100     if (language.empty()) {
1101         TAG_LOGW(AAFwkTag::APPKIT, "language is empty");
1102         return false;
1103     }
1104     if (level < AbilityRuntime::ApplicationConfigurationManager::GetInstance().GetLanguageSetLevel()) {
1105         config.RemoveItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE);
1106         TAG_LOGI(AAFwkTag::APPKIT, "language remove");
1107         return false;
1108     }
1109     AbilityRuntime::ApplicationConfigurationManager::GetInstance().SetLanguageSetLevel(level);
1110     config.AddItem(AAFwk::GlobalConfigurationKey::IS_PREFERRED_LANGUAGE,
1111         level == AbilityRuntime::SetLevel::Application ? "1" : "0");
1112     return true;
1113 }
1114 
IsMainProcess(const std::string & bundleName,const std::string & process)1115 bool OHOSApplication::IsMainProcess(const std::string &bundleName, const std::string &process)
1116 {
1117     auto processInfo = GetProcessInfo();
1118     if (processInfo == nullptr) {
1119         TAG_LOGE(AAFwkTag::APPKIT, "null processInfo");
1120         return false;
1121     }
1122     ProcessType processType = processInfo->GetProcessType();
1123     if (processType == ProcessType::NORMAL) {
1124         return true;
1125     }
1126 
1127     std::string processName = processInfo->GetProcessName();
1128     if (processName == bundleName || processName == process) {
1129         return true;
1130     }
1131     TAG_LOGD(AAFwkTag::APPKIT, "not main process");
1132     return false;
1133 }
1134 }  // namespace AppExecFwk
1135 }  // namespace OHOS
1136