1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#include "disposed_observer.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include "interceptor/disposed_rule_interceptor.h"
19eace7efcSopenharmony_ci#include "ability_record.h"
20eace7efcSopenharmony_ci#include "modal_system_ui_extension.h"
21eace7efcSopenharmony_ci
22eace7efcSopenharmony_cinamespace OHOS {
23eace7efcSopenharmony_cinamespace AAFwk {
24eace7efcSopenharmony_cinamespace {
25eace7efcSopenharmony_ciconstexpr const char* UIEXTENSION_MODAL_TYPE = "ability.want.params.modalType";
26eace7efcSopenharmony_ciconstexpr const char* INTERCEPT_MISSION_ID = "intercept_missionId";
27eace7efcSopenharmony_ci}
28eace7efcSopenharmony_ci
29eace7efcSopenharmony_ciDisposedObserver::DisposedObserver(const AppExecFwk::DisposedRule &disposedRule,
30eace7efcSopenharmony_ci    const std::shared_ptr<DisposedRuleInterceptor> &interceptor)
31eace7efcSopenharmony_ci    : disposedRule_(disposedRule), interceptor_(interceptor)
32eace7efcSopenharmony_ci{}
33eace7efcSopenharmony_ci
34eace7efcSopenharmony_civoid DisposedObserver::OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData)
35eace7efcSopenharmony_ci{
36eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> guard(observerLock_);
37eace7efcSopenharmony_ci    if (abilityStateData.abilityState != static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND)) {
38eace7efcSopenharmony_ci        return;
39eace7efcSopenharmony_ci    }
40eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "Call");
41eace7efcSopenharmony_ci    token_ = abilityStateData.token;
42eace7efcSopenharmony_ci    auto abilityRecord = Token::GetAbilityRecordByToken(token_);
43eace7efcSopenharmony_ci    if (abilityRecord && !abilityRecord->GetAbilityInfo().isStageBasedModel) {
44eace7efcSopenharmony_ci        auto systemUIExtension = std::make_shared<OHOS::Rosen::ModalSystemUiExtension>();
45eace7efcSopenharmony_ci        Want want = *disposedRule_.want;
46eace7efcSopenharmony_ci        want.SetParam(UIEXTENSION_MODAL_TYPE, 1);
47eace7efcSopenharmony_ci        auto sessionInfo = abilityRecord->GetSessionInfo();
48eace7efcSopenharmony_ci        if (sessionInfo != nullptr) {
49eace7efcSopenharmony_ci            want.SetParam(INTERCEPT_MISSION_ID, sessionInfo->persistentId);
50eace7efcSopenharmony_ci        } else {
51eace7efcSopenharmony_ci            want.SetParam(INTERCEPT_MISSION_ID, abilityRecord->GetMissionId());
52eace7efcSopenharmony_ci        }
53eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::ABILITYMGR, "FA modal system");
54eace7efcSopenharmony_ci        bool ret = IN_PROCESS_CALL(systemUIExtension->CreateModalUIExtension(want));
55eace7efcSopenharmony_ci        if (!ret) {
56eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::ABILITYMGR, "call failed");
57eace7efcSopenharmony_ci        }
58eace7efcSopenharmony_ci        interceptor_->UnregisterObserver(abilityStateData.bundleName);
59eace7efcSopenharmony_ci    }
60eace7efcSopenharmony_ci}
61eace7efcSopenharmony_ci
62eace7efcSopenharmony_civoid DisposedObserver::OnPageShow(const AppExecFwk::PageStateData &pageStateData)
63eace7efcSopenharmony_ci{
64eace7efcSopenharmony_ci    if (disposedRule_.componentType == AppExecFwk::ComponentType::UI_ABILITY) {
65eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::ABILITYMGR, "Call");
66eace7efcSopenharmony_ci        int ret = IN_PROCESS_CALL(AbilityManagerClient::GetInstance()->StartAbility(*disposedRule_.want));
67eace7efcSopenharmony_ci        if (ret != ERR_OK) {
68eace7efcSopenharmony_ci            interceptor_->UnregisterObserver(pageStateData.bundleName);
69eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::ABILITYMGR, "call failed");
70eace7efcSopenharmony_ci            return;
71eace7efcSopenharmony_ci        }
72eace7efcSopenharmony_ci    }
73eace7efcSopenharmony_ci    if (disposedRule_.componentType == AppExecFwk::ComponentType::UI_EXTENSION) {
74eace7efcSopenharmony_ci        auto abilityRecord = Token::GetAbilityRecordByToken(token_);
75eace7efcSopenharmony_ci        if (abilityRecord == nullptr || abilityRecord->GetAbilityInfo().type != AppExecFwk::AbilityType::PAGE) {
76eace7efcSopenharmony_ci            auto systemUIExtension = std::make_shared<OHOS::Rosen::ModalSystemUiExtension>();
77eace7efcSopenharmony_ci            Want want = *disposedRule_.want;
78eace7efcSopenharmony_ci            want.SetParam(UIEXTENSION_MODAL_TYPE, 1);
79eace7efcSopenharmony_ci            TAG_LOGD(AAFwkTag::ABILITYMGR, "modal system");
80eace7efcSopenharmony_ci            bool ret = IN_PROCESS_CALL(systemUIExtension->CreateModalUIExtension(want));
81eace7efcSopenharmony_ci            if (!ret) {
82eace7efcSopenharmony_ci                interceptor_->UnregisterObserver(pageStateData.bundleName);
83eace7efcSopenharmony_ci                TAG_LOGE(AAFwkTag::ABILITYMGR, "call failed");
84eace7efcSopenharmony_ci                return;
85eace7efcSopenharmony_ci            }
86eace7efcSopenharmony_ci        } else {
87eace7efcSopenharmony_ci            Want want = *disposedRule_.want;
88eace7efcSopenharmony_ci            auto sessionInfo = abilityRecord->GetSessionInfo();
89eace7efcSopenharmony_ci            if (sessionInfo != nullptr) {
90eace7efcSopenharmony_ci                want.SetParam(INTERCEPT_MISSION_ID, sessionInfo->persistentId);
91eace7efcSopenharmony_ci            } else {
92eace7efcSopenharmony_ci                want.SetParam(INTERCEPT_MISSION_ID, abilityRecord->GetMissionId());
93eace7efcSopenharmony_ci            }
94eace7efcSopenharmony_ci            TAG_LOGD(AAFwkTag::ABILITYMGR, "modal app");
95eace7efcSopenharmony_ci            int ret = abilityRecord->CreateModalUIExtension(want);
96eace7efcSopenharmony_ci            if (ret != ERR_OK) {
97eace7efcSopenharmony_ci                interceptor_->UnregisterObserver(pageStateData.bundleName);
98eace7efcSopenharmony_ci                TAG_LOGE(AAFwkTag::ABILITYMGR, "call failed");
99eace7efcSopenharmony_ci                return;
100eace7efcSopenharmony_ci            }
101eace7efcSopenharmony_ci        }
102eace7efcSopenharmony_ci    }
103eace7efcSopenharmony_ci    interceptor_->UnregisterObserver(pageStateData.bundleName);
104eace7efcSopenharmony_ci}
105eace7efcSopenharmony_ci} // namespace AAFwk
106eace7efcSopenharmony_ci} // namespace OHOS
107