Move SharedPreferenceLogger to instrumentation package.

Bug: 30914916
Test: SettingsUnitTests and RunSettingsRoboTests

- Added interface to abstract logger
- Added test for SharePrefLogger
- Moved Existing tests for instrumentation to robotests.

Change-Id: I2b431ea4b0fd09d0f11389d8b9181448f08a52c5
This commit is contained in:
Fan Zhang
2016-08-18 16:04:19 -07:00
parent cbb2f5f498
commit 5f79ae9677
9 changed files with 184 additions and 26 deletions

View File

@@ -21,9 +21,9 @@ import android.content.Context;
import com.android.internal.logging.MetricsLogger;
/**
* Pass-through Metrics logger for {@link MetricsLogger}.
* {@link LogWriter} that writes data to eventlog.
*/
public class EventLogWriter {
public class EventLogWriter implements LogWriter {
public void visible(Context context, int category) {
MetricsLogger.visible(context, category);

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.core.instrumentation;
import android.content.Context;
/**
* Generic log writer interface.
*/
public interface LogWriter {
/**
* Logs a visibility event when view becomes visible.
*/
void visible(Context context, int category);
/**
* Logs a visibility event when view becomes hidden.
*/
void hidden(Context context, int category);
/**
* Logs an user action.
*/
void action(Context context, int category);
/**
* Logs an user action.
*/
void action(Context context, int category, int value);
/**
* Logs an user action.
*/
void action(Context context, int category, boolean value);
/**
* Logs an user action.
*/
void action(Context context, int category, String pkg);
/**
* Logs a count.
*/
void count(Context context, String name, int value);
/**
* Logs a histogram event.
*/
void histogram(Context context, String name, int bucket);
}

View File

@@ -16,11 +16,13 @@
package com.android.settings.core.instrumentation;
import android.support.annotation.VisibleForTesting;
public class MetricsFactory {
private static MetricsFactory sInstance;
private EventLogWriter mLogger;
private LogWriter mLogger;
public static MetricsFactory get() {
if (sInstance == null) {
@@ -29,10 +31,15 @@ public class MetricsFactory {
return sInstance;
}
public EventLogWriter getLogger() {
public LogWriter getLogger() {
if (mLogger == null) {
mLogger = new EventLogWriter();
}
return mLogger;
}
@VisibleForTesting
void setLogger(LogWriter logger) {
mLogger = logger;
}
}

View File

@@ -0,0 +1,192 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.android.settings.core.instrumentation;
import android.annotation.Nullable;
import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.text.TextUtils;
import com.android.internal.logging.MetricsProto.MetricsEvent;
import java.util.Map;
import java.util.Set;
public class SharedPreferencesLogger implements SharedPreferences {
private final String mTag;
private final Context mContext;
private final LogWriter mLogWriter;
public SharedPreferencesLogger(Context context, String tag) {
mContext = context;
mTag = tag;
mLogWriter = MetricsFactory.get().getLogger();
}
@Override
public Map<String, ?> getAll() {
return null;
}
@Override
public String getString(String key, @Nullable String defValue) {
return defValue;
}
@Override
public Set<String> getStringSet(String key, @Nullable Set<String> defValues) {
return defValues;
}
@Override
public int getInt(String key, int defValue) {
return defValue;
}
@Override
public long getLong(String key, long defValue) {
return defValue;
}
@Override
public float getFloat(String key, float defValue) {
return defValue;
}
@Override
public boolean getBoolean(String key, boolean defValue) {
return defValue;
}
@Override
public boolean contains(String key) {
return false;
}
@Override
public Editor edit() {
return new EditorLogger();
}
@Override
public void registerOnSharedPreferenceChangeListener(
OnSharedPreferenceChangeListener listener) {
}
@Override
public void unregisterOnSharedPreferenceChangeListener(
OnSharedPreferenceChangeListener listener) {
}
private void logValue(String key, String value) {
mLogWriter.count(mContext, mTag + "/" + key + "|" + value, 1);
}
private void logPackageName(String key, String value) {
mLogWriter.count(mContext, mTag + "/" + key, 1);
mLogWriter.action(mContext, MetricsEvent.ACTION_GENERIC_PACKAGE,
mTag + "/" + key + "|" + value);
}
private void safeLogValue(String key, String value) {
new AsyncPackageCheck().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, key, value);
}
private class AsyncPackageCheck extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
String key = params[0];
String value = params[1];
PackageManager pm = mContext.getPackageManager();
try {
// Check if this might be a component.
ComponentName name = ComponentName.unflattenFromString(value);
if (value != null) {
value = name.getPackageName();
}
} catch (Exception e) {
}
try {
pm.getPackageInfo(value, PackageManager.MATCH_UNINSTALLED_PACKAGES);
logPackageName(key, value);
} catch (PackageManager.NameNotFoundException e) {
// Clearly not a package, lets log it.
logValue(key, value);
}
return null;
}
}
public class EditorLogger implements Editor {
@Override
public Editor putString(String key, @Nullable String value) {
safeLogValue(key, value);
return this;
}
@Override
public Editor putStringSet(String key, @Nullable Set<String> values) {
safeLogValue(key, TextUtils.join(",", values));
return this;
}
@Override
public Editor putInt(String key, int value) {
logValue(key, String.valueOf(value));
return this;
}
@Override
public Editor putLong(String key, long value) {
logValue(key, String.valueOf(value));
return this;
}
@Override
public Editor putFloat(String key, float value) {
logValue(key, String.valueOf(value));
return this;
}
@Override
public Editor putBoolean(String key, boolean value) {
logValue(key, String.valueOf(value));
return this;
}
@Override
public Editor remove(String key) {
return this;
}
@Override
public Editor clear() {
return this;
}
@Override
public boolean commit() {
return true;
}
@Override
public void apply() {
}
}
}

View File

@@ -24,22 +24,22 @@ import android.content.Context;
public class VisibilityLoggerMixin {
private final Instrumentable mInstrumentable;
private final EventLogWriter mEventLogWriter;
private final LogWriter mLogWriter;
public VisibilityLoggerMixin(Instrumentable instrumentable) {
this(instrumentable, MetricsFactory.get().getLogger());
}
public VisibilityLoggerMixin(Instrumentable instrumentable, EventLogWriter eventLogWriter) {
public VisibilityLoggerMixin(Instrumentable instrumentable, LogWriter logWriter) {
mInstrumentable = instrumentable;
mEventLogWriter = eventLogWriter;
mLogWriter = logWriter;
}
public void onResume(Context context) {
mEventLogWriter.visible(context, mInstrumentable.getMetricsCategory());
mLogWriter.visible(context, mInstrumentable.getMetricsCategory());
}
public void onPause(Context context) {
mEventLogWriter.hidden(context, mInstrumentable.getMetricsCategory());
mLogWriter.hidden(context, mInstrumentable.getMetricsCategory());
}
}