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