Added more options to CarService.dump().

--list: prints the names of all services.
--services <SERVICES>: dumps the specific services.

Test: adb shell dumpsys car_service --list
Test: adb shell dumpsys car_service --services CarUserService BAD_NAME

Bug: 143815470
Change-Id: I7ef05e945abfb6c8a6226063a150c787acf1f106
diff --git a/service/src/com/android/car/ICarImpl.java b/service/src/com/android/car/ICarImpl.java
index 7db2068..dfdee04 100644
--- a/service/src/com/android/car/ICarImpl.java
+++ b/service/src/com/android/car/ICarImpl.java
@@ -17,6 +17,7 @@
 package com.android.car;
 
 import android.annotation.MainThread;
+import android.annotation.Nullable;
 import android.app.ActivityManager;
 import android.app.UiModeManager;
 import android.car.Car;
@@ -496,6 +497,19 @@
                 writer.println("Failed dumping: " + mHal.getClass().getName());
                 e.printStackTrace(writer);
             }
+        } else if ("--list".equals(args[0])) {
+            dumpListOfServices(writer);
+            return;
+        } else if ("--services".equals(args[0])) {
+            if (args.length < 2) {
+                writer.print("Must pass services to dump when using --services");
+                return;
+            }
+            int length = args.length - 1;
+            String[] services = new String[length];
+            System.arraycopy(args, 1, services, 0, length);
+            dumpIndividualServices(writer, services);
+            return;
         } else if ("--metrics".equals(args[0])) {
             writer.println("*Dump car service metrics*");
             dumpAllServices(writer, true);
@@ -515,6 +529,12 @@
         new CarShellCommand().exec(this, in, out, err, args, callback, resultReceiver);
     }
 
+    private void dumpListOfServices(PrintWriter writer) {
+        for (CarServiceBase service : mAllServices) {
+            writer.println(service.getClass().getName());
+        }
+    }
+
     private void dumpAllServices(PrintWriter writer, boolean dumpMetricsOnly) {
         for (CarServiceBase service : mAllServices) {
             dumpService(service, writer, dumpMetricsOnly);
@@ -522,7 +542,26 @@
         if (mCarTestService != null) {
             dumpService(mCarTestService, writer, dumpMetricsOnly);
         }
+    }
 
+    private void dumpIndividualServices(PrintWriter writer, String... serviceNames) {
+        for (String serviceName : serviceNames) {
+            writer.println("** Dumping " + serviceName + "\n");
+            CarServiceBase service = getCarServiceBySubstring(serviceName);
+            if (service == null) {
+                writer.println("No such service!");
+            } else {
+                dumpService(service, writer, /* dumpMetricsOnly= */ false);
+            }
+            writer.println();
+        }
+    }
+
+    @Nullable
+    private CarServiceBase getCarServiceBySubstring(String className) {
+        return Arrays.asList(mAllServices).stream()
+                .filter(s -> s.getClass().getSimpleName().equals(className))
+                .findFirst().orElse(null);
     }
 
     private void dumpService(CarServiceBase service, PrintWriter writer, boolean dumpMetricsOnly) {