1 public class LoadDialog extends Dialog {
2
3 /**
4 * LoadDialog
5 */
6 private static LoadDialog loadDialog;
7 /**
8 * canNotCancel, the dialog dimiss or undimiss flag
9 */
10 private boolean canNotCancel;
11 /**
12 * if the dialog don't dimiss, what is the tips.
13 */
14 private String tipMsg;
15
16 private static TextView messageView;
17
18 // private static OnTimeListener onTimeListener = null;
19
20 /**
21 * the LoadDialog constructor
22 *
23 * @param ctx
24 * Context
25 * @param canNotCancel
26 * boolean
27 * @param tipMsg
28 * String
29 */
30 public LoadDialog(final Context ctx, boolean canNotCancel, String tipMsg) {
31 super(ctx, R.style.BaseDialogTheme);
32 this.canNotCancel = canNotCancel;
33 this.tipMsg = tipMsg;
34 this.getContext().setTheme(android.R.style.Theme_DeviceDefault_Dialog_NoActionBar_MinWidth);
35 setContentView(R.layout.layout_dialog_loading);
36 // add by zhangshengda 2014.07.23
37 messageView = (TextView) findViewById(android.R.id.message);
38 messageView.setText(tipMsg);
39 setCancelable(canNotCancel);
40 setCanceledOnTouchOutside(false);
41
42 Window window = getWindow();
43 window.getDecorView().getBackground().setAlpha(0);
44
45 }
46
47 @Override
48 public boolean onKeyDown(int keyCode, KeyEvent event) {
49 if (keyCode == KeyEvent.KEYCODE_BACK) {
50 if (!canNotCancel) {
51 Toast.makeText(getContext(), tipMsg, Toast.LENGTH_SHORT).show();
52 //Intent intent = new Intent("RESET_LOCAL_DIAG");
53 //MainActivity.contexts.sendBroadcast(intent);
54 return true;
55 }
56 }
57 return super.onKeyDown(keyCode, event);
58 }
59
60 /**
61 * show the dialog
62 *
63 * @param context
64 */
65 public static void show(Context context) {
66 show(context, null, false);
67 }
68
69 /**
70 * show the dialog
71 *
72 * @param context
73 * Context
74 * @param message
75 * String
76 */
77 public static void show(Context context, String message) {
78 show(context, message, false);
79 }
80
81 /**
82 * show the dialog
83 *
84 * @param context
85 * Context
86 * @param message
87 * String, show the message to user when isCancel is true.
88 * @param isCancel
89 * boolean, true is can't dimiss,false is can dimiss
90 */
91 public static void show(Context context, String message, boolean isCancel) {
92 if (context instanceof Activity) {
93 if (((Activity) context).isFinishing()) {
94 return;
95 }
96 }
97 if (loadDialog != null && loadDialog.isShowing()) {
98 timerStop();
99 messageView.setText(message);
100 } else {
101 timerStop();
102 loadDialog = new LoadDialog(context, isCancel, message);
103 if (((Activity) context).isFinishing()) {
104 Log.e("Sanda","LoadDialog leaked window");
105 return;
106 }else{
107 loadDialog.show();
108 }
109 }
110 }
111
112 public static void show(Context context, String message, OnCancelListener l) {
113 if (context instanceof Activity) {
114 if (((Activity) context).isFinishing()) {
115 return;
116 }
117 }
118 if (loadDialog != null && loadDialog.isShowing()) {
119 timerStop();
120 messageView.setText(message);
121 loadDialog.setOnCancelListener(l);
122 } else {
123 timerStop();
124 loadDialog = new LoadDialog(context, true, message);
125 loadDialog.setOnCancelListener(l);
126 if (((Activity) context).isFinishing()) {
127 Log.e("Sanda","LoadDialog leaked window");
128 return;
129 }else{
130 loadDialog.show();
131 }
132 }
133 }
134
135 public static void show(Context context, String message, boolean isTime, boolean isCancel) {
136 if (context instanceof Activity) {
137 if (((Activity) context).isFinishing()) {
138 return;
139 }
140 }
141 if (loadDialog != null && loadDialog.isShowing()) {
142 messageView.setText(message);
143 } else {
144 loadDialog = new LoadDialog(context, true, message);
145 if (((Activity) context).isFinishing()) {
146 Log.e("Sanda","LoadDialog leaked window");
147 return;
148 }else{
149 loadDialog.show();
150 }
151 }
152 if (isTime)
153 timerAction(context);
154 }
155
156 /**
157 * dismiss the dialog
158 */
159 public static void dismiss(Context context) {
160 try {
161 if (context instanceof Activity) {
162 if (((Activity) context).isFinishing()) {
163 loadDialog = null;
164 timerStop();
165 return;
166 }
167 }
168
169 if (loadDialog != null && loadDialog.isShowing()) {
170 Context loadContext = loadDialog.getContext();
171 if (loadContext != null && loadContext instanceof Activity) {
172 if (((Activity) loadContext).isFinishing()) {
173 loadDialog = null;
174 timerStop();
175 return;
176 }
177 }
178 loadDialog.dismiss();
179 loadDialog = null;
180 timerStop();
181 }
182 } catch (Exception e) {
183 e.printStackTrace();
184 loadDialog = null;
185 timerStop();
186 }
187 }
188
189 public static void setCancelListener(OnCancelListener l) {
190 if (loadDialog != null) {
191 loadDialog.setOnCancelListener(l);
192 }
193 }
194
195 private static Timer timer = null;
196 private static TimerTask timerTask = null;
197
198 public static void timerStop() {
199 if (timer != null) {
200 timer.cancel();
201 timer = null;
202 }
203 if (timer != null) {
204 timerTask.cancel();
205 timerTask = null;
206 }
207 }
208
209 public static final int TASK_DELAY=60;
210 public static void timerAction(final Context context) {
211 timerStop();
212 timer = new Timer();
213 timerTask = new TimerTask() {
214 @Override
215 public void run() {
216 Intent intentStatus = new Intent("RemoteDiagStatus");
217 Bundle bundle = new Bundle();
218 bundle.putInt("type", Constant.MESSAGE_RCU_STATE0F);
219 intentStatus.putExtras(bundle);
220 MainActivity.contexts.sendBroadcast(intentStatus);
221 }
222 };
223 timer.schedule(timerTask, TASK_DELAY*1000);
224 }
225 }