• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RECV_MANAGER_H
17 #define DMS_CONTINUE_RECV_MANAGER_H
18 
19 #include <deque>
20 #include <map>
21 #include <mutex>
22 #include <queue>
23 #include <string>
24 #include <thread>
25 #include <utility>
26 #include <vector>
27 
28 #include "bundle/bundle_manager_internal.h"
29 #include "event_handler.h"
30 #include "mission_info.h"
31 #include "mission/distributed_mission_broadcast_listener.h"
32 #include "mission/distributed_mission_died_listener.h"
33 #include "mission/distributed_mission_focused_listener.h"
34 
35 namespace OHOS {
36 namespace DistributedSchedule {
37 const std::string CONTINUE_RECV_MANAGER = "continue_recv_manager";
38 struct currentIconInfo {
39     std::string senderNetworkId;
40     std::string bundleName;
41     std::string continueType;
42 
43     std::string sourceBundleName;
44 
isEmptycurrentIconInfo45     bool isEmpty()
46     {
47         return (this->senderNetworkId == "" && this->bundleName == "" && this->continueType == "");
48     }
49 
50     currentIconInfo(const std::string &source_device_id, const std::string &source_bundle_name,
51         const std::string &sink_bundle_name, const std::string &continueType = "")
senderNetworkIdcurrentIconInfo52         : senderNetworkId(source_device_id),
53           bundleName(sink_bundle_name),
54           continueType(continueType),
55           sourceBundleName(source_bundle_name) {
56     }
57 
58     currentIconInfo() = default;
59 
60     ~currentIconInfo() = default;
61 };
62 
63 class DMSContinueRecvMgr {
64 public:
65     constexpr static uint8_t DMS_DATA_LEN = 3; // Dms data Length
66     constexpr static int32_t DMS_SEND_LEN = 4; // Maximum Broadcast Length
67     constexpr static uint8_t DMS_0XF0 = 0xf0;
68     constexpr static uint8_t DMS_0X0F = 0x0f;
69     constexpr static uint8_t DMS_0XFF = 0xff;
70     constexpr static uint8_t DMS_FOCUSED_TYPE = 0x00;
71     constexpr static uint8_t DMS_UNFOCUSED_TYPE = 0x01;
72     constexpr static uint8_t CONTINUE_SHIFT_24 = 0x18;
73     constexpr static uint8_t CONTINUE_SHIFT_16 = 0x10;
74     constexpr static uint8_t CONTINUE_SHIFT_08 = 0x08;
75     constexpr static uint8_t CONTINUE_SHIFT_04 = 0x04;
76     constexpr static int32_t INVALID_MISSION_ID = -1;
77 
78     ~DMSContinueRecvMgr();
79     void Init(int32_t accountId);
80     void UnInit();
81     void NotifyDataRecv(std::string& senderNetworkId, uint8_t* payload, uint32_t dataLen);
82     int32_t RegisterOnListener(const std::string& type, const sptr<IRemoteObject>& obj);
83     int32_t RegisterOffListener(const std::string& type, const sptr<IRemoteObject>& obj);
84     void NotifyDied(const sptr<IRemoteObject>& obj);
85     void NotifyDeviceOffline(const std::string& networkId);
86     void NotifyPackageRemoved(const std::string& sinkBundleName);
87     void NotifyIconDisappear(uint16_t bundleNameId, const std::string &senderNetworkId, const int32_t state);
88     int32_t NotifyDockDisplay(uint16_t bundleNameId, const currentIconInfo& continueInfo, const int32_t state);
89     void OnDeviceScreenOff();
90     void OnContinueSwitchOff();
91     void OnUserSwitch();
92     std::string GetContinueType(const std::string& bundleName);
93 
94 private:
95     void StartEvent();
96     int32_t RetryPostBroadcast(const std::string& senderNetworkId, uint16_t bundleNameId, uint8_t continueTypeId,
97         const int32_t state, const int32_t retry);
98     bool GetFinalBundleName(DmsBundleInfo& distributedBundleInfo,  std::string &finalBundleName,
99         AppExecFwk::BundleInfo& localBundleInfo, std::string& continueType);
100     int32_t VerifyBroadcastSource(const std::string& senderNetworkId, const std::string& srcBundleName,
101         const std::string& sinkBundleName, const std::string& continueType, const int32_t state);
102     void PostOnBroadcastBusiness(const std::string& senderNetworkId, uint16_t bundleNameId, uint8_t continueTypeId,
103         const int32_t state, const int32_t delay = 0, const int32_t retry = 0);
104     void FindContinueType(const DmsBundleInfo &distributedBundleInfo, uint8_t &continueTypeId,
105         std::string &continueType, DmsAbilityInfo &abilityInfo);
106     int32_t DealOnBroadcastBusiness(const std::string& senderNetworkId, uint16_t bundleNameId, uint8_t continueTypeId,
107         const int32_t state, const int32_t retry = 0);
108     void NotifyRecvBroadcast(const sptr<IRemoteObject>& obj, const currentIconInfo& continueInfo, const int32_t state);
109     bool IsBundleContinuable(const AppExecFwk::BundleInfo& bundleInfo, const std::string &srcAbilityName,
110         const std::string &srcModuleName, const std::string &srcContinueType);
111     std::string ContinueTypeFormat(const std::string &continueType);
112     void FindToNotifyRecvBroadcast(const std::string& senderNetworkId, const std::string& bundleName,
113         const std::string& continueType);
114 private:
115     currentIconInfo iconInfo_;
116     sptr<DistributedMissionDiedListener> missionDiedListener_;
117     std::string onType_;
118     std::map<std::string, std::vector<sptr<IRemoteObject>>> registerOnListener_;
119     std::thread eventThread_;
120     std::condition_variable eventCon_;
121     std::mutex eventMutex_;
122     std::mutex iconMutex_;
123     std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_;
124     int32_t accountId_ = -1;
125 };
126 } // namespace DistributedSchedule
127 } // namespace OHOS
128 #endif // DMS_CONTINUE_RECV_MANAGER_H
129