Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cbc4d681 authored by Corina's avatar Corina Committed by Corina Grigoras
Browse files

Move UiEventLogger interface to modules-utils.

This is so the soures can be accessed both fom framework and modules.
The implementation needs the local StatsLog, so cannot be reused.

Test: m
Test: atest SystemUITests
Test: make statsd_testdrive $ANDROID_HOST_OUT/bin/statsd_testdrive 90
Bug: 218862369
Change-Id: I27ebffa6817575f1377beb80f4092287d9b12c49
Merged-In: I27ebffa6817575f1377beb80f4092287d9b12c49
parent aa917e54
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -328,6 +328,7 @@ java_defaults {
        "modules-utils-preconditions",
        "modules-utils-preconditions",
        "modules-utils-synchronous-result-receiver",
        "modules-utils-synchronous-result-receiver",
        "modules-utils-os",
        "modules-utils-os",
        "modules-utils-uieventlogger-interface",
        "framework-permission-aidl-java",
        "framework-permission-aidl-java",
        "spatializer-aidl-java",
        "spatializer-aidl-java",
        "audiopolicy-types-aidl-java",
        "audiopolicy-types-aidl-java",
+4 −4
Original line number Original line Diff line number Diff line
@@ -145,16 +145,16 @@ genrule {
    out: ["com/android/internal/util/FrameworkStatsLog.java"],
    out: ["com/android/internal/util/FrameworkStatsLog.java"],
}
}


// Library that provides functionality to log UiEvents in framework space.
// If this functionality is needed outside the framework, the interfaces library
// can be re-used and a local implementation is needed.
java_library {
java_library {
    name: "uieventloggerlib",
    name: "uieventloggerlib",
    srcs: [
    srcs: [
        "com/android/internal/logging/UiEvent.java",
        "com/android/internal/logging/UiEventLogger.java",
        "com/android/internal/logging/UiEventLoggerImpl.java",
        "com/android/internal/logging/UiEventLoggerImpl.java",
        "com/android/internal/logging/InstanceId.java",
        "com/android/internal/logging/InstanceIdSequence.java",
        ":statslog-framework-java-gen",
        ":statslog-framework-java-gen",
    ],
    ],
    static_libs: ["modules-utils-uieventlogger-interface"],
}
}


filegroup {
filegroup {
+0 −98
Original line number Original line 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 android.annotation.Nullable;
import android.os.Parcel;
import android.os.Parcelable;

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.  Clipped to range [0, INSTANCE_ID_MAX].
 */
public final class InstanceId implements Parcelable {
    // At most 20 bits: ~1m possibilities, ~0.5% probability of collision in 100 values
    static final int INSTANCE_ID_MAX = 1 << 20;

    private final int mId;
    InstanceId(int id) {
        mId = min(max(0, id), INSTANCE_ID_MAX);
    }

    private InstanceId(Parcel in) {
        this(in.readInt());
    }

    @VisibleForTesting
    public int getId() {
        return mId;
    }

    /**
     * Create a fake instance ID for testing purposes.  Not for production use. See also
     * InstanceIdSequenceFake, which is a testing replacement for InstanceIdSequence.
     * @param id The ID you want to assign.
     * @return new InstanceId.
     */
    @VisibleForTesting
    public static InstanceId fakeInstanceId(int id) {
        return new InstanceId(id);
    }

    @Override
    public int hashCode() {
        return mId;
    }

    @Override
    public boolean equals(@Nullable Object obj) {
        if (!(obj instanceof InstanceId)) {
            return false;
        }
        return mId == ((InstanceId) obj).mId;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mId);
    }

    public static final Parcelable.Creator<InstanceId> CREATOR =
            new Parcelable.Creator<InstanceId>() {
        @Override
        public InstanceId createFromParcel(Parcel in) {
            return new InstanceId(in);
        }

        @Override
        public InstanceId[] newArray(int size) {
            return new InstanceId[size];
        }
    };

}
+0 −62
Original line number Original line 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 com.android.internal.annotations.VisibleForTesting;

import java.security.SecureRandom;
import java.util.Random;

/**
 * Generates random InstanceIds in range [1, 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 {
    protected final int mInstanceIdMax;
    private final Random mRandom = new SecureRandom();

    /**
     * Constructs a sequence with identifiers [1, instanceIdMax].  Capped at INSTANCE_ID_MAX.
     * @param instanceIdMax Limiting value of identifiers. Normally positive: otherwise you get
     *                      an all-1 sequence.
     */
    public InstanceIdSequence(int instanceIdMax) {
        mInstanceIdMax = min(max(1, instanceIdMax), InstanceId.INSTANCE_ID_MAX);
    }

    /**
     * Gets the next instance from the sequence.  Safe for concurrent use.
     * @return new InstanceId
     */
    public InstanceId newInstanceId() {
        return newInstanceIdInternal(1 + mRandom.nextInt(mInstanceIdMax));
    }

    /**
     * Factory function for instance IDs, used for testing.
     * @param id
     * @return new InstanceId(id)
     */
    @VisibleForTesting
    protected InstanceId newInstanceIdInternal(int id) {
        return new InstanceId(id);
    }
}
+0 −30
Original line number Original line Diff line number Diff line
/*
 * 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 com.android.internal.logging;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.SOURCE;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Retention(SOURCE)
@Target(FIELD)
public @interface UiEvent {
    /** An explanation, suitable for Android analysts, of the UI event that this log represents. */
    String doc();
}
Loading