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