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