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

Commit 9a253e12 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Refactor ChangeReporter and rate limit stats logging."

parents 6e1e6fd6 6624ffa2
Loading
Loading
Loading
Loading
+3 −8
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package android.app;

import android.compat.Compatibility;
import android.os.Process;
import android.util.Log;
import android.util.StatsLog;

import com.android.internal.compat.ChangeReporter;
@@ -31,8 +30,6 @@ import java.util.Arrays;
 * @hide
 */
public final class AppCompatCallbacks extends Compatibility.Callbacks {
    private static final String TAG = "Compatibility";

    private final long[] mDisabledChanges;
    private final ChangeReporter mChangeReporter;

@@ -48,7 +45,8 @@ public final class AppCompatCallbacks extends Compatibility.Callbacks {
    private AppCompatCallbacks(long[] disabledChanges) {
        mDisabledChanges = Arrays.copyOf(disabledChanges, disabledChanges.length);
        Arrays.sort(mDisabledChanges);
        mChangeReporter = new ChangeReporter();
        mChangeReporter = new ChangeReporter(
                StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__APP_PROCESS);
    }

    protected void reportChange(long changeId) {
@@ -67,10 +65,7 @@ public final class AppCompatCallbacks extends Compatibility.Callbacks {

    private void reportChange(long changeId, int state) {
        int uid = Process.myUid();
        //TODO(b/138374585): Implement rate limiting for the logs.
        Log.d(TAG, ChangeReporter.createLogString(uid, changeId, state));
        mChangeReporter.reportChange(uid, changeId,
                state, /* source */StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__APP_PROCESS);
        mChangeReporter.reportChange(uid, changeId, state);
    }

}
+75 −27
Original line number Diff line number Diff line
@@ -16,14 +16,89 @@

package com.android.internal.compat;

import android.util.Log;
import android.util.Slog;
import android.util.StatsLog;

import com.android.internal.annotations.GuardedBy;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * A helper class to report changes to stats log.
 *
 * @hide
 */
public final class ChangeReporter {
    private static final String TAG = "CompatibilityChangeReporter";
    private int mSource;

    private final class ChangeReport {
        int mUid;
        long mChangeId;
        int mState;

        ChangeReport(int uid, long changeId, int state) {
            mUid = uid;
            mChangeId = changeId;
            mState = state;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            ChangeReport that = (ChangeReport) o;
            return mUid == that.mUid
                    && mChangeId == that.mChangeId
                    && mState == that.mState;
        }

        @Override
        public int hashCode() {
            return Objects.hash(mUid, mChangeId, mState);
        }
    }

    @GuardedBy("mReportedChanges")
    private Set<ChangeReport> mReportedChanges =  new HashSet<>();

    public ChangeReporter(int source) {
        mSource = source;
    }

    /**
     * Report the change to stats log.
     *
     * @param uid      affected by the change
     * @param changeId the reported change id
     * @param state    of the reported change - enabled/disabled/only logged
     */
    public void reportChange(int uid, long changeId, int state) {
        debugLog(uid, changeId, state);
        ChangeReport report = new ChangeReport(uid, changeId, state);
        synchronized (mReportedChanges) {
            if (!mReportedChanges.contains(report)) {
                StatsLog.write(StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED, uid, changeId,
                        state, mSource);
                mReportedChanges.add(report);
            }
        }
    }

    private void debugLog(int uid, long changeId, int state) {
        //TODO(b/138374585): Implement rate limiting for the logs.
        String message = String.format("Compat change id reported: %d; UID %d; state: %s", changeId,
                uid, stateToString(state));
        if (mSource == StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER) {
            Slog.d(TAG, message);
        } else {
            Log.d(TAG, message);
        }

    }

    /**
     * Transforms StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE enum to a string.
@@ -43,31 +118,4 @@ public final class ChangeReporter {
                return "UNKNOWN";
        }
    }

    /**
     * Constructs and returns a string to be logged to logcat when a change is reported.
     *
     * @param uid      affected by the change
     * @param changeId the reported change id
     * @param state    of the reported change - enabled/disabled/only logged
     * @return string to log
     */
    public static String createLogString(int uid, long changeId, int state) {
        return String.format("Compat change id reported: %d; UID %d; state: %s", changeId, uid,
                stateToString(state));
    }

    /**
     * Report the change to stats log.
     *
     * @param uid      affected by the change
     * @param changeId the reported change id
     * @param state    of the reported change - enabled/disabled/only logged
     * @param source   of the logging - app process or system server
     */
    public void reportChange(int uid, long changeId, int state, int source) {
        //TODO(b/138374585): Implement rate limiting for stats log.
        StatsLog.write(StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED, uid, changeId,
                state, source);
    }
}
+3 −6
Original line number Diff line number Diff line
@@ -41,7 +41,8 @@ public class PlatformCompat extends IPlatformCompat.Stub {

    public PlatformCompat(Context context) {
        mContext = context;
        mChangeReporter = new ChangeReporter();
        mChangeReporter = new ChangeReporter(
                StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER);
    }

    @Override
@@ -96,10 +97,6 @@ public class PlatformCompat extends IPlatformCompat.Stub {

    private void reportChange(long changeId, ApplicationInfo appInfo, int state) {
        int uid = appInfo.uid;
        //TODO(b/138374585): Implement rate limiting for the logs.
        Slog.d(TAG, ChangeReporter.createLogString(uid, changeId, state));
        mChangeReporter.reportChange(uid, changeId,
                state, /* source */
                StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER);
        mChangeReporter.reportChange(uid, changeId, state);
    }
}