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 "distributed_mission_focused_listener.h" 30 #include "event_handler.h" 31 #include "mission_info.h" 32 33 namespace OHOS { 34 namespace DistributedSchedule { 35 const std::string CONTINUE_MANAGER = "continue_manager"; 36 struct currentMissionInfo { 37 int32_t currentMissionId; 38 bool currentIsContinuable; 39 }; 40 41 struct lastUnfoInfo { 42 int32_t missionId; 43 int32_t unfoTime; 44 std::string bundleName; 45 uint32_t accessToken; 46 }; 47 48 enum class FocusedReason { 49 MIN = -1, 50 NORMAL, 51 INIT, 52 SCREENOFF, 53 MMI, 54 MAX 55 }; 56 57 enum class UnfocusedReason { 58 MIN = -1, 59 NORMAL, 60 DESTORY, 61 CLOSE, 62 TIMEOUT, 63 SCREENOFF, 64 MAX 65 }; 66 67 class DMSContinueSendMgr { 68 DECLARE_SINGLE_INSTANCE(DMSContinueSendMgr); 69 70 public: 71 constexpr static int32_t DMS_SEND_LEN = 5; 72 constexpr static uint8_t DMS_0XF0 = 0xf0; 73 constexpr static uint8_t DMS_0X0F = 0x0f; 74 constexpr static uint8_t DMS_0XFF = 0xff; 75 constexpr static uint8_t DMS_FOCUSED_TYPE = 0x00; 76 constexpr static uint8_t DMS_UNFOCUSED_TYPE = 0x01; 77 constexpr static uint8_t CONTINUE_SHIFT_24 = 0x18; 78 constexpr static uint8_t CONTINUE_SHIFT_16 = 0x10; 79 constexpr static uint8_t CONTINUE_SHIFT_08 = 0x08; 80 constexpr static uint8_t CONTINUE_SHIFT_04 = 0x04; 81 constexpr static int32_t INVALID_MISSION_ID = -1; 82 83 class ScreenOffHandler { 84 public: 85 ScreenOffHandler() = default; 86 virtual ~ScreenOffHandler() = default; 87 88 int32_t GetMissionId(); 89 std::string GetBundleName(); 90 uint32_t GetAccessTokenId(); 91 bool IsDeviceScreenOn(); 92 void OnDeviceScreenOff(int32_t missionId); 93 void OnDeviceScreenOn(); 94 void ClearScreenOffInfo(); 95 void SetScreenOffInfo(int32_t missionId, std::string bundleName, uint32_t accessTokenId); 96 97 private: 98 bool isScreenOn_ = true; 99 lastUnfoInfo unfoInfo_ = { INVALID_MISSION_ID, 0, "", 0 }; 100 }; 101 102 void Init(); 103 void UnInit(); 104 void NotifyMissionFocused(const int32_t missionId, FocusedReason reason); 105 void NotifyMissionUnfocused(const int32_t missionId, UnfocusedReason reason); 106 int32_t GetMissionIdByBundleName(const std::string& bundleName, int32_t& missionId); 107 int32_t SetMissionContinueState(const int32_t missionId, const AAFwk::ContinueState& state); 108 void OnMMIEvent(); 109 void OnDeviceScreenOff(); 110 void OnDeviceScreenOn(); 111 int32_t SendScreenOffEvent(uint8_t type); 112 113 private: 114 int32_t GetCurrentMissionId(); 115 void PostUnfocusedTaskWithDelay(const int32_t missionId, UnfocusedReason reason); 116 int32_t SendSoftbusEvent(uint32_t accessTokenId, uint8_t type); 117 void StartEvent(); 118 int32_t DealFocusedBusiness(const int32_t missionId); 119 int32_t DealUnfocusedBusiness(const int32_t missionId, UnfocusedReason reason); 120 void DealScreenOff(); 121 void DealTimerUnfocusedBussiness(const int32_t missionId); 122 int32_t GetBundleNameByMissionId(const int32_t missionId, std::string& bundleName); 123 bool IsContinue(const int32_t& missionId, const std::string& bundleName); 124 int32_t DealSetMissionContinueStateBusiness(const int32_t missionId, const AAFwk::ContinueState& state); 125 int32_t CheckContinueState(const int32_t missionId); 126 void AddMMIListener(); 127 void RemoveMMIListener(); 128 private: 129 currentMissionInfo info_ = { INVALID_MISSION_ID, false }; 130 sptr<DistributedMissionFocusedListener> missionFocusedListener_; 131 std::map<std::string, int32_t> focusedMission_; 132 std::thread eventThread_; 133 std::condition_variable eventCon_; 134 std::mutex eventMutex_; 135 std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_; 136 std::shared_ptr<ScreenOffHandler> screenOffHandler_; 137 int32_t mmiMonitorId_ = INVALID_MISSION_ID; 138 }; 139 } // namespace DistributedSchedule 140 } // namespace OHOS 141 #endif // DMS_CONTINUE_SEND_MANAGER_H 142