• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 "distributed_observer_service.h"
16 
17 #include "distributed_service.h"
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "distributed_operation_service.h"
21 
22 namespace OHOS {
23 namespace Notification {
24 
25 namespace {
26 const static int32_t SCREEN_OFF = 0;
27 const static int32_t SCREEN_ON = 1;
28 }
29 
OnReceiveEvent(const EventFwk::CommonEventData & data)30 void DistributedEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
31 {
32     auto const &want = data.GetWant();
33     std::string action = want.GetAction();
34     ANS_LOGI("DistributedEventSubscriber receiver event %{public}s", action.c_str());
35     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) {
36         DistributedService::GetInstance().SyncDeviceState(SCREEN_OFF);
37         return;
38     }
39     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
40         DistributedService::GetInstance().SyncDeviceState(SCREEN_ON);
41         return;
42     }
43     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
44         OperationService::GetInstance().HandleScreenEvent();
45         return;
46     }
47     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED ||
48         action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
49         OHOS::AppExecFwk::ElementName ele = want.GetElement();
50         std::string bundleName = ele.GetBundleName();
51         if (bundleName.empty()) {
52             ANS_LOGE("Illegal bundle name.");
53             return;
54         }
55         DistributedService::GetInstance().HandleBundlesEvent(bundleName, action);
56         return;
57     }
58 }
59 
GetInstance()60 OberverService& OberverService::GetInstance()
61 {
62     static OberverService oberverService;
63     return oberverService;
64 }
65 
Init(uint16_t deviceType)66 void OberverService::Init(uint16_t deviceType)
67 {
68     EventFwk::MatchingSkills matchingSkills;
69     if (deviceType != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) {
70         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
71         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
72     }
73     if (deviceType == DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) {
74         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
75         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
76         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
77     }
78     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
79     subscriber_ = std::make_shared<DistributedEventSubscriber>(subscribeInfo);
80     if (subscriber_ == nullptr) {
81         ANS_LOGE("subscriber_ is nullptr");
82         return;
83     }
84     EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
85     ANS_LOGI("OberverService service successfully.");
86 }
87 
IsScreenLocked()88 int32_t OberverService::IsScreenLocked()
89 {
90     bool state = ScreenLock::ScreenLockManager::GetInstance()->IsScreenLocked();
91     return state ? SCREEN_OFF : SCREEN_ON;
92 }
93 
Destory()94 void OberverService::Destory()
95 {
96     EventFwk::CommonEventManager::NewUnSubscribeCommonEventSync(subscriber_);
97     ANS_LOGI("OberverService service destory.");
98 }
99 
Unlock(const ScreenLock::Action & action,const sptr<ScreenLock::ScreenLockCallbackInterface> & listener)100 int32_t OberverService::Unlock(
101     const ScreenLock::Action &action, const sptr<ScreenLock::ScreenLockCallbackInterface> &listener)
102 {
103     return ScreenLock::ScreenLockManager::GetInstance()->Unlock(action, listener);
104 }
105 }
106 }
107