From 795893eb6ff0c02c30a5765e06807ce1ee43978f Mon Sep 17 00:00:00 2001 From: Daniel Sandler Date: Wed, 29 Oct 2014 00:01:25 -0400 Subject: More flexible intent extra parsing. Specifically, --ei (int extras) and --eia (int[] extras) now use Integer.decode(), which means they accept negative integers, base-16 integers formatted as #NNN and 0xNNN, and base-8 integers formatted as 0NNN. Additionally, --ez (boolean extras) can now be specified as "true", "false", "t", "f", or an integer (any nonzero treated as true). The previous behavior, based on Boolean.valueOf(), would silently assign false if you managed to get the spelling of "true" wrong. Change-Id: I058254e907308006d403b5b7866c86bcaa03d8d3 --- cmds/am/src/com/android/commands/am/Am.java | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/cmds/am/src/com/android/commands/am/Am.java b/cmds/am/src/com/android/commands/am/Am.java index ba11a815d5f4..bc57030e3da0 100644 --- a/cmds/am/src/com/android/commands/am/Am.java +++ b/cmds/am/src/com/android/commands/am/Am.java @@ -417,7 +417,7 @@ public class Am extends BaseCommand { } else if (opt.equals("--ei")) { String key = nextArgRequired(); String value = nextArgRequired(); - intent.putExtra(key, Integer.valueOf(value)); + intent.putExtra(key, Integer.decode(value)); } else if (opt.equals("--eu")) { String key = nextArgRequired(); String value = nextArgRequired(); @@ -434,7 +434,7 @@ public class Am extends BaseCommand { String[] strings = value.split(","); int[] list = new int[strings.length]; for (int i = 0; i < strings.length; i++) { - list[i] = Integer.valueOf(strings[i]); + list[i] = Integer.decode(strings[i]); } intent.putExtra(key, list); } else if (opt.equals("--el")) { @@ -477,8 +477,23 @@ public class Am extends BaseCommand { hasIntentInfo = true; } else if (opt.equals("--ez")) { String key = nextArgRequired(); - String value = nextArgRequired(); - intent.putExtra(key, Boolean.valueOf(value)); + String value = nextArgRequired().toLowerCase(); + // Boolean.valueOf() results in false for anything that is not "true", which is + // error-prone in shell commands + boolean arg; + if ("true".equals(value) || "t".equals(value)) { + arg = true; + } else if ("false".equals(value) || "f".equals(value)) { + arg = false; + } else { + try { + arg = Integer.decode(value) != 0; + } catch (NumberFormatException ex) { + throw new IllegalArgumentException("Invalid boolean value: " + value); + } + } + + intent.putExtra(key, arg); } else if (opt.equals("-n")) { String str = nextArgRequired(); ComponentName cn = ComponentName.unflattenFromString(str); -- cgit v1.2.3-59-g8ed1b