Loading src/com/android/settings/core/instrumentation/SharedPreferencesLogger.java +12 −1 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.os.AsyncTask; import android.text.TextUtils; import android.util.ArraySet; import com.android.internal.logging.MetricsProto.MetricsEvent; import com.android.settings.overlay.FeatureFactory; Loading @@ -33,11 +34,13 @@ public class SharedPreferencesLogger implements SharedPreferences { private final String mTag; private final Context mContext; private final MetricsFeatureProvider mMetricsFeature; private final Set<String> mPreferenceKeySet; public SharedPreferencesLogger(Context context, String tag) { mContext = context; mTag = tag; mMetricsFeature = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); mPreferenceKeySet = new ArraySet<>(); } @Override Loading Loading @@ -96,7 +99,15 @@ public class SharedPreferencesLogger implements SharedPreferences { } private void logValue(String key, String value) { mMetricsFeature.count(mContext, mTag + "/" + key + "|" + value, 1); final String prefKey = mTag + "/" + key; if (!mPreferenceKeySet.contains(prefKey)) { // Pref key doesn't exist in set, this is initial display so we skip metrics but // keeps track of this key. mPreferenceKeySet.add(prefKey); return; } // Pref key exists in set, log it's change in metrics. mMetricsFeature.count(mContext, prefKey + "|" + value, 1); } private void logPackageName(String key, String value) { Loading tests/robotests/src/com/android/settings/core/instrumentation/SharedPreferenceLoggerTest.java +40 −13 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ package com.android.settings.core.instrumentation; import android.content.Context; import android.content.SharedPreferences; import com.android.settings.TestConfig; import com.android.settings.overlay.FeatureFactory; Loading @@ -32,7 +33,7 @@ import org.robolectric.shadows.ShadowApplication; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @RunWith(RobolectricTestRunner.class) Loading Loading @@ -62,27 +63,53 @@ public class SharedPreferenceLoggerTest { } @Test public void putInt_shouldLogCount() { mSharedPrefLogger.edit().putInt(TEST_KEY, 1); verify(mLogWriter).count(any(Context.class), anyString(), anyInt()); public void putInt_shouldNotLogInitialPut() { final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); editor.putInt(TEST_KEY, 1); editor.putInt(TEST_KEY, 1); editor.putInt(TEST_KEY, 1); editor.putInt(TEST_KEY, 2); editor.putInt(TEST_KEY, 2); editor.putInt(TEST_KEY, 2); editor.putInt(TEST_KEY, 2); verify(mLogWriter, times(6)).count(any(Context.class), anyString(), anyInt()); } @Test public void putBoolean_shouldLogCount() { mSharedPrefLogger.edit().putBoolean(TEST_KEY, true); verify(mLogWriter).count(any(Context.class), anyString(), anyInt()); public void putBoolean_shouldNotLogInitialPut() { final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); editor.putBoolean(TEST_KEY, true); editor.putBoolean(TEST_KEY, true); editor.putBoolean(TEST_KEY, false); editor.putBoolean(TEST_KEY, false); editor.putBoolean(TEST_KEY, false); verify(mLogWriter, times(4)).count(any(Context.class), anyString(), anyInt()); } @Test public void putLong_shouldLogCount() { mSharedPrefLogger.edit().putLong(TEST_KEY, 1); verify(mLogWriter).count(any(Context.class), anyString(), anyInt()); public void putLong_shouldNotLogInitialPut() { final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); editor.putLong(TEST_KEY, 1); editor.putLong(TEST_KEY, 1); editor.putLong(TEST_KEY, 1); editor.putLong(TEST_KEY, 1); editor.putLong(TEST_KEY, 2); verify(mLogWriter, times(4)).count(any(Context.class), anyString(), anyInt()); } @Test public void putFloat_shouldLogCount() { mSharedPrefLogger.edit().putInt(TEST_KEY, 1); verify(mLogWriter).count(any(Context.class), anyString(), anyInt()); public void putFloat_shouldNotLogInitialPut() { final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); editor.putFloat(TEST_KEY, 1); editor.putFloat(TEST_KEY, 1); editor.putFloat(TEST_KEY, 1); editor.putFloat(TEST_KEY, 1); editor.putFloat(TEST_KEY, 2); verify(mLogWriter, times(4)).count(any(Context.class), anyString(), anyInt()); } } Loading
src/com/android/settings/core/instrumentation/SharedPreferencesLogger.java +12 −1 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.os.AsyncTask; import android.text.TextUtils; import android.util.ArraySet; import com.android.internal.logging.MetricsProto.MetricsEvent; import com.android.settings.overlay.FeatureFactory; Loading @@ -33,11 +34,13 @@ public class SharedPreferencesLogger implements SharedPreferences { private final String mTag; private final Context mContext; private final MetricsFeatureProvider mMetricsFeature; private final Set<String> mPreferenceKeySet; public SharedPreferencesLogger(Context context, String tag) { mContext = context; mTag = tag; mMetricsFeature = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); mPreferenceKeySet = new ArraySet<>(); } @Override Loading Loading @@ -96,7 +99,15 @@ public class SharedPreferencesLogger implements SharedPreferences { } private void logValue(String key, String value) { mMetricsFeature.count(mContext, mTag + "/" + key + "|" + value, 1); final String prefKey = mTag + "/" + key; if (!mPreferenceKeySet.contains(prefKey)) { // Pref key doesn't exist in set, this is initial display so we skip metrics but // keeps track of this key. mPreferenceKeySet.add(prefKey); return; } // Pref key exists in set, log it's change in metrics. mMetricsFeature.count(mContext, prefKey + "|" + value, 1); } private void logPackageName(String key, String value) { Loading
tests/robotests/src/com/android/settings/core/instrumentation/SharedPreferenceLoggerTest.java +40 −13 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ package com.android.settings.core.instrumentation; import android.content.Context; import android.content.SharedPreferences; import com.android.settings.TestConfig; import com.android.settings.overlay.FeatureFactory; Loading @@ -32,7 +33,7 @@ import org.robolectric.shadows.ShadowApplication; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @RunWith(RobolectricTestRunner.class) Loading Loading @@ -62,27 +63,53 @@ public class SharedPreferenceLoggerTest { } @Test public void putInt_shouldLogCount() { mSharedPrefLogger.edit().putInt(TEST_KEY, 1); verify(mLogWriter).count(any(Context.class), anyString(), anyInt()); public void putInt_shouldNotLogInitialPut() { final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); editor.putInt(TEST_KEY, 1); editor.putInt(TEST_KEY, 1); editor.putInt(TEST_KEY, 1); editor.putInt(TEST_KEY, 2); editor.putInt(TEST_KEY, 2); editor.putInt(TEST_KEY, 2); editor.putInt(TEST_KEY, 2); verify(mLogWriter, times(6)).count(any(Context.class), anyString(), anyInt()); } @Test public void putBoolean_shouldLogCount() { mSharedPrefLogger.edit().putBoolean(TEST_KEY, true); verify(mLogWriter).count(any(Context.class), anyString(), anyInt()); public void putBoolean_shouldNotLogInitialPut() { final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); editor.putBoolean(TEST_KEY, true); editor.putBoolean(TEST_KEY, true); editor.putBoolean(TEST_KEY, false); editor.putBoolean(TEST_KEY, false); editor.putBoolean(TEST_KEY, false); verify(mLogWriter, times(4)).count(any(Context.class), anyString(), anyInt()); } @Test public void putLong_shouldLogCount() { mSharedPrefLogger.edit().putLong(TEST_KEY, 1); verify(mLogWriter).count(any(Context.class), anyString(), anyInt()); public void putLong_shouldNotLogInitialPut() { final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); editor.putLong(TEST_KEY, 1); editor.putLong(TEST_KEY, 1); editor.putLong(TEST_KEY, 1); editor.putLong(TEST_KEY, 1); editor.putLong(TEST_KEY, 2); verify(mLogWriter, times(4)).count(any(Context.class), anyString(), anyInt()); } @Test public void putFloat_shouldLogCount() { mSharedPrefLogger.edit().putInt(TEST_KEY, 1); verify(mLogWriter).count(any(Context.class), anyString(), anyInt()); public void putFloat_shouldNotLogInitialPut() { final SharedPreferences.Editor editor = mSharedPrefLogger.edit(); editor.putFloat(TEST_KEY, 1); editor.putFloat(TEST_KEY, 1); editor.putFloat(TEST_KEY, 1); editor.putFloat(TEST_KEY, 1); editor.putFloat(TEST_KEY, 2); verify(mLogWriter, times(4)).count(any(Context.class), anyString(), anyInt()); } }