Merge "Minor refactor of ChangeReporter and PlatformCompat"
This commit is contained in:
@@ -18,7 +18,6 @@ package android.app;
|
||||
|
||||
import android.compat.Compatibility;
|
||||
import android.os.Process;
|
||||
import android.util.StatsLog;
|
||||
|
||||
import com.android.internal.compat.ChangeReporter;
|
||||
|
||||
@@ -46,20 +45,20 @@ public final class AppCompatCallbacks extends Compatibility.Callbacks {
|
||||
mDisabledChanges = Arrays.copyOf(disabledChanges, disabledChanges.length);
|
||||
Arrays.sort(mDisabledChanges);
|
||||
mChangeReporter = new ChangeReporter(
|
||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__APP_PROCESS);
|
||||
ChangeReporter.SOURCE_APP_PROCESS);
|
||||
}
|
||||
|
||||
protected void reportChange(long changeId) {
|
||||
reportChange(changeId, StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED);
|
||||
reportChange(changeId, ChangeReporter.STATE_LOGGED);
|
||||
}
|
||||
|
||||
protected boolean isChangeEnabled(long changeId) {
|
||||
if (Arrays.binarySearch(mDisabledChanges, changeId) < 0) {
|
||||
// Not present in the disabled array
|
||||
reportChange(changeId, StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__ENABLED);
|
||||
reportChange(changeId, ChangeReporter.STATE_ENABLED);
|
||||
return true;
|
||||
}
|
||||
reportChange(changeId, StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__DISABLED);
|
||||
reportChange(changeId, ChangeReporter.STATE_DISABLED);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.android.internal.compat;
|
||||
|
||||
import android.annotation.IntDef;
|
||||
import android.util.Log;
|
||||
import android.util.Slog;
|
||||
import android.util.StatsLog;
|
||||
@@ -23,6 +24,8 @@ import android.util.StatsLog;
|
||||
import com.android.internal.annotations.GuardedBy;
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
@@ -42,7 +45,7 @@ public final class ChangeReporter {
|
||||
long mChangeId;
|
||||
int mState;
|
||||
|
||||
ChangeReport(long changeId, int state) {
|
||||
ChangeReport(long changeId, @State int state) {
|
||||
mChangeId = changeId;
|
||||
mState = state;
|
||||
}
|
||||
@@ -69,7 +72,7 @@ public final class ChangeReporter {
|
||||
// When true will of every time to debug (logcat).
|
||||
private boolean mDebugLogAll;
|
||||
|
||||
public ChangeReporter(int source) {
|
||||
public ChangeReporter(@Source int source) {
|
||||
mSource = source;
|
||||
mReportedChanges = new HashMap<>();
|
||||
mDebugLogAll = false;
|
||||
@@ -174,7 +177,7 @@ public final class ChangeReporter {
|
||||
private void debugLog(int uid, long changeId, int state) {
|
||||
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) {
|
||||
if (mSource == SOURCE_SYSTEM_SERVER) {
|
||||
Slog.d(TAG, message);
|
||||
} else {
|
||||
Log.d(TAG, message);
|
||||
@@ -183,21 +186,56 @@ public final class ChangeReporter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE enum to a string.
|
||||
* Transforms {@link #ChangeReporter.State} enum to a string.
|
||||
*
|
||||
* @param state to transform
|
||||
* @return a string representing the state
|
||||
*/
|
||||
private static String stateToString(int state) {
|
||||
private static String stateToString(@State int state) {
|
||||
switch (state) {
|
||||
case StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED:
|
||||
case STATE_LOGGED:
|
||||
return "LOGGED";
|
||||
case StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__ENABLED:
|
||||
case STATE_ENABLED:
|
||||
return "ENABLED";
|
||||
case StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__DISABLED:
|
||||
case STATE_DISABLED:
|
||||
return "DISABLED";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
/** These values should be kept in sync with those in atoms.proto */
|
||||
public static final int STATE_UNKNOWN_STATE =
|
||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__UNKNOWN_STATE;
|
||||
public static final int STATE_ENABLED =
|
||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__ENABLED;
|
||||
public static final int STATE_DISABLED =
|
||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__DISABLED;
|
||||
public static final int STATE_LOGGED =
|
||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED;
|
||||
public static final int SOURCE_UNKNOWN_SOURCE =
|
||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__UNKNOWN_SOURCE;
|
||||
public static final int SOURCE_APP_PROCESS =
|
||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__APP_PROCESS;
|
||||
public static final int SOURCE_SYSTEM_SERVER =
|
||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER;
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@IntDef(flag = true, prefix = { "STATE_" }, value = {
|
||||
STATE_UNKNOWN_STATE,
|
||||
STATE_ENABLED,
|
||||
STATE_DISABLED,
|
||||
STATE_LOGGED
|
||||
})
|
||||
public @interface State {
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@IntDef(flag = true, prefix = { "SOURCE_" }, value = {
|
||||
SOURCE_UNKNOWN_SOURCE,
|
||||
SOURCE_APP_PROCESS,
|
||||
SOURCE_SYSTEM_SERVER
|
||||
})
|
||||
public @interface Source {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,10 +24,10 @@ import org.junit.Test;
|
||||
public class ChangeReporterTest {
|
||||
@Test
|
||||
public void testStatsLogOnce() {
|
||||
ChangeReporter reporter = new ChangeReporter(0);
|
||||
ChangeReporter reporter = new ChangeReporter(ChangeReporter.SOURCE_UNKNOWN_SOURCE);
|
||||
int myUid = 1022, otherUid = 1023;
|
||||
long myChangeId = 500L, otherChangeId = 600L;
|
||||
int myState = 1, otherState = 2;
|
||||
int myState = ChangeReporter.STATE_ENABLED, otherState = ChangeReporter.STATE_DISABLED;
|
||||
|
||||
assertTrue(reporter.shouldWriteToStatsLog(myUid, myChangeId, myState));
|
||||
reporter.reportChange(myUid, myChangeId, myState);
|
||||
@@ -42,10 +42,10 @@ public class ChangeReporterTest {
|
||||
|
||||
@Test
|
||||
public void testStatsLogAfterReset() {
|
||||
ChangeReporter reporter = new ChangeReporter(0);
|
||||
ChangeReporter reporter = new ChangeReporter(ChangeReporter.SOURCE_UNKNOWN_SOURCE);
|
||||
int myUid = 1022;
|
||||
long myChangeId = 500L;
|
||||
int myState = 1;
|
||||
int myState = ChangeReporter.STATE_ENABLED;
|
||||
|
||||
assertTrue(reporter.shouldWriteToStatsLog(myUid, myChangeId, myState));
|
||||
reporter.reportChange(myUid, myChangeId, myState);
|
||||
@@ -60,10 +60,10 @@ public class ChangeReporterTest {
|
||||
|
||||
@Test
|
||||
public void testDebugLogOnce() {
|
||||
ChangeReporter reporter = new ChangeReporter(0);
|
||||
ChangeReporter reporter = new ChangeReporter(ChangeReporter.SOURCE_UNKNOWN_SOURCE);
|
||||
int myUid = 1022, otherUid = 1023;
|
||||
long myChangeId = 500L, otherChangeId = 600L;
|
||||
int myState = 1, otherState = 2;
|
||||
int myState = ChangeReporter.STATE_ENABLED, otherState = ChangeReporter.STATE_DISABLED;
|
||||
|
||||
assertTrue(reporter.shouldWriteToDebug(myUid, myChangeId, myState));
|
||||
reporter.reportChange(myUid, myChangeId, myState);
|
||||
@@ -78,10 +78,10 @@ public class ChangeReporterTest {
|
||||
|
||||
@Test
|
||||
public void testDebugLogAfterReset() {
|
||||
ChangeReporter reporter = new ChangeReporter(0);
|
||||
ChangeReporter reporter = new ChangeReporter(ChangeReporter.SOURCE_UNKNOWN_SOURCE);
|
||||
int myUid = 1022;
|
||||
long myChangeId = 500L;
|
||||
int myState = 1;
|
||||
int myState = ChangeReporter.STATE_ENABLED;
|
||||
|
||||
assertTrue(reporter.shouldWriteToDebug(myUid, myChangeId, myState));
|
||||
reporter.reportChange(myUid, myChangeId, myState);
|
||||
@@ -96,10 +96,10 @@ public class ChangeReporterTest {
|
||||
|
||||
@Test
|
||||
public void testDebugLogWithLogAll() {
|
||||
ChangeReporter reporter = new ChangeReporter(0);
|
||||
ChangeReporter reporter = new ChangeReporter(ChangeReporter.SOURCE_UNKNOWN_SOURCE);
|
||||
int myUid = 1022;
|
||||
long myChangeId = 500L;
|
||||
int myState = 1;
|
||||
int myState = ChangeReporter.STATE_ENABLED;
|
||||
|
||||
assertTrue(reporter.shouldWriteToDebug(myUid, myChangeId, myState));
|
||||
reporter.reportChange(myUid, myChangeId, myState);
|
||||
|
||||
@@ -25,7 +25,6 @@ import android.os.Binder;
|
||||
import android.os.RemoteException;
|
||||
import android.os.UserHandle;
|
||||
import android.util.Slog;
|
||||
import android.util.StatsLog;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.internal.compat.AndroidBuildClassifier;
|
||||
@@ -53,7 +52,7 @@ public class PlatformCompat extends IPlatformCompat.Stub {
|
||||
public PlatformCompat(Context context) {
|
||||
mContext = context;
|
||||
mChangeReporter = new ChangeReporter(
|
||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER);
|
||||
ChangeReporter.SOURCE_SYSTEM_SERVER);
|
||||
mCompatConfig = CompatConfig.create(new AndroidBuildClassifier(), mContext);
|
||||
}
|
||||
|
||||
@@ -61,14 +60,14 @@ public class PlatformCompat extends IPlatformCompat.Stub {
|
||||
PlatformCompat(Context context, CompatConfig compatConfig) {
|
||||
mContext = context;
|
||||
mChangeReporter = new ChangeReporter(
|
||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER);
|
||||
ChangeReporter.SOURCE_SYSTEM_SERVER);
|
||||
mCompatConfig = compatConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reportChange(long changeId, ApplicationInfo appInfo) {
|
||||
reportChange(changeId, appInfo.uid,
|
||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED);
|
||||
ChangeReporter.STATE_LOGGED);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -82,18 +81,18 @@ public class PlatformCompat extends IPlatformCompat.Stub {
|
||||
|
||||
@Override
|
||||
public void reportChangeByUid(long changeId, int uid) {
|
||||
reportChange(changeId, uid, StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED);
|
||||
reportChange(changeId, uid, ChangeReporter.STATE_LOGGED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isChangeEnabled(long changeId, ApplicationInfo appInfo) {
|
||||
if (mCompatConfig.isChangeEnabled(changeId, appInfo)) {
|
||||
reportChange(changeId, appInfo.uid,
|
||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__ENABLED);
|
||||
ChangeReporter.STATE_ENABLED);
|
||||
return true;
|
||||
}
|
||||
reportChange(changeId, appInfo.uid,
|
||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__DISABLED);
|
||||
ChangeReporter.STATE_DISABLED);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user