1 /*
2 * Copyright (C) 2025 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_flash_reminder.h"
17
18 #include "os_account_manager.h"
19 #include "settings_datashare_helper.h"
20 #include "telephony_errors.h"
21 #include "telephony_log_wrapper.h"
22 #ifdef ABILITY_CAMERA_FRAMEWORK_SUPPORT
23 #include "input/camera_manager.h"
24 #endif
25 #ifdef ABILITY_SCREENLOCKMGR_SUPPORT
26 #include "screenlock_manager.h"
27 #endif
28
29 namespace OHOS {
30 namespace Telephony {
31 constexpr int64_t DELAY_SET_TORCH_MODE_TIME = 300;
32 constexpr uint32_t DELAY_SET_TORCH_EVENT = 1000000;
33 constexpr uint32_t STOP_FLASH_REMIND_EVENT = 1000001;
34 constexpr uint32_t START_FLASH_REMIND_EVENT = 1000002;
35 const std::string FLASH_REMINDER_SWITCH_SUBSTRING = "INCOMING_CALL";
IncomingFlashReminder(const std::shared_ptr<AppExecFwk::EventRunner> & runner,std::function<void ()> stopFlashRemindDone)36 IncomingFlashReminder::IncomingFlashReminder(const std::shared_ptr<AppExecFwk::EventRunner> &runner,
37 std::function<void()> stopFlashRemindDone)
38 : AppExecFwk::EventHandler(runner), stopFlashRemindDone_(std::move(stopFlashRemindDone)) {}
39
~IncomingFlashReminder()40 IncomingFlashReminder::~IncomingFlashReminder()
41 {
42 if (!isFlashRemindUsed_) {
43 TELEPHONY_LOGI("no need to stop");
44 return;
45 }
46 #ifdef ABILITY_CAMERA_FRAMEWORK_SUPPORT
47 sptr<CameraStandard::CameraManager> camMgr = CameraStandard::CameraManager::GetInstance();
48 int32_t result = camMgr->SetTorchMode(CameraStandard::TORCH_MODE_OFF);
49 TELEPHONY_LOGI("set torch mode result: %{public}d", result);
50 #endif
51 }
52
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)53 void IncomingFlashReminder::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
54 {
55 switch (event->GetInnerEventId()) {
56 case DELAY_SET_TORCH_EVENT:
57 HandleSetTorchMode();
58 break;
59 case STOP_FLASH_REMIND_EVENT:
60 HandleStopFlashRemind();
61 break;
62 case START_FLASH_REMIND_EVENT:
63 HandleStartFlashRemind();
64 break;
65 default:
66 TELEPHONY_LOGE("receive unknown event %{public}u", event->GetInnerEventId());
67 break;
68 }
69 }
70
IsFlashRemindNecessary()71 bool IncomingFlashReminder::IsFlashRemindNecessary()
72 {
73 if (!IsFlashReminderSwitchOn()) {
74 TELEPHONY_LOGI("flash remind switch off");
75 return false;
76 }
77
78 return IsScreenStatusSatisfied() && IsTorchReady();
79 }
80
IsScreenStatusSatisfied()81 bool IncomingFlashReminder::IsScreenStatusSatisfied()
82 {
83 #ifdef ABILITY_SCREENLOCKMGR_SUPPORT
84 if (!OHOS::ScreenLock::ScreenLockManager::GetInstance()->IsScreenLocked()) {
85 TELEPHONY_LOGI("screen is unlocked");
86 return false;
87 }
88 return true;
89 #else
90 TELEPHONY_LOGI("screen manager not support");
91 return false;
92 #endif
93 }
94
IsTorchReady()95 bool IncomingFlashReminder::IsTorchReady()
96 {
97 #ifdef ABILITY_CAMERA_FRAMEWORK_SUPPORT
98 sptr<CameraStandard::CameraManager> camMgr = CameraStandard::CameraManager::GetInstance();
99 if (!camMgr->IsTorchSupported()) {
100 TELEPHONY_LOGI("torch not support");
101 return false;
102 }
103 CameraStandard::TorchMode currentMode = camMgr->GetTorchMode();
104 if (currentMode == CameraStandard::TORCH_MODE_ON) {
105 TELEPHONY_LOGI("torch being used");
106 return false;
107 }
108 return true;
109 #else
110 TELEPHONY_LOGI("camera manager not support");
111 return false;
112 #endif
113 }
114
IsFlashReminderSwitchOn()115 bool IncomingFlashReminder::IsFlashReminderSwitchOn()
116 {
117 std::vector<int> activedOsAccountIds;
118 OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activedOsAccountIds);
119 if (activedOsAccountIds.empty()) {
120 TELEPHONY_LOGW("activedOsAccountIds is empty");
121 return false;
122 }
123 int userId = activedOsAccountIds[0];
124 OHOS::Uri uri(
125 "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_"
126 + std::to_string(userId) + "?Proxy=true");
127 auto datashareHelper = SettingsDataShareHelper::GetInstance();
128 std::string value;
129 int32_t result = datashareHelper->Query(uri, "", value);
130 bool isSwitchOn = (result == TELEPHONY_SUCCESS && value == "1");
131 if (!isSwitchOn) {
132 TELEPHONY_LOGI("switch off");
133 return false;
134 }
135 result = datashareHelper->Query(uri, "", value);
136 TELEPHONY_LOGI("query reminder switch, result: %{public}d", result);
137 return (result == TELEPHONY_SUCCESS && value.find(FLASH_REMINDER_SWITCH_SUBSTRING) != std::string::npos);
138 }
139
StartFlashRemind()140 void IncomingFlashReminder::StartFlashRemind()
141 {
142 SendEvent(AppExecFwk::InnerEvent::Get(START_FLASH_REMIND_EVENT, 0));
143 }
144
HandleStartFlashRemind()145 void IncomingFlashReminder::HandleStartFlashRemind()
146 {
147 if (isFlashRemindUsed_) {
148 return;
149 }
150 isFlashRemindUsed_ = true;
151 SendEvent(AppExecFwk::InnerEvent::Get(DELAY_SET_TORCH_EVENT, 0));
152 }
153
HandleSetTorchMode()154 void IncomingFlashReminder::HandleSetTorchMode()
155 {
156 #ifdef ABILITY_CAMERA_FRAMEWORK_SUPPORT
157 sptr<CameraStandard::CameraManager> camMgr = CameraStandard::CameraManager::GetInstance();
158 CameraStandard::TorchMode currentMode = camMgr->GetTorchMode();
159 CameraStandard::TorchMode nextMode = (currentMode == CameraStandard::TORCH_MODE_ON?
160 CameraStandard::TORCH_MODE_OFF : CameraStandard::TORCH_MODE_ON);
161 int32_t result = camMgr->SetTorchMode(nextMode);
162 TELEPHONY_LOGI("set torch mode result: %{public}d", result);
163 SendEvent(AppExecFwk::InnerEvent::Get(DELAY_SET_TORCH_EVENT, 0), DELAY_SET_TORCH_MODE_TIME);
164 #endif
165 }
166
StopFlashRemind()167 void IncomingFlashReminder::StopFlashRemind()
168 {
169 SendEvent(AppExecFwk::InnerEvent::Get(STOP_FLASH_REMIND_EVENT, 0));
170 }
171
HandleStopFlashRemind()172 void IncomingFlashReminder::HandleStopFlashRemind()
173 {
174 if (!isFlashRemindUsed_) {
175 TELEPHONY_LOGI("no need to stop");
176 if (stopFlashRemindDone_ != nullptr) {
177 stopFlashRemindDone_();
178 }
179 return;
180 }
181 isFlashRemindUsed_ = false;
182 RemoveEvent(DELAY_SET_TORCH_EVENT);
183 #ifdef ABILITY_CAMERA_FRAMEWORK_SUPPORT
184 sptr<CameraStandard::CameraManager> camMgr = CameraStandard::CameraManager::GetInstance();
185 int32_t result = camMgr->SetTorchMode(CameraStandard::TORCH_MODE_OFF);
186 TELEPHONY_LOGI("set torch mode result: %{public}d", result);
187 #endif
188 if (stopFlashRemindDone_ != nullptr) {
189 stopFlashRemindDone_();
190 }
191 }
192 }
193 }