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

Commit 1904bb31 authored by atrost's avatar atrost Committed by android-build-merger
Browse files

Merge "Refactor ChangeReporter and rate limit stats logging." am: 9a253e12...

Merge "Refactor ChangeReporter and rate limit stats logging." am: 9a253e12 am: 76191fa6 am: 604f8002
am: 7564285e

Change-Id: Ib6605757514b90ec5fb2df03f08fc7d48d12e08e
parents 6c98b3a6 7564285e
Loading
Loading
Loading
Loading
+3 −8
Original line number Original line Diff line number Diff line
@@ -18,7 +18,6 @@ package android.app;


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


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

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


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


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


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


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


package com.android.internal.compat;
package com.android.internal.compat;


import android.util.Log;
import android.util.Slog;
import android.util.StatsLog;
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.
 * A helper class to report changes to stats log.
 *
 *
 * @hide
 * @hide
 */
 */
public final class ChangeReporter {
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.
     * Transforms StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE enum to a string.
@@ -43,31 +118,4 @@ public final class ChangeReporter {
                return "UNKNOWN";
                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 Original line Diff line number Diff line
@@ -41,7 +41,8 @@ public class PlatformCompat extends IPlatformCompat.Stub {


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


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


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