1 /* 2 * Copyright (c) 2022-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 #ifndef OHOS_ABILITY_APP_RECOVERY_H 17 #define OHOS_ABILITY_APP_RECOVERY_H 18 19 #include <memory> 20 #include <mutex> 21 #include <vector> 22 #include <functional> 23 24 #include "ability_info.h" 25 #include "ability_recovery.h" 26 #include "application_info.h" 27 #include "event_handler.h" 28 #include "iremote_object.h" 29 #include "parcel.h" 30 #include "ui_ability.h" 31 #include "want.h" 32 #include "want_params.h" 33 34 namespace OHOS { 35 namespace AppExecFwk { 36 class AppRecovery { 37 using FreezeFunction = std::function<void()>; 38 public: 39 static AppRecovery& GetInstance(); 40 void EnableAppRecovery(uint16_t restartFlag, uint16_t saveFlag, uint16_t saveMode); 41 bool InitApplicationInfo(const std::shared_ptr<AppExecFwk::EventHandler>& mainHandler, 42 const std::shared_ptr<ApplicationInfo>& applicationInfo); 43 bool AddAbility(std::shared_ptr<AbilityRuntime::UIAbility> ability, const std::shared_ptr<AbilityInfo> &abilityInfo, 44 const sptr<IRemoteObject> &token); 45 bool RemoveAbility(const sptr<IRemoteObject>& tokenId); 46 47 bool IsEnabled() const; 48 bool ScheduleRecoverApp(StateReason reason); 49 bool ScheduleSaveAppState(StateReason reason, uintptr_t ability = 0); 50 bool TryRecoverApp(StateReason reason); 51 bool PersistAppState(); 52 void SetRestartWant(std::shared_ptr<AAFwk::Want> want); 53 54 uint16_t GetRestartFlag() const; 55 uint16_t GetSaveOccasionFlag() const; 56 uint16_t GetSaveModeFlag() const; 57 void DeleteInValidMissionFiles(); 58 void ClearPageStack(std::string bundleName); 59 void SetFreezeCallback(FreezeFunction freezeCallback); 60 bool IsNeedSaveAppState(StateReason reason); 61 private: 62 AppRecovery(); 63 ~AppRecovery(); 64 bool ShouldSaveAppState(StateReason reason); 65 bool ShouldRecoverApp(StateReason reason); 66 67 void DoRecoverApp(StateReason reason); 68 void DoRecoverMainApp(StateReason reason); 69 void DoSaveAppState(StateReason reason, uintptr_t ability = 0); 70 void DeleteInValidMissionFileById(std::string path, int32_t missionId); 71 bool GetMissionIds(std::string path, std::vector<int32_t> &missionIds); 72 73 bool isEnable_; 74 uint16_t restartFlag_; 75 uint16_t saveOccasion_; 76 uint16_t saveMode_; 77 wptr<IRemoteObject> token_; 78 AAFwk::Want mainWant_; 79 std::weak_ptr<AppExecFwk::EventHandler> mainHandler_; 80 std::weak_ptr<AppExecFwk::ApplicationInfo> applicationInfo_; 81 std::weak_ptr<AbilityRuntime::UIAbility> ability_; 82 std::vector<std::shared_ptr<AbilityRecovery>> abilityRecoverys_; 83 std::shared_ptr<AAFwk::Want> want_ = nullptr; 84 std::atomic<bool> useAppSettedValue_ = false; // If the value is true means app call appRecovery.enableAppRecovery 85 FreezeFunction freezeCallback = nullptr; 86 }; 87 } // namespace AbilityRuntime 88 } // namespace OHOS 89 #endif // OHOS_ABILITY_APP_RECOVERY_H