Monitors the am process for error messages. If an error occurs, will reattempt launch up to
MAX_ATTEMPT_COUNT
times.
// first we check if one starts with error
ArrayList<String> array = new ArrayList<String>();
boolean error = false;
boolean warning = false;
for (String s : lines) {
// ignore empty lines.
if (s.length() == 0) {
continue;
}
// check for errors that output an error type, if the attempt count is still
// valid. If not the whole text will be output in the console
if (mLaunchInfo.getAttemptCount() < MAX_ATTEMPT_COUNT &&
mLaunchInfo.isCancelled() == false) {
Matcher m = sAmErrorType.matcher(s);
if (m.matches()) {
// get the error type
int type = Integer.parseInt(m.group(1));
final int waitTime = 3;
String msg;
switch (type) {
case 1:
/* Intended fall through */
case 2:
msg = String.format(
"Device not ready. Waiting %1$d seconds before next attempt.",
waitTime);
break;
case 3:
msg = String.format(
"New package not yet registered with the system. Waiting %1$d seconds before next attempt.",
waitTime);
break;
default:
msg = String.format(
"Device not ready (%2$d). Waiting %1$d seconds before next attempt.",
waitTime, type);
break;
}
AdtPlugin.printToConsole(mLaunchInfo.getProject(), msg);
// launch another thread, that waits a bit and attempts another launch
new Thread("Delayed Launch attempt") {
@Override
public void run() {
try {
sleep(waitTime * 1000);
} catch (InterruptedException e) {
// ignore
}
mLaunchController.launchApp(mLaunchInfo, mDevice);
}
}.start();
// no need to parse the rest
return;
}
}
// check for error if needed
if (error == false && s.startsWith("Error:")) { //$NON-NLS-1$
error = true;
}
if (warning == false && s.startsWith("Warning:")) { //$NON-NLS-1$
warning = true;
}
// add the line to the list
array.add("ActivityManager: " + s); //$NON-NLS-1$
}
// then we display them in the console
if (warning || error) {
AdtPlugin.printErrorToConsole(mLaunchInfo.getProject(), array.toArray());
} else {
AdtPlugin.printToConsole(mLaunchInfo.getProject(), array.toArray());
}
// if error then we cancel the launch, and remove the delayed info
if (error) {
mLaunchController.stopLaunch(mLaunchInfo);
}