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 "call_dialog.h"
17 
18 #include "extension_manager_client.h"
19 #include "ipc_skeleton.h"
20 #include "nlohmann/json.hpp"
21 #include "telephony_log_wrapper.h"
22 #include <securec.h>
23 
24 namespace OHOS {
25 namespace Telephony {
26 constexpr int32_t DEFAULT_USER_ID = -1;
27 const int32_t SOURCE_SCREENLOCKED = 2;
28 
DialogConnectExtension(const std::string & dialogReason)29 bool CallDialog::DialogConnectExtension(const std::string &dialogReason)
30 {
31     return DialogConnectExtension(dialogReason, -1);
32 }
33 
DialogConnectExtension(const std::string & dialogReason,int32_t slotId)34 bool CallDialog::DialogConnectExtension(const std::string &dialogReason, int32_t slotId)
35 {
36     std::string commandStr = BuildStartCommand(dialogReason, slotId);
37     AAFwk::Want want;
38     std::string bundleName = "com.ohos.sceneboard";
39     std::string abilityName = "com.ohos.sceneboard.systemdialog";
40     want.SetElementName(bundleName, abilityName);
41     bool connectResult = false;
42     if (dialogReason.find("SATELLITE") != std::string::npos) {
43         connectResult = CallSettingDialogConnectExtensionAbility(want, commandStr);
44     } else {
45         connectResult = DialogConnectExtensionAbility(want, commandStr);
46     }
47     if (!connectResult) {
48         TELEPHONY_LOGE("DialogConnectExtensionAbility failed!");
49         return false;
50     }
51     return true;
52 }
53 
DialogConnectExtensionAbility(const AAFwk::Want & want,const std::string commandStr)54 bool CallDialog::DialogConnectExtensionAbility(const AAFwk::Want &want, const std::string commandStr)
55 {
56     TELEPHONY_LOGI("DialogConnectExtensionAbility start");
57     sptr<CallAbilityConnection> connection =
58         sptr<CallAbilityConnection> (new (std::nothrow) CallAbilityConnection(commandStr));
59     if (connection == nullptr) {
60         TELEPHONY_LOGE("connection is nullptr");
61         return false;
62     }
63     std::string identity = IPCSkeleton::ResetCallingIdentity();
64     auto connectResult = AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want,
65         connection, nullptr, DEFAULT_USER_ID);
66     IPCSkeleton::SetCallingIdentity(identity);
67     if (connectResult != 0) {
68         TELEPHONY_LOGE("ConnectServiceExtensionAbility Failed!");
69         return false;
70     }
71     return true;
72 }
73 
CallSettingDialogConnectExtensionAbility(const AAFwk::Want & want,const std::string commandStr)74 bool CallDialog::CallSettingDialogConnectExtensionAbility(const AAFwk::Want &want,
75     const std::string commandStr)
76 {
77     TELEPHONY_LOGI("CallSettingDialogConnectExtensionAbility start");
78     sptr<CallSettingAbilityConnection> callSettingConnection = sptr<CallSettingAbilityConnection> (new (std::nothrow)
79         CallSettingAbilityConnection(commandStr));
80     if (callSettingConnection == nullptr) {
81         TELEPHONY_LOGE("connection is nullptr");
82         return false;
83     }
84     std::string identity = IPCSkeleton::ResetCallingIdentity();
85     auto connectResult = AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want,
86         callSettingConnection, nullptr, DEFAULT_USER_ID);
87     IPCSkeleton::SetCallingIdentity(identity);
88     if (connectResult != 0) {
89         TELEPHONY_LOGE("ConnectServiceExtensionAbility Failed!");
90         return false;
91     }
92     return true;
93 }
94 
BuildStartCommand(const std::string & dialogReason,int32_t slotId)95 std::string CallDialog::BuildStartCommand(const std::string &dialogReason, int32_t slotId)
96 {
97     nlohmann::json root;
98     std::string uiExtensionType = "sysDialog/common";
99     root["ability.want.params.uiExtensionType"] = uiExtensionType;
100     root["sysDialogZOrder"] = SOURCE_SCREENLOCKED;
101     root["dialogReason"] = dialogReason;
102     root["slotId"] = slotId;
103     std::string startCommand = root.dump();
104     TELEPHONY_LOGI("startCommand is: %{public}s", startCommand.c_str());
105     return startCommand;
106 }
107 
DialogConnectPrivpacyModeExtension(const std::string & dialogReason,std::u16string & number,int32_t & accountId,int32_t & videoState,int32_t & dialType,int32_t & dialScene,int32_t & callType,bool isVideo)108 bool CallDialog::DialogConnectPrivpacyModeExtension(const std::string &dialogReason, std::u16string &number,
109     int32_t &accountId, int32_t &videoState, int32_t &dialType, int32_t &dialScene, int32_t &callType, bool isVideo)
110 {
111     std::string commandStr = BuildStartPrivpacyModeCommand(dialogReason, number,
112     accountId, videoState, dialType, dialScene, callType, isVideo);
113     AAFwk::Want want;
114     std::string bundleName = "com.ohos.sceneboard";
115     std::string abilityName = "com.ohos.sceneboard.systemdialog";
116     want.SetElementName(bundleName, abilityName);
117     bool connectResult = CallSettingDialogConnectExtensionAbility(want, commandStr);
118     if (!connectResult) {
119         TELEPHONY_LOGE("CallSettingDialogConnectExtensionAbility failed!");
120         return false;
121     }
122     return true;
123 }
124 
DialogConnectAnswerPrivpacyModeExtension(const std::string & dialogReason,int32_t & callId,int32_t & videoState,bool isVideo)125 bool CallDialog::DialogConnectAnswerPrivpacyModeExtension(const std::string &dialogReason,
126     int32_t &callId, int32_t &videoState, bool isVideo)
127 {
128     std::string commandStr = BuildStartAnswerPrivpacyModeCommand(dialogReason, callId, videoState, isVideo);
129     AAFwk::Want want;
130     std::string bundleName = "com.ohos.sceneboard";
131     std::string abilityName = "com.ohos.sceneboard.systemdialog";
132     want.SetElementName(bundleName, abilityName);
133     bool connectResult = CallSettingDialogConnectExtensionAbility(want, commandStr);
134     if (!connectResult) {
135         TELEPHONY_LOGE("CallSettingDialogConnectExtensionAbility failed!");
136         return false;
137     }
138     return true;
139 }
140 
BuildStartPrivpacyModeCommand(const std::string & dialogReason,std::u16string & number,int32_t & accountId,int32_t & videoState,int32_t & dialType,int32_t & dialScene,int32_t & callType,bool isVideo)141 std::string CallDialog::BuildStartPrivpacyModeCommand(const std::string &dialogReason, std::u16string &number,
142     int32_t &accountId, int32_t &videoState, int32_t &dialType, int32_t &dialScene, int32_t &callType, bool isVideo)
143 {
144     nlohmann::json root;
145     std::string uiExtensionType = "sysDialog/common";
146     root["ability.want.params.uiExtensionType"] = uiExtensionType;
147     root["sysDialogZOrder"] = SOURCE_SCREENLOCKED;
148     root["dialogReason"] = dialogReason;
149     root["number"] = Str16ToStr8(number);
150     root["accountId"] = accountId;
151     root["videoState"] = videoState;
152     root["dialType"] = dialType;
153     root["dialScene"] = dialScene;
154     root["callType"] = callType;
155     root["isAnswer"] = false;
156     root["isVideo"] = isVideo;
157     std::string startCommand = root.dump();
158     TELEPHONY_LOGI("startCommand is: %{public}s", startCommand.c_str());
159     return startCommand;
160 }
161 
BuildStartAnswerPrivpacyModeCommand(const std::string & dialogReason,int32_t & callId,int32_t & videoState,bool isVideo)162 std::string CallDialog::BuildStartAnswerPrivpacyModeCommand(const std::string &dialogReason,
163     int32_t &callId, int32_t &videoState, bool isVideo)
164 {
165     nlohmann::json root;
166     std::string uiExtensionType = "sysDialog/common";
167     root["ability.want.params.uiExtensionType"] = uiExtensionType;
168     root["sysDialogZOrder"] = SOURCE_SCREENLOCKED;
169     root["dialogReason"] = dialogReason;
170     root["callId"] = callId;
171     root["videoState"] = videoState;
172     root["isAnswer"] = true;
173     root["isVideo"] = isVideo;
174     std::string startCommand = root.dump();
175     TELEPHONY_LOGI("startCommand is: %{public}s", startCommand.c_str());
176     return startCommand;
177 }
178 } // namespace Telephony
179 } // namespace OHOS