diff --git a/core/api/current.txt b/core/api/current.txt index 6eaefe59e4dda..8a138ea56eea5 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -47167,6 +47167,15 @@ package android.util { field public float ydpi; } + public interface Dumpable { + method public void dump(@NonNull java.io.PrintWriter, @Nullable String[]); + method @NonNull public default String getDumpableName(); + } + + public interface DumpableContainer { + method public boolean addDumpable(@NonNull android.util.Dumpable); + } + public class EventLog { method public static int getTagCode(String); method public static String getTagName(int); diff --git a/core/api/module-lib-current.txt b/core/api/module-lib-current.txt index 4d8453725205b..8365e5623e8fa 100644 --- a/core/api/module-lib-current.txt +++ b/core/api/module-lib-current.txt @@ -9,6 +9,10 @@ package android { package android.app { + @UiContext public class Activity extends android.view.ContextThemeWrapper implements android.content.ComponentCallbacks2 android.view.KeyEvent.Callback android.view.LayoutInflater.Factory2 android.view.View.OnCreateContextMenuListener android.view.Window.Callback { + method public final boolean addDumpable(@NonNull android.util.Dumpable); + } + public class ActivityManager { method @RequiresPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) public void addHomeVisibilityListener(@NonNull java.util.concurrent.Executor, @NonNull android.app.HomeVisibilityListener); method @RequiresPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) public void removeHomeVisibilityListener(@NonNull android.app.HomeVisibilityListener); diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java index cf2b7aca8e520..283345f073370 100644 --- a/core/java/android/app/Activity.java +++ b/core/java/android/app/Activity.java @@ -90,6 +90,7 @@ import android.transition.Scene; import android.transition.TransitionManager; import android.util.ArrayMap; import android.util.AttributeSet; +import android.util.Dumpable; import android.util.EventLog; import android.util.Log; import android.util.PrintWriterPrinter; @@ -145,6 +146,7 @@ import com.android.internal.app.IVoiceInteractor; import com.android.internal.app.ToolbarActionBar; import com.android.internal.app.WindowDecorActionBar; import com.android.internal.policy.PhoneWindow; +import com.android.internal.util.dump.DumpableContainerImpl; import dalvik.system.VMRuntime; @@ -954,6 +956,9 @@ public class Activity extends ContextThemeWrapper private SplashScreen mSplashScreen; + @Nullable + private DumpableContainerImpl mDumpableContainer; + private final WindowControllerCallback mWindowControllerCallback = new WindowControllerCallback() { /** @@ -7081,8 +7086,23 @@ public class Activity extends ContextThemeWrapper dumpInner(prefix, fd, writer, args); } + /** + * See {@link android.util.DumpableContainer#addDumpable(Dumpable)}. + * + * @hide + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + public final boolean addDumpable(@NonNull Dumpable dumpable) { + if (mDumpableContainer == null) { + mDumpableContainer = new DumpableContainerImpl(); + } + return mDumpableContainer.addDumpable(dumpable); + } + void dumpInner(@NonNull String prefix, @Nullable FileDescriptor fd, @NonNull PrintWriter writer, @Nullable String[] args) { + String innerPrefix = prefix + " "; + if (args != null && args.length > 0) { // Handle special cases switch (args[0]) { @@ -7095,12 +7115,33 @@ public class Activity extends ContextThemeWrapper case "--translation": dumpUiTranslation(prefix, writer); return; + case "--list-dumpables": + if (mDumpableContainer == null) { + writer.print(prefix); writer.println("No dumpables"); + return; + } + mDumpableContainer.listDumpables(prefix, writer); + return; + case "--dump-dumpable": + if (args.length == 1) { + writer.println("--dump-dumpable requires the dumpable name"); + return; + } + if (mDumpableContainer == null) { + writer.println("no dumpables"); + return; + } + // Strips --dump-dumpable NAME + String[] prunedArgs = new String[args.length - 2]; + System.arraycopy(args, 2, prunedArgs, 0, prunedArgs.length); + mDumpableContainer.dumpOneDumpable(prefix, writer, args[1], prunedArgs); + return; } } + writer.print(prefix); writer.print("Local Activity "); writer.print(Integer.toHexString(System.identityHashCode(this))); writer.println(" State:"); - String innerPrefix = prefix + " "; writer.print(innerPrefix); writer.print("mResumed="); writer.print(mResumed); writer.print(" mStopped="); writer.print(mStopped); writer.print(" mFinished="); @@ -7138,6 +7179,10 @@ public class Activity extends ContextThemeWrapper dumpUiTranslation(prefix, writer); ResourcesManager.getInstance().dump(prefix, writer); + + if (mDumpableContainer != null) { + mDumpableContainer.dumpAllDumpables(prefix, writer, args); + } } void dumpContentCaptureManager(String prefix, PrintWriter writer) { diff --git a/core/java/android/util/Dumpable.java b/core/java/android/util/Dumpable.java new file mode 100644 index 0000000000000..79c576d088669 --- /dev/null +++ b/core/java/android/util/Dumpable.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.util; + +import android.annotation.NonNull; +import android.annotation.Nullable; + +import java.io.PrintWriter; + +/** + * Represents an object whose state can be dumped into a {@link PrintWriter}. + */ +public interface Dumpable { + + /** + * Gets the name of the {@link Dumpable}. + * + * @return class name, by default. + */ + @NonNull + default String getDumpableName() { + return getClass().getName(); + } + + //TODO(b/149254050): decide whether it should take a ParcelFileDescription as well. + + /** + * Dumps the internal state into the given {@code writer}. + * + * @param writer writer to be written to + * @param args optional list of arguments + */ + void dump(@NonNull PrintWriter writer, @Nullable String[] args); +} diff --git a/core/java/android/util/DumpableContainer.java b/core/java/android/util/DumpableContainer.java new file mode 100644 index 0000000000000..04d19dc419268 --- /dev/null +++ b/core/java/android/util/DumpableContainer.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.util; + +import android.annotation.NonNull; + +/** + * Objects that contains a list of {@link Dumpable}, which will be dumped when the object itself + * is dumped. + */ +public interface DumpableContainer { + + /** + * Adds the given {@link Dumpable dumpable} to the container. + * + *
If a dumpable with the same {@link Dumpable#getDumpableName() name} was added before, this
+ * call is ignored.
+ *
+ * @param dumpable dumpable to be added.
+ *
+ * @throws IllegalArgumentException if the {@link Dumpable#getDumpableName() dumpable name} is
+ * {@code null}.
+ *
+ * @return {@code true} if the dumpable was added, {@code false} if the call was ignored.
+ */
+ boolean addDumpable(@NonNull Dumpable dumpable);
+}
diff --git a/core/java/com/android/internal/util/dump/DumpableContainerImpl.java b/core/java/com/android/internal/util/dump/DumpableContainerImpl.java
new file mode 100644
index 0000000000000..d48b4b136f4a4
--- /dev/null
+++ b/core/java/com/android/internal/util/dump/DumpableContainerImpl.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.internal.util.dump;
+
+import android.annotation.Nullable;
+import android.util.ArrayMap;
+import android.util.Dumpable;
+import android.util.DumpableContainer;
+import android.util.IndentingPrintWriter;
+import android.util.Log;
+
+import java.io.PrintWriter;
+import java.util.Objects;
+
+// TODO(b/149254050): add unit tests
+/**
+ * Helper class for {@link DumpableContainer} implementations - they can "implement it by
+ * association", i.e., by delegating the interface methods to a {@code DumpableContainerImpl}.
+ *
+ * @hide
+ */
+public final class DumpableContainerImpl implements DumpableContainer {
+
+ private static final String TAG = DumpableContainerImpl.class.getSimpleName();
+
+ private static final boolean DEBUG = false;
+
+ @Nullable
+ private final ArrayMap See {@link SystemServer.SystemServerDumper} for usage example.
*/
+// TODO(b/149254050): replace / merge with package android.util.Dumpable (it would require
+// exporting IndentingPrintWriter as @SystemApi) and/or changing the method to use a prefix
public interface Dumpable {
/**