• 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 DEVICESTATUS_MANAGER_H
17 #define DEVICESTATUS_MANAGER_H
18 
19 #include <set>
20 #include <map>
21 
22 #include <atomic>
23 #include <iostream>
24 #include "accesstoken_kit.h"
25 
26 #include "boomerang_data.h"
27 #include "devicestatus_msdp_client_impl.h"
28 #include "iremote_boomerang_callback.h"
29 #include "iremote_dev_sta_callback.h"
30 #include "stationary_data.h"
31 #include "system_ability_definition.h"
32 #include "system_ability_status_change_stub.h"
33 #ifdef BOOMERANG_ONESTEP
34 #include "window_manager.h"
35 #endif
36 #include "bundle_mgr_proxy.h"
37 
38 namespace OHOS {
39 namespace Msdp {
40 namespace DeviceStatus {
41 using namespace Security::AccessToken;
42 using namespace OHOS::Rosen;
43 class DeviceStatusService;
44 class DeviceStatusManager {
45 public:
46     DeviceStatusManager() = default;
47     ~DeviceStatusManager() = default;
48 
49     class DeviceStatusCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
50     public:
51         DeviceStatusCallbackDeathRecipient() = default;
52         virtual void OnRemoteDied(const wptr<IRemoteObject> &remote);
53         virtual ~DeviceStatusCallbackDeathRecipient() = default;
54     };
55 
56     class BoomerangCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
57     public:
BoomerangCallbackDeathRecipient(DeviceStatusManager * deviceStatusManager)58         BoomerangCallbackDeathRecipient(DeviceStatusManager* deviceStatusManager) : manager_(deviceStatusManager) {}
59         virtual void OnRemoteDied(const wptr<IRemoteObject> &remote);
60         virtual ~BoomerangCallbackDeathRecipient() = default;
61     private:
62         DeviceStatusManager* manager_ { nullptr };;
63         friend class DeviceStatusManager;
64     };
65 
66 #ifdef BOOMERANG_ONESTEP
67     class AccessibilityStatusChange : public SystemAbilityStatusChangeStub {
68     public:
69         AccessibilityStatusChange() = default;
70         ~AccessibilityStatusChange() = default;
71         void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
72         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
73     private:
74         friend class DeviceStatusManager;
75         std::mutex mutex_;
76     };
77 #endif
78 
79 #ifdef BOOMERANG_ONESTEP
80     class SystemBarStyleChangedListener : public IWindowSystemBarPropertyChangedListener {
81     public:
82         void OnWindowSystemBarPropertyChanged(WindowType type, const SystemBarProperty& systemBarProperty) override;
83     private:
84         std::mutex mutex_;
85     };
86 #endif
87 
88     bool Init();
89     bool Enable(Type type);
90     bool InitAlgoMngrInterface(Type type);
91     bool Disable(Type type);
92     int32_t InitDataCallback();
93     int32_t NotifyDeviceStatusChange(const Data &devicestatusData);
94     void Subscribe(Type type, ActivityEvent event, ReportLatencyNs latency, sptr<IRemoteDevStaCallback> callback);
95     void Unsubscribe(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback);
96     int32_t Subscribe(int32_t type, const std::string &bundleName, sptr<IRemoteBoomerangCallback> callback);
97     int32_t Unsubscribe(int32_t type, const std::string &bundleName, sptr<IRemoteBoomerangCallback> callback);
98     int32_t NotifyMetadata(const std::string &bundleName, sptr<IRemoteBoomerangCallback> callback);
99     int32_t SubmitMetadata(const std::string &metadata);
100     int32_t BoomerangEncodeImage(std::shared_ptr<Media::PixelMap> pixelMap, const std::string &metadata,
101         sptr<IRemoteBoomerangCallback> callback);
102     int32_t BoomerangDecodeImage(std::shared_ptr<Media::PixelMap> pixelMap, sptr<IRemoteBoomerangCallback> callback);
103     Data GetLatestDeviceStatusData(Type type);
104     int32_t MsdpDataCallback(const Data &data);
105     int32_t LoadAlgorithm();
106     int32_t UnloadAlgorithm();
107     int32_t GetPackageName(AccessTokenID tokenId, std::string &packageName);
108 #ifdef BOOMERANG_ONESTEP
109     void OnSurfaceCapture(int32_t windowId, std::shared_ptr<Media::PixelMap> &screenShot);
110 #endif
111 
112 private:
113     struct classcomp {
operatorclasscomp114         bool operator()(sptr<IRemoteDevStaCallback> left, sptr<IRemoteDevStaCallback> right) const
115         {
116             return left->AsObject() < right->AsObject();
117         }
118     };
119 
120     struct boomerangClasscomp {
operatorboomerangClasscomp121         bool operator()(sptr<IRemoteBoomerangCallback> left, sptr<IRemoteBoomerangCallback> right) const
122         {
123             return left->AsObject() < right->AsObject();
124         }
125     };
126 #ifdef BOOMERANG_ONESTEP
127     int32_t GetFocuseWindowId(int32_t &windowId, std::string &bundleName);
128 #endif
129     int32_t GetBundleNameByCallback(std::string &bundleName);
130     int32_t GetBundleNameByApplink(std::string &bundleName, const std::string &metadata);
131 #ifdef BOOMERANG_ONESTEP
132     void HandlerPageScrollerEvent(int32_t event);
133     void SystemBarHiddedInit();
134 #endif
135     void TimerTask();
136     static constexpr int32_t argSize_ { TYPE_MAX };
137 
138     std::mutex mutex_;
139     sptr<IRemoteObject::DeathRecipient> devicestatusCBDeathRecipient_ { nullptr };
140     sptr<IRemoteObject::DeathRecipient> boomerangCBDeathRecipient_ { nullptr };
141     std::shared_ptr<DeviceStatusMsdpClientImpl> msdpImpl_ { nullptr };
142     std::map<Type, OnChangedValue> msdpData_;
143     std::map<Type, std::set<const sptr<IRemoteDevStaCallback>, classcomp>> listeners_;
144     std::map<std::string, std::set<const sptr<IRemoteBoomerangCallback>, boomerangClasscomp>> boomerangListeners_;
145     sptr<IRemoteBoomerangCallback> notityListener_ { nullptr };
146     sptr<IRemoteBoomerangCallback> encodeCallback_ { nullptr };
147     std::map<sptr<IRemoteBoomerangCallback>, std::string> bundleNameCache_;
148     sptr<AppExecFwk::IBundleMgr> bundleManager_ { nullptr };
149     int32_t type_ { -1 };
150     int32_t boomerangType_ { -1 };
151     int32_t event_ { -1 };
152     int32_t arrs_[argSize_] {};
153 #ifdef BOOMERANG_ONESTEP
154     bool isAccessibilityInit_ = false;
155     std::atomic<bool> lastEnable_ { true };
156     int32_t retryCount { 0 };
157     static std::shared_ptr<DeviceStatusManager> g_deviceManager_;
158 #endif
159     std::atomic<bool> hasSubmitted_ { false };
160     static std::mutex g_mutex_;
161     std::recursive_mutex countMutex_;
162 };
163 } // namespace DeviceStatus
164 } // namespace Msdp
165 } // namespace OHOS
166 #endif // DEVICESTATUS_MANAGER_H
167