• 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 
16 #include "distributed_operation_service.h"
17 
18 #include "screenlock_common.h"
19 #include "in_process_call_wrapper.h"
20 #include "distributed_service.h"
21 
22 namespace OHOS {
23 namespace Notification {
24 
25 namespace {
26 constexpr const int32_t ANS_CUSTOMIZE_CODE = 7;
27 constexpr const int32_t BRANCH3_ID = 3;
28 constexpr const int32_t BRANCH7_ID = 7;
29 constexpr const int32_t BRANCH8_ID = 8;
30 }
UnlockScreenCallback(const std::string & eventId)31 UnlockScreenCallback::UnlockScreenCallback(const std::string& eventId) : eventId_(eventId) {}
32 
~UnlockScreenCallback()33 UnlockScreenCallback::~UnlockScreenCallback() {}
34 
OnCallBack(int32_t screenLockResult)35 void UnlockScreenCallback::OnCallBack(int32_t screenLockResult)
36 {
37     ANS_LOGI("Unlock Screen result: %{public}d", screenLockResult);
38     if (screenLockResult == ScreenLock::ScreenChange::SCREEN_SUCC) {
39         OperationService::GetInstance().TriggerOperation(eventId_);
40     } else {
41         std::string errorReason = "unlock screen failed";
42         int32_t messageType = 0;
43         DistributedService::GetInstance().SendEventReport(messageType, -1, errorReason);
44         DistributedService::GetInstance().SendHaReport(-1, BRANCH7_ID, errorReason);
45     }
46 }
47 
GetInstance()48 OperationService& OperationService::GetInstance()
49 {
50     static OperationService operationService;
51     return operationService;
52 }
53 
AddOperation(OperationInfo operationInfo)54 void OperationService::AddOperation(OperationInfo operationInfo)
55 {
56     std::lock_guard<std::mutex> lock(operationMutex_);
57     auto iter = operationInfoMaps_.find(operationInfo.eventId);
58     if (iter != operationInfoMaps_.end()) {
59         ANS_LOGW("OperationService operation exist %{public}s.", operationInfo.eventId.c_str());
60         return;
61     }
62     ANS_LOGI("OperationService add operation %{public}s %{public}d.",
63         operationInfo.eventId.c_str(), operationInfo.type);
64     operationInfoMaps_[operationInfo.eventId] = operationInfo;
65 }
66 
HandleScreenEvent()67 void OperationService::HandleScreenEvent()
68 {
69     std::lock_guard<std::mutex> lock(operationMutex_);
70     for (auto iter = operationInfoMaps_.begin(); iter != operationInfoMaps_.end();) {
71         ANS_LOGI("OperationService erase operation %{public}s %{public}d.",
72             iter->second.eventId.c_str(), iter->second.type);
73         iter = operationInfoMaps_.erase(iter);
74     }
75 }
76 
TriggerOperation(std::string eventId)77 void OperationService::TriggerOperation(std::string eventId)
78 {
79     std::lock_guard<std::mutex> lock(operationMutex_);
80     auto iter = operationInfoMaps_.find(eventId);
81     if (iter == operationInfoMaps_.end()) {
82         ANS_LOGW("Operation not exist %{public}s.", eventId.c_str());
83         return;
84     }
85     if (iter->second.type == OperationType::DISTRIBUTE_OPERATION_JUMP) {
86         auto ret = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->StartAbility(iter->second.want));
87         std::string errorReason = "pull up success";
88         if (ret == ERR_OK) {
89             DistributedService::GetInstance().SendHaReport(
90                 NotificationConstant::SlotType::LIVE_VIEW, BRANCH3_ID, errorReason, ANS_CUSTOMIZE_CODE);
91         } else {
92             errorReason = "pull up failed";
93             int32_t messageType = 0;
94             DistributedService::GetInstance().SendEventReport(messageType, ret, errorReason);
95         }
96         DistributedService::GetInstance().SendHaReport(ret, BRANCH8_ID, errorReason);
97         operationInfoMaps_.erase(iter);
98         ANS_LOGI("StartAbility result:%{public}s %{public}d", eventId.c_str(), ret);
99         return;
100     }
101 }
102 
TimeOutOperation(std::string eventId)103 void OperationService::TimeOutOperation(std::string eventId)
104 {
105     std::lock_guard<std::mutex> lock(operationMutex_);
106     auto iter = operationInfoMaps_.find(eventId);
107     if (iter == operationInfoMaps_.end()) {
108         ANS_LOGI("Operation not exist %{public}s.", eventId.c_str());
109         return;
110     }
111     ANS_LOGI("OperationService timeout operation %{public}s %{public}d.",
112         iter->second.eventId.c_str(), iter->second.type);
113     operationInfoMaps_.erase(iter);
114 }
115 }
116 }
117