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.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;
26import java.util.ArrayList;
28class QtNativeDialogHelper
30 static native
void dialogResult(
long handler,
int buttonID);
33class ButtonStruct
implements View.OnClickListener
35 ButtonStruct(QtMessageDialogHelper dialog,
int id,
String text)
39 m_text = Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY);
41 final QtMessageDialogHelper m_dialog;
42 private final int m_id;
46 public void onClick(View
view) {
47 QtNativeDialogHelper.dialogResult(m_dialog.handler(), m_id);
51class QtMessageDialogHelper
53 QtMessageDialogHelper(Activity activity)
59 void setStandardIcon(
int icon)
61 m_standardIcon = icon;
65 private Drawable getIconDrawable()
67 if (m_standardIcon == 0)
71 switch (m_standardIcon)
74 return m_activity.getResources().getDrawable(android.R.drawable.ic_dialog_info,
75 m_activity.getTheme());
77 return m_activity.getResources().getDrawable(android.R.drawable.stat_sys_warning,
78 m_activity.getTheme());
80 return m_activity.getResources().getDrawable(android.R.drawable.ic_dialog_alert,
81 m_activity.getTheme());
83 return m_activity.getResources().getDrawable(android.R.drawable.ic_menu_help,
84 m_activity.getTheme());
92 m_title = Html.fromHtml(
title, Html.FROM_HTML_MODE_LEGACY);
98 m_text = Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY);
102 void setInformativeText(
String informativeText)
104 m_informativeText = Html.fromHtml(informativeText, Html.FROM_HTML_MODE_LEGACY);
108 void setDetailedText(
String text)
110 m_detailedText = Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY);
114 void addButton(
int id,
String text)
116 if (m_buttonsList ==
null)
117 m_buttonsList =
new ArrayList<>();
118 m_buttonsList.add(
new ButtonStruct(
this,
id, text));
121 private Drawable getStyledDrawable(
int id)
123 int[]
attrs = {
id };
125 TypedArray
a = m_theme.obtainStyledAttributes(
attrs);
127 d =
a.getDrawable(0);
136 void show(
long handler)
139 m_activity.runOnUiThread(() -> {
140 if (m_dialog !=
null && m_dialog.isShowing())
143 m_dialog =
new AlertDialog.Builder(m_activity).create();
146 m_theme =
window.getContext().getTheme();
148 Log.w(QtTAG,
"show(): cannot set theme from null window!");
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);
159 View lastView =
null;
160 View.OnLongClickListener copyText =
view -> {
161 TextView tv = (TextView)
view;
163 ClipboardManager cm = (ClipboardManager) m_activity.getSystemService(
165 cm.setPrimaryClip(ClipData.newPlainText(tv.getText(), tv.getText()));
171 TextView
view =
new TextView(m_activity);
173 view.setOnLongClickListener(copyText);
174 view.setLongClickable(
true);
176 view.setText(m_text);
177 view.setTextAppearance(android.R.style.TextAppearance_Medium);
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);
188 if (m_informativeText !=
null)
190 TextView
view=
new TextView(m_activity);
192 view.setOnLongClickListener(copyText);
193 view.setLongClickable(
true);
195 view.setText(m_informativeText);
196 view.setTextAppearance(android.R.style.TextAppearance_Medium);
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());
205 layout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
210 if (m_detailedText !=
null)
212 TextView
view=
new TextView(m_activity);
214 view.setOnLongClickListener(copyText);
215 view.setLongClickable(
true);
217 view.setText(m_detailedText);
218 view.setTextAppearance(android.R.style.TextAppearance_Small);
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());
227 layout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
232 if (m_buttonsList !=
null)
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)
242 bv =
new Button(m_activity,
null, android.R.attr.borderlessButtonStyle);
243 }
catch (Exception e) {
244 bv =
new Button(m_activity);
248 bv.setText(
button.m_text);
249 bv.setOnClickListener(
button);
252 View spacer =
new View(m_activity);
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) {
262 LinearLayout.LayoutParams
layout =
new LinearLayout.LayoutParams(
263 RelativeLayout.LayoutParams.MATCH_PARENT,
264 RelativeLayout.LayoutParams.WRAP_CONTENT, 1.0f);
265 buttonsLayout.addView(bv,
layout);
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());
281 relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
282 dialogLayout.addView(horizontalDivider, relativeParams);
283 lastView = horizontalDivider;
284 }
catch (Exception e) {
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());
294 relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
295 relativeParams.setMargins(2, 0, 2, 0);
296 dialogLayout.addView(buttonsLayout, relativeParams);
298 scrollView.addView(dialogLayout);
299 m_dialog.setView(scrollView);
307 m_activity.runOnUiThread(() -> {
308 if (m_dialog !=
null && m_dialog.isShowing())
324 m_informativeText =
null;
325 m_detailedText =
null;
326 m_buttonsList =
null;
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;
void show()
Shows the window.
Q_CORE_EXPORT QtJniTypes::Activity activity()
static struct AttrInfo attrs[]
GLboolean GLboolean GLboolean GLboolean a
QGraphicsGridLayout * layout