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