1 /* 2 * Copyright (c) 2022 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 BUNDLE_ACTIVE_CLIENT_H 17 #define BUNDLE_ACTIVE_CLIENT_H 18 19 #include <mutex> 20 21 #include "ibundle_active_service.h" 22 #include "bundle_active_package_stats.h" 23 #include "bundle_active_event.h" 24 #include "bundle_active_event_stats.h" 25 #include "bundle_active_package_stats.h" 26 #include "bundle_active_module_record.h" 27 #include "bundle_state_inner_errors.h" 28 #include "event_handler.h" 29 #include "event_runner.h" 30 31 namespace OHOS { 32 namespace DeviceUsageStats { 33 class BundleActiveClient { 34 public: 35 // max number of query modules result. 36 const int32_t MAXNUM_UP_LIMIT = 1000; 37 38 /** 39 * @brief ReportEvent, used to report event. 40 * 41 * @param event . 42 * @param userId . 43 * @return errCode. 44 */ 45 ErrCode ReportEvent(BundleActiveEvent event, const int32_t userId); 46 47 /** 48 * @brief IsBundleIdle, used to check whether specific bundle is idle. 49 * 50 * @param isBundleIdle The result of IsBundleIdle. 51 * @param bundleName . 52 * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId. 53 * @return errCode. 54 */ 55 ErrCode IsBundleIdle(bool& isBundleIdle, const std::string& bundleName, int32_t userId = -1); 56 57 /** 58 * @brief QueryBundleStatsInfoByInterval, query all bundle usage statistics in specific time span for calling user. 59 * 60 * @param bundleActivePackageStats . 61 * @param intervalType . 62 * @param beginTime . 63 * @param endTime . 64 * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId. 65 * @return errCode. 66 */ 67 ErrCode QueryBundleStatsInfoByInterval(std::vector<BundleActivePackageStats>& bundleActivePackageStats, 68 const int32_t intervalType, const int64_t beginTime, const int64_t endTime, int32_t userId = -1); 69 70 /** 71 * @brief QueryBundleEvents, query all events in specific time span for calling user. 72 * 73 * @param bundleActiveEvents . 74 * @param beginTime . 75 * @param endTime . 76 * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId. 77 * @return errCode. 78 */ 79 ErrCode QueryBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents, const int64_t beginTime, 80 const int64_t endTime, int32_t userId = -1); 81 82 /** 83 * @brief SetAppGroup, set specific bundle of specific user to a priority group. 84 * 85 * @param bundleName . 86 * @param newGroup . 87 * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId. 88 * @return errCode. 89 */ 90 ErrCode SetAppGroup(std::string bundleName, const int32_t newGroup, int32_t userId = -1); 91 92 /** 93 * @brief QueryBundleStatsInfos, query bundle usage statistics in specific time span for calling bundle. 94 * 95 * @param bundleActivePackageStats bundleActivePackageStats, The result of QueryBundleStatsInfos. 96 * @param intervalType . 97 * @param beginTime . 98 * @param endTime . 99 * @return errCode. 100 */ 101 ErrCode QueryBundleStatsInfos(std::vector<BundleActivePackageStats>& bundleActivePackageStats, 102 const int32_t intervalType, const int64_t beginTime, const int64_t endTime); 103 104 /** 105 * @brief QueryCurrentBundleEvents, query bundle usage statistics in specific time span for calling bundle. 106 * 107 * @param bundleActiveEvents the std::vector<BundleActiveEvent>, as the result of QueryCurrentBundleEvents. 108 * @param beginTime . 109 * @param endTime . 110 * @return errCode. 111 */ 112 ErrCode QueryCurrentBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents, 113 const int64_t beginTime, const int64_t endTime); 114 115 /** 116 * @brief QueryAppGroup, query appGroup by bundleName and userId. 117 * 118 * @param appGroup as the result of QueryAppGroup. 119 * @param bundleName . 120 * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId. 121 * @return errCode. 122 */ 123 ErrCode QueryAppGroup(int32_t& appGroup, std::string& bundleName, const int32_t userId = -1); 124 125 /** 126 * @brief QueryModuleUsageRecords, query all from usage statistics in specific time span for calling user. 127 * 128 * @param maxNum . 129 * @param results . 130 * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId. 131 * @return errCode. 132 */ 133 ErrCode QueryModuleUsageRecords(int32_t maxNum, 134 std::vector<BundleActiveModuleRecord>& results, int32_t userId = -1); 135 136 /** 137 * @brief RegisterAppGroupCallBack, observe bundle group change event. 138 * 139 * @param observer . 140 * @return errCode. 141 */ 142 ErrCode RegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer); 143 144 /** 145 * @brief UnRegisterAppGroupCallBack, unobserve bundle group change event. 146 * 147 * @param observer . 148 * @return errCode. 149 */ 150 ErrCode UnRegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer); 151 152 /** 153 * @brief QueryDeviceEventStats, query all from event stats in specific time span for calling user. 154 * 155 * @param beginTime . 156 * @param endTime . 157 * @param eventStats . 158 * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId. 159 * @return errCode. 160 */ 161 ErrCode QueryDeviceEventStats(int64_t beginTime, int64_t endTime, 162 std::vector<BundleActiveEventStats>& eventStats, int32_t userId = -1); 163 164 /** 165 * @brief QueryNotificationEventStats, query all app notification number in specific time span for calling user. 166 * 167 * @param beginTime . 168 * @param endTime . 169 * @param eventStats . 170 * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId. 171 * @return errCode. 172 */ 173 ErrCode QueryNotificationEventStats(int64_t beginTime, int64_t endTime, 174 std::vector<BundleActiveEventStats>& eventStats, int32_t userId = -1); 175 /* 176 * function: GetInstance, get single instance of client. 177 * return: object of BundleActiveClient. 178 */ 179 static BundleActiveClient& GetInstance(); 180 private: 181 class BundleActiveClientDeathRecipient : public IRemoteObject::DeathRecipient { 182 public: 183 /* 184 * function: BundleActiveClientDeathRecipient, default constructor. 185 */ 186 BundleActiveClientDeathRecipient() = default; 187 188 /* 189 * function: ~BundleActiveClientDeathRecipient, default destructor. 190 */ 191 ~BundleActiveClientDeathRecipient() = default; 192 193 /* 194 * function: AddObserver. 195 */ 196 void AddObserver(const sptr<IAppGroupCallback> &observer); 197 198 /* 199 * function: RemoveObserver. 200 */ 201 void RemoveObserver(); 202 203 /* 204 * function: OnRemoteDied, PostTask when service(bundleActiveProxy_) is died. 205 */ 206 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 207 208 /* 209 * function: OnServiceDiedInner, get bundleActiveProxy_ and RegisterAppGroupCallBack again. 210 */ 211 void OnServiceDiedInner(); 212 213 private: 214 sptr<IAppGroupCallback> observer_ = nullptr; 215 }; 216 private: 217 ErrCode GetBundleActiveProxy(); BundleActiveClient()218 BundleActiveClient() {} ~BundleActiveClient()219 ~BundleActiveClient() {} 220 sptr<IBundleActiveService> bundleActiveProxy_; 221 sptr<BundleActiveClientDeathRecipient> recipient_; 222 std::shared_ptr<AppExecFwk::EventRunner> bundleClientRunner_ {nullptr}; 223 std::shared_ptr<AppExecFwk::EventHandler> bundleClientHandler_ {nullptr}; 224 std::recursive_mutex mutex_; 225 }; 226 } // namespace DeviceUsageStats 227 } // namespace OHOS 228 #endif // BUNDLE_ACTIVE_CLIENT_H 229 230