diff options
4 files changed, 75 insertions, 1 deletions
diff --git a/core/java/android/os/Handler.java b/core/java/android/os/Handler.java index 9af9edae9a3f..45a9cf5ab869 100644 --- a/core/java/android/os/Handler.java +++ b/core/java/android/os/Handler.java @@ -295,6 +295,10 @@ public class Handler { /** {@hide} */ @NonNull public String getTraceName(@NonNull Message message) { + if (message.callback instanceof TraceNameSupplier) { + return ((TraceNameSupplier) message.callback).getTraceName(); + } + final StringBuilder sb = new StringBuilder(); sb.append(getClass().getName()).append(": "); if (message.callback != null) { diff --git a/core/java/android/os/TraceNameSupplier.java b/core/java/android/os/TraceNameSupplier.java new file mode 100644 index 000000000000..e4b3a4edce38 --- /dev/null +++ b/core/java/android/os/TraceNameSupplier.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2019 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.os; + +import android.annotation.NonNull; + +/** + * Supplier for custom trace messages. + * + * @hide + */ +public interface TraceNameSupplier { + + /** + * Gets the name used for trace messages. + */ + @NonNull String getTraceName(); +} diff --git a/core/java/com/android/internal/util/function/pooled/PooledLambdaImpl.java b/core/java/com/android/internal/util/function/pooled/PooledLambdaImpl.java index 1fdb1f30125f..e12c031f0036 100755 --- a/core/java/com/android/internal/util/function/pooled/PooledLambdaImpl.java +++ b/core/java/com/android/internal/util/function/pooled/PooledLambdaImpl.java @@ -16,6 +16,7 @@ package com.android.internal.util.function.pooled; +import android.annotation.NonNull; import android.annotation.Nullable; import android.os.Message; import android.text.TextUtils; @@ -527,6 +528,36 @@ final class PooledLambdaImpl<R> extends OmniFunction<Object, return r; } + // TODO: add unit test + @NonNull + private static String getFriendlyName(@NonNull Object function) { + // Full function has one of the following formats: + // package-$$Lambda$class$randomId + // package-$$Lambda$randomId + // + // We just want just package.class$Lambda (or package$Lambda) respectively + + final String fullFunction = function.toString(); + + final int endPkgIdx = fullFunction.indexOf("-$$"); + if (endPkgIdx == -1) return fullFunction; + + // firstDollarIdx could be either beginning of class or beginning of the random id + final int firstDollarIdx = fullFunction.indexOf('$', endPkgIdx + 3); + if (firstDollarIdx == -1) return fullFunction; + + final int endClassIdx = fullFunction.indexOf('$', firstDollarIdx + 1); + if (endClassIdx == -1) { + // Just package + return fullFunction.substring(0, endPkgIdx - 1) + "$Lambda"; + } + + // Package + class + return fullFunction.substring(0, endPkgIdx) + + fullFunction.substring(firstDollarIdx + 1, endClassIdx) + + "$Lambda"; + } + private static void setIfInBounds(Object[] array, int i, Object a) { if (i < ArrayUtils.size(array)) array[i] = a; } @@ -566,6 +597,11 @@ final class PooledLambdaImpl<R> extends OmniFunction<Object, return this; } + @Override + public String getTraceName() { + return getFriendlyName(mFunc); + } + private boolean isRecycled() { return (mFlags & FLAG_RECYCLED) != 0; } diff --git a/core/java/com/android/internal/util/function/pooled/PooledRunnable.java b/core/java/com/android/internal/util/function/pooled/PooledRunnable.java index 89ca82e2f3ce..f0bc2cadb1c5 100644 --- a/core/java/com/android/internal/util/function/pooled/PooledRunnable.java +++ b/core/java/com/android/internal/util/function/pooled/PooledRunnable.java @@ -16,6 +16,8 @@ package com.android.internal.util.function.pooled; +import android.os.TraceNameSupplier; + import com.android.internal.util.FunctionalUtils.ThrowingRunnable; /** @@ -24,7 +26,8 @@ import com.android.internal.util.FunctionalUtils.ThrowingRunnable; * @see PooledLambda * @hide */ -public interface PooledRunnable extends PooledLambda, Runnable, ThrowingRunnable { +public interface PooledRunnable + extends PooledLambda, Runnable, ThrowingRunnable, TraceNameSupplier { /** @inheritDoc */ PooledRunnable recycleOnUse(); } |