Default --stop-when-done on S.

Change-Id: I785a46ada76af94e5a1f9486f9bf10dad12243cf
diff --git a/tools/java_heap_dump b/tools/java_heap_dump
index 03abb01..8e0f31f 100755
--- a/tools/java_heap_dump
+++ b/tools/java_heap_dump
@@ -65,6 +65,20 @@
                 'perfetto --txt -c - -o '
                 '/data/misc/perfetto-traces/java-profile-{user} -d')
 
+SDK = {
+    'S': 31,
+}
+
+def release_or_newer(release):
+  sdk = int(subprocess.check_output(
+    ['adb', 'shell', 'getprop', 'ro.system.build.version.sdk']
+  ).decode('utf-8').strip())
+  if sdk >= SDK[release]:
+    return True
+  codename = subprocess.check_output(
+    ['adb', 'shell', 'getprop', 'ro.build.version.codename']
+  ).decode('utf-8').strip()
+  return codename == release
 
 def main(argv):
   parser = argparse.ArgumentParser()
@@ -107,11 +121,20 @@
   parser.add_argument(
       "--stop-when-done",
       action="store_true",
-      help="On recent builds of S, use a new method to stop the profile when "
-           "the dump is done. Previously, we would hardcode a duration.")
+      default=None,
+      help="Use a new method to stop the profile when the dump is done. "
+      "Previously, we would hardcode a duration. Available and default on S.")
+  parser.add_argument(
+      "--no-stop-when-done",
+      action="store_false",
+      dest='stop_when_done',
+      help="Do not use a new method to stop the profile when the dump is done.")
 
   args = parser.parse_args()
 
+  if args.stop_when_done is None:
+    args.stop_when_done = release_or_newer('S')
+
   fail = False
   if args.pid is None and args.name is None:
     print("FATAL: Neither PID nor NAME given.", file=sys.stderr)