forked from creativein/AndroidTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DialogsHelper.java
138 lines (123 loc) · 4.74 KB
/
DialogsHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.soi.modules;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import com.letfriendsin.app.R;
public class DialogsHelper {
public static ProgressDialog waitingDialog;
private static DialogsHelper _instance;
/**
* Singleton pattern for static methods and variables
* You must run the singletons which you will use in the first Activity
*/
public static void initInstance(){
if (_instance == null){
_instance = new DialogsHelper();
}
}
public static DialogsHelper getInstance() {
return _instance;
}
/**
* Shows a dialog with a custom msg, two buttons (OK and Cancel) and the logo of the app
* WARNING!!! Must show the dialog in Android's runOnUiThread method of the current activity
* because of can't show a dialog out of the activity's ui thread
* @param act is the current activity eg. SoiActivity.this
* @param msg is the msg which we want to show in our dialog
*/
public void showDialog(final Activity act, final String msg) {
try {
Builder builder = new Builder(act);
builder.setMessage(msg)
.setTitle(act.getResources().getString(R.string.app_name))
.setCancelable(true)
//.setIcon(R.drawable.logoicon)
.setPositiveButton(act.getResources().getString(R.string.OK),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
final AlertDialog alert = builder.create();
act.runOnUiThread(new Runnable() {
@Override
public void run() {
alert.show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Shows a dialog with a default msg for "No Internet Access", two buttons (OK and Cancel) and the logo of the app
* WARNING!!! Must show the dialog in Android's runOnUiThread method of the current activity
* @param act is the current activity eg. SoiActivity.this
*/
public void simpleNoInternet(final Activity act) {
try {
Builder builder = new Builder(act);
builder.setMessage(act.getResources().getString(R.string.nointernetaccess))
.setTitle(act.getResources().getString(R.string.app_name))
.setCancelable(false)
//.setIcon(R.drawable.logoicon)
.setPositiveButton(act.getResources().getString(R.string.OK),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
final AlertDialog alert = builder.create();
act.runOnUiThread(new Runnable() {
@Override
public void run() {
alert.show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Shows a progress dialog when must run tasks,jobs for some time like an async event
* WARNING!!! Must show the dialog in Android's runOnUiThread method of the current activity
* @param act is the current activity eg. SoiActivity.this
* @param message
*/
public void showWaitingDialog(Activity act, String message) {
if (waitingDialog != null) {
if (waitingDialog.isShowing())
return;
}
waitingDialog = ProgressDialog.show(act, act.getResources().getString(R.string.app_name), message, true);
waitingDialog.setIndeterminate(true);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(40 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (waitingDialog != null && waitingDialog.isShowing()) {
closeWaitingDialog();
}
}
}).start();
act.runOnUiThread(new Runnable() {
@Override
public void run() {
waitingDialog.show();
}
});
waitingDialog.setCancelable(false);
}
/**
*
*/
public static void closeWaitingDialog() {
if (waitingDialog != null && waitingDialog.isShowing()) {
waitingDialog.dismiss();
}
}
}