1 /*
2  * Copyright (C) 2021-2022 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 "incoming_call_wake_up.h"
17 
18 #ifdef ABILITY_POWER_SUPPORT
19 #include "power_mgr_client.h"
20 #endif
21 #include "iservice_registry.h"
22 #include "system_ability.h"
23 #include "system_ability_definition.h"
24 #include "telephony_log_wrapper.h"
25 
26 namespace OHOS {
27 namespace Telephony {
NewCallCreated(sptr<CallBase> & callObjectPtr)28 void IncomingCallWakeup::NewCallCreated(sptr<CallBase> &callObjectPtr)
29 {
30     // Should wake up the device and only Voip callType set the screen on while a new incoming call created.
31     if (callObjectPtr != nullptr && callObjectPtr->GetTelCallState() == TelCallState::CALL_STATUS_INCOMING) {
32         WakeupDevice(callObjectPtr);
33     }
34 }
35 
AcquireIncomingLock()36 void IncomingCallWakeup::AcquireIncomingLock()
37 {
38 #ifdef ABILITY_POWER_SUPPORT
39     if (incomingRunningLock_ == nullptr) {
40         incomingRunningLock_ = PowerMgr::PowerMgrClient::GetInstance().
41             CreateRunningLock("incomingrunninglock", PowerMgr::RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
42     }
43     if (incomingRunningLock_ != nullptr) {
44         incomingRunningLock_->Lock(INCOMING_LOCK_TIMEOUT);
45         TELEPHONY_LOGI("incomingRunningLock_ locked");
46     }
47 #endif
48 }
49 
ReleaseIncomingLock()50 void IncomingCallWakeup::ReleaseIncomingLock()
51 {
52 #ifdef ABILITY_POWER_SUPPORT
53     if (incomingRunningLock_ == nullptr || !incomingRunningLock_->IsUsed()) {
54         return;
55     }
56     incomingRunningLock_->UnLock();
57     TELEPHONY_LOGI("incomingRunningLock_ unlocked");
58 #endif
59 }
60 
WakeupDevice(sptr<CallBase> & callObjectPtr)61 void IncomingCallWakeup::WakeupDevice(sptr<CallBase> &callObjectPtr)
62 {
63 #ifdef ABILITY_POWER_SUPPORT
64     if (phoneRunningLock_ == nullptr) {
65         phoneRunningLock_ = PowerMgr::PowerMgrClient::GetInstance().
66             CreateRunningLock("phonerunninglock", PowerMgr::RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
67     }
68     if (phoneRunningLock_ != nullptr && !isPhoneLocked) {
69         phoneRunningLock_->Lock();
70         isPhoneLocked = true;
71         TELEPHONY_LOGI("phoneRunningLock_ locked");
72     }
73     if (screenRunningLock_ == nullptr) {
74         screenRunningLock_ = PowerMgr::PowerMgrClient::GetInstance().
75             CreateRunningLock("screenonrunninglock", PowerMgr::RunningLockType::RUNNINGLOCK_SCREEN);
76     }
77     if (screenRunningLock_ != nullptr && !isScreenOnLocked) {
78         screenRunningLock_->Lock();
79         isScreenOnLocked = true;
80         TELEPHONY_LOGI("screenRunningLock_ locked");
81     }
82     // if call type is voip, set the screen on
83     if (callObjectPtr != nullptr && callObjectPtr->GetCallType() == CallType::TYPE_VOIP) {
84         PowerMgr::PowerMgrClient::GetInstance().WakeupDevice(
85             PowerMgr::WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, wakeupReason_);
86     }
87 #endif
88 }
89 
IsPowerAbilityExist()90 bool IncomingCallWakeup::IsPowerAbilityExist()
91 {
92     std::lock_guard<std::mutex> lock(mutex_);
93     sptr<ISystemAbilityManager> sysAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
94     if (!sysAbilityMgr) {
95         TELEPHONY_LOGE("system ability manager nullptr");
96         return false;
97     }
98     sptr<IRemoteObject> remote = sysAbilityMgr->CheckSystemAbility(POWER_MANAGER_SERVICE_ID);
99     if (!remote) {
100         TELEPHONY_LOGE("no power ability");
101         return false;
102     }
103     return true;
104 }
105 
CallDestroyed(const DisconnectedDetails & details)106 void IncomingCallWakeup::CallDestroyed(const DisconnectedDetails &details) {}
107 
IncomingCallActivated(sptr<CallBase> & callObjectPtr)108 void IncomingCallWakeup::IncomingCallActivated(sptr<CallBase> &callObjectPtr) {}
109 
IncomingCallHungUp(sptr<CallBase> & callObjectPtr,bool isSendSms,std::string content)110 void IncomingCallWakeup::IncomingCallHungUp(sptr<CallBase> &callObjectPtr, bool isSendSms, std::string content) {}
111 
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)112 void IncomingCallWakeup::CallStateUpdated(
113     sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
114 {
115     if (!isPhoneLocked && !isScreenOnLocked) {
116         return;
117     }
118     bool hasRingCall = false;
119     CallObjectManager::HasRingingCall(hasRingCall);
120     if (!hasRingCall) {
121     #ifdef ABILITY_POWER_SUPPORT
122         if (screenRunningLock_ != nullptr && isScreenOnLocked) {
123             screenRunningLock_->UnLock();
124             isScreenOnLocked = false;
125             TELEPHONY_LOGI("screenRunningLock_ unlocked");
126         }
127         if (phoneRunningLock_ != nullptr && isPhoneLocked) {
128             phoneRunningLock_->UnLock();
129             isPhoneLocked = false;
130             TELEPHONY_LOGI("phoneRunningLock_ unlocked");
131         }
132     #endif
133     }
134 }
135 } // namespace Telephony
136 } // namespace OHOS