• 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 "devicestatus_manager.h"
17 
18 #include "image_packer.h"
19 #include "iservice_registry.h"
20 #include "os_account_manager.h"
21 #ifdef BOOMERANG_ONESTEP
22 #include "wm_common.h"
23 #endif
24 #ifdef BOOMERANG_SUPPORT_HDR
25 #include "vpe_utils.h"
26 #endif // BOOMERANG_SUPPORT_HDR
27 
28 #ifdef BOOMERANG_ONESTEP
29 #include "accessibility_manager.h"
30 #endif
31 #include "devicestatus_define.h"
32 #include "devicestatus_napi_manager.h"
33 #include "fi_log.h"
34 
35 #undef LOG_TAG
36 #define LOG_TAG "DeviceStatusManager"
37 
38 namespace OHOS {
39 namespace Msdp {
40 namespace DeviceStatus {
41 namespace {
42 #ifdef BOOMERANG_ONESTEP
43     const std::string BUNDLE_NAME = "";
44     const int32_t SYSTEM_BAR_HIDDEN = 0;
45     const int32_t PAGE_SCROLL_ENVENT = 1;
46 #endif
47     const int32_t SLEEP_TIME = 150;
48 }
49 #ifdef BOOMERANG_ONESTEP
50 std::shared_ptr<DeviceStatusManager> DeviceStatusManager::g_deviceManager_;
51 #endif
52 std::mutex DeviceStatusManager::g_mutex_;
53 
OnRemoteDied(const wptr<IRemoteObject> & remote)54 void DeviceStatusManager::DeviceStatusCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
55 {
56     CHKPV(remote);
57     FI_HILOGI("Recv death notice");
58 }
59 
OnRemoteDied(const wptr<IRemoteObject> & remote)60 void DeviceStatusManager::BoomerangCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
61 {
62     CHKPV(remote);
63     FI_HILOGI("Recv death notice");
64     if (manager_ == nullptr) {
65         return;
66     }
67     std::lock_guard lock(manager_->mutex_);
68     for (auto& entry : manager_->boomerangListeners_) {
69         auto& callbacks = entry.second;
70         auto it = callbacks.begin();
71         if (it == callbacks.end()) {
72             FI_HILOGI("this app is not Subscribe");
73         }
74         while (it != callbacks.end()) {
75             auto callback_remote = (*it)->AsObject();
76             if (callback_remote && callback_remote == remote) {
77                 it = callbacks.erase(it);
78             } else {
79                 ++it;
80             }
81         }
82     }
83     if (manager_->notityListener_ != nullptr && remote == manager_->notityListener_->AsObject()) {
84         FI_HILOGI("the screenshot app has died");
85         manager_->notityListener_ = nullptr;
86     }
87 }
88 
89 #ifdef BOOMERANG_ONESTEP
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)90 void DeviceStatusManager::AccessibilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId,
91     const std::string &deviceId)
92 {
93     std::lock_guard<std::mutex> guard(g_mutex_);
94     switch (systemAbilityId) {
95         case WINDOW_MANAGER_SERVICE_ID: {
96             g_deviceManager_ = std::make_shared<DeviceStatusManager>();
97             sptr<IWindowSystemBarPropertyChangedListener> systemBarListener =
98                 sptr<SystemBarStyleChangedListener>::MakeSptr();
99             CHKPV(systemBarListener);
100             Rosen::WMError ret =
101                 WindowManager::GetInstance().RegisterWindowSystemBarPropertyChangedListener(systemBarListener);
102             if (ret != Rosen::WMError::WM_OK) {
103                 FI_HILOGI("Register Window SystemBarStyle Change faild");
104             }
105             break;
106         }
107         default: {
108             FI_HILOGW("Service ID that does not need to be processed");
109             break;
110         }
111     }
112 }
113 #endif
114 
115 #ifdef BOOMERANG_ONESTEP
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)116 void DeviceStatusManager::AccessibilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId,
117     const std::string &deviceId)
118 {
119     std::lock_guard<std::mutex> guard(g_mutex_);
120     CHKPV(g_deviceManager_);
121     if (systemAbilityId == WINDOW_MANAGER_SERVICE_ID) {
122         CHKPV(g_deviceManager_);
123         g_deviceManager_->lastEnable_ = true;
124     }
125 }
126 #endif
127 
128 #ifdef BOOMERANG_ONESTEP
OnWindowSystemBarPropertyChanged(WindowType type,const SystemBarProperty & systemBarProperty)129 void DeviceStatusManager::SystemBarStyleChangedListener::OnWindowSystemBarPropertyChanged(WindowType type,
130     const SystemBarProperty& systemBarProperty)
131 {
132     std::lock_guard<std::mutex> guard(g_mutex_);
133     FI_HILOGI("OnWindowSystemBarPropertyChanged status:%{public}d", systemBarProperty.enable_);
134     CHKPV(g_deviceManager_);
135     if (type == WindowType::WINDOW_TYPE_STATUS_BAR && systemBarProperty.enable_ != g_deviceManager_->lastEnable_) {
136         g_deviceManager_->lastEnable_ = systemBarProperty.enable_;
137     }
138     if (g_deviceManager_->lastEnable_) {
139         if (g_deviceManager_->isAccessibilityInit_) {
140             ACCESSIBILITY_MANAGER.AccessibilityDisconnect();
141         }
142     } else {
143         g_deviceManager_->HandlerPageScrollerEvent(SYSTEM_BAR_HIDDEN);
144     }
145 }
146 #endif
147 
Init()148 bool DeviceStatusManager::Init()
149 {
150     CALL_DEBUG_ENTER;
151     if (devicestatusCBDeathRecipient_ == nullptr) {
152         devicestatusCBDeathRecipient_ = new (std::nothrow) DeviceStatusCallbackDeathRecipient();
153         if (devicestatusCBDeathRecipient_ == nullptr) {
154             FI_HILOGE("devicestatusCBDeathRecipient_ failed");
155             return false;
156         }
157     }
158 
159     if (boomerangCBDeathRecipient_ == nullptr) {
160         boomerangCBDeathRecipient_ = new (std::nothrow) BoomerangCallbackDeathRecipient(this);
161         if (boomerangCBDeathRecipient_ == nullptr) {
162             FI_HILOGE("boomerangCBDeathRecipient_ failed");
163             return false;
164         }
165     }
166 
167     msdpImpl_ = std::make_shared<DeviceStatusMsdpClientImpl>();
168     CHKPF(msdpImpl_);
169 
170 #ifdef BOOMERANG_ONESTEP
171     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
172     CHKPF(samgrProxy);
173 
174     auto windowStatusChange = sptr<AccessibilityStatusChange>::MakeSptr();
175     int32_t ret = samgrProxy->SubscribeSystemAbility(WINDOW_MANAGER_SERVICE_ID, windowStatusChange);
176     if (ret != RET_OK) {
177         FI_HILOGE("SubscribeSystemAbility service faild on window manager");
178         return false;
179     }
180 #endif
181     FI_HILOGD("Init success");
182     return true;
183 }
184 
GetLatestDeviceStatusData(Type type)185 Data DeviceStatusManager::GetLatestDeviceStatusData(Type type)
186 {
187     CALL_DEBUG_ENTER;
188     Data data = {type, OnChangedValue::VALUE_EXIT};
189     if ((type <= TYPE_INVALID) || (type >= TYPE_MAX)) {
190         FI_HILOGE("GetLatestDeviceStatusData type_:%{public}d is error", type);
191         return data;
192     }
193     if (msdpImpl_ == nullptr) {
194         FI_HILOGE("msdpImpl_ is nullptr");
195         data.value = OnChangedValue::VALUE_INVALID;
196         return data;
197     }
198     msdpData_ = msdpImpl_->GetObserverData();
199     for (auto iter = msdpData_.begin(); iter != msdpData_.end(); ++iter) {
200         if (data.type == iter->first) {
201             data.value = iter->second;
202             return data;
203         }
204     }
205     return {type, OnChangedValue::VALUE_INVALID};
206 }
207 
Enable(Type type)208 bool DeviceStatusManager::Enable(Type type)
209 {
210     CALL_DEBUG_ENTER;
211     if ((type <= TYPE_INVALID) || (type >= TYPE_MAX)) {
212         FI_HILOGE("Check type is invalid");
213         return false;
214     }
215     InitAlgoMngrInterface(type);
216     InitDataCallback();
217     return true;
218 }
219 
Disable(Type type)220 bool DeviceStatusManager::Disable(Type type)
221 {
222     CALL_DEBUG_ENTER;
223     CHKPF(msdpImpl_);
224 
225     if (msdpImpl_->Disable(type) != RET_OK) {
226         FI_HILOGE("Disable msdp impl failed");
227         return false;
228     }
229 
230     return true;
231 }
232 
InitAlgoMngrInterface(Type type)233 bool DeviceStatusManager::InitAlgoMngrInterface(Type type)
234 {
235     CALL_DEBUG_ENTER;
236     CHKPF(msdpImpl_);
237 
238     if (msdpImpl_->InitMsdpImpl(type) != RET_OK) {
239         FI_HILOGE("Init msdp impl failed");
240         return false;
241     };
242     return true;
243 }
244 
InitDataCallback()245 int32_t DeviceStatusManager::InitDataCallback()
246 {
247     CALL_DEBUG_ENTER;
248     CHKPF(msdpImpl_);
249     DeviceStatusMsdpClientImpl::CallbackManager callback = [this](const Data &data) {
250         return this->MsdpDataCallback(data);
251     };
252     if (msdpImpl_->RegisterImpl(callback) == RET_ERR) {
253         FI_HILOGE("Register impl failed");
254     }
255     return true;
256 }
257 
MsdpDataCallback(const Data & data)258 int32_t DeviceStatusManager::MsdpDataCallback(const Data &data)
259 {
260     NotifyDeviceStatusChange(data);
261     return RET_OK;
262 }
263 
NotifyDeviceStatusChange(const Data & devicestatusData)264 int32_t DeviceStatusManager::NotifyDeviceStatusChange(const Data &devicestatusData)
265 {
266     CALL_DEBUG_ENTER;
267     FI_HILOGI("type:%{public}d, value:%{public}d", devicestatusData.type, devicestatusData.value);
268     std::set<const sptr<IRemoteDevStaCallback>, classcomp> listeners;
269     std::lock_guard lock(mutex_);
270     auto iter = listeners_.find(devicestatusData.type);
271     if (iter == listeners_.end()) {
272         FI_HILOGE("type:%{public}d is not exits", devicestatusData.type);
273         return false;
274     }
275     if ((devicestatusData.type <= TYPE_INVALID) || (devicestatusData.type >= TYPE_MAX)) {
276         FI_HILOGE("Check devicestatusData.type is invalid");
277         return false;
278     }
279     listeners = (std::set<const sptr<IRemoteDevStaCallback>, classcomp>)(iter->second);
280     for (const auto &listener : listeners) {
281         if (listener == nullptr) {
282             FI_HILOGE("listener is nullptr");
283             return false;
284         }
285         FI_HILOGI("type:%{public}d, arrs_:%{public}d", devicestatusData.type, arrs_[devicestatusData.type]);
286         switch (arrs_[devicestatusData.type]) {
287             case ENTER: {
288                 if (devicestatusData.value == VALUE_ENTER) {
289                     listener->OnDeviceStatusChanged(devicestatusData);
290                 }
291                 break;
292             }
293             case EXIT: {
294                 if (devicestatusData.value == VALUE_EXIT) {
295                     listener->OnDeviceStatusChanged(devicestatusData);
296                 }
297                 break;
298             }
299             case ENTER_EXIT: {
300                 listener->OnDeviceStatusChanged(devicestatusData);
301                 break;
302             }
303             default: {
304                 FI_HILOGE("OnChangedValue is unknown");
305                 break;
306             }
307         }
308     }
309     return RET_OK;
310 }
311 
Subscribe(Type type,ActivityEvent event,ReportLatencyNs latency,sptr<IRemoteDevStaCallback> callback)312 void DeviceStatusManager::Subscribe(Type type, ActivityEvent event, ReportLatencyNs latency,
313     sptr<IRemoteDevStaCallback> callback)
314 {
315     CALL_DEBUG_ENTER;
316     CHKPV(callback);
317     if ((type <= TYPE_INVALID) || (type >= TYPE_MAX)) {
318         FI_HILOGE("Subscribe type_:%{public}d is error", type_);
319         return;
320     }
321     if ((event < ENTER) || (event > ENTER_EXIT)) {
322         FI_HILOGE("Subscribe event_:%{public}d is error", event_);
323         return;
324     }
325     event_ = event;
326     type_ = type;
327     std::lock_guard lock(mutex_);
328     arrs_ [type_] = event_;
329     FI_HILOGI("type_:%{public}d, event:%{public}d", type_, event);
330     std::set<const sptr<IRemoteDevStaCallback>, classcomp> listeners;
331     auto object = callback->AsObject();
332     CHKPV(object);
333     FI_HILOGI("listeners_.size:%{public}zu", listeners_.size());
334     auto dtTypeIter = listeners_.find(type);
335     if (dtTypeIter == listeners_.end()) {
336         if (listeners.insert(callback).second) {
337             FI_HILOGI("No found set list of type, insert success");
338             object->AddDeathRecipient(devicestatusCBDeathRecipient_);
339         }
340         auto [_, ret] = listeners_.insert(std::make_pair(type, listeners));
341         if (!ret) {
342             FI_HILOGW("type is duplicated");
343         }
344     } else {
345         FI_HILOGI("callbacklist.size:%{public}zu", listeners_[dtTypeIter->first].size());
346         auto iter = listeners_[dtTypeIter->first].find(callback);
347         if (iter != listeners_[dtTypeIter->first].end()) {
348             return;
349         }
350         if (listeners_[dtTypeIter->first].insert(callback).second) {
351             FI_HILOGI("Find set list of type, insert success");
352             object->AddDeathRecipient(devicestatusCBDeathRecipient_);
353         }
354     }
355     if (!Enable(type)) {
356         FI_HILOGE("Enable failed");
357         return;
358     }
359 }
360 
Unsubscribe(Type type,ActivityEvent event,sptr<IRemoteDevStaCallback> callback)361 void DeviceStatusManager::Unsubscribe(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback)
362 {
363     CALL_DEBUG_ENTER;
364     CHKPV(callback);
365     if ((type <= TYPE_INVALID) || (type >= TYPE_MAX)) {
366         FI_HILOGE("Unsubscribe type_:%{public}d is error", type);
367         return;
368     }
369     if ((event < ENTER) || (event > ENTER_EXIT)) {
370         FI_HILOGE("Unsubscribe event_:%{public}d is error", event);
371         return;
372     }
373     auto object = callback->AsObject();
374     CHKPV(object);
375     std::lock_guard lock(mutex_);
376     FI_HILOGI("listeners_.size:%{public}zu, type:%{public}d event:%{public}d", listeners_.size(),
377         static_cast<int32_t>(type), event);
378     auto dtTypeIter = listeners_.find(type);
379     if (dtTypeIter == listeners_.end()) {
380         FI_HILOGE("Failed to find listener for type");
381         return;
382     }
383     FI_HILOGI("callbacklist.size:%{public}zu", listeners_[dtTypeIter->first].size());
384     auto iter = listeners_[dtTypeIter->first].find(callback);
385     if (iter != listeners_[dtTypeIter->first].end()) {
386         if (listeners_[dtTypeIter->first].erase(callback) != 0) {
387             object->RemoveDeathRecipient(devicestatusCBDeathRecipient_);
388             if (listeners_[dtTypeIter->first].empty()) {
389                 listeners_.erase(dtTypeIter);
390             }
391         }
392     }
393     FI_HILOGI("listeners_.size:%{public}zu", listeners_.size());
394     if (listeners_.empty()) {
395         Disable(type);
396     } else {
397         FI_HILOGI("Other subscribe exist");
398     }
399 }
400 
Subscribe(int32_t type,const std::string & bundleName,sptr<IRemoteBoomerangCallback> callback)401 int32_t DeviceStatusManager::Subscribe(int32_t type, const std::string &bundleName,
402     sptr<IRemoteBoomerangCallback> callback)
403 {
404     CALL_DEBUG_ENTER;
405     CHKPR(callback, RET_ERR);
406     if ((type <= BOOMERANG_TYPE_INVALID) || (type >= BOOMERANG_TYPE_MAX)) {
407         FI_HILOGE("Subscribe boomerangType_:%{public}d is error", boomerangType_);
408         return RET_ERR;
409     }
410     boomerangType_ = type;
411     std::lock_guard lock(mutex_);
412     std::set<const sptr<IRemoteBoomerangCallback>, boomerangClasscomp> listeners;
413     auto object = callback->AsObject();
414     CHKPR(object, RET_ERR);
415     FI_HILOGI("boomerangListeners_.size:%{public}zu", boomerangListeners_.size());
416     auto dtTypeIter = boomerangListeners_.find(bundleName);
417     if (dtTypeIter == boomerangListeners_.end()) {
418         if (listeners.insert(callback).second) {
419             FI_HILOGI("No found set list of type, insert success");
420             object->AddDeathRecipient(boomerangCBDeathRecipient_);
421         }
422         auto [_, ret] = boomerangListeners_.insert(std::make_pair(bundleName, listeners));
423         if (!ret) {
424             FI_HILOGW("type is duplicated");
425         }
426     } else {
427         FI_HILOGI("callbacklist.size:%{public}zu", boomerangListeners_[dtTypeIter->first].size());
428         auto iter = boomerangListeners_[dtTypeIter->first].find(callback);
429         if (iter != boomerangListeners_[dtTypeIter->first].end()) {
430             FI_HILOGI("Subscription information of this type already exists");
431         }
432         if (boomerangListeners_[dtTypeIter->first].insert(callback).second) {
433             FI_HILOGI("Find set list of type, insert success");
434             object->AddDeathRecipient(boomerangCBDeathRecipient_);
435         }
436     }
437     return RET_OK;
438 }
439 
Unsubscribe(int32_t type,const std::string & bundleName,sptr<IRemoteBoomerangCallback> callback)440 int32_t DeviceStatusManager::Unsubscribe(int32_t type, const std::string &bundleName,
441     sptr<IRemoteBoomerangCallback> callback)
442 {
443     CALL_DEBUG_ENTER;
444     CHKPR(callback, RET_ERR);
445     if ((type <= BOOMERANG_TYPE_INVALID) || (type >= BOOMERANG_TYPE_MAX)) {
446         FI_HILOGE("Unsubscribe type_:%{public}d is error", type);
447         return RET_ERR;
448     }
449     auto object = callback->AsObject();
450     CHKPR(object, RET_ERR);
451     std::lock_guard lock(mutex_);
452     auto dtTypeIter = boomerangListeners_.find(bundleName);
453     if (dtTypeIter == boomerangListeners_.end()) {
454         FI_HILOGE("Failed to find listener for type");
455         return RET_ERR;
456     }
457     FI_HILOGI("callbacklist.size:%{public}zu", boomerangListeners_[dtTypeIter->first].size());
458     auto iter = boomerangListeners_[dtTypeIter->first].find(callback);
459     if (iter != boomerangListeners_[dtTypeIter->first].end()) {
460         if (boomerangListeners_[dtTypeIter->first].erase(callback) != 0) {
461             object->RemoveDeathRecipient(boomerangCBDeathRecipient_);
462             if (boomerangListeners_[dtTypeIter->first].empty()) {
463                 boomerangListeners_.erase(dtTypeIter);
464             }
465         }
466     }
467     FI_HILOGI("listeners_.size:%{public}zu", boomerangListeners_.size());
468     return RET_OK;
469 }
470 
NotifyMetadata(const std::string & bundleName,sptr<IRemoteBoomerangCallback> callback)471 int32_t DeviceStatusManager::NotifyMetadata(const std::string &bundleName, sptr<IRemoteBoomerangCallback> callback)
472 {
473     CALL_DEBUG_ENTER;
474     CHKPR(callback, RET_ERR);
475     auto object = callback->AsObject();
476     CHKPR(object, RET_ERR);
477     std::lock_guard lock(mutex_);
478     auto iter = boomerangListeners_.find(bundleName);
479     if (iter == boomerangListeners_.end()) {
480         FI_HILOGE("bundleName:%{public}s is not exits", bundleName.c_str());
481         return RET_ERR;
482     }
483     auto& callbacks = iter->second;
484     if (callbacks.size() == 0) {
485         FI_HILOGE("this hap is not Subscribe envent");
486         return RET_ERR;
487     }
488 
489     for (const auto &listener : callbacks) {
490         CHKPR(listener, RET_ERR);
491         BoomerangData data {};
492         data.type = BoomerangType::BOOMERANG_TYPE_BOOMERANG;
493         data.status = BoomerangStatus::BOOMERANG_STATUS_SCREEN_SHOT;
494         listener->OnScreenshotResult(data);
495     }
496     object->AddDeathRecipient(boomerangCBDeathRecipient_);
497     notityListener_ = callback;
498     auto callbackIter = bundleNameCache_.find(callback);
499     if (callbackIter == bundleNameCache_.end()) {
500         bundleNameCache_.emplace(callback, bundleName);
501     }
502     hasSubmitted_.store(false);
503     std::thread timerThread(std::bind(&DeviceStatusManager::TimerTask, this));
504     timerThread.detach();
505     return RET_OK;
506 }
507 
GetBundleNameByCallback(std::string & bundleName)508 int32_t DeviceStatusManager::GetBundleNameByCallback(std::string &bundleName)
509 {
510     auto iter = bundleNameCache_.find(notityListener_);
511     if (iter != bundleNameCache_.end()) {
512         bundleName = iter->second;
513         bundleNameCache_.erase(iter);
514         return RET_OK;
515     }
516     return RET_ERR;
517 }
518 
GetBundleNameByApplink(std::string & bundleName,const std::string & metadata)519 int32_t DeviceStatusManager::GetBundleNameByApplink(std::string &bundleName, const std::string &metadata)
520 {
521     if (bundleManager_ == nullptr) {
522         sptr<ISystemAbilityManager> systemAbilityManager =
523             SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
524         CHKPR(systemAbilityManager, E_DEVICESTATUS_GET_SYSTEM_ABILITY_MANAGER_FAILED);
525         sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
526         CHKPR(remoteObject, E_DEVICESTATUS_GET_SERVICE_FAILED);
527         bundleManager_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
528         CHKPR(bundleManager_, RET_ERR);
529     }
530     OHOS::AAFwk::Want want;
531     want.SetUri(metadata);
532     std::vector<int32_t> ids;
533     ErrCode accountRet = OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
534     if (accountRet != ERR_OK || ids.empty()) {
535         FI_HILOGE("Get userId from active Os AccountIds fail, ret : %{public}d", accountRet);
536         return RET_ERR;
537     }
538     int32_t userId = ids[0];
539     int32_t flags = static_cast<int32_t>(AppExecFwk::GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_APP_LINKING);
540     std::vector<AppExecFwk::AbilityInfo> abilityInfos;
541     bundleManager_->QueryAbilityInfosV9(want, flags, userId, abilityInfos);
542     if (abilityInfos.empty()) {
543         FI_HILOGE("Get abilityInfos fail.");
544         return RET_ERR;
545     }
546     bundleName = abilityInfos[0].bundleName;
547     return RET_OK;
548 }
549 
SubmitMetadata(const std::string & metadata)550 int32_t DeviceStatusManager::SubmitMetadata(const std::string &metadata)
551 {
552     CALL_DEBUG_ENTER;
553     std::lock_guard lock(mutex_);
554     if (hasSubmitted_) {
555         FI_HILOGE("get metadata timeout");
556         return RET_ERR;
557     }
558     hasSubmitted_.store(true);
559     CHKPR(notityListener_, RET_ERR);
560     std::string emptyMetadata;
561     std::string callbackBundleName;
562     auto callbackRet = GetBundleNameByCallback(callbackBundleName);
563     if (callbackRet != RET_OK) {
564         FI_HILOGE("Get callbackBundleName fail.");
565         notityListener_->OnNotifyMetadata(emptyMetadata);
566         return RET_OK;
567     }
568 
569     std::string applinkBundleName;
570     auto applinkRet = GetBundleNameByApplink(applinkBundleName, metadata);
571     if (applinkRet != RET_OK) {
572         FI_HILOGE("Get applinkBundleName fail.");
573         notityListener_->OnNotifyMetadata(emptyMetadata);
574         return RET_OK;
575     }
576 
577     if (callbackBundleName.compare(applinkBundleName) == 0) {
578         notityListener_->OnNotifyMetadata(metadata);
579     } else {
580         notityListener_->OnNotifyMetadata(emptyMetadata);
581     }
582     return RET_OK;
583 }
584 
BoomerangEncodeImage(std::shared_ptr<Media::PixelMap> pixelMap,const std::string & metadata,sptr<IRemoteBoomerangCallback> callback)585 int32_t DeviceStatusManager::BoomerangEncodeImage(std::shared_ptr<Media::PixelMap> pixelMap,
586     const std::string &metadata, sptr<IRemoteBoomerangCallback> callback)
587 {
588     CALL_DEBUG_ENTER;
589     CHKPR(pixelMap, RET_ERR);
590     CHKPR(callback, RET_ERR);
591     std::lock_guard lock(mutex_);
592 
593 #ifdef BOOMERANG_SUPPORT_HDR
594     sptr<SurfaceBuffer> surfaceBuf(reinterpret_cast<SurfaceBuffer*>(pixelMap->GetFd()));
595     CHKPR(surfaceBuf, RET_ERR);
596 
597     HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceType colorSpaceType;
598     Media::VpeUtils::GetSbColorSpaceType(surfaceBuf, colorSpaceType);
599 
600     HDI::Display::Graphic::Common::V1_0::CM_HDR_Metadata_Type metadatType;
601     Media::VpeUtils::GetSbMetadataType(surfaceBuf, metadatType);
602 #endif // BOOMERANG_SUPPORT_HDR
603 
604     auto algo = std::make_shared<BoomerangAlgoImpl>();
605     CHKPR(algo, RET_ERR);
606     std::shared_ptr<Media::PixelMap> encodePixelMap;
607     algo->EncodeImage(pixelMap, metadata, encodePixelMap);
608     CHKPR(encodePixelMap, RET_ERR);
609 
610 #ifdef BOOMERANG_SUPPORT_HDR
611     sptr<SurfaceBuffer> encodeSurfaceBuf(reinterpret_cast<SurfaceBuffer*>(encodePixelMap->GetFd()));
612     CHKPR(encodeSurfaceBuf, RET_ERR);
613 
614     bool ret = Media::VpeUtils::SetSbColorSpaceType(encodeSurfaceBuf, colorSpaceType);
615     if (!ret) {
616         FI_HILOGE("encode iamge faild by SetSbColorSpaceType");
617         return RET_ERR;
618     }
619 
620     ret = Media::VpeUtils::SetSbMetadadataType(encodeSurfaceBuf, metadatType);
621     if (!ret) {
622         FI_HILOGE("encode iamge faild by SetSbMetadadataType");
623         return RET_ERR;
624     }
625 #endif // BOOMERANG_SUPPORT_HDR
626 
627     callback->OnEncodeImageResult(encodePixelMap);
628     return RET_OK;
629 }
630 
BoomerangDecodeImage(std::shared_ptr<Media::PixelMap> pixelMap,sptr<IRemoteBoomerangCallback> callback)631 int32_t DeviceStatusManager::BoomerangDecodeImage(std::shared_ptr<Media::PixelMap> pixelMap,
632     sptr<IRemoteBoomerangCallback> callback)
633 {
634     CALL_DEBUG_ENTER;
635     CHKPR(pixelMap, RET_ERR);
636     CHKPR(callback, RET_ERR);
637     std::lock_guard lock(mutex_);
638     std::string metadata;
639     auto algo = std::make_shared<BoomerangAlgoImpl>();
640     CHKPR(algo, RET_ERR);
641     algo->DecodeImage(pixelMap, metadata);
642     callback->OnNotifyMetadata(metadata);
643     return RET_OK;
644 }
645 
LoadAlgorithm()646 int32_t DeviceStatusManager::LoadAlgorithm()
647 {
648     CALL_DEBUG_ENTER;
649     if (msdpImpl_ != nullptr) {
650         msdpImpl_->LoadAlgoLibrary();
651     }
652     return RET_OK;
653 }
654 
UnloadAlgorithm()655 int32_t DeviceStatusManager::UnloadAlgorithm()
656 {
657     CALL_DEBUG_ENTER;
658     if (msdpImpl_ != nullptr) {
659         msdpImpl_->UnloadAlgoLibrary();
660     }
661     return RET_OK;
662 }
663 
GetPackageName(AccessTokenID tokenId,std::string & packageName)664 int32_t DeviceStatusManager::GetPackageName(AccessTokenID tokenId, std::string &packageName)
665 {
666     int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(tokenId);
667     switch (tokenType) {
668         case ATokenTypeEnum::TOKEN_HAP: {
669             HapTokenInfo hapInfo;
670             if (AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != 0) {
671                 FI_HILOGE("Get hap token info failed");
672                 return RET_ERR;
673             }
674             packageName = hapInfo.bundleName;
675             break;
676         }
677         case ATokenTypeEnum::TOKEN_NATIVE:
678         case ATokenTypeEnum::TOKEN_SHELL: {
679             NativeTokenInfo tokenInfo;
680             if (AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) != 0) {
681                 FI_HILOGE("Get native token info failed");
682                 return RET_ERR;
683             }
684             packageName = tokenInfo.processName;
685             break;
686         }
687         default: {
688             FI_HILOGE("token type not match");
689             break;
690         }
691     }
692     return RET_OK;
693 }
694 
TimerTask()695 void DeviceStatusManager::TimerTask()
696 {
697     if (!hasSubmitted_) {
698         std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
699         SubmitMetadata("");
700     }
701 }
702 
703 #ifdef BOOMERANG_ONESTEP
GetFocuseWindowId(int32_t & windowId,std::string & bundleName)704 int32_t DeviceStatusManager::GetFocuseWindowId(int32_t &windowId, std::string &bundleName)
705 {
706     std::vector<sptr<Rosen::WindowVisibilityInfo>> winInfos;
707     Rosen::WMError ret = Rosen::WindowManager::GetInstance().GetVisibilityWindowInfo(winInfos);
708     if (ret != Rosen::WMError::WM_OK) {
709         FI_HILOGI("get windowInfos failed, ret=%{public}d", ret);
710         return RET_ERR;
711     }
712     for (const auto &winInfo : winInfos) {
713         if (winInfo == nullptr) {
714             continue;
715         }
716         if (winInfo->IsFocused()) {
717             windowId = static_cast<int32_t>(winInfo->GetWindowId());
718             bundleName =  winInfo->GetBundleName();
719             FI_HILOGD("get focuse windowId, ret=%{public}d, bundleName:%{public}s",
720                 winInfo->GetWindowId(), winInfo->GetBundleName().c_str());
721             return RET_OK;
722         }
723     }
724     return RET_ERR;
725 }
726 #endif
727 
728 #ifdef BOOMERANG_ONESTEP
SystemBarHiddedInit()729 void DeviceStatusManager::SystemBarHiddedInit()
730 {
731     ACCESSIBILITY_MANAGER.AccessibilityConnect([this](int32_t value) {
732         std::lock_guard<std::mutex> guard(g_mutex_);
733         CHKPV(g_deviceManager_);
734         switch (value) {
735             case AccessibilityStatus::ON_ABILITY_CONNECTED: {
736                 g_deviceManager_->isAccessibilityInit_ = true;
737                 FI_HILOGI("Accessibility service has connect");
738                 break;
739             }
740             case AccessibilityStatus::ON_ABILITY_SCROLLED_EVENT: {
741                 g_deviceManager_->HandlerPageScrollerEvent(PAGE_SCROLL_ENVENT);
742                 break;
743             }
744             case AccessibilityStatus::ON_ABILITY_DISCONNECTED: {
745                 g_deviceManager_->isAccessibilityInit_ = false;
746                 FI_HILOGI("Accessibility service has disConnect");
747                 break;
748             }
749             default: {
750                 FI_HILOGW("Accessibility Unknown Event");
751                 break;
752             }
753         }
754     });
755 }
756 #endif
757 
758 #ifdef BOOMERANG_ONESTEP
HandlerPageScrollerEvent(int32_t event)759 void DeviceStatusManager::HandlerPageScrollerEvent(int32_t event)
760 {
761     int32_t windowId;
762     std::string bundleName;
763     int32_t result = GetFocuseWindowId(windowId, bundleName);
764     if (result != RET_OK) {
765         FI_HILOGE("get the focuse widowId faild, result=%{public}d", result);
766         return;
767     }
768     CHKPV(g_deviceManager_);
769     if (g_deviceManager_->lastEnable_ || bundleName != BUNDLE_NAME) {
770         FI_HILOGD("The current status bar is in display mode or does not belong to the whitelist application");
771         return;
772     }
773 
774     if (event == SYSTEM_BAR_HIDDEN) {
775         SystemBarHiddedInit();
776     }
777 
778     std::shared_ptr<Media::PixelMap> screenShot;
779     Rosen::WMError ret = WindowManager::GetInstance().GetSnapshotByWindowId(windowId, screenShot);
780     if (ret == Rosen::WMError::WM_OK) {
781         OnSurfaceCapture(windowId, screenShot);
782     }
783 }
784 #endif
785 
786 #ifdef BOOMERANG_ONESTEP
OnSurfaceCapture(int32_t windowId,std::shared_ptr<Media::PixelMap> & screenShot)787 void DeviceStatusManager::OnSurfaceCapture(int32_t windowId, std::shared_ptr<Media::PixelMap> &screenShot)
788 {
789     auto algo = std::make_shared<BoomerangAlgoImpl>();
790     CHKPV(algo);
791     std::string metadata;
792     algo->DecodeImage(screenShot, metadata);
793     std::lock_guard lock(countMutex_);
794     if (!metadata.empty()) {
795         FI_HILOGI("Boomerang Algo decode image result:%{public}s", metadata.c_str());
796         retryCount = 0;
797         return;
798     }
799     if (retryCount < 1) {
800         retryCount++;
801         Rosen::WMError ret = WindowManager::GetInstance().GetSnapshotByWindowId(windowId, screenShot);
802         if (ret == Rosen::WMError::WM_OK) {
803             OnSurfaceCapture(windowId, screenShot);
804         }
805         retryCount = 0;
806     }
807 }
808 #endif
809 } // namespace DeviceStatus
810 } // namespace Msdp
811 } // namespace OHOS
812