termux-app
99 строк · 4.2 Кб
1package com.termux.shared.interact;
2
3import android.app.Activity;
4import android.app.AlertDialog;
5import android.content.Context;
6import android.content.DialogInterface;
7import android.graphics.Color;
8import android.view.LayoutInflater;
9import android.view.View;
10import android.widget.Button;
11import android.widget.TextView;
12
13import com.termux.shared.R;
14import com.termux.shared.logger.Logger;
15
16public class MessageDialogUtils {
17
18/**
19* Show a message in a dialog
20*
21* @param context The {@link Context} to use to start the dialog. An {@link Activity} {@link Context}
22* must be passed, otherwise exceptions will be thrown.
23* @param titleText The title text of the dialog.
24* @param messageText The message text of the dialog.
25* @param onDismiss The {@link DialogInterface.OnDismissListener} to run when dialog is dismissed.
26*/
27public static void showMessage(Context context, String titleText, String messageText, final DialogInterface.OnDismissListener onDismiss) {
28showMessage(context, titleText, messageText, null, null, null, null, onDismiss);
29}
30
31/**
32* Show a message in a dialog
33*
34* @param context The {@link Context} to use to start the dialog. An {@link Activity} {@link Context}
35* must be passed, otherwise exceptions will be thrown.
36* @param titleText The title text of the dialog.
37* @param messageText The message text of the dialog.
38* @param positiveText The positive button text of the dialog.
39* @param onPositiveButton The {@link DialogInterface.OnClickListener} to run when positive button
40* is pressed.
41* @param negativeText The negative button text of the dialog. If this is {@code null}, then
42* negative button will not be shown.
43* @param onNegativeButton The {@link DialogInterface.OnClickListener} to run when negative button
44* is pressed.
45* @param onDismiss The {@link DialogInterface.OnDismissListener} to run when dialog is dismissed.
46*/
47public static void showMessage(Context context, String titleText, String messageText,
48String positiveText,
49final DialogInterface.OnClickListener onPositiveButton,
50String negativeText,
51final DialogInterface.OnClickListener onNegativeButton,
52final DialogInterface.OnDismissListener onDismiss) {
53
54AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.Theme_AppCompat_Light_Dialog);
55
56LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
57View view = inflater.inflate(R.layout.dialog_show_message, null);
58if (view != null) {
59builder.setView(view);
60
61TextView titleView = view.findViewById(R.id.dialog_title);
62if (titleView != null)
63titleView.setText(titleText);
64
65TextView messageView = view.findViewById(R.id.dialog_message);
66if (messageView != null)
67messageView.setText(messageText);
68}
69
70if (positiveText == null)
71positiveText = context.getString(android.R.string.ok);
72builder.setPositiveButton(positiveText, onPositiveButton);
73
74if (negativeText != null)
75builder.setNegativeButton(negativeText, onNegativeButton);
76
77if (onDismiss != null)
78builder.setOnDismissListener(onDismiss);
79
80AlertDialog dialog = builder.create();
81
82dialog.setOnShowListener(dialogInterface -> {
83Logger.logError("dialog");
84Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
85if (button != null)
86button.setTextColor(Color.BLACK);
87button = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
88if (button != null)
89button.setTextColor(Color.BLACK);
90});
91
92dialog.show();
93}
94
95public static void exitAppWithErrorMessage(Context context, String titleText, String messageText) {
96showMessage(context, titleText, messageText, dialog -> System.exit(0));
97}
98
99}
100