5package org.qtproject.qt.android;
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;
27import java.util.ArrayList;
29class QtNativeDialogHelper
31 static native
void dialogResult(
long handler,
int buttonID);
34class ButtonStruct
implements View.OnClickListener
36 ButtonStruct(QtMessageDialogHelper
dialog,
int id, String
text)
40 m_text = Html.fromHtml(
text);
42 QtMessageDialogHelper m_dialog;
43 private final int m_id;
48 QtNativeDialogHelper.dialogResult(m_dialog.handler(), m_id);
52class QtMessageDialogHelper
55 public QtMessageDialogHelper(Activity activity)
61 public void setStandardIcon(
int icon)
63 m_standardIcon =
icon;
69 if (m_standardIcon == 0)
73 switch (m_standardIcon)
76 return m_activity.getResources().getDrawable(android.R.drawable.ic_dialog_info,
77 m_activity.getTheme());
79 return m_activity.getResources().getDrawable(android.R.drawable.stat_sys_warning,
80 m_activity.getTheme());
82 return m_activity.getResources().getDrawable(android.R.drawable.ic_dialog_alert,
83 m_activity.getTheme());
85 return m_activity.getResources().getDrawable(android.R.drawable.ic_menu_help,
86 m_activity.getTheme());
92 public void setTile(String
title)
94 m_title = Html.fromHtml(
title);
98 public void setText(String
text)
100 m_text = Html.fromHtml(
text);
104 public void setInformativeText(String informativeText)
106 m_informativeText = Html.fromHtml(informativeText);
110 public void setDetailedText(String
text)
112 m_detailedText = Html.fromHtml(
text);
116 public void addButton(
int id, String
text)
118 if (m_buttonsList ==
null)
119 m_buttonsList =
new ArrayList<>();
120 m_buttonsList.add(
new ButtonStruct(
this,
id,
text));
123 private Drawable getStyledDrawable(
int id)
125 int[]
attrs = {
id };
126 final TypedArray
a = m_theme.obtainStyledAttributes(
attrs);
133 public void show(
long handler)
136 m_activity.runOnUiThread(() -> {
137 if (m_dialog !=
null && m_dialog.isShowing())
140 m_dialog =
new AlertDialog.Builder(m_activity).create();
143 m_theme =
window.getContext().getTheme();
145 Log.w(QtTAG,
"show(): cannot set theme from null window!");
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);
156 View lastView =
null;
157 View.OnLongClickListener copyText =
view -> {
158 TextView tv = (TextView)
view;
160 ClipboardManager cm = (ClipboardManager) m_activity.getSystemService(
161 Context.CLIPBOARD_SERVICE);
162 cm.setPrimaryClip(
ClipData.newPlainText(tv.getText(), tv.getText()));
168 TextView
view =
new TextView(m_activity);
170 view.setOnLongClickListener(copyText);
171 view.setLongClickable(
true);
173 view.setText(m_text);
174 view.setTextAppearance(android.R.style.TextAppearance_Medium);
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);
185 if (m_informativeText !=
null)
187 TextView
view=
new TextView(m_activity);
189 view.setOnLongClickListener(copyText);
190 view.setLongClickable(
true);
192 view.setText(m_informativeText);
193 view.setTextAppearance(android.R.style.TextAppearance_Medium);
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());
202 layout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
207 if (m_detailedText !=
null)
209 TextView
view=
new TextView(m_activity);
211 view.setOnLongClickListener(copyText);
212 view.setLongClickable(
true);
214 view.setText(m_detailedText);
215 view.setTextAppearance(android.R.style.TextAppearance_Small);
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());
224 layout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
229 if (m_buttonsList !=
null)
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)
239 bv =
new Button(m_activity,
null, android.R.attr.borderlessButtonStyle);
240 }
catch (Exception e) {
241 bv =
new Button(m_activity);
245 bv.setText(
button.m_text);
246 bv.setOnClickListener(
button);
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) {
259 LinearLayout.LayoutParams
layout =
new LinearLayout.LayoutParams(
260 RelativeLayout.LayoutParams.MATCH_PARENT,
261 RelativeLayout.LayoutParams.WRAP_CONTENT, 1.0f);
262 buttonsLayout.addView(bv,
layout);
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());
278 relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
279 dialogLayout.addView(horizontalDivider, relativeParams);
280 lastView = horizontalDivider;
281 }
catch (Exception e) {
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());
291 relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
292 relativeParams.setMargins(2, 0, 2, 0);
293 dialogLayout.addView(buttonsLayout, relativeParams);
295 scrollView.addView(dialogLayout);
296 m_dialog.setView(scrollView);
304 m_activity.runOnUiThread(() -> {
305 if (m_dialog !=
null && m_dialog.isShowing())
311 public long handler()
321 m_informativeText =
null;
322 m_detailedText =
null;
323 m_buttonsList =
null;
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;
[Window class with invokable method]
Q_CORE_EXPORT QtJniTypes::Activity activity()
static struct AttrInfo attrs[]
GLboolean GLboolean GLboolean GLboolean a
[7]
QFileDialog dialog(this)
[1]