Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
QtMessageDialogHelper.java
Go to the documentation of this file.
1// Copyright (C) 2016 BogDan Vatra <bogdan@kde.org>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
5package org.qtproject.qt.android;
6
7import android.app.Activity;
8import android.app.AlertDialog;
9import android.content.Context;
10import android.content.DialogInterface;
11import android.content.res.Resources;
12import android.content.res.TypedArray;
13import android.graphics.drawable.Drawable;
14import android.os.Build;
15import android.text.ClipboardManager;
16import android.text.Html;
17import android.text.Spanned;
18import android.util.TypedValue;
19import android.view.View;
20import android.widget.Button;
21import android.widget.ImageView;
22import android.widget.LinearLayout;
23import android.widget.RelativeLayout;
24import android.widget.ScrollView;
25import android.widget.TextView;
26import android.widget.Toast;
27
28import java.util.ArrayList;
29
30class QtNativeDialogHelper
31{
32 static native void dialogResult(long handler, int buttonID);
33}
34
35class ButtonStruct implements View.OnClickListener
36{
37 ButtonStruct(QtMessageDialogHelper dialog, int id, String text)
38 {
39 m_dialog = dialog;
40 m_id = id;
41 m_text = Html.fromHtml(text);
42 }
43 QtMessageDialogHelper m_dialog;
44 private int m_id;
45 Spanned m_text;
46
47 @Override
48 public void onClick(View view) {
49 QtNativeDialogHelper.dialogResult(m_dialog.handler(), m_id);
50 }
51}
52
54{
55
56 public QtMessageDialogHelper(Activity activity)
57 {
58 m_activity = activity;
59 }
60
61
62 public void setStandardIcon(int icon)
63 {
64 m_standardIcon = icon;
65
66 }
67
68 private Drawable getIconDrawable()
69 {
70 if (m_standardIcon == 0)
71 return null;
72
73 try {
74 TypedValue typedValue = new TypedValue();
75 m_theme.resolveAttribute(android.R.attr.alertDialogIcon, typedValue, true);
76 return m_activity.getResources().getDrawable(typedValue.resourceId,
77 m_activity.getTheme());
78 } catch (Exception e) {
79 e.printStackTrace();
80 }
81
82 // Information, Warning, Critical, Question
83 switch (m_standardIcon)
84 {
85 case 1: // Information
86 try {
87 return m_activity.getResources().getDrawable(android.R.drawable.ic_dialog_info,
88 m_activity.getTheme());
89 } catch (Exception e) {
90 e.printStackTrace();
91 }
92 break;
93 case 2: // Warning
94// try {
95// return Class.forName("android.R$drawable").getDeclaredField("stat_sys_warning").getInt(null);
96// } catch (Exception e) {
97// e.printStackTrace();
98// }
99// break;
100 case 3: // Critical
101 try {
102 return m_activity.getResources().getDrawable(android.R.drawable.ic_dialog_alert,
103 m_activity.getTheme());
104 } catch (Exception e) {
105 e.printStackTrace();
106 }
107 break;
108 case 4: // Question
109 try {
110 return m_activity.getResources().getDrawable(android.R.drawable.ic_menu_help,
111 m_activity.getTheme());
112 } catch (Exception e) {
113 e.printStackTrace();
114 }
115 break;
116 }
117 return null;
118 }
119
120 public void setTile(String title)
121 {
122 m_title = Html.fromHtml(title);
123 }
124
125 public void setText(String text)
126 {
127 m_text = Html.fromHtml(text);
128 }
129
130 public void setInformativeText(String informativeText)
131 {
132 m_informativeText = Html.fromHtml(informativeText);
133 }
134
135 public void setDetailedText(String text)
136 {
137 m_detailedText = Html.fromHtml(text);
138 }
139
140 public void addButton(int id, String text)
141 {
142 if (m_buttonsList == null)
143 m_buttonsList = new ArrayList<ButtonStruct>();
144 m_buttonsList.add(new ButtonStruct(this, id, text));
145 }
146
147 private Drawable getStyledDrawable(String drawable) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException
148 {
149 int[] attrs = {Class.forName("android.R$attr").getDeclaredField(drawable).getInt(null)};
150 final TypedArray a = m_theme.obtainStyledAttributes(attrs);
151 Drawable d = a.getDrawable(0);
152 a.recycle();
153 return d;
154 }
155
156
157 public void show(long handler)
158 {
159 m_handler = handler;
160 m_activity.runOnUiThread( new Runnable() {
161 @Override
162 public void run() {
163 if (m_dialog != null && m_dialog.isShowing())
164 m_dialog.dismiss();
165
166 m_dialog = new AlertDialog.Builder(m_activity).create();
167 m_theme = m_dialog.getWindow().getContext().getTheme();
168
169 if (m_title != null)
170 m_dialog.setTitle(m_title);
171 m_dialog.setOnCancelListener( new DialogInterface.OnCancelListener() {
172 @Override
173 public void onCancel(DialogInterface dialogInterface) {
174 QtNativeDialogHelper.dialogResult(handler(), -1);
175 }
176 });
177 m_dialog.setCancelable(m_buttonsList == null);
178 m_dialog.setCanceledOnTouchOutside(m_buttonsList == null);
179 m_dialog.setIcon(getIconDrawable());
180 ScrollView scrollView = new ScrollView(m_activity);
181 RelativeLayout dialogLayout = new RelativeLayout(m_activity);
182 int id = 1;
183 View lastView = null;
184 View.OnLongClickListener copyText = new View.OnLongClickListener() {
185 @Override
186 public boolean onLongClick(View view) {
187 TextView tv = (TextView)view;
188 if (tv != null) {
189 ClipboardManager cm = (android.text.ClipboardManager) m_activity.getSystemService(Context.CLIPBOARD_SERVICE);
190 cm.setText(tv.getText());
191 }
192 return true;
193 }
194 };
195 if (m_text != null)
196 {
197 TextView view = new TextView(m_activity);
198 view.setId(id++);
199 view.setOnLongClickListener(copyText);
200 view.setLongClickable(true);
201
202 view.setText(m_text);
203 view.setTextAppearance(m_activity, android.R.style.TextAppearance_Medium);
204
205 RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
206 layout.setMargins(16, 8, 16, 8);
207 layout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
208 dialogLayout.addView(view, layout);
209 lastView = view;
210 }
211
212 if (m_informativeText != null)
213 {
214 TextView view= new TextView(m_activity);
215 view.setId(id++);
216 view.setOnLongClickListener(copyText);
217 view.setLongClickable(true);
218
219 view.setText(m_informativeText);
220 view.setTextAppearance(m_activity, android.R.style.TextAppearance_Medium);
221
222 RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
223 layout.setMargins(16, 8, 16, 8);
224 if (lastView != null)
225 layout.addRule(RelativeLayout.BELOW, lastView.getId());
226 else
227 layout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
228 dialogLayout.addView(view, layout);
229 lastView = view;
230 }
231
232 if (m_detailedText != null)
233 {
234 TextView view= new TextView(m_activity);
235 view.setId(id++);
236 view.setOnLongClickListener(copyText);
237 view.setLongClickable(true);
238
239 view.setText(m_detailedText);
240 view.setTextAppearance(m_activity, android.R.style.TextAppearance_Small);
241
242 RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
243 layout.setMargins(16, 8, 16, 8);
244 if (lastView != null)
245 layout.addRule(RelativeLayout.BELOW, lastView.getId());
246 else
247 layout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
248 dialogLayout.addView(view, layout);
249 lastView = view;
250 }
251
252 if (m_buttonsList != null)
253 {
254 LinearLayout buttonsLayout = new LinearLayout(m_activity);
255 buttonsLayout.setOrientation(LinearLayout.HORIZONTAL);
256 buttonsLayout.setId(id++);
257 boolean firstButton = true;
258 for (ButtonStruct button: m_buttonsList)
259 {
260 Button bv;
261 try {
262 bv = new Button(m_activity, null, Class.forName("android.R$attr").getDeclaredField("borderlessButtonStyle").getInt(null));
263 } catch (Exception e) {
264 bv = new Button(m_activity);
265 e.printStackTrace();
266 }
267
268 bv.setText(button.m_text);
269 bv.setOnClickListener(button);
270 if (!firstButton) // first button
271 {
272 LinearLayout.LayoutParams layout = null;
273 View spacer = new View(m_activity);
274 try {
275 layout = new LinearLayout.LayoutParams(1, RelativeLayout.LayoutParams.MATCH_PARENT);
276 spacer.setBackgroundDrawable(getStyledDrawable("dividerVertical"));
277 buttonsLayout.addView(spacer, layout);
278 } catch (Exception e) {
279 e.printStackTrace();
280 }
281 }
282 LinearLayout.LayoutParams layout = null;
283 layout = new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT, 1.0f);
284 buttonsLayout.addView(bv, layout);
285 firstButton = false;
286 }
287
288 try {
289 View horizontalDevider = new View(m_activity);
290 horizontalDevider.setId(id++);
291 horizontalDevider.setBackgroundDrawable(getStyledDrawable("dividerHorizontal"));
292 RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 1);
293 relativeParams.setMargins(0, 10, 0, 0);
294 if (lastView != null) {
295 relativeParams.addRule(RelativeLayout.BELOW, lastView.getId());
296 }
297 else
298 relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
299 dialogLayout.addView(horizontalDevider, relativeParams);
300 lastView = horizontalDevider;
301 } catch (Exception e) {
302 e.printStackTrace();
303 }
304 RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
305 if (lastView != null) {
306 relativeParams.addRule(RelativeLayout.BELOW, lastView.getId());
307 }
308 else
309 relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
310 relativeParams.setMargins(2, 0, 2, 0);
311 dialogLayout.addView(buttonsLayout, relativeParams);
312 }
313 scrollView.addView(dialogLayout);
314 m_dialog.setView(scrollView);
315 m_dialog.show();
316 }
317 });
318 }
319
320 public void hide()
321 {
322 m_activity.runOnUiThread( new Runnable() {
323 @Override
324 public void run() {
325 if (m_dialog != null && m_dialog.isShowing())
326 m_dialog.dismiss();
327 reset();
328 }
329 });
330 }
331
332 public long handler()
333 {
334 return m_handler;
335 }
336
337 public void reset()
338 {
339 m_standardIcon = 0;
340 m_title = null;
341 m_text = null;
342 m_informativeText = null;
343 m_detailedText = null;
344 m_buttonsList = null;
345 m_dialog = null;
346 m_handler = 0;
347 }
348
349 private Activity m_activity;
350 private int m_standardIcon = 0;
351 private Spanned m_title, m_text, m_informativeText, m_detailedText;
352 private ArrayList<ButtonStruct> m_buttonsList;
353 private AlertDialog m_dialog;
354 private long m_handler = 0;
355 private Resources.Theme m_theme;
356}
QString text
QPushButton * button
[2]
double e
static struct AttrInfo attrs[]
Button
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint id
[7]
GLboolean reset
XID Drawable
QVBoxLayout * layout
QString title
[35]
QFileDialog dialog(this)
[1]
QQuickView * view
[0]