1 /* 2 * Copyright (c) 2023-2024 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 DMS_CONTINUE_SEND_MANAGER_H 17 #define DMS_CONTINUE_SEND_MANAGER_H 18 19 #include <deque> 20 #include <map> 21 #include <mutex> 22 #include <queue> 23 #include <string> 24 #include <thread> 25 #include <vector> 26 27 #include "bundle/bundle_manager_internal.h" 28 #include "distributed_mission_died_listener.h" 29 #include "event_handler.h" 30 #include "mission_info.h" 31 32 namespace OHOS { 33 namespace DistributedSchedule { 34 const std::string CONTINUE_MANAGER = "continue_manager"; 35 struct currentMissionInfo { 36 int32_t currentMissionId; 37 bool currentIsContinuable; 38 }; 39 40 struct lastUnfoInfo { 41 int32_t missionId; 42 int32_t unfoTime; 43 std::string bundleName; 44 uint32_t accessToken; 45 std::string abilityName; 46 }; 47 48 struct ContinueLaunchMissionInfo { 49 std::string bundleName; 50 std::string abilityName; 51 52 bool operator == (const ContinueLaunchMissionInfo &other) const 53 { 54 return this->bundleName == other.bundleName && this->abilityName == other.abilityName; 55 } 56 57 bool operator < (const ContinueLaunchMissionInfo &other) const 58 { 59 return this->bundleName == other.bundleName 60 ? this->abilityName < other.abilityName 61 : this->bundleName < other.bundleName; 62 } 63 }; 64 65 struct LastFocusedMissionInfo { 66 int32_t missionId; 67 std::string bundleName; 68 }; 69 70 enum class FocusedReason { 71 MIN = -1, 72 NORMAL, 73 INIT, 74 SCREENOFF, 75 ONLINE, 76 MMI, 77 MAX 78 }; 79 80 enum class UnfocusedReason { 81 MIN = -1, 82 NORMAL, 83 DESTORY, 84 CLOSE, 85 TIMEOUT, 86 SCREENOFF, 87 MAX 88 }; 89 90 class DMSContinueSendMgr { 91 DECLARE_SINGLE_INSTANCE(DMSContinueSendMgr); 92 93 public: 94 constexpr static uint8_t DMS_DATA_LEN = 3; // Dms data Length 95 constexpr static int32_t DMS_SEND_LEN = 4; // Maximum broadcast length 96 constexpr static uint8_t DMS_0XF0 = 0xf0; 97 constexpr static uint8_t DMS_0X0F = 0x0f; 98 constexpr static uint8_t DMS_0XFF = 0xff; 99 constexpr static uint8_t DMS_FOCUSED_TYPE = 0x00; 100 constexpr static uint8_t DMS_UNFOCUSED_TYPE = 0x01; 101 constexpr static uint8_t CONTINUE_SHIFT_24 = 0x18; 102 constexpr static uint8_t CONTINUE_SHIFT_16 = 0x10; 103 constexpr static uint8_t CONTINUE_SHIFT_08 = 0x08; 104 constexpr static uint8_t CONTINUE_SHIFT_04 = 0x04; 105 constexpr static int32_t INVALID_MISSION_ID = -1; 106 107 class ScreenOffHandler { 108 public: 109 ScreenOffHandler() = default; 110 virtual ~ScreenOffHandler() = default; 111 112 int32_t GetMissionId(); 113 std::string GetBundleName(); 114 std::string GetAbilityName(); 115 uint32_t GetAccessTokenId(); 116 bool IsDeviceScreenOn(); 117 void OnDeviceScreenOff(int32_t missionId); 118 void OnDeviceScreenOn(); 119 void ClearScreenOffInfo(); 120 void SetScreenOffInfo(int32_t missionId, std::string bundleName, uint16_t bundleNameId, 121 std::string abilityName); 122 123 private: 124 bool isScreenOn_ = true; 125 lastUnfoInfo unfoInfo_ = { INVALID_MISSION_ID, 0, "", 0 }; 126 }; 127 128 void Init(); 129 void UnInit(); 130 void NotifyMissionFocused(const int32_t missionId, FocusedReason reason); 131 void NotifyMissionUnfocused(const int32_t missionId, UnfocusedReason reason); 132 int32_t GetMissionIdByBundleName(const std::string& bundleName, int32_t& missionId); 133 int32_t SetMissionContinueState(const int32_t missionId, const AAFwk::ContinueState& state); 134 void OnMMIEvent(); 135 void OnDeviceScreenOff(); 136 void OnDeviceScreenOn(); 137 int32_t NotifyDeviceOnline(); 138 int32_t SendScreenOffEvent(uint8_t type); 139 void DeleteContinueLaunchMissionInfo(const int32_t missionId); 140 int32_t GetContinueLaunchMissionInfo(const int32_t missionId, ContinueLaunchMissionInfo& missionInfo); 141 142 private: 143 int32_t GetCurrentMissionId(); 144 void PostUnfocusedTaskWithDelay(const int32_t missionId, UnfocusedReason reason); 145 int32_t SendSoftbusEvent(uint16_t bundleNameId, uint8_t continueTypeId, uint8_t type); 146 void StartEvent(); 147 int32_t DealFocusedBusiness(const int32_t missionId, FocusedReason reason); 148 int32_t DealUnfocusedBusiness(const int32_t missionId, UnfocusedReason reason); 149 void DealScreenOff(); 150 void DealTimerUnfocusedBussiness(const int32_t missionId); 151 int32_t GetBundleNameByMissionId(const int32_t missionId, std::string& bundleName); 152 int32_t GetBundleNameByScreenOffInfo(const int32_t missionId, std::string& bundleName); 153 bool IsContinue(const int32_t& missionId, const std::string& bundleName); 154 int32_t DealSetMissionContinueStateBusiness(const int32_t missionId, const AAFwk::ContinueState& state); 155 int32_t CheckContinueState(const int32_t missionId); 156 void AddMMIListener(); 157 void RemoveMMIListener(); 158 int32_t GetAccessTokenIdSendEvent(std::string bundleName, UnfocusedReason reason, uint16_t& bundleNameId, 159 uint8_t& continueTypeId); 160 int32_t SetStateSendEvent(const uint16_t bundleNameId, const uint8_t& continueTypeId, 161 const AAFwk::ContinueState &state); 162 int32_t GetAbilityNameByMissionId(const int32_t missionId, std::string& abilityName); 163 int32_t FocusedBusinessSendEvent(std::string bundleName, const std::string& abilityName); 164 int32_t GetBundleNameIdAndContinueTypeId(const int32_t missionId, const AAFwk::ContinueState& state, 165 uint16_t& bundleNameId, uint8_t& continueTypeId); 166 void EraseFocusedMission(const std::string& bundleName, const int32_t& missionId, const UnfocusedReason& reason); 167 bool UpdateContinueLaunchMission(const AAFwk::MissionInfo& info); 168 private: 169 currentMissionInfo info_ = { INVALID_MISSION_ID, false }; 170 // Record the bundleName and missionId of the focused application, and clear them when out of focus 171 std::map<std::string, int32_t> focusedMission_; 172 // Record the missionId and abilityName of the focused application, and clear them when out of focus 173 std::map<int32_t, std::string> focusedMissionAbility_; 174 std::thread eventThread_; 175 std::condition_variable eventCon_; 176 std::mutex eventMutex_; 177 std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_; 178 std::shared_ptr<ScreenOffHandler> screenOffHandler_; 179 int32_t mmiMonitorId_ = INVALID_MISSION_ID; 180 // Record the mission information of the continuation application, and clear them when the application is destroyed 181 std::map<ContinueLaunchMissionInfo, int32_t> continueLaunchMission_; 182 // Record the missionId and bundle name of the previous focused application 183 LastFocusedMissionInfo lastFocusedMissionInfo_ = { INVALID_MISSION_ID, "" }; 184 }; 185 } // namespace DistributedSchedule 186 } // namespace OHOS 187 #endif // DMS_CONTINUE_SEND_MANAGER_H 188