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