2
2
3
3
import android .app .Activity ;
4
4
import android .app .AlertDialog ;
5
- import android .app .AlertDialog .Builder ;
6
5
import android .app .Dialog ;
7
- import android .content .Context ;
8
6
import android .content .DialogInterface ;
9
- import android .os .Build ;
10
- import androidx .annotation .Nullable ;
11
- import androidx .fragment .app .DialogFragment ;
12
- import androidx .fragment .app .FragmentActivity ;
13
7
import android .view .View ;
14
8
15
9
import fr .free .nrw .commons .R ;
16
10
import timber .log .Timber ;
17
11
18
12
public class DialogUtil {
19
13
20
- /**
21
- * Dismisses a dialog safely.
22
- * @param activity the activity
23
- * @param dialog the dialog to be dismissed
24
- */
25
- public static void dismissSafely (@ Nullable Activity activity , @ Nullable DialogFragment dialog ) {
26
- boolean isActivityDestroyed = false ;
27
-
28
- if (activity == null || dialog == null ) {
29
- Timber .d ("dismiss called with null activity / dialog. Ignoring." );
30
- return ;
31
- }
32
-
33
- isActivityDestroyed = activity .isDestroyed ();
34
- if (activity .isFinishing () || isActivityDestroyed ) {
35
- return ;
36
- }
37
- try {
38
- dialog .dismiss ();
39
-
40
- } catch (IllegalStateException e ) {
41
- Timber .e (e , "Could not dismiss dialog." );
42
- }
43
- }
44
-
45
14
/**
46
15
* Shows a dialog safely.
47
16
* @param activity the activity
48
17
* @param dialog the dialog to be shown
49
18
*/
50
- public static void showSafely (Activity activity , Dialog dialog ) {
19
+ private static void showSafely (Activity activity , Dialog dialog ) {
51
20
if (activity == null || dialog == null ) {
52
21
Timber .d ("Show called with null activity / dialog. Ignoring." );
53
22
return ;
@@ -65,52 +34,6 @@ public static void showSafely(Activity activity, Dialog dialog) {
65
34
}
66
35
}
67
36
68
- /**
69
- * Shows a dialog safely.
70
- * @param activity the activity
71
- * @param dialog the dialog to be shown
72
- */
73
- public static void showSafely (FragmentActivity activity , DialogFragment dialog ) {
74
- boolean isActivityDestroyed = false ;
75
-
76
- if (activity == null || dialog == null ) {
77
- Timber .d ("show called with null activity / dialog. Ignoring." );
78
- return ;
79
- }
80
-
81
- isActivityDestroyed = activity .isDestroyed ();
82
- if (activity .isFinishing () || isActivityDestroyed ) {
83
- return ;
84
- }
85
-
86
- try {
87
- if (dialog .getDialog () == null || !dialog .getDialog ().isShowing ()) {
88
- dialog .show (activity .getSupportFragmentManager (), dialog .getClass ().getSimpleName ());
89
- }
90
- } catch (IllegalStateException e ) {
91
- Timber .e (e , "Could not show dialog." );
92
- }
93
- }
94
-
95
- public static AlertDialog getAlertDialogWithPositiveAndNegativeCallbacks (
96
- Context context , String title , String message , int iconResourceId , Callback callback ) {
97
-
98
- AlertDialog alertDialog = new Builder (context )
99
- .setTitle (title )
100
- .setMessage (message )
101
- .setPositiveButton (context .getString (R .string .ok ), (dialog , which ) -> {
102
- callback .onPositiveButtonClicked ();
103
- dialog .dismiss ();
104
- })
105
- .setNegativeButton (context .getString (R .string .cancel ), (dialog , which ) -> {
106
- callback .onNegativeButtonClicked ();
107
- dialog .dismiss ();
108
- })
109
- .setIcon (iconResourceId ).create ();
110
-
111
- return alertDialog ;
112
- }
113
-
114
37
public static void showAlertDialog (Activity activity ,
115
38
String title ,
116
39
String message ,
@@ -182,14 +105,14 @@ public static void showAlertDialog(Activity activity,
182
105
/*
183
106
Shows alert dialog with custom view
184
107
*/
185
- public static void showAlertDialog (Activity activity ,
186
- String title ,
187
- String message ,
188
- String positiveButtonText ,
189
- String negativeButtonText ,
190
- final Runnable onPositiveBtnClick ,
191
- final Runnable onNegativeBtnClick ,
192
- View customView ,
108
+ private static void showAlertDialog (Activity activity ,
109
+ String title ,
110
+ String message ,
111
+ String positiveButtonText ,
112
+ String negativeButtonText ,
113
+ final Runnable onPositiveBtnClick ,
114
+ final Runnable onNegativeBtnClick ,
115
+ View customView ,
193
116
boolean cancelable ) {
194
117
AlertDialog .Builder builder = new AlertDialog .Builder (activity );
195
118
builder .setTitle (title );
@@ -215,10 +138,4 @@ public static void showAlertDialog(Activity activity,
215
138
showSafely (activity , dialog );
216
139
}
217
140
218
- public interface Callback {
219
-
220
- void onPositiveButtonClicked ();
221
-
222
- void onNegativeButtonClicked ();
223
- }
224
141
}
0 commit comments