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 "accesstoken_kit.h" 23 #include "boomerang_callback.h" 24 #include "boomerang_data.h" 25 #include "devicestatus_msdp_client_impl.h" 26 #include "stationary_callback.h" 27 #include "stationary_data.h" 28 29 namespace OHOS { 30 namespace Msdp { 31 namespace DeviceStatus { 32 using namespace Security::AccessToken; 33 class DeviceStatusService; 34 class DeviceStatusManager { 35 public: 36 DeviceStatusManager() = default; 37 ~DeviceStatusManager() = default; 38 39 class DeviceStatusCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 40 public: 41 DeviceStatusCallbackDeathRecipient() = default; 42 virtual void OnRemoteDied(const wptr<IRemoteObject> &remote); 43 virtual ~DeviceStatusCallbackDeathRecipient() = default; 44 }; 45 46 class BoomerangCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 47 public: BoomerangCallbackDeathRecipient(DeviceStatusManager * deviceStatusManager)48 BoomerangCallbackDeathRecipient(DeviceStatusManager* deviceStatusManager) : manager_(deviceStatusManager) {} 49 virtual void OnRemoteDied(const wptr<IRemoteObject> &remote); 50 virtual ~BoomerangCallbackDeathRecipient() = default; 51 private: 52 DeviceStatusManager* manager_; 53 friend class DeviceStatusManager; 54 }; 55 56 bool Init(); 57 bool Enable(Type type); 58 bool InitAlgoMngrInterface(Type type); 59 bool Disable(Type type); 60 int32_t InitDataCallback(); 61 int32_t NotifyDeviceStatusChange(const Data &devicestatusData); 62 void Subscribe(Type type, ActivityEvent event, ReportLatencyNs latency, sptr<IRemoteDevStaCallback> callback); 63 void Unsubscribe(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback); 64 void Subscribe(BoomerangType type, std::string bundleName, sptr<IRemoteBoomerangCallback> callback); 65 void Unsubscribe(BoomerangType type, std::string bundleName, sptr<IRemoteBoomerangCallback> callback); 66 int32_t NotifyMedata(std::string bundleName, sptr<IRemoteBoomerangCallback> callback); 67 void SubmitMetadata(std::string metadata); 68 void BoomerangEncodeImage(std::shared_ptr<Media::PixelMap> pixelMap, std::string matedata, 69 sptr<IRemoteBoomerangCallback> callback); 70 void BoomerangDecodeImage(std::shared_ptr<Media::PixelMap> pixelMap, sptr<IRemoteBoomerangCallback> callback); 71 Data GetLatestDeviceStatusData(Type type); 72 int32_t MsdpDataCallback(const Data &data); 73 int32_t LoadAlgorithm(); 74 int32_t UnloadAlgorithm(); 75 int32_t GetPackageName(AccessTokenID tokenId, std::string &packageName); 76 77 private: 78 struct classcomp { operatorclasscomp79 bool operator()(sptr<IRemoteDevStaCallback> left, sptr<IRemoteDevStaCallback> right) const 80 { 81 return left->AsObject() < right->AsObject(); 82 } 83 }; 84 85 struct boomerangClasscomp { operatorboomerangClasscomp86 bool operator()(sptr<IRemoteBoomerangCallback> left, sptr<IRemoteBoomerangCallback> right) const 87 { 88 return left->AsObject() < right->AsObject(); 89 } 90 }; 91 static constexpr int32_t argSize_ { TYPE_MAX }; 92 93 std::mutex mutex_; 94 sptr<IRemoteObject::DeathRecipient> devicestatusCBDeathRecipient_ { nullptr }; 95 sptr<IRemoteObject::DeathRecipient> boomerangCBDeathRecipient_ { nullptr }; 96 std::shared_ptr<DeviceStatusMsdpClientImpl> msdpImpl_ { nullptr }; 97 std::map<Type, OnChangedValue> msdpData_; 98 std::map<Type, std::set<const sptr<IRemoteDevStaCallback>, classcomp>> listeners_; 99 std::map<std::string, std::set<const sptr<IRemoteBoomerangCallback>, boomerangClasscomp>> boomerangListeners_; 100 sptr<IRemoteBoomerangCallback> notityListener_; 101 sptr<IRemoteBoomerangCallback> encodeCallback_; 102 int32_t type_ { -1 }; 103 int32_t boomerangType_ { -1 }; 104 int32_t event_ { -1 }; 105 int32_t arrs_[argSize_] {}; 106 }; 107 } // namespace DeviceStatus 108 } // namespace Msdp 109 } // namespace OHOS 110 #endif // DEVICESTATUS_MANAGER_H 111