Loading cmds/statsd/src/atoms.proto +3 −0 Original line number Diff line number Diff line Loading @@ -3303,6 +3303,9 @@ message UiEventReported { // For example, the package posting a notification, or the destination package of a share. optional int32 uid = 2 [(is_uid) = true]; optional string package_name = 3; // An identifier used to disambiguate which logs refer to a particular instance of some // UI element. Useful when there might be multiple instances simultaneously active. optional int32 instance_id = 4; } /** Loading core/java/com/android/internal/logging/InstanceId.java 0 → 100644 +50 −0 Original line number Diff line number Diff line /* * Copyright (C) 2020 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.logging; import android.annotation.Nullable; import com.android.internal.annotations.VisibleForTesting; /** * An opaque identifier used to disambiguate which logs refer to a particular instance of some * UI element. Useful when there might be multiple instances simultaneously active. * Obtain from InstanceIdSequence. */ public class InstanceId { private int mId; protected InstanceId(int id) { mId = id; } @VisibleForTesting public int getId() { return mId; } @Override public int hashCode() { return mId; } @Override public boolean equals(@Nullable Object obj) { if (!(obj instanceof InstanceId)) { return false; } return mId == ((InstanceId) obj).mId; } } core/java/com/android/internal/logging/InstanceIdSequence.java 0 → 100644 +52 −0 Original line number Diff line number Diff line /* * Copyright (C) 2020 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.logging; import static java.lang.Math.max; import static java.lang.Math.min; import java.security.SecureRandom; import java.util.Random; /** * Generates random InstanceIds in range [0, instanceIdMax) for passing to * UiEventLogger.logWithInstanceId(). Holds a SecureRandom, which self-seeds on * first use; try to give it a long lifetime. Safe for concurrent use. */ public class InstanceIdSequence { // At most 20 bits: ~1m possibilities, ~0.5% probability of collision in 100 values private static final int INSTANCE_ID_MAX = 1 << 20; protected final int mInstanceIdMax; private final Random mRandom = new SecureRandom(); /** * Constructs a sequence with identifiers [0, instanceIdMax). Capped at INSTANCE_ID_MAX. * @param instanceIdMax Limiting value of identifiers. Normally positive: otherwise you get * an all-zero sequence. */ public InstanceIdSequence(int instanceIdMax) { mInstanceIdMax = min(max(0, instanceIdMax), INSTANCE_ID_MAX); } /** * Gets the next instance from the sequence. Safe for concurrent use. * @return new InstanceId */ public InstanceId newInstanceId() { return new InstanceId(mRandom.nextInt(mInstanceIdMax)); } } core/java/com/android/internal/logging/UiEventLogger.java +11 −0 Original line number Diff line number Diff line Loading @@ -49,4 +49,15 @@ public interface UiEventLogger { * @param packageName the package name of the relevant app, if known (null otherwise). */ void log(@NonNull UiEventEnum event, int uid, @Nullable String packageName); /** * Log an event with package information and an instance ID. * Does nothing if event.getId() <= 0. * @param event an enum implementing UiEventEnum interface. * @param uid the uid of the relevant app, if known (0 otherwise). * @param packageName the package name of the relevant app, if known (null otherwise). * @param instance An identifier obtained from an InstanceIdSequence. */ void logWithInstanceId(@NonNull UiEventEnum event, int uid, @Nullable String packageName, @NonNull InstanceId instance); } core/java/com/android/internal/logging/UiEventLoggerImpl.java +10 −0 Original line number Diff line number Diff line Loading @@ -36,4 +36,14 @@ public class UiEventLoggerImpl implements UiEventLogger { StatsLog.write(StatsLog.UI_EVENT_REPORTED, eventID, uid, packageName); } } @Override public void logWithInstanceId(UiEventEnum event, int uid, String packageName, InstanceId instance) { final int eventID = event.getId(); if (eventID > 0) { StatsLog.write(StatsLog.UI_EVENT_REPORTED, eventID, uid, packageName, instance.getId()); } } } Loading
cmds/statsd/src/atoms.proto +3 −0 Original line number Diff line number Diff line Loading @@ -3303,6 +3303,9 @@ message UiEventReported { // For example, the package posting a notification, or the destination package of a share. optional int32 uid = 2 [(is_uid) = true]; optional string package_name = 3; // An identifier used to disambiguate which logs refer to a particular instance of some // UI element. Useful when there might be multiple instances simultaneously active. optional int32 instance_id = 4; } /** Loading
core/java/com/android/internal/logging/InstanceId.java 0 → 100644 +50 −0 Original line number Diff line number Diff line /* * Copyright (C) 2020 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.logging; import android.annotation.Nullable; import com.android.internal.annotations.VisibleForTesting; /** * An opaque identifier used to disambiguate which logs refer to a particular instance of some * UI element. Useful when there might be multiple instances simultaneously active. * Obtain from InstanceIdSequence. */ public class InstanceId { private int mId; protected InstanceId(int id) { mId = id; } @VisibleForTesting public int getId() { return mId; } @Override public int hashCode() { return mId; } @Override public boolean equals(@Nullable Object obj) { if (!(obj instanceof InstanceId)) { return false; } return mId == ((InstanceId) obj).mId; } }
core/java/com/android/internal/logging/InstanceIdSequence.java 0 → 100644 +52 −0 Original line number Diff line number Diff line /* * Copyright (C) 2020 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.logging; import static java.lang.Math.max; import static java.lang.Math.min; import java.security.SecureRandom; import java.util.Random; /** * Generates random InstanceIds in range [0, instanceIdMax) for passing to * UiEventLogger.logWithInstanceId(). Holds a SecureRandom, which self-seeds on * first use; try to give it a long lifetime. Safe for concurrent use. */ public class InstanceIdSequence { // At most 20 bits: ~1m possibilities, ~0.5% probability of collision in 100 values private static final int INSTANCE_ID_MAX = 1 << 20; protected final int mInstanceIdMax; private final Random mRandom = new SecureRandom(); /** * Constructs a sequence with identifiers [0, instanceIdMax). Capped at INSTANCE_ID_MAX. * @param instanceIdMax Limiting value of identifiers. Normally positive: otherwise you get * an all-zero sequence. */ public InstanceIdSequence(int instanceIdMax) { mInstanceIdMax = min(max(0, instanceIdMax), INSTANCE_ID_MAX); } /** * Gets the next instance from the sequence. Safe for concurrent use. * @return new InstanceId */ public InstanceId newInstanceId() { return new InstanceId(mRandom.nextInt(mInstanceIdMax)); } }
core/java/com/android/internal/logging/UiEventLogger.java +11 −0 Original line number Diff line number Diff line Loading @@ -49,4 +49,15 @@ public interface UiEventLogger { * @param packageName the package name of the relevant app, if known (null otherwise). */ void log(@NonNull UiEventEnum event, int uid, @Nullable String packageName); /** * Log an event with package information and an instance ID. * Does nothing if event.getId() <= 0. * @param event an enum implementing UiEventEnum interface. * @param uid the uid of the relevant app, if known (0 otherwise). * @param packageName the package name of the relevant app, if known (null otherwise). * @param instance An identifier obtained from an InstanceIdSequence. */ void logWithInstanceId(@NonNull UiEventEnum event, int uid, @Nullable String packageName, @NonNull InstanceId instance); }
core/java/com/android/internal/logging/UiEventLoggerImpl.java +10 −0 Original line number Diff line number Diff line Loading @@ -36,4 +36,14 @@ public class UiEventLoggerImpl implements UiEventLogger { StatsLog.write(StatsLog.UI_EVENT_REPORTED, eventID, uid, packageName); } } @Override public void logWithInstanceId(UiEventEnum event, int uid, String packageName, InstanceId instance) { final int eventID = event.getId(); if (eventID > 0) { StatsLog.write(StatsLog.UI_EVENT_REPORTED, eventID, uid, packageName, instance.getId()); } } }