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 
17 #include <satellite_call_control.h>
18 
19 #include "call_control_manager.h"
20 #include "call_dialog.h"
21 #include "call_object_manager.h"
22 #include "common_event.h"
23 #include "common_event_manager.h"
24 #include "common_event_support.h"
25 #include "notification_content.h"
26 #include "notification_helper.h"
27 #include "notification_normal_content.h"
28 #include "notification_request.h"
29 #include "settings_datashare_helper.h"
30 #include "telephony_errors.h"
31 #include "telephony_log_wrapper.h"
32 #include "telephony_permission.h"
33 #include "audio_control_manager.h"
34 
35 #include "want.h"
36 #include "uri.h"
37 #include "parameters.h"
38 
39 namespace OHOS {
40 namespace Telephony {
41 const int32_t DELAYED_TIME = 270000;
42 const int32_t COUNT_DOWN_TIME = 30000;
43 const int32_t SATELLITE_CONNECTED = 1;
44 const int32_t SATELLITE_MODE_ON = 1;
45 constexpr const char *SATELLITE_TYPE_DEFAULT_VALUE = "0";
46 constexpr const char *TEL_SATELLITE_SUPPORT_TYPE = "const.telephony.satellite.support_type";
47 
48 SatelliteCallControl::SatelliteCallControl() = default;
49 
50 SatelliteCallControl::~SatelliteCallControl() = default;
51 
SetSatcommTempLevel(int32_t level)52 void SatelliteCallControl::SetSatcommTempLevel(int32_t level)
53 {
54     if ((SatCommTempLevel)level == SatCommTempLevel_) {
55         return;
56     } else {
57         if ((SatCommTempLevel)level == SatCommTempLevel::TEMP_LEVEL_HIGH &&
58             CallObjectManager::HasSatelliteCallExist()) {
59             DelayedSingleton<CallDialog>::GetInstance()->DialogConnectExtension("SATELLITE_CALL_DISCONNECT");
60             SetShowDialog(true);
61         }
62         SatCommTempLevel_ = (SatCommTempLevel)level;
63     }
64 }
65 
IsAllowedSatelliteDialCall()66 int32_t SatelliteCallControl::IsAllowedSatelliteDialCall()
67 {
68     auto datashareHelper = SettingsDataShareHelper::GetInstance();
69     OHOS::Uri satelliteModeUri(SettingsDataShareHelper::SETTINGS_DATASHARE_URI);
70     std::string is_satellite_connected{"0"};
71     int32_t err = datashareHelper->Query(satelliteModeUri, SettingsDataShareHelper::QUERY_SATELLITE_CONNECTED_KEY,
72         is_satellite_connected);
73     if (err == TELEPHONY_SUCCESS && stoi(is_satellite_connected) == SATELLITE_CONNECTED) {
74         TELEPHONY_LOGI("satellite service is connected");
75         std::string tempLevel = OHOS::system::GetParameter("persist.thermal.log.satcomm", "-1");
76         if (tempLevel != "-1") {
77             SetSatcommTempLevel(stoi(tempLevel));
78         }
79         TELEPHONY_LOGI("GetSatcommTempLevel = %{public}s", tempLevel.c_str());
80         if (GetSatcommTempLevel() >= SatCommTempLevel::TEMP_LEVEL_MIDDLE) {
81             DelayedSingleton<CallDialog>::GetInstance()->DialogConnectExtension("CANNOT_DIAL_SATELLITE_CALL");
82             return TELEPHONY_ERROR;
83         }
84         SetUsedModem();
85         return TELEPHONY_SUCCESS;
86     } else {
87         PublishSatelliteConnectEvent();
88     }
89     return TELEPHONY_ERROR;
90 }
91 
IsSatelliteSwitchEnable()92 int32_t SatelliteCallControl::IsSatelliteSwitchEnable()
93 {
94     auto datashareHelper = SettingsDataShareHelper::GetInstance();
95     OHOS::Uri satelliteModeUri(SettingsDataShareHelper::SETTINGS_DATASHARE_URI);
96     std::string is_satellite_mode_on{ "0" };
97     int32_t err = datashareHelper->Query(satelliteModeUri, SettingsDataShareHelper::QUERY_SATELLITE_MODE_KEY,
98         is_satellite_mode_on);
99     if (err == TELEPHONY_SUCCESS && stoi(is_satellite_mode_on) == SATELLITE_MODE_ON) {
100         TELEPHONY_LOGI("satellite mode on");
101         return TELEPHONY_SUCCESS;
102     }
103     return TELEPHONY_ERROR;
104 }
105 
GetSatcommTempLevel()106 SatCommTempLevel SatelliteCallControl::GetSatcommTempLevel()
107 {
108     return SatCommTempLevel_;
109 }
110 
SetUsedModem()111 void SatelliteCallControl::SetUsedModem()
112 {
113     char satelliteSupportType[SYSPARA_SIZE] = { 0 };
114     GetParameter(TEL_SATELLITE_SUPPORT_TYPE, SATELLITE_TYPE_DEFAULT_VALUE, satelliteSupportType, SYSPARA_SIZE);
115     uint32_t satelliteMode = static_cast<unsigned int>(std::atoi(satelliteSupportType));
116     TELEPHONY_LOGI("satellite satelliteMode = %{public}d.", satelliteMode);
117     if ((satelliteMode & 0b00000100) || (satelliteMode & 0b00010000)) {
118         std::vector<std::pair<std::string, std::string>> vec = {
119             std::pair<std::string, std::string>("USEDMODEM", "satemodem")
120         };
121         AudioStandard::AudioSystemManager::GetInstance()->SetExtraParameters("mmi",
122             vec);
123     }
124 }
125 
HandleSatelliteCallStateUpdate(sptr<CallBase> & call,TelCallState priorState,TelCallState nextState)126 void SatelliteCallControl::HandleSatelliteCallStateUpdate(sptr<CallBase> &call,
127     TelCallState priorState, TelCallState nextState)
128 {
129     bool isDisconnected = (nextState == TelCallState::CALL_STATUS_DISCONNECTED
130         || nextState == TelCallState::CALL_STATUS_DISCONNECTING) ? true : false;
131     if (isDisconnected && IsShowDialog()) {
132         RemoveCallCountDownEventHandlerTask();
133         SetShowDialog(false);
134     }
135     if (nextState == TelCallState::CALL_STATUS_INCOMING || nextState == TelCallState::CALL_STATUS_WAITING) {
136         SetUsedModem();
137     }
138     if (nextState == TelCallState::CALL_STATUS_ACTIVE) {
139         if (GetSatcommTempLevel() == SatCommTempLevel::TEMP_LEVEL_HIGH &&
140             CallObjectManager::HasSatelliteCallExist()) {
141             DelayedSingleton<CallDialog>::GetInstance()->DialogConnectExtension("SATELLITE_CALL_DISCONNECT");
142             SetShowDialog(true);
143         }
144     }
145     std::string model = OHOS::system::GetParameter("const.build.product", "0");
146     if (model != "") {
147         return;
148     }
149     if (nextState == TelCallState::CALL_STATUS_ACTIVE && !IsShowDialog()) {
150         SetSatelliteCallDurationProcessing();
151     }
152     if (isDisconnected) {
153         RemoveCallDurationEventHandlerTask();
154     }
155 }
156 
IsShowDialog()157 bool SatelliteCallControl::IsShowDialog()
158 {
159     return isShowingDialog_;
160 }
161 
SetShowDialog(bool isShowDialog)162 void SatelliteCallControl::SetShowDialog(bool isShowDialog)
163 {
164     isShowingDialog_ = isShowDialog;
165     if (isShowingDialog_) {
166         SetSatelliteCallCountDownProcessing();
167     }
168 }
169 
PublishSatelliteConnectEvent()170 void SatelliteCallControl::PublishSatelliteConnectEvent()
171 {
172     AAFwk::Want want;
173     want.SetAction("event.custom.satellite.ACTION_SATELLITE_CONNECT");
174     EventFwk::CommonEventData data;
175     data.SetWant(want);
176     EventFwk::CommonEventPublishInfo publishInfo;
177     std::vector<std::string> callPermissions;
178     callPermissions.emplace_back(Permission::SET_TELEPHONY_STATE);
179     publishInfo.SetSubscriberPermissions(callPermissions);
180     bool result = EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
181     TELEPHONY_LOGI("publish satellite call event result : %{public}d.", result);
182 }
183 
SetSatelliteCallDurationProcessing()184 int32_t SatelliteCallControl::SetSatelliteCallDurationProcessing()
185 {
186     TELEPHONY_LOGI("start satellite call duration task");
187     auto runner = AppExecFwk::EventRunner::Create("start_satellite_call_duration_dialog");
188     if (CallDurationLimitHandler_ == nullptr) {
189         CallDurationLimitHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
190     }
191     if (CallDurationLimitHandler_ == nullptr) {
192         TELEPHONY_LOGI("CallDurationLimitHandler_ is nullptr");
193         return TELEPHONY_ERROR;
194     }
195     auto task = [this]() {
196         if (!IsShowDialog()) {
197             DelayedSingleton<CallDialog>::GetInstance()->DialogConnectExtension("SATELLITE_CALL_DURATION_LIMIT");
198             SetShowDialog(true);
199         }
200     };
201     CallDurationLimitHandler_->RemoveTask("start_satellite_call_duration_dialog");
202     bool ret = CallDurationLimitHandler_->PostTask(task, "start_satellite_call_duration_dialog", DELAYED_TIME);
203     if (ret) {
204         TELEPHONY_LOGI("start satellite call duration task failed");
205         return TELEPHONY_ERROR;
206     }
207     return TELEPHONY_SUCCESS;
208 }
209 
SetSatelliteCallCountDownProcessing()210 int32_t SatelliteCallControl::SetSatelliteCallCountDownProcessing()
211 {
212         TELEPHONY_LOGI("start satellite call count down task");
213     auto runner = AppExecFwk::EventRunner::Create("satellite_call_count_down");
214     if (CallCountDownHandler_ == nullptr) {
215         CallCountDownHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
216     }
217     if (CallCountDownHandler_ == nullptr) {
218         TELEPHONY_LOGI("CallCountDownHandler_ is nullptr");
219         return TELEPHONY_ERROR;
220     }
221     auto task = []() {
222         std::list<int32_t> satelliteCallIdList;
223         int32_t ret = CallObjectManager::GetSatelliteCallList(satelliteCallIdList);
224         if (ret != TELEPHONY_SUCCESS) {
225             TELEPHONY_LOGI("GetSatelliteCallList failed");
226             return;
227         }
228         for (auto satelliteCallId : satelliteCallIdList) {
229             sptr<CallBase> satelliteCall = CallObjectManager::GetOneCallObject(satelliteCallId);
230             if (satelliteCall->GetTelCallState() == TelCallState::CALL_STATUS_INCOMING ||
231                 satelliteCall->GetTelCallState() == TelCallState::CALL_STATUS_WAITING) {
232                 DelayedSingleton<CallControlManager>::GetInstance()->RejectCall(satelliteCallId,
233                     true, u"satellitecalldurationlimit");
234             } else if (satelliteCall->GetTelCallState() == TelCallState::CALL_STATUS_ACTIVE) {
235                 DelayedSingleton<CallControlManager>::GetInstance()->HangUpCall(satelliteCallId);
236             }
237         }
238     };
239     CallCountDownHandler_->RemoveTask("satellite_call_count_down");
240     bool ret = CallCountDownHandler_->PostTask(task, "satellite_call_count_down", COUNT_DOWN_TIME);
241     if (ret) {
242         TELEPHONY_LOGI("start satellite call count down task failed");
243         return TELEPHONY_ERROR;
244     }
245     return TELEPHONY_SUCCESS;
246 }
247 
RemoveCallDurationEventHandlerTask()248 void SatelliteCallControl::RemoveCallDurationEventHandlerTask()
249 {
250     if (CallDurationLimitHandler_ != nullptr) {
251         CallDurationLimitHandler_->RemoveTask("start_satellite_call_duration_dialog");
252     }
253 }
254 
RemoveCallCountDownEventHandlerTask()255 void SatelliteCallControl::RemoveCallCountDownEventHandlerTask()
256 {
257     if (CallCountDownHandler_ != nullptr) {
258         CallCountDownHandler_->RemoveTask("satellite_call_count_down");
259     }
260 }
261 } // namespace Telephony
262 } // namespace OHOSss