Merge "Minor refactor of ChangeReporter and PlatformCompat" am: 55d16fac97
Change-Id: I691696fa0ffa50c87bb3afb26173f4cc595453a2
This commit is contained in:
@@ -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.StatsLog;
|
|
||||||
|
|
||||||
import com.android.internal.compat.ChangeReporter;
|
import com.android.internal.compat.ChangeReporter;
|
||||||
|
|
||||||
@@ -46,20 +45,20 @@ public final class AppCompatCallbacks extends Compatibility.Callbacks {
|
|||||||
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);
|
ChangeReporter.SOURCE_APP_PROCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void reportChange(long changeId) {
|
protected void reportChange(long changeId) {
|
||||||
reportChange(changeId, StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED);
|
reportChange(changeId, ChangeReporter.STATE_LOGGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean isChangeEnabled(long changeId) {
|
protected boolean isChangeEnabled(long changeId) {
|
||||||
if (Arrays.binarySearch(mDisabledChanges, changeId) < 0) {
|
if (Arrays.binarySearch(mDisabledChanges, changeId) < 0) {
|
||||||
// Not present in the disabled array
|
// Not present in the disabled array
|
||||||
reportChange(changeId, StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__ENABLED);
|
reportChange(changeId, ChangeReporter.STATE_ENABLED);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
reportChange(changeId, StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__DISABLED);
|
reportChange(changeId, ChangeReporter.STATE_DISABLED);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.android.internal.compat;
|
package com.android.internal.compat;
|
||||||
|
|
||||||
|
import android.annotation.IntDef;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.Slog;
|
import android.util.Slog;
|
||||||
import android.util.StatsLog;
|
import android.util.StatsLog;
|
||||||
@@ -23,6 +24,8 @@ import android.util.StatsLog;
|
|||||||
import com.android.internal.annotations.GuardedBy;
|
import com.android.internal.annotations.GuardedBy;
|
||||||
import com.android.internal.annotations.VisibleForTesting;
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -42,7 +45,7 @@ public final class ChangeReporter {
|
|||||||
long mChangeId;
|
long mChangeId;
|
||||||
int mState;
|
int mState;
|
||||||
|
|
||||||
ChangeReport(long changeId, int state) {
|
ChangeReport(long changeId, @State int state) {
|
||||||
mChangeId = changeId;
|
mChangeId = changeId;
|
||||||
mState = state;
|
mState = state;
|
||||||
}
|
}
|
||||||
@@ -69,7 +72,7 @@ public final class ChangeReporter {
|
|||||||
// When true will of every time to debug (logcat).
|
// When true will of every time to debug (logcat).
|
||||||
private boolean mDebugLogAll;
|
private boolean mDebugLogAll;
|
||||||
|
|
||||||
public ChangeReporter(int source) {
|
public ChangeReporter(@Source int source) {
|
||||||
mSource = source;
|
mSource = source;
|
||||||
mReportedChanges = new HashMap<>();
|
mReportedChanges = new HashMap<>();
|
||||||
mDebugLogAll = false;
|
mDebugLogAll = false;
|
||||||
@@ -174,7 +177,7 @@ public final class ChangeReporter {
|
|||||||
private void debugLog(int uid, long changeId, int state) {
|
private void debugLog(int uid, long changeId, int state) {
|
||||||
String message = String.format("Compat change id reported: %d; UID %d; state: %s", changeId,
|
String message = String.format("Compat change id reported: %d; UID %d; state: %s", changeId,
|
||||||
uid, stateToString(state));
|
uid, stateToString(state));
|
||||||
if (mSource == StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER) {
|
if (mSource == SOURCE_SYSTEM_SERVER) {
|
||||||
Slog.d(TAG, message);
|
Slog.d(TAG, message);
|
||||||
} else {
|
} else {
|
||||||
Log.d(TAG, message);
|
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
|
* @param state to transform
|
||||||
* @return a string representing the state
|
* @return a string representing the state
|
||||||
*/
|
*/
|
||||||
private static String stateToString(int state) {
|
private static String stateToString(@State int state) {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED:
|
case STATE_LOGGED:
|
||||||
return "LOGGED";
|
return "LOGGED";
|
||||||
case StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__ENABLED:
|
case STATE_ENABLED:
|
||||||
return "ENABLED";
|
return "ENABLED";
|
||||||
case StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__DISABLED:
|
case STATE_DISABLED:
|
||||||
return "DISABLED";
|
return "DISABLED";
|
||||||
default:
|
default:
|
||||||
return "UNKNOWN";
|
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 {
|
public class ChangeReporterTest {
|
||||||
@Test
|
@Test
|
||||||
public void testStatsLogOnce() {
|
public void testStatsLogOnce() {
|
||||||
ChangeReporter reporter = new ChangeReporter(0);
|
ChangeReporter reporter = new ChangeReporter(ChangeReporter.SOURCE_UNKNOWN_SOURCE);
|
||||||
int myUid = 1022, otherUid = 1023;
|
int myUid = 1022, otherUid = 1023;
|
||||||
long myChangeId = 500L, otherChangeId = 600L;
|
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));
|
assertTrue(reporter.shouldWriteToStatsLog(myUid, myChangeId, myState));
|
||||||
reporter.reportChange(myUid, myChangeId, myState);
|
reporter.reportChange(myUid, myChangeId, myState);
|
||||||
@@ -42,10 +42,10 @@ public class ChangeReporterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStatsLogAfterReset() {
|
public void testStatsLogAfterReset() {
|
||||||
ChangeReporter reporter = new ChangeReporter(0);
|
ChangeReporter reporter = new ChangeReporter(ChangeReporter.SOURCE_UNKNOWN_SOURCE);
|
||||||
int myUid = 1022;
|
int myUid = 1022;
|
||||||
long myChangeId = 500L;
|
long myChangeId = 500L;
|
||||||
int myState = 1;
|
int myState = ChangeReporter.STATE_ENABLED;
|
||||||
|
|
||||||
assertTrue(reporter.shouldWriteToStatsLog(myUid, myChangeId, myState));
|
assertTrue(reporter.shouldWriteToStatsLog(myUid, myChangeId, myState));
|
||||||
reporter.reportChange(myUid, myChangeId, myState);
|
reporter.reportChange(myUid, myChangeId, myState);
|
||||||
@@ -60,10 +60,10 @@ public class ChangeReporterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDebugLogOnce() {
|
public void testDebugLogOnce() {
|
||||||
ChangeReporter reporter = new ChangeReporter(0);
|
ChangeReporter reporter = new ChangeReporter(ChangeReporter.SOURCE_UNKNOWN_SOURCE);
|
||||||
int myUid = 1022, otherUid = 1023;
|
int myUid = 1022, otherUid = 1023;
|
||||||
long myChangeId = 500L, otherChangeId = 600L;
|
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));
|
assertTrue(reporter.shouldWriteToDebug(myUid, myChangeId, myState));
|
||||||
reporter.reportChange(myUid, myChangeId, myState);
|
reporter.reportChange(myUid, myChangeId, myState);
|
||||||
@@ -78,10 +78,10 @@ public class ChangeReporterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDebugLogAfterReset() {
|
public void testDebugLogAfterReset() {
|
||||||
ChangeReporter reporter = new ChangeReporter(0);
|
ChangeReporter reporter = new ChangeReporter(ChangeReporter.SOURCE_UNKNOWN_SOURCE);
|
||||||
int myUid = 1022;
|
int myUid = 1022;
|
||||||
long myChangeId = 500L;
|
long myChangeId = 500L;
|
||||||
int myState = 1;
|
int myState = ChangeReporter.STATE_ENABLED;
|
||||||
|
|
||||||
assertTrue(reporter.shouldWriteToDebug(myUid, myChangeId, myState));
|
assertTrue(reporter.shouldWriteToDebug(myUid, myChangeId, myState));
|
||||||
reporter.reportChange(myUid, myChangeId, myState);
|
reporter.reportChange(myUid, myChangeId, myState);
|
||||||
@@ -96,10 +96,10 @@ public class ChangeReporterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDebugLogWithLogAll() {
|
public void testDebugLogWithLogAll() {
|
||||||
ChangeReporter reporter = new ChangeReporter(0);
|
ChangeReporter reporter = new ChangeReporter(ChangeReporter.SOURCE_UNKNOWN_SOURCE);
|
||||||
int myUid = 1022;
|
int myUid = 1022;
|
||||||
long myChangeId = 500L;
|
long myChangeId = 500L;
|
||||||
int myState = 1;
|
int myState = ChangeReporter.STATE_ENABLED;
|
||||||
|
|
||||||
assertTrue(reporter.shouldWriteToDebug(myUid, myChangeId, myState));
|
assertTrue(reporter.shouldWriteToDebug(myUid, myChangeId, myState));
|
||||||
reporter.reportChange(myUid, myChangeId, myState);
|
reporter.reportChange(myUid, myChangeId, myState);
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import android.os.Binder;
|
|||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.util.Slog;
|
import android.util.Slog;
|
||||||
import android.util.StatsLog;
|
|
||||||
|
|
||||||
import com.android.internal.annotations.VisibleForTesting;
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
import com.android.internal.compat.AndroidBuildClassifier;
|
import com.android.internal.compat.AndroidBuildClassifier;
|
||||||
@@ -53,7 +52,7 @@ 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);
|
ChangeReporter.SOURCE_SYSTEM_SERVER);
|
||||||
mCompatConfig = CompatConfig.create(new AndroidBuildClassifier(), mContext);
|
mCompatConfig = CompatConfig.create(new AndroidBuildClassifier(), mContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,14 +60,14 @@ public class PlatformCompat extends IPlatformCompat.Stub {
|
|||||||
PlatformCompat(Context context, CompatConfig compatConfig) {
|
PlatformCompat(Context context, CompatConfig compatConfig) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mChangeReporter = new ChangeReporter(
|
mChangeReporter = new ChangeReporter(
|
||||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER);
|
ChangeReporter.SOURCE_SYSTEM_SERVER);
|
||||||
mCompatConfig = compatConfig;
|
mCompatConfig = compatConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reportChange(long changeId, ApplicationInfo appInfo) {
|
public void reportChange(long changeId, ApplicationInfo appInfo) {
|
||||||
reportChange(changeId, appInfo.uid,
|
reportChange(changeId, appInfo.uid,
|
||||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED);
|
ChangeReporter.STATE_LOGGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -82,18 +81,18 @@ public class PlatformCompat extends IPlatformCompat.Stub {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reportChangeByUid(long changeId, int uid) {
|
public void reportChangeByUid(long changeId, int uid) {
|
||||||
reportChange(changeId, uid, StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED);
|
reportChange(changeId, uid, ChangeReporter.STATE_LOGGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isChangeEnabled(long changeId, ApplicationInfo appInfo) {
|
public boolean isChangeEnabled(long changeId, ApplicationInfo appInfo) {
|
||||||
if (mCompatConfig.isChangeEnabled(changeId, appInfo)) {
|
if (mCompatConfig.isChangeEnabled(changeId, appInfo)) {
|
||||||
reportChange(changeId, appInfo.uid,
|
reportChange(changeId, appInfo.uid,
|
||||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__ENABLED);
|
ChangeReporter.STATE_ENABLED);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
reportChange(changeId, appInfo.uid,
|
reportChange(changeId, appInfo.uid,
|
||||||
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__DISABLED);
|
ChangeReporter.STATE_DISABLED);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user