Extend MetricsFactory so it can log to different LogWriters
- Renamed MetricsFractory to MetricsFeatureProvider, and access it using FeatureFactory. - Instead of containing exactly 1 logWriter, MetricsFeatureProvider now contain a list. - Added OnAttach event in Lifecycle. This is needed when a mixin requires Context to initialize itself. - Updated tests. Bug: 29575437 Test: make RunSettingsRoboTests Change-Id: I7cc1528b9a744cd40088701e2bd115f41a8bf744
This commit is contained in:
@@ -13,33 +13,10 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.core.instrumentation;
|
||||
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
|
||||
public class MetricsFactory {
|
||||
|
||||
private static MetricsFactory sInstance;
|
||||
|
||||
private LogWriter mLogger;
|
||||
|
||||
public static MetricsFactory get() {
|
||||
if (sInstance == null) {
|
||||
sInstance = new MetricsFactory();
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
public LogWriter getLogger() {
|
||||
if (mLogger == null) {
|
||||
mLogger = new EventLogWriter();
|
||||
}
|
||||
return mLogger;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void setLogger(LogWriter logger) {
|
||||
mLogger = logger;
|
||||
}
|
||||
/**
|
||||
* FeatureProvider for metrics.
|
||||
*/
|
||||
public interface MetricsFeatureProvider extends LogWriter {
|
||||
}
|
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* 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;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Implementation for {@link MetricsFeatureProvider}
|
||||
*/
|
||||
public class MetricsFeatureProviderImpl implements MetricsFeatureProvider {
|
||||
|
||||
private List<LogWriter> mLoggerWriters;
|
||||
|
||||
public MetricsFeatureProviderImpl() {
|
||||
mLoggerWriters = new ArrayList<>();
|
||||
installLogWriters();
|
||||
}
|
||||
|
||||
protected void installLogWriters() {
|
||||
mLoggerWriters.add(new EventLogWriter());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visible(Context context, int category) {
|
||||
for (LogWriter writer : mLoggerWriters) {
|
||||
writer.visible(context, category);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hidden(Context context, int category) {
|
||||
for (LogWriter writer : mLoggerWriters) {
|
||||
writer.hidden(context, category);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void action(Context context, int category) {
|
||||
for (LogWriter writer : mLoggerWriters) {
|
||||
writer.action(context, category);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void action(Context context, int category, int value) {
|
||||
for (LogWriter writer : mLoggerWriters) {
|
||||
writer.action(context, category, value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void action(Context context, int category, boolean value) {
|
||||
for (LogWriter writer : mLoggerWriters) {
|
||||
writer.action(context, category, value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void action(Context context, int category, String pkg) {
|
||||
for (LogWriter writer : mLoggerWriters) {
|
||||
writer.action(context, category, pkg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void count(Context context, String name, int value) {
|
||||
for (LogWriter writer : mLoggerWriters) {
|
||||
writer.count(context, name, value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void histogram(Context context, String name, int bucket) {
|
||||
for (LogWriter writer : mLoggerWriters) {
|
||||
writer.histogram(context, name, bucket);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void addLogWriter(LogWriter logWriter) {
|
||||
mLoggerWriters.add(logWriter);
|
||||
}
|
||||
}
|
@@ -23,6 +23,7 @@ import android.os.AsyncTask;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.android.internal.logging.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -31,12 +32,12 @@ public class SharedPreferencesLogger implements SharedPreferences {
|
||||
|
||||
private final String mTag;
|
||||
private final Context mContext;
|
||||
private final LogWriter mLogWriter;
|
||||
private final MetricsFeatureProvider mMetricsFeature;
|
||||
|
||||
public SharedPreferencesLogger(Context context, String tag) {
|
||||
mContext = context;
|
||||
mTag = tag;
|
||||
mLogWriter = MetricsFactory.get().getLogger();
|
||||
mMetricsFeature = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -95,12 +96,12 @@ public class SharedPreferencesLogger implements SharedPreferences {
|
||||
}
|
||||
|
||||
private void logValue(String key, String value) {
|
||||
mLogWriter.count(mContext, mTag + "/" + key + "|" + value, 1);
|
||||
mMetricsFeature.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,
|
||||
mMetricsFeature.count(mContext, mTag + "/" + key, 1);
|
||||
mMetricsFeature.action(mContext, MetricsEvent.ACTION_GENERIC_PACKAGE,
|
||||
mTag + "/" + key + "|" + value);
|
||||
}
|
||||
|
||||
|
@@ -16,34 +16,45 @@
|
||||
|
||||
package com.android.settings.core.instrumentation;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settings.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settings.core.lifecycle.events.OnAttach;
|
||||
import com.android.settings.core.lifecycle.events.OnPause;
|
||||
import com.android.settings.core.lifecycle.events.OnResume;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
|
||||
/**
|
||||
* Logs visibility change of a fragment.
|
||||
*/
|
||||
public class VisibilityLoggerMixin implements LifecycleObserver, OnResume, OnPause {
|
||||
public class VisibilityLoggerMixin implements LifecycleObserver, OnResume, OnPause, OnAttach {
|
||||
|
||||
private final int mMetricsCategory;
|
||||
private final LogWriter mLogWriter;
|
||||
|
||||
private MetricsFeatureProvider mMetricsFeature;
|
||||
|
||||
public VisibilityLoggerMixin(int metricsCategory) {
|
||||
this(metricsCategory, MetricsFactory.get().getLogger());
|
||||
// MetricsFeature will be set during onAttach.
|
||||
this(metricsCategory, null /* metricsFeature */);
|
||||
}
|
||||
|
||||
public VisibilityLoggerMixin(int metricsCategory, LogWriter logWriter) {
|
||||
public VisibilityLoggerMixin(int metricsCategory, MetricsFeatureProvider metricsFeature) {
|
||||
mMetricsCategory = metricsCategory;
|
||||
mLogWriter = logWriter;
|
||||
mMetricsFeature = metricsFeature;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
mMetricsFeature = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
mLogWriter.visible(null /* context */, mMetricsCategory);
|
||||
mMetricsFeature.visible(null /* context */, mMetricsCategory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
mLogWriter.hidden(null /* context */, mMetricsCategory);
|
||||
mMetricsFeature.hidden(null /* context */, mMetricsCategory);
|
||||
}
|
||||
}
|
||||
|
@@ -16,7 +16,9 @@
|
||||
package com.android.settings.core.lifecycle;
|
||||
|
||||
import android.annotation.UiThread;
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settings.core.lifecycle.events.OnAttach;
|
||||
import com.android.settings.core.lifecycle.events.OnDestroy;
|
||||
import com.android.settings.core.lifecycle.events.OnPause;
|
||||
import com.android.settings.core.lifecycle.events.OnResume;
|
||||
@@ -44,6 +46,14 @@ public class Lifecycle {
|
||||
return observer;
|
||||
}
|
||||
|
||||
public void onAttach(Context context) {
|
||||
for (LifecycleObserver observer : mObservers) {
|
||||
if (observer instanceof OnAttach) {
|
||||
((OnAttach) observer).onAttach(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onStart() {
|
||||
for (LifecycleObserver observer : mObservers) {
|
||||
if (observer instanceof OnStart) {
|
||||
|
@@ -15,7 +15,10 @@
|
||||
*/
|
||||
package com.android.settings.core.lifecycle;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.os.PersistableBundle;
|
||||
|
||||
/**
|
||||
* {@link Activity} that has hooks to observe activity lifecycle events.
|
||||
@@ -28,6 +31,19 @@ public class ObservableActivity extends Activity {
|
||||
return mLifecycle;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
mLifecycle.onAttach(this);
|
||||
super.onCreate(savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState,
|
||||
@Nullable PersistableBundle persistentState) {
|
||||
mLifecycle.onAttach(this);
|
||||
super.onCreate(savedInstanceState, persistentState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
mLifecycle.onStart();
|
||||
|
@@ -16,6 +16,7 @@
|
||||
package com.android.settings.core.lifecycle;
|
||||
|
||||
import android.app.DialogFragment;
|
||||
import android.content.Context;
|
||||
|
||||
/**
|
||||
* {@link DialogFragment} that has hooks to observe fragment lifecycle events.
|
||||
@@ -24,6 +25,12 @@ public class ObservableDialogFragment extends DialogFragment {
|
||||
|
||||
protected final Lifecycle mLifecycle = new Lifecycle();
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
mLifecycle.onAttach(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
mLifecycle.onStart();
|
||||
|
@@ -17,6 +17,7 @@ package com.android.settings.core.lifecycle;
|
||||
|
||||
|
||||
import android.annotation.CallSuper;
|
||||
import android.content.Context;
|
||||
import android.support.v14.preference.PreferenceFragment;
|
||||
|
||||
/**
|
||||
@@ -30,6 +31,13 @@ public abstract class ObservablePreferenceFragment extends PreferenceFragment {
|
||||
return mLifecycle;
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
mLifecycle.onAttach(context);
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
@Override
|
||||
public void onStart() {
|
||||
|
22
src/com/android/settings/core/lifecycle/events/OnAttach.java
Normal file
22
src/com/android/settings/core/lifecycle/events/OnAttach.java
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.lifecycle.events;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
public interface OnAttach {
|
||||
void onAttach(Context context);
|
||||
}
|
@@ -21,6 +21,7 @@ import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
|
||||
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
|
||||
|
||||
/**
|
||||
@@ -62,6 +63,8 @@ public abstract class FeatureFactory {
|
||||
|
||||
public abstract SupportFeatureProvider getSupportFeatureProvider(Context context);
|
||||
|
||||
public abstract MetricsFeatureProvider getMetricsFeatureProvider();
|
||||
|
||||
public abstract PowerUsageFeatureProvider getPowerUsageFeatureProvider();
|
||||
|
||||
public static final class FactoryNotFoundException extends RuntimeException {
|
||||
|
@@ -18,6 +18,9 @@ package com.android.settings.overlay;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.Keep;
|
||||
|
||||
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
|
||||
import com.android.settings.core.instrumentation.MetricsFeatureProviderImpl;
|
||||
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
|
||||
|
||||
/**
|
||||
@@ -26,11 +29,21 @@ import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
|
||||
@Keep
|
||||
public final class FeatureFactoryImpl extends FeatureFactory {
|
||||
|
||||
private MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
|
||||
@Override
|
||||
public SupportFeatureProvider getSupportFeatureProvider(Context context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MetricsFeatureProvider getMetricsFeatureProvider() {
|
||||
if (mMetricsFeatureProvider == null) {
|
||||
mMetricsFeatureProvider = new MetricsFeatureProviderImpl();
|
||||
}
|
||||
return mMetricsFeatureProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PowerUsageFeatureProvider getPowerUsageFeatureProvider() {
|
||||
return null;
|
||||
|
Reference in New Issue
Block a user