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