1 /* 2 * Copyright (c) 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 FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_INTERFACE_INNERKITS_INCLUDE_STANDBY_SERVICE_CLIENT_H 17 #define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_INTERFACE_INNERKITS_INCLUDE_STANDBY_SERVICE_CLIENT_H 18 19 #include <iremote_proxy.h> 20 #include <nocopyable.h> 21 22 #include "istandby_service.h" 23 #include "standby_service_proxy.h" 24 #include "standby_res_data.h" 25 #include "standby_service_errors.h" 26 27 namespace OHOS { 28 namespace DevStandbyMgr { 29 class StandbyServiceClient { 30 public: 31 StandbyServiceClient(); 32 33 virtual ~StandbyServiceClient(); 34 35 static StandbyServiceClient& GetInstance(); 36 37 /** 38 * @brief Subscribes sleep state change event. 39 * 40 * @param subscriber Subscriber token. 41 * @return ERR_OK if success, else fail. 42 */ 43 ErrCode SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber); 44 45 /** 46 * @brief Unsubscribes sleep state change event. 47 * 48 * @param subscriber Subscriber token. 49 * @return ERR_OK if success, else fail. 50 */ 51 ErrCode UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber); 52 53 /** 54 * @brief add allow list for some services or apps. 55 * 56 * @param resourceRequest resource to be added. 57 * @return ErrCode ERR_OK if success, others if fail. 58 */ 59 ErrCode ApplyAllowResource(const sptr<ResourceRequest>& resourceRequest); 60 61 /** 62 * @brief remove uid with allow type from allow list. 63 * 64 * @param resourceRequest resource to be removed. 65 * @return ErrCode ErrCode ERR_OK if success, others if fail. 66 */ 67 ErrCode UnapplyAllowResource(const sptr<ResourceRequest>& resourceRequest); 68 69 /** 70 * @brief Get the Allow List object. 71 * 72 * @param allowType the allow type to be retrieved. 73 * @param allowInfoList result represents allowed types and apps. 74 * @param reasonCode represents the reason why invoke the api. 75 * @return ErrCode ERR_OK if success, else fail. 76 */ 77 ErrCode GetAllowList(uint32_t allowType, std::vector<AllowInfo>& allowInfoList, 78 uint32_t reasonCode); 79 80 /** 81 * @brief Get the Restrict List object. 82 * 83 * @param restrictType the restrict type to be retrieved. 84 * @param restrictInfoList result represents restricted types and apps. 85 * @param reasonCode represents the reason why invoke the api. 86 * @return ErrCode ERR_OK if success, others if fail. 87 */ 88 ErrCode GetRestrictList(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList, 89 uint32_t reasonCode); 90 91 /** 92 * @brief Construct a new Report Work Scheduler Status object. 93 * 94 * @param started true if the work is triggered, else false. 95 * @param uid uid of the applicatoin. 96 * @param bundleName bundleName of the application. 97 * @return ErrCode ERR_OK if success, others if fail. 98 */ 99 ErrCode ReportWorkSchedulerStatus(bool started, int32_t uid, const std::string& bundleName); 100 101 /** 102 * @brief Whether the restriction strategy enbaled or not. 103 * 104 * @param strategyName the strategy name. 105 * @param enabled true if the strategy is enabled. 106 * @return ErrCode ERR_OK if success, others if fail. 107 */ 108 ErrCode IsStrategyEnabled(const std::string& strategyName, bool& isEnabled); 109 110 /** 111 * @brief Report event when device state change, such as discomponent device, bluetooth socket.. 112 * 113 * @param type type of device state. 114 * @param enabled true if the device state is on. 115 * @return ErrCode ERR_OK if success, others if fail. 116 */ 117 ErrCode ReportDeviceStateChanged(DeviceStateType type, bool enabled); 118 119 /** 120 * @brief query if the device is in standby mode; 121 * 122 * @param isStandby true if device in standby, else false. 123 * @return ErrCode ERR_OK if success, else fail. 124 */ 125 ErrCode IsDeviceInStandby(bool& isStandby); 126 127 /** 128 * @brief Unified handling of events; 129 * 130 * @param resData event data. 131 * @return ErrCode ERR_OK if success, else fail. 132 */ 133 ErrCode HandleEvent(const std::shared_ptr<ResData> &resData); 134 135 private: 136 bool GetStandbyServiceProxy(); 137 void ResetStandbyServiceClient(); 138 139 class StandbyServiceDeathRecipient : public IRemoteObject::DeathRecipient { 140 public: 141 explicit StandbyServiceDeathRecipient(StandbyServiceClient& standbyServiceClient); 142 143 ~StandbyServiceDeathRecipient() override; 144 145 void OnRemoteDied(const wptr<IRemoteObject>& object) override; 146 147 private: 148 StandbyServiceClient& standbyServiceClient_; 149 }; 150 151 private: 152 std::mutex mutex_; 153 sptr<IStandbyService> standbyServiceProxy_; 154 sptr<StandbyServiceDeathRecipient> deathRecipient_; 155 }; 156 } // namespace DevStandbyMgr 157 } // namespace OHOS 158 #endif // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_INTERFACE_INNERKITS_INCLUDE_STANDBY_SERVICE_CLIENT_H 159