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 16 #include "commeventsubscriber.h" 17 #include "sclock_log.h" 18 #include "screenlock_common.h" 19 #include "preferences_util.h" 20 21 namespace OHOS { 22 namespace ScreenLock { 23 const std::string AUTH_PIN = "1"; 24 const std::string HAS_NO_CREDENTIAL = "0"; 25 const std::string TAG_USERID = "userId"; 26 const std::string TAG_AUTHTYPE = "authType"; 27 const std::string TAG_CREDENTIALCOUNT = "credentialCount"; 28 const std::string USER_CREDENTIAL_UPDATED_EVENT = "USER_CREDENTIAL_UPDATED_EVENT"; 29 CommeventMgr()30CommeventMgr::CommeventMgr() {} 31 ~CommeventMgr()32CommeventMgr::~CommeventMgr() 33 { 34 UnSubscribeEvent(); 35 } 36 OnReceiveEvent(const AAFwk::Want & want)37void CommeventMgr::OnReceiveEvent(const AAFwk::Want &want) 38 { 39 std::string action = want.GetAction(); 40 SCLOCK_HILOGI("recive param update event: %{public}s", action.c_str()); 41 if (action == USER_CREDENTIAL_UPDATED_EVENT) { 42 std::string userId = want.GetStringParam(TAG_USERID); 43 std::string authType = want.GetStringParam(TAG_AUTHTYPE); 44 std::string credentialCount = want.GetStringParam(TAG_CREDENTIALCOUNT); 45 if (authType == AUTH_PIN && credentialCount != HAS_NO_CREDENTIAL) { 46 SCLOCK_HILOGI("set passwd"); 47 auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance(); 48 if (preferencesUtil == nullptr) { 49 SCLOCK_HILOGE("preferencesUtil is nullptr!"); 50 return; 51 } 52 preferencesUtil->RemoveKey(userId); 53 preferencesUtil->Refresh(); 54 } 55 } 56 } 57 SubscribeEvent()58void CommeventMgr::SubscribeEvent() 59 { 60 SCLOCK_HILOGD("SubscribeEvent start."); 61 if (subscriber_) { 62 SCLOCK_HILOGI("Common Event is already subscribered."); 63 return; 64 } 65 66 EventFwk::MatchingSkills matchingSkills; 67 matchingSkills.AddEvent(USER_CREDENTIAL_UPDATED_EVENT); 68 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills); 69 subscriber_ = std::make_shared<CommEventSubscriber>(subscribeInfo, *this); 70 71 bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_); 72 if (!subscribeResult) { 73 SCLOCK_HILOGE("SubscribeEvent failed."); 74 subscriber_ = nullptr; 75 } 76 return; 77 } 78 UnSubscribeEvent()79void CommeventMgr::UnSubscribeEvent() 80 { 81 if (subscriber_) { 82 bool subscribeResult = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_); 83 SCLOCK_HILOGI("subscribeResult = %{public}d", subscribeResult); 84 subscriber_ = nullptr; 85 } 86 return; 87 } 88 } // namespace ScreenLock 89 } // namespace OHOS