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