1 /* 2 * Copyright (c) 2024-2025 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 <atomic> 20 #include <mutex> 21 #include <queue> 22 #include <string> 23 #include <thread> 24 #include <cstdint> 25 26 #include "event_handler.h" 27 #include "single_instance.h" 28 #include "mission/dms_continue_condition_manager.h" 29 #include "mission/notification/dms_continue_send_strategy.h" 30 31 namespace OHOS { 32 namespace DistributedSchedule { 33 namespace { 34 constexpr uint8_t BROADCAST_TYPE_APPEAR = 0; 35 constexpr uint8_t BROADCAST_TYPE_DISAPPEAR = 1; 36 constexpr int32_t DEFAULT_USER = 100; 37 } 38 39 class DMSContinueSendMgr : public std::enable_shared_from_this<DMSContinueSendMgr> { 40 friend class SendStrategyFocused; 41 friend class SendStrategyUnfocused; 42 friend class SendStrategyDestoryed; 43 friend class SendStrategyBackground; 44 friend class SendStrategyActive; 45 friend class SendStrategyInactive; 46 friend class SendStrategyTimeout; 47 friend class SendStrategyMMI; 48 friend class SendStrategyContinueSwitchOff; 49 50 public: 51 constexpr static uint8_t DMS_DATA_LEN = 3; // Dms data Length 52 constexpr static int32_t DMS_SEND_LEN = 4; // Maximum broadcast length 53 constexpr static uint8_t DMS_0XF0 = 0xf0; 54 constexpr static uint8_t DMS_0X0F = 0x0f; 55 constexpr static uint8_t DMS_0XFF = 0xff; 56 constexpr static uint8_t DMS_FOCUSED_TYPE = 0x00; 57 constexpr static uint8_t DMS_UNFOCUSED_TYPE = 0x01; 58 constexpr static uint8_t CONTINUE_SHIFT_24 = 0x18; 59 constexpr static uint8_t CONTINUE_SHIFT_16 = 0x10; 60 constexpr static uint8_t CONTINUE_SHIFT_08 = 0x08; 61 constexpr static uint8_t CONTINUE_SHIFT_04 = 0x04; 62 constexpr static int32_t INVALID_ID = -1; 63 constexpr static int32_t INVALID_MISSION_ID = -1; 64 65 ~DMSContinueSendMgr(); 66 void Init(int32_t currentUserId); 67 void UnInit(); 68 69 void OnMissionStatusChanged(int32_t missionId, MissionEventType type); 70 void OnMMIEvent(); 71 int32_t OnDeviceOnline(); 72 void OnDeviceScreenLocked(); 73 void OnDeviceScreenOn(); 74 void OnUserSwitched(); 75 76 private: 77 void StartEvent(); 78 void SendContinueBroadcast(int32_t missionId, MissionEventType type); 79 void SendContinueBroadcast(const MissionStatus& status, MissionEventType type); 80 void SendContinueBroadcastAfterDelay(int32_t missionId); 81 int32_t ExecuteSendStrategy(MissionEventType type, const MissionStatus& status, uint8_t &sendType); 82 int32_t QueryBroadcastInfo(const MissionStatus& status, uint16_t& bundleNameId, uint8_t& continueTypeId); 83 void SendSoftbusEvent(uint16_t& bundleNameId, uint8_t& continueTypeId, uint8_t type); 84 void AddMMIListener(); 85 void RemoveMMIListener(); 86 87 private: 88 class ScreenLockedHandler { 89 public: 90 struct LastUnfoInfo { 91 int32_t missionId; 92 int32_t unfoTime; 93 MissionStatus status; 94 }; 95 96 ScreenLockedHandler() = default; 97 explicit ScreenLockedHandler(const std::shared_ptr<DMSContinueSendMgr>& dmsContinueSendMgr); 98 virtual ~ScreenLockedHandler() = default; 99 100 int32_t GetMissionId(); 101 MissionStatus GetMissionStatus(); 102 void OnDeviceScreenLocked(); 103 void ResetScreenLockedInfo(); 104 void SetScreenLockedInfo(LastUnfoInfo info); 105 void SetMissionContinueStateInfo(const MissionStatus& status); 106 107 private: 108 LastUnfoInfo unfoInfo_ = { INVALID_MISSION_ID, 0, { -1, INVALID_MISSION_ID, "", "", "", false, 0, false } }; 109 std::weak_ptr<DMSContinueSendMgr> dmsContinueSendMgr_; 110 }; 111 112 int32_t SendScreenLockedEvent(uint8_t type); 113 void PostScreenLockedEventAfterDelay(int32_t missionId, uint8_t type, int32_t timeout); 114 115 private: 116 int32_t accountId_ = DEFAULT_USER; 117 int32_t mmiMonitorId_ = INVALID_ID; 118 std::thread eventThread_; 119 std::condition_variable eventCon_; 120 std::mutex eventMutex_; 121 std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_; 122 std::shared_ptr<ScreenLockedHandler> screenLockedHandler_; 123 std::map<MissionEventType, std::shared_ptr<ContinueSendStrategy>> strategyMap_; 124 std::atomic<bool> initFlag_ = false; 125 }; 126 } // namespace DistributedSchedule 127 } // namespace OHOS 128 #endif // DMS_CONTINUE_SEND_MANAGER_H 129