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