• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "unistd.h"
16 
17 #include "bundle_active_client.h"
18 
19 namespace OHOS {
20 namespace DeviceUsageStats {
21 namespace {
22     const std::string BUNDLE_ACTIVE_CLIENT_NAME = "bundleActiveName";
23     static const int32_t DELAY_TIME = 5000;
24     static const int32_t SLEEP_TIME = 1000;
25 }
GetInstance()26 BundleActiveClient& BundleActiveClient::GetInstance()
27 {
28     static BundleActiveClient instance;
29     return instance;
30 }
31 
GetBundleActiveProxy()32 ErrCode BundleActiveClient::GetBundleActiveProxy()
33 {
34     if (bundleActiveProxy_ != nullptr) {
35         return ERR_OK;
36     }
37     std::lock_guard<std::mutex> lock(mutex_);
38     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
39     if (!samgr) {
40         BUNDLE_ACTIVE_LOGE("Failed to get SystemAbilityManager.");
41         return ERR_GET_SYSTEM_ABILITY_MANAGER_FAILED;
42     }
43 
44     sptr<IRemoteObject> object = samgr->GetSystemAbility(DEVICE_USAGE_STATISTICS_SYS_ABILITY_ID);
45     if (!object) {
46         BUNDLE_ACTIVE_LOGE("Failed to get SystemAbility[1907] .");
47         return ERR_GET_SYSTEM_ABILITY_FAILED;
48     }
49 
50     bundleActiveProxy_ = iface_cast<IBundleActiveService>(object);
51     if (!bundleActiveProxy_) {
52         BUNDLE_ACTIVE_LOGE("Failed to get BundleActiveProxy.");
53         return ERR_REMOTE_OBJECT_IF_CAST_FAILED;
54     }
55     if (!recipient_) {
56         recipient_ = new (std::nothrow) BundleActiveClientDeathRecipient();
57     }
58     if (recipient_) {
59         bundleActiveProxy_->AsObject()->AddDeathRecipient(recipient_);
60     }
61 
62     bundleClientRunner_ = AppExecFwk::EventRunner::Create(BUNDLE_ACTIVE_CLIENT_NAME);
63     if (!bundleClientRunner_) {
64         BUNDLE_ACTIVE_LOGE("BundleActiveClient runner create failed!");
65         return ERR_MEMORY_OPERATION_FAILED;
66     }
67     bundleClientHandler_ = std::make_shared<OHOS::AppExecFwk::EventHandler>(bundleClientRunner_);
68     if (!bundleClientHandler_) {
69         BUNDLE_ACTIVE_LOGE("BundleActiveClient handler create failed!");
70         return ERR_MEMORY_OPERATION_FAILED;
71     }
72     return ERR_OK;
73 }
74 
ReportEvent(BundleActiveEvent event,const int32_t userId)75 ErrCode BundleActiveClient::ReportEvent(BundleActiveEvent event, const int32_t userId)
76 {
77     BUNDLE_ACTIVE_LOGI("BundleActiveClient::ReportEvent called");
78     ErrCode ret = GetBundleActiveProxy();
79     if (ret != ERR_OK) {
80         return ret;
81     }
82     return bundleActiveProxy_->ReportEvent(event, userId);
83 }
84 
IsBundleIdle(bool & isBundleIdle,const std::string & bundleName,int32_t userId)85 ErrCode BundleActiveClient::IsBundleIdle(bool& isBundleIdle, const std::string& bundleName, int32_t userId)
86 {
87     ErrCode ret = GetBundleActiveProxy();
88     if (ret != ERR_OK) {
89         return ret;
90     }
91     return bundleActiveProxy_->IsBundleIdle(isBundleIdle, bundleName, userId);
92 }
93 
QueryBundleStatsInfoByInterval(std::vector<BundleActivePackageStats> & PackageStats,const int32_t intervalType,const int64_t beginTime,const int64_t endTime,int32_t userId)94 ErrCode BundleActiveClient::QueryBundleStatsInfoByInterval(std::vector<BundleActivePackageStats>& PackageStats,
95     const int32_t intervalType, const int64_t beginTime, const int64_t endTime, int32_t userId)
96 {
97     ErrCode ret = GetBundleActiveProxy();
98     if (ret != ERR_OK) {
99         return ret;
100     }
101     return bundleActiveProxy_->QueryBundleStatsInfoByInterval(PackageStats, intervalType, beginTime, endTime, userId);
102 }
103 
QueryBundleEvents(std::vector<BundleActiveEvent> & bundleActiveEvents,const int64_t beginTime,const int64_t endTime,int32_t userId)104 ErrCode BundleActiveClient::QueryBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents,
105     const int64_t beginTime, const int64_t endTime, int32_t userId)
106 {
107     ErrCode ret = GetBundleActiveProxy();
108     if (ret != ERR_OK) {
109         return ret;
110     }
111     auto err = bundleActiveProxy_->QueryBundleEvents(bundleActiveEvents, beginTime, endTime, userId);
112     BUNDLE_ACTIVE_LOGI("QueryBundleEvents bundleActiveEvents is %{public}zu", bundleActiveEvents.size());
113     return err;
114 }
115 
SetAppGroup(std::string bundleName,const int32_t newGroup,int32_t userId)116 ErrCode BundleActiveClient::SetAppGroup(std::string bundleName, const int32_t newGroup, int32_t userId)
117 {
118     ErrCode ret = GetBundleActiveProxy();
119     if (ret != ERR_OK) {
120         return ret;
121     }
122     return bundleActiveProxy_->SetAppGroup(bundleName, newGroup, userId);
123 }
124 
QueryBundleStatsInfos(std::vector<BundleActivePackageStats> & bundleActivePackageStats,const int32_t intervalType,const int64_t beginTime,const int64_t endTime)125 ErrCode BundleActiveClient::QueryBundleStatsInfos(std::vector<BundleActivePackageStats>& bundleActivePackageStats,
126     const int32_t intervalType, const int64_t beginTime, const int64_t endTime)
127 {
128     ErrCode ret = GetBundleActiveProxy();
129     if (ret != ERR_OK) {
130         return ret;
131     }
132     return bundleActiveProxy_->QueryBundleStatsInfos(bundleActivePackageStats, intervalType, beginTime, endTime);
133 }
134 
QueryCurrentBundleEvents(std::vector<BundleActiveEvent> & bundleActiveEvents,const int64_t beginTime,const int64_t endTime)135 ErrCode BundleActiveClient::QueryCurrentBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents,
136     const int64_t beginTime, const int64_t endTime)
137 {
138     ErrCode ret = GetBundleActiveProxy();
139     if (ret != ERR_OK) {
140         return ret;
141     }
142     return bundleActiveProxy_->QueryCurrentBundleEvents(bundleActiveEvents, beginTime, endTime);
143 }
144 
QueryAppGroup(int32_t & appGroup,std::string & bundleName,const int32_t userId)145 ErrCode BundleActiveClient::QueryAppGroup(int32_t& appGroup, std::string& bundleName, const int32_t userId)
146 {
147     ErrCode ret = GetBundleActiveProxy();
148     if (ret != ERR_OK) {
149         return ret;
150     }
151     return bundleActiveProxy_->QueryAppGroup(appGroup, bundleName, userId);
152 }
153 
QueryModuleUsageRecords(int32_t maxNum,std::vector<BundleActiveModuleRecord> & results,int32_t userId)154 ErrCode BundleActiveClient::QueryModuleUsageRecords(int32_t maxNum, std::vector<BundleActiveModuleRecord>& results,
155     int32_t userId)
156 {
157     if (maxNum <= 0 || maxNum > MAXNUM_UP_LIMIT) {
158         BUNDLE_ACTIVE_LOGI("maxNum is illegal, maxNum is %{public}d", maxNum);
159         return ERR_MAX_RECORDS_NUM_BIGER_THEN_ONE_THOUSAND;
160     }
161     ErrCode ret = GetBundleActiveProxy();
162     if (ret != ERR_OK) {
163         return ret;
164     }
165     return bundleActiveProxy_->QueryModuleUsageRecords(maxNum, results, userId);
166 }
167 
RegisterAppGroupCallBack(const sptr<IAppGroupCallback> & observer)168 ErrCode BundleActiveClient::RegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer)
169 {
170     ErrCode ret = GetBundleActiveProxy();
171     if (ret != ERR_OK) {
172         return ret;
173     }
174     ret = bundleActiveProxy_->RegisterAppGroupCallBack(observer);
175     if (recipient_ && ret == ERR_OK) {
176         recipient_->AddObserver(observer);
177     }
178     return ret;
179 }
180 
UnRegisterAppGroupCallBack(const sptr<IAppGroupCallback> & observer)181 ErrCode BundleActiveClient::UnRegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer)
182 {
183     ErrCode ret = GetBundleActiveProxy();
184     if (ret != ERR_OK) {
185         return ret;
186     }
187     ret = bundleActiveProxy_->UnRegisterAppGroupCallBack(observer);
188     if (recipient_ && ret == ERR_OK) {
189         recipient_->RemoveObserver();
190     }
191     return ret;
192 }
193 
QueryDeviceEventStats(int64_t beginTime,int64_t endTime,std::vector<BundleActiveEventStats> & eventStats,int32_t userId)194 ErrCode BundleActiveClient::QueryDeviceEventStats(int64_t beginTime, int64_t endTime,
195     std::vector<BundleActiveEventStats>& eventStats, int32_t userId)
196 {
197     ErrCode ret = GetBundleActiveProxy();
198     if (ret != ERR_OK) {
199         return ret;
200     }
201     return bundleActiveProxy_->QueryDeviceEventStats(beginTime, endTime, eventStats, userId);
202 }
203 
QueryNotificationEventStats(int64_t beginTime,int64_t endTime,std::vector<BundleActiveEventStats> & eventStats,int32_t userId)204 ErrCode BundleActiveClient::QueryNotificationEventStats(int64_t beginTime, int64_t endTime,
205     std::vector<BundleActiveEventStats>& eventStats, int32_t userId)
206 {
207     ErrCode ret = GetBundleActiveProxy();
208     if (ret != ERR_OK) {
209         return ret;
210     }
211     return bundleActiveProxy_->QueryNotificationEventStats(beginTime, endTime, eventStats, userId);
212 }
213 
AddObserver(const sptr<IAppGroupCallback> & observer)214 void BundleActiveClient::BundleActiveClientDeathRecipient::AddObserver(const sptr<IAppGroupCallback> &observer)
215 {
216     if (observer) {
217         observer_ = observer;
218     }
219 }
220 
RemoveObserver()221 void BundleActiveClient::BundleActiveClientDeathRecipient::RemoveObserver()
222 {
223     if (observer_) {
224         observer_ = nullptr;
225     }
226 }
227 
OnRemoteDied(const wptr<IRemoteObject> & object)228 void BundleActiveClient::BundleActiveClientDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
229 {
230     (void)object;
231     BundleActiveClient::GetInstance().bundleActiveProxy_ = nullptr;
232     BundleActiveClient::GetInstance().bundleClientHandler_->PostTask([this]() {
233             this->OnServiceDiedInner();
234         }, DELAY_TIME);
235 }
236 
OnServiceDiedInner()237 void BundleActiveClient::BundleActiveClientDeathRecipient::OnServiceDiedInner()
238 {
239     while (BundleActiveClient::GetInstance().GetBundleActiveProxy() != ERR_OK) {
240         sleep(SLEEP_TIME);
241     }
242     if (observer_) {
243         BundleActiveClient::GetInstance().RegisterAppGroupCallBack(observer_);
244     }
245 }
246 }  // namespace DeviceUsageStats
247 }  // namespace OHOS