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