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.os.Handler;
17import android.os.Looper;
18import android.util.TypedValue;
19import android.view.Gravity;
20import android.view.View;
21import android.view.ViewGroup;
22import android.widget.EditText;
23import android.widget.LinearLayout;
24import android.widget.ProgressBar;
25import android.widget.TextView;
26
27import com.freerdp.freerdpcore.R;
28import com.freerdp.freerdpcore.services.LibFreeRDP;
29
36public class SessionDialogs
37{
39 public interface OnUserCancelListener
40 {
41 void onUserCancel();
42 }
43
45 public interface OnConnectCancelListener
46 {
47 void onConnectCancel();
48 }
49
50 private final Activity activity;
51 private final OnUserCancelListener cancelListener;
52 private final Handler mainHandler = new Handler(Looper.getMainLooper());
53
54 private final AlertDialog dlgVerifyCertificate;
55 private final AlertDialog dlgUserCredentials;
56 private final View userCredView;
57
58 private AlertDialog progressDialog;
59
60 private boolean callbackDialogResult;
61
62 public SessionDialogs(Activity activity, OnUserCancelListener cancelListener)
63 {
64 this.activity = activity;
65 this.cancelListener = cancelListener;
66
67 dlgVerifyCertificate = new AlertDialog.Builder(activity)
68 .setTitle(R.string.dlg_title_verify_certificate)
69 .setPositiveButton(android.R.string.yes,
70 (dialog, which) -> {
71 callbackDialogResult = true;
72 synchronized (dialog)
73 {
74 dialog.notify();
75 }
76 })
77 .setNegativeButton(android.R.string.no,
78 (dialog, which) -> {
79 callbackDialogResult = false;
80 notifyCancel();
81 synchronized (dialog)
82 {
83 dialog.notify();
84 }
85 })
86 .setCancelable(false)
87 .create();
88
89 userCredView = activity.getLayoutInflater().inflate(R.layout.credentials, null, true);
90 dlgUserCredentials = new AlertDialog.Builder(activity)
91 .setView(userCredView)
92 .setTitle(R.string.dlg_title_credentials)
93 .setPositiveButton(android.R.string.ok,
94 (dialog, which) -> {
95 callbackDialogResult = true;
96 synchronized (dialog)
97 {
98 dialog.notify();
99 }
100 })
101 .setNegativeButton(android.R.string.cancel,
102 (dialog, which) -> {
103 callbackDialogResult = false;
104 notifyCancel();
105 synchronized (dialog)
106 {
107 dialog.notify();
108 }
109 })
110 .setCancelable(false)
111 .create();
112 }
113
118 public boolean promptCredentials(StringBuilder username, StringBuilder domain,
119 StringBuilder password)
120 {
121 callbackDialogResult = false;
122
123 ((EditText)userCredView.findViewById(R.id.editTextUsername)).setText(username);
124 ((EditText)userCredView.findViewById(R.id.editTextDomain)).setText(domain);
125 ((EditText)userCredView.findViewById(R.id.editTextPassword)).setText(password);
126
127 showOnUiThread(dlgUserCredentials);
128
129 try
130 {
131 synchronized (dlgUserCredentials)
132 {
133 dlgUserCredentials.wait();
134 }
135 }
136 catch (InterruptedException e)
137 {
138 }
139
140 username.setLength(0);
141 domain.setLength(0);
142 password.setLength(0);
143
144 username.append(
145 ((EditText)userCredView.findViewById(R.id.editTextUsername)).getText().toString());
146 domain.append(
147 ((EditText)userCredView.findViewById(R.id.editTextDomain)).getText().toString());
148 password.append(
149 ((EditText)userCredView.findViewById(R.id.editTextPassword)).getText().toString());
150
151 return callbackDialogResult;
152 }
153
157 public int verifyCertificate(String host, long port, String subject, String issuer,
158 String fingerprint, long flags)
159 {
160 String msg = activity.getResources().getString(R.string.dlg_msg_verify_certificate);
161 String type = "RDP-Server";
162 if ((flags & LibFreeRDP.VERIFY_CERT_FLAG_GATEWAY) != 0)
163 type = "RDP-Gateway";
164 if ((flags & LibFreeRDP.VERIFY_CERT_FLAG_REDIRECT) != 0)
165 type = "RDP-Redirect";
166 msg += "\n\n" + type + ": " + host + ":" + port;
167 msg += "\n\nSubject: " + subject + "\nIssuer: " + issuer;
168 if ((flags & LibFreeRDP.VERIFY_CERT_FLAG_FP_IS_PEM) != 0)
169 msg += "\nCertificate: " + fingerprint;
170 else
171 msg += "\nFingerprint: " + fingerprint;
172
173 return showVerifyDialog(msg);
174 }
175
179 public int verifyChangedCertificate(String host, long port, String subject, String issuer,
180 String fingerprint, long flags)
181 {
182 // The "changed" variant currently shows the same information as the
183 // regular verify dialog (matches prior behaviour).
184 return verifyCertificate(host, port, subject, issuer, fingerprint, flags);
185 }
186
187 private int showVerifyDialog(String msg)
188 {
189 callbackDialogResult = false;
190 dlgVerifyCertificate.setMessage(msg);
191
192 showOnUiThread(dlgVerifyCertificate);
193
194 try
195 {
196 synchronized (dlgVerifyCertificate)
197 {
198 dlgVerifyCertificate.wait();
199 }
200 }
201 catch (InterruptedException e)
202 {
203 }
204
205 return callbackDialogResult ? 1 : 0;
206 }
207
208 private void showOnUiThread(final AlertDialog dialog)
209 {
210 mainHandler.post(dialog::show);
211 }
212
213 private void notifyCancel()
214 {
215 if (cancelListener != null)
216 cancelListener.onUserCancel();
217 }
218
223 public void showProgress(String title, final OnConnectCancelListener listener)
224 {
225 dismissProgress();
226
227 int pad = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24,
228 activity.getResources().getDisplayMetrics());
229 LinearLayout content = new LinearLayout(activity);
230 content.setOrientation(LinearLayout.HORIZONTAL);
231 content.setPadding(pad, pad, pad, pad);
232 content.setGravity(Gravity.CENTER_VERTICAL);
233
234 ProgressBar progressBar = new ProgressBar(activity);
235 progressBar.setIndeterminate(true);
236 content.addView(progressBar,
237 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
238 ViewGroup.LayoutParams.WRAP_CONTENT));
239
240 TextView messageView = new TextView(activity);
241 messageView.setText(R.string.dlg_msg_connecting);
242 LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
243 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
244 textParams.leftMargin = pad;
245 content.addView(messageView, textParams);
246
247 progressDialog = new AlertDialog.Builder(activity)
248 .setTitle(title)
249 .setView(content)
250 .setNegativeButton(android.R.string.cancel,
251 (dialog, which) -> {
252 if (listener != null)
253 listener.onConnectCancel();
254 })
255 .setCancelable(false)
256 .create();
257 progressDialog.show();
258 }
259
261 public void dismissProgress()
262 {
263 if (progressDialog != null)
264 {
265 progressDialog.dismiss();
266 progressDialog = null;
267 }
268 }
269}
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)