FreeRDP
Loading...
Searching...
No Matches
SessionDialogs.java
1/*
2 Android Session Auth Dialogs
3
4 Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5 Copyright 2026 Ibrahim Sevinc <ibrahim.sevinc.mail@gmail.com>
6
7 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
8 If a copy of the MPL was not distributed with this file, You can obtain one at
9 http://mozilla.org/MPL/2.0/.
10 */
11
12package com.freerdp.freerdpcore.presentation;
13
14import android.app.Activity;
15import android.app.AlertDialog;
16import android.content.Intent;
17import android.os.Handler;
18import android.os.Looper;
19import android.util.TypedValue;
20import android.view.Gravity;
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.EditText;
24import android.widget.LinearLayout;
25import android.widget.ProgressBar;
26import android.widget.TextView;
27
28import com.freerdp.freerdpcore.R;
29import com.freerdp.freerdpcore.services.LibFreeRDP;
30
37public class SessionDialogs
38{
40 public interface OnUserCancelListener
41 {
42 void onUserCancel();
43 }
44
46 public interface OnConnectCancelListener
47 {
48 void onConnectCancel();
49 }
50
51 private final Activity activity;
52 private final OnUserCancelListener cancelListener;
53 private final Handler mainHandler = new Handler(Looper.getMainLooper());
54
55 private final AlertDialog dlgVerifyCertificate;
56 private final AlertDialog dlgUserCredentials;
57 private final AlertDialog dlgExperimental;
58 private final View userCredView;
59
60 private AlertDialog progressDialog;
61
62 private boolean callbackDialogResult;
63
64 public SessionDialogs(Activity activity, OnUserCancelListener cancelListener)
65 {
66 this.activity = activity;
67 this.cancelListener = cancelListener;
68
69 dlgVerifyCertificate = new AlertDialog.Builder(activity)
70 .setTitle(R.string.dlg_title_verify_certificate)
71 .setPositiveButton(android.R.string.yes,
72 (dialog, which) -> {
73 callbackDialogResult = true;
74 synchronized (dialog)
75 {
76 dialog.notify();
77 }
78 })
79 .setNegativeButton(android.R.string.no,
80 (dialog, which) -> {
81 callbackDialogResult = false;
82 notifyCancel();
83 synchronized (dialog)
84 {
85 dialog.notify();
86 }
87 })
88 .setCancelable(false)
89 .create();
90
91 userCredView = activity.getLayoutInflater().inflate(R.layout.credentials, null, true);
92 dlgUserCredentials = new AlertDialog.Builder(activity)
93 .setView(userCredView)
94 .setTitle(R.string.dlg_title_credentials)
95 .setPositiveButton(android.R.string.ok,
96 (dialog, which) -> {
97 callbackDialogResult = true;
98 synchronized (dialog)
99 {
100 dialog.notify();
101 }
102 })
103 .setNegativeButton(android.R.string.cancel,
104 (dialog, which) -> {
105 callbackDialogResult = false;
106 notifyCancel();
107 synchronized (dialog)
108 {
109 dialog.notify();
110 }
111 })
112 .setCancelable(false)
113 .create();
114
115 dlgExperimental = new AlertDialog.Builder(activity)
116 .setTitle(R.string.dlg_title_experimental_feature)
117 .setPositiveButton(R.string.menu_app_settings,
118 (dialog, which) -> {
119 openExperimentalSettings();
120 synchronized (dialog)
121 {
122 dialog.notify();
123 }
124 })
125 .setNegativeButton(android.R.string.cancel,
126 (dialog, which) -> {
127 notifyCancel();
128 synchronized (dialog)
129 {
130 dialog.notify();
131 }
132 })
133 .setCancelable(false)
134 .create();
135 }
136
141 public boolean promptCredentials(StringBuilder username, StringBuilder domain,
142 StringBuilder password)
143 {
144 callbackDialogResult = false;
145
146 ((EditText)userCredView.findViewById(R.id.editTextUsername)).setText(username);
147 ((EditText)userCredView.findViewById(R.id.editTextDomain)).setText(domain);
148 ((EditText)userCredView.findViewById(R.id.editTextPassword)).setText(password);
149
150 showOnUiThread(dlgUserCredentials);
151
152 try
153 {
154 synchronized (dlgUserCredentials)
155 {
156 dlgUserCredentials.wait();
157 }
158 }
159 catch (InterruptedException e)
160 {
161 }
162
163 username.setLength(0);
164 domain.setLength(0);
165 password.setLength(0);
166
167 username.append(
168 ((EditText)userCredView.findViewById(R.id.editTextUsername)).getText().toString());
169 domain.append(
170 ((EditText)userCredView.findViewById(R.id.editTextDomain)).getText().toString());
171 password.append(
172 ((EditText)userCredView.findViewById(R.id.editTextPassword)).getText().toString());
173
174 return callbackDialogResult;
175 }
176
180 public int verifyCertificate(String host, long port, String subject, String issuer,
181 String fingerprint, long flags)
182 {
183 String msg = activity.getResources().getString(R.string.dlg_msg_verify_certificate);
184 String type = "RDP-Server";
185 if ((flags & LibFreeRDP.VERIFY_CERT_FLAG_GATEWAY) != 0)
186 type = "RDP-Gateway";
187 if ((flags & LibFreeRDP.VERIFY_CERT_FLAG_REDIRECT) != 0)
188 type = "RDP-Redirect";
189 msg += "\n\n" + type + ": " + host + ":" + port;
190 msg += "\n\nSubject: " + subject + "\nIssuer: " + issuer;
191 if ((flags & LibFreeRDP.VERIFY_CERT_FLAG_FP_IS_PEM) != 0)
192 msg += "\nCertificate: " + fingerprint;
193 else
194 msg += "\nFingerprint: " + fingerprint;
195
196 return showVerifyDialog(msg);
197 }
198
202 public int verifyChangedCertificate(String host, long port, String subject, String issuer,
203 String fingerprint, long flags)
204 {
205 // The "changed" variant currently shows the same information as the
206 // regular verify dialog (matches prior behaviour).
207 return verifyCertificate(host, port, subject, issuer, fingerprint, flags);
208 }
209
211 public void showExperimentalBlocked(String featureName)
212 {
213 dlgExperimental.setMessage(
214 activity.getString(R.string.dlg_msg_experimental_feature, featureName));
215
216 showOnUiThread(dlgExperimental);
217
218 try
219 {
220 synchronized (dlgExperimental)
221 {
222 dlgExperimental.wait();
223 }
224 }
225 catch (InterruptedException e)
226 {
227 }
228 }
229
230 private void openExperimentalSettings()
231 {
232 activity.startActivity(new Intent(activity, ApplicationSettingsActivity.class));
233 }
234
235 private int showVerifyDialog(String msg)
236 {
237 callbackDialogResult = false;
238 dlgVerifyCertificate.setMessage(msg);
239
240 showOnUiThread(dlgVerifyCertificate);
241
242 try
243 {
244 synchronized (dlgVerifyCertificate)
245 {
246 dlgVerifyCertificate.wait();
247 }
248 }
249 catch (InterruptedException e)
250 {
251 }
252
253 return callbackDialogResult ? 1 : 0;
254 }
255
256 private void showOnUiThread(final AlertDialog dialog)
257 {
258 mainHandler.post(dialog::show);
259 }
260
261 private void notifyCancel()
262 {
263 if (cancelListener != null)
264 cancelListener.onUserCancel();
265 }
266
271 public void showProgress(String title, final OnConnectCancelListener listener)
272 {
273 dismissProgress();
274
275 int pad = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24,
276 activity.getResources().getDisplayMetrics());
277 LinearLayout content = new LinearLayout(activity);
278 content.setOrientation(LinearLayout.HORIZONTAL);
279 content.setPadding(pad, pad, pad, pad);
280 content.setGravity(Gravity.CENTER_VERTICAL);
281
282 ProgressBar progressBar = new ProgressBar(activity);
283 progressBar.setIndeterminate(true);
284 content.addView(progressBar,
285 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
286 ViewGroup.LayoutParams.WRAP_CONTENT));
287
288 TextView messageView = new TextView(activity);
289 messageView.setText(R.string.dlg_msg_connecting);
290 LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
291 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
292 textParams.leftMargin = pad;
293 content.addView(messageView, textParams);
294
295 progressDialog = new AlertDialog.Builder(activity)
296 .setTitle(title)
297 .setView(content)
298 .setNegativeButton(android.R.string.cancel,
299 (dialog, which) -> {
300 if (listener != null)
301 listener.onConnectCancel();
302 })
303 .setCancelable(false)
304 .create();
305 progressDialog.show();
306 }
307
309 public void dismissProgress()
310 {
311 if (progressDialog != null)
312 {
313 progressDialog.dismiss();
314 progressDialog = null;
315 }
316 }
317}
void showProgress(String title, final OnConnectCancelListener listener)
boolean promptCredentials(StringBuilder username, StringBuilder domain, StringBuilder password)
int verifyCertificate(String host, long port, String subject, String issuer, String fingerprint, long flags)
int verifyChangedCertificate(String host, long port, String subject, String issuer, String fingerprint, long flags)