• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 #include "device_status_collect_manager.h"
17 
18 #include "datetime_ex.h"
19 #include "device_timed_collect.h"
20 #ifdef SUPPORT_DEVICE_MANAGER
21 #include "device_networking_collect.h"
22 #endif
23 #ifdef SUPPORT_COMMON_EVENT
24 #include "common_event_collect.h"
25 #endif
26 #ifdef SUPPORT_SWITCH_COLLECT
27 #include "device_switch_collect.h"
28 #endif
29 #include "device_param_collect.h"
30 #include "ref_count_collect.h"
31 #include "memory_guard.h"
32 #include "sam_log.h"
33 #include "system_ability_manager.h"
34 
35 namespace OHOS {
36 namespace {
37 constexpr int32_t TO_MILLISECOND = 1000;
38 }
Init(const std::list<SaProfile> & saProfiles)39 void DeviceStatusCollectManager::Init(const std::list<SaProfile>& saProfiles)
40 {
41     HILOGI("DeviceStaMgr Init begin");
42     FilterOnDemandSaProfiles(saProfiles);
43     collectHandler_ = std::make_shared<FFRTHandler>("collect");
44     sptr<ICollectPlugin> deviceParamCollect = new DeviceParamCollect(this);
45     deviceParamCollect->Init(saProfiles);
46     collectPluginMap_[PARAM] = deviceParamCollect;
47 #ifdef SUPPORT_DEVICE_MANAGER
48     sptr<ICollectPlugin> networkingCollect = new DeviceNetworkingCollect(this);
49     collectPluginMap_[DEVICE_ONLINE] = networkingCollect;
50 #endif
51 #ifdef SUPPORT_COMMON_EVENT
52     sptr<ICollectPlugin> eventStatuscollect = new CommonEventCollect(this);
53     eventStatuscollect->Init(saProfiles);
54     collectPluginMap_[COMMON_EVENT] = eventStatuscollect;
55 #endif
56 #ifdef SUPPORT_SWITCH_COLLECT
57     sptr<ICollectPlugin> deviceSwitchCollect = new DeviceSwitchCollect(this);
58     deviceSwitchCollect->Init(saProfiles);
59     collectPluginMap_[SETTING_SWITCH] = deviceSwitchCollect;
60 #endif
61     sptr<ICollectPlugin> timedCollect = new DeviceTimedCollect(this);
62     timedCollect->Init(saProfiles);
63     collectPluginMap_[TIMED_EVENT] = timedCollect;
64     sptr<ICollectPlugin> refCountCollect = new RefCountCollect(this);
65     refCountCollect->Init(saProfiles);
66     collectPluginMap_[UNREF_EVENT] = refCountCollect;
67 
68     StartCollect();
69     HILOGI("DeviceStaMgr Init end");
70 }
71 
RemoveWhiteCommonEvent()72 void DeviceStatusCollectManager::RemoveWhiteCommonEvent()
73 {
74     if (IsExistInPluginMap(COMMON_EVENT) == ERR_OK) {
75         collectPluginMap_[COMMON_EVENT]->RemoveWhiteCommonEvent();
76     }
77 }
78 
FilterOnDemandSaProfiles(const std::list<SaProfile> & saProfiles)79 void DeviceStatusCollectManager::FilterOnDemandSaProfiles(const std::list<SaProfile>& saProfiles)
80 {
81     std::unique_lock<samgr::shared_mutex> writeLock(saProfilesLock_);
82     for (auto& saProfile : saProfiles) {
83         if (saProfile.startOnDemand.onDemandEvents.empty() && saProfile.stopOnDemand.onDemandEvents.empty()) {
84             continue;
85         }
86         CollMgrSaProfile collMgrSaProfile;
87         collMgrSaProfile.saId = saProfile.saId;
88         collMgrSaProfile.startOnDemandEvents.assign(saProfile.startOnDemand.onDemandEvents.begin(),
89             saProfile.startOnDemand.onDemandEvents.end());
90         collMgrSaProfile.stopOnDemandEvents.assign(saProfile.stopOnDemand.onDemandEvents.begin(),
91             saProfile.stopOnDemand.onDemandEvents.end());
92         collMgrSaProfile.cacheCommonEvent = saProfile.cacheCommonEvent;
93         onDemandSaProfiles_.push_back(collMgrSaProfile);
94     }
95 }
96 
GetSaControlListByPersistEvent(const OnDemandEvent & event,std::list<SaControlInfo> & saControlList)97 void DeviceStatusCollectManager::GetSaControlListByPersistEvent(const OnDemandEvent& event,
98     std::list<SaControlInfo>& saControlList)
99 {
100 #ifdef PREFERENCES_ENABLE
101     std::shared_ptr<PreferencesUtil> preferencesUtil = PreferencesUtil::GetInstance();
102     if (preferencesUtil == nullptr) {
103         HILOGW("GetSaControlListByPersistEvent preferencesUtil is nullptr");
104         return;
105     }
106     std::string value = preferencesUtil->ObtainString(event.ToString(), std::string());
107     if (value == std::string()) {
108         return;
109     }
110     std::vector<std::string> strVector;
111     SplitStr(value, "+", strVector);
112     size_t vectorSize = strVector.size();
113     for (size_t i = 0; i < vectorSize; i++) {
114         OnDemandPolicyType type = OnDemandPolicyType::START_POLICY;
115         int32_t systemAbilityId = -1;
116         HILOGD("vector is : %{public}s", strVector[i].c_str());
117         StringToTypeAndSaid(strVector[i], type, systemAbilityId);
118         SaControlInfo control;
119         if (type == OnDemandPolicyType::START_POLICY) {
120             control = {START_ON_DEMAND, systemAbilityId };
121         } else {
122             control = {STOP_ON_DEMAND, systemAbilityId };
123         }
124         saControlList.emplace_back(control);
125     }
126 #endif
127 }
128 
GetSaControlListByEvent(const OnDemandEvent & event,std::list<SaControlInfo> & saControlList)129 void DeviceStatusCollectManager::GetSaControlListByEvent(const OnDemandEvent& event,
130     std::list<SaControlInfo>& saControlList)
131 {
132     std::shared_lock<samgr::shared_mutex> readLock(saProfilesLock_);
133     for (auto& profile : onDemandSaProfiles_) {
134         // start on demand
135         for (auto iterStart = profile.startOnDemandEvents.begin();
136             iterStart != profile.startOnDemandEvents.end(); iterStart++) {
137             if (IsSameEvent(event, *iterStart) && CheckConditions(*iterStart) &&
138                 CheckExtraMessages(event, *iterStart)) {
139                 // maybe the process is being killed, let samgr make decisions.
140                 SaControlInfo control = { START_ON_DEMAND, profile.saId, iterStart->enableOnce,
141                     iterStart->loadPriority, profile.cacheCommonEvent };
142                 saControlList.emplace_back(control);
143                 break;
144             }
145         }
146         // stop on demand
147         for (auto iterStop = profile.stopOnDemandEvents.begin();
148             iterStop != profile.stopOnDemandEvents.end(); iterStop++) {
149             if (IsSameEvent(event, *iterStop) && CheckConditions(*iterStop) &&
150                 CheckExtraMessages(event, *iterStop)) {
151                 // maybe the process is starting, let samgr make decisions.
152                 SaControlInfo control = { STOP_ON_DEMAND, profile.saId, iterStop->enableOnce,
153                     iterStop->loadPriority, profile.cacheCommonEvent };
154                 saControlList.emplace_back(control);
155                 break;
156             }
157         }
158     }
159     HILOGD("DeviceStaMgr saControlList size %{public}zu", saControlList.size());
160 }
161 
SortSaControlListByLoadPriority(std::list<SaControlInfo> & saControlList)162 void DeviceStatusCollectManager::SortSaControlListByLoadPriority(std::list<SaControlInfo>& saControlList)
163 {
164     saControlList.sort([](const SaControlInfo& control1, const SaControlInfo& control2) {
165         return control1.loadPriority < control2.loadPriority;
166     });
167 }
168 
IsSameEvent(const OnDemandEvent & ev1,const OnDemandEvent & ev2)169 bool DeviceStatusCollectManager::IsSameEvent(const OnDemandEvent& ev1, const OnDemandEvent& ev2)
170 {
171     return (ev1.eventId == ev2.eventId && ev1.name == ev2.name &&
172         ev1.persistence == ev2.persistence && (ev1.value == ev2.value || "" == ev2.value));
173 }
174 
IsSameEventName(const OnDemandEvent & ev1,const OnDemandEvent & ev2)175 bool DeviceStatusCollectManager::IsSameEventName(const OnDemandEvent& ev1, const OnDemandEvent& ev2)
176 {
177     if (ev1.eventId != TIMED_EVENT) {
178         if (ev1.eventId == ev2.eventId && ev1.name == ev2.name) {
179             return true;
180         }
181     } else {
182         if (ev1.eventId == ev2.eventId && ev1.name == ev2.name && ev1.value == ev2.value &&
183             ev1.persistence == ev2.persistence) {
184             return true;
185         }
186     }
187     return false;
188 }
189 
CheckConditions(const OnDemandEvent & onDemandEvent)190 bool DeviceStatusCollectManager::CheckConditions(const OnDemandEvent& onDemandEvent)
191 {
192     if (onDemandEvent.conditions.empty()) {
193         return true;
194     }
195     for (auto& condition : onDemandEvent.conditions) {
196         if (collectPluginMap_.count(condition.eventId) == 0) {
197             HILOGE("not support condition: %{public}d", condition.eventId);
198             return false;
199         }
200         if (collectPluginMap_[condition.eventId] == nullptr) {
201             HILOGE("not support condition: %{public}d", condition.eventId);
202             return false;
203         }
204         bool ret = collectPluginMap_[condition.eventId]->CheckCondition(condition);
205         if (!ret) {
206             HILOGW("CheckCondition:%{public}s, value:%{public}s not pass",
207                 condition.name.c_str(), condition.value.c_str());
208             return false;
209         }
210     }
211     return true;
212 }
213 
CheckExtraMessages(const OnDemandEvent & ev1,const OnDemandEvent & ev2)214 bool DeviceStatusCollectManager::CheckExtraMessages(const OnDemandEvent& ev1, const OnDemandEvent& ev2)
215 {
216     HILOGD("CheckExtraMessages begin evt1:%{public}d, evt2:%{public}d", ev1.eventId, ev2.eventId);
217     if (collectPluginMap_.count(ev1.eventId) == 0) {
218         HILOGE("not support CheckExtraMessages");
219         return false;
220     }
221     if (collectPluginMap_[ev1.eventId] == nullptr) {
222         HILOGE("CommonEventCollect is nullptr");
223         return false;
224     }
225     if (collectPluginMap_[ev1.eventId]->CheckExtraMessage(ev1.extraDataId, ev2)) {
226         return true;
227     }
228     return false;
229 }
230 
UnInit()231 void DeviceStatusCollectManager::UnInit()
232 {
233     for (auto& iter : collectPluginMap_) {
234         if (iter.second != nullptr) {
235             iter.second->OnStop();
236         }
237     }
238     collectPluginMap_.clear();
239 
240     if (collectHandler_ != nullptr) {
241         collectHandler_ = nullptr;
242     }
243 }
244 
CleanFfrt()245 void DeviceStatusCollectManager::CleanFfrt()
246 {
247     for (auto& iter : collectPluginMap_) {
248         if ((iter.first == DEVICE_ONLINE || iter.first == COMMON_EVENT) && (iter.second != nullptr)) {
249             iter.second->CleanFfrt();
250         }
251     }
252     if (collectHandler_ != nullptr) {
253         collectHandler_->CleanFfrt();
254     }
255 }
256 
SetFfrt()257 void DeviceStatusCollectManager::SetFfrt()
258 {
259     for (auto& iter : collectPluginMap_) {
260         if ((iter.first == DEVICE_ONLINE || iter.first == COMMON_EVENT) && (iter.second != nullptr)) {
261             iter.second->SetFfrt();
262         }
263     }
264     if (collectHandler_ != nullptr) {
265         collectHandler_->SetFfrt("collect");
266     }
267 }
268 
StartCollect()269 void DeviceStatusCollectManager::StartCollect()
270 {
271     HILOGI("DeviceStaMgr OnStart begin");
272     if (collectHandler_ == nullptr) {
273         return;
274     }
275     auto callback = [this] () {
276         for (auto& iter : collectPluginMap_) {
277             iter.second->OnStart();
278         }
279     };
280     collectHandler_->PostTask(callback);
281 }
282 
ReportEvent(const OnDemandEvent & event)283 void DeviceStatusCollectManager::ReportEvent(const OnDemandEvent& event)
284 {
285     if (collectHandler_ == nullptr) {
286         HILOGW("DeviceStaMgr collectHandler_ is nullptr");
287         return;
288     }
289     auto callback = [event, this] () {
290         std::list<SaControlInfo> saControlList;
291         GetSaControlListByEvent(event, saControlList);
292         GetSaControlListByPersistEvent(event, saControlList);
293         SortSaControlListByLoadPriority(saControlList);
294         if (saControlList.empty()) {
295             HILOGD("DeviceStaMgr no matched event");
296             if (event.eventId == DEVICE_ONLINE) {
297                 HILOGI("deviceOnline is empty");
298             }
299             return;
300         }
301         SystemAbilityManager::GetInstance()->ProcessOnDemandEvent(event, saControlList);
302     };
303     collectHandler_->PostTask(callback);
304 }
305 
PostTask(std::function<void ()> callback)306 void DeviceStatusCollectManager::PostTask(std::function<void()> callback)
307 {
308     HILOGI("DeviceStaMgr PostTask begin");
309     collectHandler_->PostTask(callback);
310 }
311 
PostDelayTask(std::function<void ()> callback,int32_t delayTime)312 void DeviceStatusCollectManager::PostDelayTask(std::function<void()> callback, int32_t delayTime)
313 {
314     HILOGI("DeviceStaMgr PostDelayTask begin, delayTime:%{public}d", delayTime);
315     if (delayTime < 0 || delayTime > std::numeric_limits<int32_t>::max() / TO_MILLISECOND) {
316         HILOGE("DeviceStaMgr PostDelayTask Failed : delayTime out of range %{public}d", delayTime);
317         return;
318     }
319     collectHandler_->PostTask(callback, delayTime * TO_MILLISECOND);
320 }
321 
IsExistInPluginMap(int32_t eventId)322 int32_t DeviceStatusCollectManager::IsExistInPluginMap(int32_t eventId)
323 {
324     if (collectPluginMap_.count(eventId) == 0) {
325         HILOGE("eventid:%{public}d collect not exist", eventId);
326         return ERR_INVALID_VALUE;
327     }
328     if (collectPluginMap_[eventId] == nullptr) {
329         HILOGE("eventid:%{public}d collect is null", eventId);
330         return ERR_INVALID_VALUE;
331     }
332     return ERR_OK;
333 }
334 
GetOnDemandReasonExtraData(int64_t extraDataId,OnDemandReasonExtraData & extraData)335 int32_t DeviceStatusCollectManager::GetOnDemandReasonExtraData(int64_t extraDataId, OnDemandReasonExtraData& extraData)
336 {
337     HILOGD("DeviceStaMgr GetOnDemandReasonExtraData begin, extraDataId:%{public}d",
338         static_cast<int32_t>(extraDataId));
339     if (IsExistInPluginMap(COMMON_EVENT) != ERR_OK) {
340         HILOGE("not support get extra data");
341         return ERR_INVALID_VALUE;
342     }
343     if (!collectPluginMap_[COMMON_EVENT]->GetOnDemandReasonExtraData(extraDataId, extraData)) {
344         HILOGE("get extra data failed");
345         return ERR_INVALID_VALUE;
346     }
347     return ERR_OK;
348 }
349 
SaveSaExtraDataId(int32_t saId,int64_t extraDataId)350 void DeviceStatusCollectManager::SaveSaExtraDataId(int32_t saId, int64_t extraDataId)
351 {
352     HILOGD("DeviceStaMgr SaveSaExtraDataId begin, SA:%{public}d, extraDataId:%{public}d",
353         saId, static_cast<int32_t>(extraDataId));
354     if (IsExistInPluginMap(COMMON_EVENT) != ERR_OK) {
355         HILOGE("CommonEventCollect is nullptr");
356         return;
357     }
358     collectPluginMap_[COMMON_EVENT]->SaveSaExtraDataId(saId, extraDataId);
359     return;
360 }
361 
ClearSaExtraDataId(int32_t saId)362 void DeviceStatusCollectManager::ClearSaExtraDataId(int32_t saId)
363 {
364     HILOGD("DeviceStaMgr ClearSaExtraDataId begin, SA:%{public}d", saId);
365     if (IsExistInPluginMap(COMMON_EVENT) != ERR_OK) {
366         HILOGE("CommonEventCollect is nullptr");
367         return;
368     }
369     collectPluginMap_[COMMON_EVENT]->ClearSaExtraDataId(saId);
370     return;
371 }
372 
SaveCacheCommonEventSaExtraId(const OnDemandEvent & event,const std::list<SaControlInfo> & saControlList)373 void DeviceStatusCollectManager::SaveCacheCommonEventSaExtraId(const OnDemandEvent& event,
374     const std::list<SaControlInfo>& saControlList)
375 {
376     HILOGD("DeviceStaMgr SaveCacheCommonEventSaExtraId begin");
377     if (IsExistInPluginMap(COMMON_EVENT) != ERR_OK) {
378         HILOGE("CommonEventCollect is nullptr");
379         return;
380     }
381     collectPluginMap_[COMMON_EVENT]->SaveCacheCommonEventSaExtraId(event, saControlList);
382     return;
383 }
384 
GetSaExtraDataIdList(int32_t saId,std::vector<int64_t> & extraDataIdList,const std::string & eventName)385 int32_t DeviceStatusCollectManager::GetSaExtraDataIdList(int32_t saId, std::vector<int64_t>& extraDataIdList,
386     const std::string& eventName)
387 {
388     HILOGD("DeviceStaMgr GetSaExtraDataIdList begin, SA:%{public}d, event:%{public}s",
389         saId, eventName.c_str());
390     if (IsExistInPluginMap(COMMON_EVENT) != ERR_OK) {
391         HILOGE("CommonEventCollect is nullptr");
392         return ERR_INVALID_VALUE;
393     }
394     return collectPluginMap_[COMMON_EVENT]->GetSaExtraDataIdList(saId, extraDataIdList, eventName);
395 }
396 
AddCollectEvents(const std::vector<OnDemandEvent> & events)397 int32_t DeviceStatusCollectManager::AddCollectEvents(const std::vector<OnDemandEvent>& events)
398 {
399     if (events.size() == 0) {
400         return ERR_OK;
401     }
402     std::map<int32_t, std::vector<OnDemandEvent>> eventMap;
403     for (auto& event : events) {
404         if (collectPluginMap_.count(event.eventId) == 0) {
405             HILOGE("not support eventId: %{public}d", event.eventId);
406             return ERR_INVALID_VALUE;
407         }
408         if (collectPluginMap_[event.eventId] == nullptr) {
409             HILOGE("not support eventId: %{public}d", event.eventId);
410             return ERR_INVALID_VALUE;
411         }
412         eventMap[event.eventId].emplace_back(event);
413     }
414     for (const auto& iter : eventMap) {
415         int32_t ret = collectPluginMap_[iter.first]->AddCollectEvent(iter.second);
416         if (ret != ERR_OK) {
417             HILOGE("add collect event failed, eventId: %{public}d", iter.first);
418             return ret;
419         }
420     }
421     return ERR_OK;
422 }
423 
GetOnDemandEvents(int32_t systemAbilityId,OnDemandPolicyType type,std::vector<OnDemandEvent> & events)424 int32_t DeviceStatusCollectManager::GetOnDemandEvents(int32_t systemAbilityId, OnDemandPolicyType type,
425     std::vector<OnDemandEvent>& events)
426 {
427     HILOGI("GetOnDemandEvents begin");
428     std::shared_lock<samgr::shared_mutex> readLock(saProfilesLock_);
429     auto iter = std::find_if(onDemandSaProfiles_.begin(), onDemandSaProfiles_.end(), [systemAbilityId](auto saProfile) {
430         return saProfile.saId == systemAbilityId;
431     });
432     if (iter == onDemandSaProfiles_.end()) {
433         HILOGI("GetOnDemandEvents invalid saId:%{public}d", systemAbilityId);
434         return ERR_INVALID_VALUE;
435     }
436     if (type == OnDemandPolicyType::START_POLICY) {
437         events = (*iter).startOnDemandEvents;
438     } else if (type == OnDemandPolicyType::STOP_POLICY) {
439         events = (*iter).stopOnDemandEvents;
440     } else {
441         HILOGE("GetOnDemandEvents invalid policy types");
442         return ERR_INVALID_VALUE;
443     }
444     return ERR_OK;
445 }
446 
RemoveUnusedEventsLocked(const std::vector<OnDemandEvent> & events)447 int32_t DeviceStatusCollectManager::RemoveUnusedEventsLocked(const std::vector<OnDemandEvent>& events)
448 {
449     HILOGD("RemoveUnusedEventsLocked start");
450     if (events.size() == 0) {
451         return ERR_OK;
452     }
453     for (auto& event : events) {
454         if (collectPluginMap_.count(event.eventId) == 0) {
455             HILOGE("not support eventId: %{public}d", event.eventId);
456             continue;
457         }
458         if (collectPluginMap_[event.eventId] == nullptr) {
459             HILOGE("not support eventId: %{public}d", event.eventId);
460             continue;
461         }
462         bool eventUsed = CheckEventUsedLocked(event);
463         if (!eventUsed) {
464             HILOGI("CheckEventUsedLocked name:%{public}s,value:%{public}s", event.name.c_str(), event.value.c_str());
465             int32_t ret = collectPluginMap_[event.eventId]->RemoveUnusedEvent(event);
466             if (ret != ERR_OK) {
467                 HILOGE("Remove event failed, eventId: %{public}d", event.eventId);
468             }
469         }
470     }
471     return ERR_OK;
472 }
473 
CheckEventUsedLocked(const OnDemandEvent & event)474 bool DeviceStatusCollectManager::CheckEventUsedLocked(const OnDemandEvent& event)
475 {
476     for (auto& profile : onDemandSaProfiles_) {
477         // start on demand
478         for (auto iterStart = profile.startOnDemandEvents.begin();
479             iterStart != profile.startOnDemandEvents.end(); iterStart++) {
480             if (IsSameEventName(event, *iterStart)) {
481                 return true;
482             }
483         }
484         // stop on demand
485         for (auto iterStop = profile.stopOnDemandEvents.begin();
486             iterStop != profile.stopOnDemandEvents.end(); iterStop++) {
487             if (IsSameEventName(event, *iterStop)) {
488                 return true;
489             }
490         }
491     }
492     return false;
493 }
494 
NeedPersistOnDemandEvent(const OnDemandEvent & event)495 bool DeviceStatusCollectManager::NeedPersistOnDemandEvent(const OnDemandEvent& event)
496 {
497     if (event.eventId == TIMED_EVENT && event.name == "timedevent" && event.persistence) {
498         return true;
499     }
500     return false;
501 }
502 
PersistOnDemandEvent(int32_t systemAbilityId,OnDemandPolicyType type,const std::vector<OnDemandEvent> & events)503 void DeviceStatusCollectManager::PersistOnDemandEvent(int32_t systemAbilityId, OnDemandPolicyType type,
504     const std::vector<OnDemandEvent>& events)
505 {
506 #ifdef PREFERENCES_ENABLE
507     std::shared_ptr<PreferencesUtil> preferencesUtil = PreferencesUtil::GetInstance();
508     if (preferencesUtil == nullptr) {
509         return;
510     }
511     for (OnDemandEvent event : events) {
512         if (!NeedPersistOnDemandEvent(event)) {
513             continue;
514         }
515         std::string strEvent = event.ToString();
516         std::string strTypeAndSaid = TypeAndSaidToString(type, systemAbilityId);
517         if (preferencesUtil->IsExist(strEvent)) {
518             std::string orgStrTypeAndSaid = preferencesUtil->ObtainString(strEvent, "");
519             orgStrTypeAndSaid += "+";
520             orgStrTypeAndSaid += strTypeAndSaid;
521             HILOGI("PersistOnDemandEvent Save orgStrTypeAndSaid is : %{public}s", orgStrTypeAndSaid.c_str());
522             preferencesUtil->SaveString(strEvent, orgStrTypeAndSaid);
523         } else {
524             preferencesUtil->SaveString(strEvent, strTypeAndSaid);
525             HILOGI("PersistOnDemandEvent Save strTypeAndSaid is : %{public}s", strTypeAndSaid.c_str());
526         }
527     }
528 #endif
529 }
530 
TypeAndSaidToString(OnDemandPolicyType type,int32_t systemAbilityId)531 std::string DeviceStatusCollectManager::TypeAndSaidToString(OnDemandPolicyType type, int32_t systemAbilityId)
532 {
533     std::string strSaid = std::to_string(systemAbilityId);
534     if (type == OnDemandPolicyType::START_POLICY) {
535         return "start#" + strSaid + "#";
536     } else if (type == OnDemandPolicyType::STOP_POLICY) {
537         return "stop#" + strSaid + "#";
538     }
539     return "";
540 }
541 
StringToTypeAndSaid(const std::string & eventStr,OnDemandPolicyType & type,int32_t & systemAbilityId)542 void DeviceStatusCollectManager::StringToTypeAndSaid(const std::string& eventStr, OnDemandPolicyType& type,
543     int32_t& systemAbilityId)
544 {
545     std::size_t pos = eventStr.find("#");
546     std::string strType = eventStr.substr(0, pos);
547     if (strType == "start") {
548         type = OnDemandPolicyType::START_POLICY;
549     } else if (strType == "stop") {
550         type = OnDemandPolicyType::STOP_POLICY;
551     } else {
552         HILOGW("StringToTypeAndSaid failed");
553         return;
554     }
555     if (pos == std::string::npos) {
556         HILOGW("StringToSaid failed");
557         return;
558     }
559     systemAbilityId = atoi((eventStr.substr(pos + 1, eventStr.size() - pos - 1)).c_str());
560     HILOGD("systemAbilityId is : %{public}d", systemAbilityId);
561 }
562 
UpdateOnDemandEvents(int32_t systemAbilityId,OnDemandPolicyType type,const std::vector<OnDemandEvent> & events)563 int32_t DeviceStatusCollectManager::UpdateOnDemandEvents(int32_t systemAbilityId, OnDemandPolicyType type,
564     const std::vector<OnDemandEvent>& events)
565 {
566     HILOGI("UpdateOnDemandEvents begin saId:%{public}d, type:%{public}d", systemAbilityId, type);
567     std::vector<OnDemandEvent> oldEvents;
568     std::unique_lock<samgr::shared_mutex> writeLock(saProfilesLock_);
569     auto iter = std::find_if(onDemandSaProfiles_.begin(), onDemandSaProfiles_.end(),
570     [systemAbilityId](auto saProfile) {
571         return saProfile.saId == systemAbilityId;
572     });
573     if (iter == onDemandSaProfiles_.end()) {
574         HILOGI("UpdateOnDemandEvents invalid saId:%{public}d", systemAbilityId);
575         return ERR_INVALID_VALUE;
576     }
577     if (AddCollectEvents(events) != ERR_OK) {
578         HILOGI("AddCollectEvents failed saId:%{public}d", systemAbilityId);
579         return ERR_INVALID_VALUE;
580     }
581     if (type == OnDemandPolicyType::START_POLICY) {
582         oldEvents = (*iter).startOnDemandEvents;
583         (*iter).startOnDemandEvents = events;
584     } else if (type == OnDemandPolicyType::STOP_POLICY) {
585         oldEvents = (*iter).stopOnDemandEvents;
586         (*iter).stopOnDemandEvents = events;
587     } else {
588         HILOGE("UpdateOnDemandEvents policy types");
589         return ERR_INVALID_VALUE;
590     }
591     PersistOnDemandEvent(systemAbilityId, type, events);
592     if (RemoveUnusedEventsLocked(oldEvents) != ERR_OK) {
593         HILOGE("RemoveUnusedEventsLocked failed saId:%{public}d", systemAbilityId);
594     }
595     return ERR_OK;
596 }
597 
GetLowMemPrepareList()598 const std::vector<int32_t>& DeviceStatusCollectManager::GetLowMemPrepareList()
599 {
600     return collectPluginMap_[PARAM]->GetLowMemPrepareList();
601 }
602 }  // namespace OHOS
603