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 QueryBundleStatsInfoByInterval, query all bundle usage statistics in specific time span for calling user. 60 * 61 * @param bundleActivePackageStats . 62 * @param intervalType . 63 * @param beginTime . 64 * @param endTime . 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 QueryBundleStatsInfoByInterval(std::vector<BundleActivePackageStats>& bundleActivePackageStats, 69 const int32_t intervalType, const int64_t beginTime, const int64_t endTime, int32_t userId = -1); 70 71 /** 72 * @brief QueryBundleEvents, query all events in specific time span for calling user. 73 * 74 * @param bundleActiveEvents . 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 QueryBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents, const int64_t beginTime, 81 const int64_t endTime, int32_t userId = -1); 82 83 /** 84 * @brief SetAppGroup, set specific bundle of specific user to a priority group. 85 * 86 * @param bundleName . 87 * @param newGroup . 88 * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId. 89 * @return errCode. 90 */ 91 ErrCode SetAppGroup(std::string bundleName, const int32_t newGroup, int32_t userId = -1); 92 93 /** 94 * @brief QueryBundleStatsInfos, query bundle usage statistics in specific time span for calling bundle. 95 * 96 * @param bundleActivePackageStats bundleActivePackageStats, The result of QueryBundleStatsInfos. 97 * @param intervalType . 98 * @param beginTime . 99 * @param endTime . 100 * @return errCode. 101 */ 102 ErrCode QueryBundleStatsInfos(std::vector<BundleActivePackageStats>& bundleActivePackageStats, 103 const int32_t intervalType, const int64_t beginTime, const int64_t endTime); 104 105 /** 106 * @brief QueryCurrentBundleEvents, query bundle usage statistics in specific time span for calling bundle. 107 * 108 * @param bundleActiveEvents the std::vector<BundleActiveEvent>, as the result of QueryCurrentBundleEvents. 109 * @param beginTime . 110 * @param endTime . 111 * @return errCode. 112 */ 113 ErrCode QueryCurrentBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents, 114 const int64_t beginTime, const int64_t endTime); 115 116 /** 117 * @brief QueryAppGroup, query appGroup by bundleName and userId. 118 * 119 * @param appGroup as the result of QueryAppGroup. 120 * @param bundleName . 121 * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId. 122 * @return errCode. 123 */ 124 ErrCode QueryAppGroup(int32_t& appGroup, std::string& bundleName, const int32_t userId = -1); 125 126 /** 127 * @brief QueryModuleUsageRecords, query all from usage statistics in specific time span for calling user. 128 * 129 * @param maxNum . 130 * @param results . 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 QueryModuleUsageRecords(int32_t maxNum, 135 std::vector<BundleActiveModuleRecord>& results, int32_t userId = -1); 136 137 /** 138 * @brief RegisterAppGroupCallBack, observe bundle group change event. 139 * 140 * @param observer . 141 * @return errCode. 142 */ 143 ErrCode RegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer); 144 145 /** 146 * @brief UnRegisterAppGroupCallBack, unobserve bundle group change event. 147 * 148 * @param observer . 149 * @return errCode. 150 */ 151 ErrCode UnRegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer); 152 153 /** 154 * @brief QueryDeviceEventStats, query all from event stats in specific time span for calling user. 155 * 156 * @param beginTime . 157 * @param endTime . 158 * @param eventStats . 159 * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId. 160 * @return errCode. 161 */ 162 ErrCode QueryDeviceEventStats(int64_t beginTime, int64_t endTime, 163 std::vector<BundleActiveEventStats>& eventStats, int32_t userId = -1); 164 165 /** 166 * @brief QueryNotificationEventStats, query all app notification number 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 QueryNotificationEventStats(int64_t beginTime, int64_t endTime, 175 std::vector<BundleActiveEventStats>& eventStats, int32_t userId = -1); 176 /* 177 * function: GetInstance, get single instance of client. 178 * return: object of BundleActiveClient. 179 */ 180 static BundleActiveClient& GetInstance(); 181 private: 182 class BundleActiveClientDeathRecipient : public IRemoteObject::DeathRecipient { 183 public: 184 /* 185 * function: BundleActiveClientDeathRecipient, default constructor. 186 */ 187 BundleActiveClientDeathRecipient() = default; 188 189 /* 190 * function: ~BundleActiveClientDeathRecipient, default destructor. 191 */ 192 ~BundleActiveClientDeathRecipient() = default; 193 194 /* 195 * function: AddObserver. 196 */ 197 void AddObserver(const sptr<IAppGroupCallback> &observer); 198 199 /* 200 * function: RemoveObserver. 201 */ 202 void RemoveObserver(); 203 204 /* 205 * function: OnRemoteDied, PostTask when service(bundleActiveProxy_) is died. 206 */ 207 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 208 209 /* 210 * function: OnServiceDiedInner, get bundleActiveProxy_ and RegisterAppGroupCallBack again. 211 */ 212 void OnServiceDiedInner(); 213 214 private: 215 sptr<IAppGroupCallback> observer_ = nullptr; 216 }; 217 private: 218 ErrCode GetBundleActiveProxy(); BundleActiveClient()219 BundleActiveClient() {} ~BundleActiveClient()220 ~BundleActiveClient() {} 221 sptr<IBundleActiveService> bundleActiveProxy_; 222 sptr<BundleActiveClientDeathRecipient> recipient_; 223 ffrt::recursive_mutex mutex_; 224 }; 225 } // namespace DeviceUsageStats 226 } // namespace OHOS 227 #endif // BUNDLE_ACTIVE_CLIENT_H 228 229