• 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 "distributed_unlock_listener_oper_service.h"
20 #include "common_event_support.h"
21 #include "distributed_operation_helper.h"
22 
23 namespace OHOS {
24 namespace Notification {
25 
26 namespace {
27 const static int32_t SCREEN_OFF = 0;
28 const static int32_t SCREEN_ON = 1;
29 }
30 
OnReceiveEvent(const EventFwk::CommonEventData & data)31 void DistributedEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
32 {
33     auto const &want = data.GetWant();
34     std::string action = want.GetAction();
35     ANS_LOGI("receive %{public}s", action.c_str());
36 #ifdef DISTRIBUTED_FEATURE_MASTER
37     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
38         OperationService::GetInstance().HandleScreenEvent();
39         return;
40     }
41     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
42         UnlockListenerOperService::GetInstance().ReplyOperationResponse();
43         return;
44     }
45 #else
46     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) {
47         DistributedService::GetInstance().SyncDeviceStatus(SCREEN_OFF);
48         return;
49     }
50     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
51         DistributedService::GetInstance().SyncDeviceStatus(SCREEN_ON);
52         return;
53     }
54     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED ||
55         action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) {
56         OHOS::AppExecFwk::ElementName ele = want.GetElement();
57         std::string bundleName = ele.GetBundleName();
58         if (bundleName.empty()) {
59             ANS_LOGE("Illegal bundle name.");
60             return;
61         }
62         bool isAddBundle =
63             (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) ? true : false;
64         DistributedService::GetInstance().SyncInstalledBundle(bundleName, isAddBundle);
65         return;
66     }
67 #endif
68 }
69 
GetInstance()70 OberverService& OberverService::GetInstance()
71 {
72     static OberverService oberverService;
73     return oberverService;
74 }
75 
Init(uint16_t deviceType)76 void OberverService::Init(uint16_t deviceType)
77 {
78     EventFwk::MatchingSkills matchingSkills;
79 #ifdef DISTRIBUTED_FEATURE_MASTER
80     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
81     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
82     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
83     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
84 #else
85     if (deviceType == DistributedHardware::DmDeviceType::DEVICE_TYPE_PAD ||
86         deviceType == DistributedHardware::DmDeviceType::DEVICE_TYPE_2IN1 ||
87         deviceType == DistributedHardware::DmDeviceType::DEVICE_TYPE_PC) {
88         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
89         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
90     }
91     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
92     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
93 #endif
94     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
95     subscriber_ = std::make_shared<DistributedEventSubscriber>(subscribeInfo);
96     if (subscriber_ == nullptr) {
97         ANS_LOGE("subscriber_ is nullptr");
98         return;
99     }
100     EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
101     ANS_LOGI("OberverService service successfully.");
102 }
103 
IsScreenLocked()104 int32_t OberverService::IsScreenLocked()
105 {
106 #ifdef SCREENLOCK_MGR_ENABLE
107     bool state = ScreenLock::ScreenLockManager::GetInstance()->IsScreenLocked();
108     return state ? SCREEN_OFF : SCREEN_ON;
109 #else
110     ANS_LOGI("Screenlock manager is disabled.");
111     return SCREEN_ON;
112 #endif
113 }
114 
Destory()115 void OberverService::Destory()
116 {
117     EventFwk::CommonEventManager::NewUnSubscribeCommonEventSync(subscriber_);
118     ANS_LOGI("OberverService service destory.");
119 }
120 
121 #ifdef SCREENLOCK_MGR_ENABLE
Unlock(const ScreenLock::Action & action,const sptr<ScreenLock::ScreenLockCallbackInterface> & listener)122 int32_t OberverService::Unlock(
123     const ScreenLock::Action &action, const sptr<ScreenLock::ScreenLockCallbackInterface> &listener)
124 {
125     return ScreenLock::ScreenLockManager::GetInstance()->Unlock(action, listener);
126 }
127 #endif
128 }
129 }
130