1 /* 2 * Copyright (c) 2023 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 "disposed_observer.h" 17 18 #include "ability_interceptor.h" 19 #include "ability_record.h" 20 #include "hilog_wrapper.h" 21 #include "modal_system_ui_extension.h" 22 #include "want.h" 23 namespace OHOS { 24 namespace AAFwk { DisposedObserver(const AppExecFwk::DisposedRule & disposedRule,const std::shared_ptr<DisposedRuleInterceptor> & interceptor)25DisposedObserver::DisposedObserver(const AppExecFwk::DisposedRule &disposedRule, 26 const std::shared_ptr<DisposedRuleInterceptor> &interceptor) 27 : disposedRule_(disposedRule), interceptor_(interceptor) 28 {} 29 OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)30void DisposedObserver::OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) 31 { 32 HILOG_DEBUG("Call"); 33 std::lock_guard<ffrt::mutex> guard(observerLock_); 34 if (abilityStateData.abilityState == static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND)) { 35 token_ = abilityStateData.token; 36 } 37 } 38 OnPageShow(const AppExecFwk::PageStateData & pageStateData)39void DisposedObserver::OnPageShow(const AppExecFwk::PageStateData &pageStateData) 40 { 41 HILOG_DEBUG("Call"); 42 if (disposedRule_.componentType == AppExecFwk::ComponentType::UI_ABILITY) { 43 int ret = IN_PROCESS_CALL(AbilityManagerClient::GetInstance()->StartAbility(*disposedRule_.want)); 44 if (ret != ERR_OK) { 45 interceptor_->UnregisterObserver(pageStateData.bundleName); 46 HILOG_ERROR("failed to start disposed ability"); 47 return; 48 } 49 } 50 if (disposedRule_.componentType == AppExecFwk::ComponentType::UI_EXTENSION) { 51 if (!token_) { 52 auto systemUIExtension = std::make_shared<OHOS::Rosen::ModalSystemUiExtension>(); 53 bool ret = systemUIExtension->CreateModalUIExtension(*disposedRule_.want); 54 if (!ret) { 55 interceptor_->UnregisterObserver(pageStateData.bundleName); 56 HILOG_ERROR("failed to start system UIExtension"); 57 return; 58 } 59 } else { 60 auto abilityRecord = Token::GetAbilityRecordByToken(token_); 61 if (!abilityRecord) { 62 interceptor_->UnregisterObserver(pageStateData.bundleName); 63 HILOG_ERROR("abilityRecord is nullptr"); 64 return; 65 } 66 int ret = abilityRecord->CreateModalUIExtension(*disposedRule_.want); 67 if (ret != ERR_OK) { 68 interceptor_->UnregisterObserver(pageStateData.bundleName); 69 HILOG_ERROR("failed to start disposed UIExtension"); 70 return; 71 } 72 } 73 } 74 interceptor_->UnregisterObserver(pageStateData.bundleName); 75 } 76 } // namespace AAFwk 77 } // namespace OHOS 78