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