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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/*
* Copyright (C) 2012 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cyanogenmod.filemanager.ui.dialogs;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.text.Layout;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.cyanogenmod.filemanager.R;
import com.cyanogenmod.filemanager.commands.AsyncResultExecutable;
import com.cyanogenmod.filemanager.commands.ExecExecutable;
import com.cyanogenmod.filemanager.model.FileSystemObject;
import com.cyanogenmod.filemanager.ui.ThemeManager;
import com.cyanogenmod.filemanager.ui.ThemeManager.Theme;
import com.cyanogenmod.filemanager.util.DialogHelper;
import com.cyanogenmod.filemanager.util.FixedQueue;
import com.cyanogenmod.filemanager.util.FixedQueue.EmptyQueueException;
import java.util.List;
/**
* A class that wraps a dialog for display the output generation for an
* {@link ExecExecutable}.
*/
public class ExecutionDialog implements DialogInterface.OnClickListener {
/**
* @hide
*/
final Context mContext;
/**
* @hide
*/
final AlertDialog mDialog;
/**
* @hide
*/
final TextView mTvOutput;
/**
* @hide
*/
final TextView mTvTime;
/**
* @hide
*/
final TextView mTvExitCode;
// For cancel the operation
private AsyncResultExecutable mCmd;
/**
* @hide
*/
boolean mFinished;
private long mStartTime;
/**
* @hide
*/
final Object mSync = new Object();
/**
* @hide
*/
FixedQueue<String> mQueue;
private final int maxLines;
private final int maxChars;
// The drawing task
private final AsyncTask<Void, String, Void> mConsoleDrawTask =
new AsyncTask<Void, String, Void>() {
@Override
protected Void doInBackground(Void... params) {
while (!ExecutionDialog.this.mFinished) {
// Extract the message
try {
while (!ExecutionDialog.this.mQueue.isEmpty()) {
// Extract all items from the queue
List<String> l = ExecutionDialog.this.mQueue.peekAll();
StringBuilder sb = new StringBuilder();
for (String s : l) {
sb.append(s);
sb.append("\n"); //$NON-NLS-1$
}
// Extract the message and redraw
publishProgress(extractMsg());
// Don't kill the processor
try {
Thread.yield();
Thread.sleep(250L);
} catch (Throwable _throw) {/**NON BLOCK**/}
}
} catch (Exception e) {/**NON BLOCK**/}
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
drawMessage(values[0], false);
}
};
/**
* Constructor of <code>ExecutionDialog</code>.
*
* @param context The current context
* @param fso The file system object to execute
*/
public ExecutionDialog(final Context context, final FileSystemObject fso) {
super();
// Limits
this.maxLines = context.getResources().getInteger(R.integer.console_max_lines);
this.maxChars = context.getResources().getInteger(R.integer.console_max_chars_per_line);
//Save the context
this.mContext = context;
this.mFinished = false;
this.mQueue = new FixedQueue<String>(this.maxLines);
//Create the layout
LayoutInflater li =
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup layout = (ViewGroup)li.inflate(R.layout.execution_dialog, null);
TextView tvScriptName = (TextView)layout.findViewById(R.id.execution_script_name);
tvScriptName.setText(fso.getFullPath());
this.mTvTime = (TextView)layout.findViewById(R.id.execution_time);
this.mTvTime.setText("-"); //$NON-NLS-1$
this.mTvExitCode = (TextView)layout.findViewById(R.id.execution_exitcode);
this.mTvExitCode.setText("-"); //$NON-NLS-1$
this.mTvOutput = (TextView)layout.findViewById(R.id.execution_output);
this.mTvOutput.setMovementMethod(new ScrollingMovementMethod());
// Apply the theme
applyTheme(context, layout);
//Create the dialog
String title = context.getString(R.string.execution_console_title);
this.mDialog = DialogHelper.createDialog(
context,
0,
title,
layout);
this.mDialog.setButton(
DialogInterface.BUTTON_NEUTRAL, context.getString(android.R.string.cancel), this);
// Start the drawing task
this.mConsoleDrawTask.execute();
}
/**
* Method that sets the console
* @param cmd the mCmd to set
*/
public void setCmd(AsyncResultExecutable cmd) {
this.mCmd = cmd;
// Enable cancel the script after 3 seconds.
this.mTvOutput.postDelayed(new Runnable() {
@Override
public void run() {
ExecutionDialog.this.mDialog.setCancelable(true);
ExecutionDialog.this.mDialog.getButton(
DialogInterface.BUTTON_NEUTRAL).setEnabled(true);
}
}, 5000L);
}
/**
* Method that shows the dialog.
*/
public void show() {
DialogHelper.delegateDialogShow(this.mContext, this.mDialog);
this.mDialog.setCancelable(false);
this.mDialog.getButton(DialogInterface.BUTTON_NEUTRAL).setEnabled(false);
}
/**
* Method that dismiss the dialog.
*/
public void dismiss() {
this.mDialog.dismiss();
}
/**
* Method invoked when the execution starts
*/
public void onStart() {
// Initialize execution
this.mStartTime = System.currentTimeMillis();
}
/**
* Method invoked when the execution ends
*
* @param exitCode The exit code of the execution
*/
public void onEnd(final int exitCode) {
// Cancel the drawing task
try {
this.mFinished = true;
this.mConsoleDrawTask.cancel(false);
} catch (Exception e) {/**NON BLOK**/}
long endTime = System.currentTimeMillis();
final String diff = String.valueOf((endTime - this.mStartTime) / 1000);
// Enable the ok button
this.mTvOutput.post(new Runnable() {
@Override
public void run() {
try {
// Draw the data one more time, and clean the queue (no more needed)
drawMessage(extractMsg(), true);
ExecutionDialog.this.mQueue.removeAll();
} catch (EmptyQueueException eqex) {/**NON BLOCK**/}
// Set the time and exit code
ExecutionDialog.this.mTvTime.setText(
ExecutionDialog.this.mContext.getString(
R.string.execution_console_script_execution_time_text, diff));
ExecutionDialog.this.mTvExitCode.setText(String.valueOf(exitCode));
// Enable the Ok button
ExecutionDialog.this.mDialog.setCancelable(true);
Button button =
ExecutionDialog.this.mDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
button.setText(R.string.ok);
button.setEnabled(true);
}
});
}
/**
* Method that append data to the console output
*
* @param msg The message to append
*/
public void onAppendData(final String msg) {
if (msg != null && msg.length() > 0) {
// Split the messages in lines
String[] lines = msg.split("\n"); //$NON-NLS-1$
for (String line : lines) {
// Don't allow lines with more that x characters
if (line.length() > this.maxChars) {
while (line.length() > this.maxChars) {
String partial = line.substring(0, Math.min(line.length(), this.maxChars));
line = line.substring(Math.min(line.length(), this.maxChars));
// The partial
this.mQueue.insert(partial);
}
if (line.length() > 0) {
// Insert the rest of the line
this.mQueue.insert(line);
}
} else {
// Insert the line
this.mQueue.insert(line);
}
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_NEUTRAL:
// Cancel the program?
try {
if (this.mCmd != null && !this.mFinished) {
if (this.mCmd.isCancellable() && !this.mCmd.isCancelled()) {
this.mCmd.cancel();
}
}
} catch (Exception e) {/**NON BLOCK**/}
this.mDialog.dismiss();
break;
default:
break;
}
}
/**
* Method that extracts the message from the queue
*
* @param String The message
* @throws EmptyQueueException If there are not message in the queue
* @hide
*/
String extractMsg() throws EmptyQueueException {
// Extract all items from the queue
List<String> l = this.mQueue.peekAll();
StringBuilder sb = new StringBuilder();
for (String s : l) {
sb.append(s);
sb.append("\n"); //$NON-NLS-1$
}
return sb.toString();
}
/**
* Method that draw the message
*
* @param msg The message
* @param scroll Scroll to bottom
* @hide
*/
void drawMessage(String msg, boolean scroll) {
// Any message?
if (msg != null && msg.length() > 0) {
final TextView tv = ExecutionDialog.this.mTvOutput;
tv.setText(msg);
// Scroll to bottom
if (scroll) {
final Layout layout = tv.getLayout();
if (layout != null) {
int scrollDelta =
layout.getLineBottom(
tv.getLineCount() - 1) - tv.getScrollY() - tv.getHeight();
if (scrollDelta > 0) {
tv.scrollBy(0, scrollDelta);
}
}
} else {
tv.scrollBy(0, 0);
}
}
}
/**
* Method that applies the current theme to the dialog
*
* @param ctx The current context
* @param root The root view
*/
private void applyTheme(Context ctx, ViewGroup root) {
// Apply the current theme
Theme theme = ThemeManager.getCurrentTheme(ctx);
theme.setBackgroundDrawable(ctx, root, "background_drawable"); //$NON-NLS-1$
View v = root.findViewById(R.id.execution_time_label);
theme.setTextColor(ctx, (TextView)v, "text_color"); //$NON-NLS-1$
v = root.findViewById(R.id.execution_script_name);
theme.setTextColor(ctx, (TextView)v, "text_color"); //$NON-NLS-1$
v = root.findViewById(R.id.execution_time_label);
theme.setTextColor(ctx, (TextView)v, "text_color"); //$NON-NLS-1$
theme.setTextColor(ctx, this.mTvTime, "text_color"); //$NON-NLS-1$
v = root.findViewById(R.id.execution_exitcode_label);
theme.setTextColor(ctx, (TextView)v, "text_color"); //$NON-NLS-1$
theme.setTextColor(ctx, this.mTvExitCode, "text_color"); //$NON-NLS-1$
theme.setBackgroundColor(ctx, this.mTvOutput, "console_bg_color"); //$NON-NLS-1$
theme.setTextColor(ctx, this.mTvOutput, "console_fg_color"); //$NON-NLS-1$
}
}
|