| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.commands.incident; |
| 18 |
|
| 19 |
import android.util.Log; |
| 20 |
|
| 21 |
import com.android.commands.incident.sections.PersistLogSection; |
| 22 |
|
| 23 |
import java.io.PrintStream; |
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.Arrays; |
| 26 |
import java.util.Collections; |
| 27 |
import java.util.List; |
| 28 |
import java.util.ListIterator; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class IncidentHelper { |
| 35 |
private static final String TAG = "IncidentHelper"; |
| 36 |
private static boolean sLog = false; |
| 37 |
private final List<String> mArgs; |
| 38 |
private ListIterator<String> mArgsIterator; |
| 39 |
|
| 40 |
private IncidentHelper(String[] args) { |
| 41 |
mArgs = Collections.unmodifiableList(Arrays.asList(args)); |
| 42 |
mArgsIterator = mArgs.listIterator(); |
| 43 |
} |
| 44 |
|
| 45 |
private static void showUsage(PrintStream out) { |
| 46 |
out.println("This command is not designed to be run manually."); |
| 47 |
out.println("Usage:"); |
| 48 |
out.println(" run [sectionName]"); |
| 49 |
} |
| 50 |
|
| 51 |
private void run(String[] args) throws ExecutionException { |
| 52 |
Section section = null; |
| 53 |
List<String> sectionArgs = new ArrayList<>(); |
| 54 |
while (mArgsIterator.hasNext()) { |
| 55 |
String arg = mArgsIterator.next(); |
| 56 |
if ("-l".equals(arg)) { |
| 57 |
sLog = true; |
| 58 |
Log.i(TAG, "Args: [" + String.join(",", args) + "]"); |
| 59 |
} else if ("run".equals(arg)) { |
| 60 |
section = getSection(nextArgRequired()); |
| 61 |
mArgsIterator.forEachRemaining(sectionArgs::add); |
| 62 |
break; |
| 63 |
} else { |
| 64 |
log(Log.WARN, TAG, "Error: Unknown argument: " + arg); |
| 65 |
return; |
| 66 |
} |
| 67 |
} |
| 68 |
section.run(System.in, System.out, sectionArgs); |
| 69 |
} |
| 70 |
|
| 71 |
private static Section getSection(String name) throws IllegalArgumentException { |
| 72 |
if ("persisted_logs".equals(name)) { |
| 73 |
return new PersistLogSection(); |
| 74 |
} |
| 75 |
throw new IllegalArgumentException("Section not found: " + name); |
| 76 |
} |
| 77 |
|
| 78 |
private String nextArgRequired() { |
| 79 |
if (!mArgsIterator.hasNext()) { |
| 80 |
throw new IllegalArgumentException( |
| 81 |
"Arg required after \"" + mArgs.get(mArgsIterator.previousIndex()) + "\""); |
| 82 |
} |
| 83 |
return mArgsIterator.next(); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public static void log(int priority, String tag, String msg) { |
| 90 |
System.err.println(tag + ": " + msg); |
| 91 |
if (sLog) { |
| 92 |
Log.println(priority, tag, msg); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public static void main(String[] args) { |
| 102 |
if (args.length == 0) { |
| 103 |
showUsage(System.err); |
| 104 |
System.exit(0); |
| 105 |
} |
| 106 |
IncidentHelper incidentHelper = new IncidentHelper(args); |
| 107 |
try { |
| 108 |
incidentHelper.run(args); |
| 109 |
} catch (IllegalArgumentException e) { |
| 110 |
showUsage(System.err); |
| 111 |
System.err.println(); |
| 112 |
e.printStackTrace(System.err); |
| 113 |
if (sLog) { |
| 114 |
Log.e(TAG, "Error: ", e); |
| 115 |
} |
| 116 |
} catch (Exception e) { |
| 117 |
e.printStackTrace(System.err); |
| 118 |
if (sLog) { |
| 119 |
Log.e(TAG, "Error: ", e); |
| 120 |
} |
| 121 |
System.exit(1); |
| 122 |
} |
| 123 |
} |
| 124 |
} |