• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
56 private:
57     AppRecovery();
58     ~AppRecovery();
59     bool ShouldSaveAppState(StateReason reason);
60     bool ShouldRecoverApp(StateReason reason);
61 
62     void DoRecoverApp(StateReason reason);
63     void DoSaveAppState(StateReason reason, uintptr_t ability = 0);
64     void DeleteInValidMissionFiles();
65     void DeleteInValidMissionFileById(std::string path, int32_t missionId);
66     bool GetMissionIds(std::string path, std::vector<int32_t> &missionIds);
67 
68     bool isEnable_;
69     uint16_t restartFlag_;
70     uint16_t saveOccasion_;
71     uint16_t saveMode_;
72     std::weak_ptr<AppExecFwk::EventHandler> mainHandler_;
73     std::weak_ptr<AppExecFwk::ApplicationInfo> applicationInfo_;
74     std::weak_ptr<AbilityRuntime::UIAbility> ability_;
75     std::vector<std::shared_ptr<AbilityRecovery>> abilityRecoverys_;
76     std::shared_ptr<AAFwk::Want> want_ = nullptr;
77 };
78 }  // namespace AbilityRuntime
79 }  // namespace OHOS
80 #endif // OHOS_ABILITY_APP_RECOVERY_H