1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.core; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 22 import com.android.settings.core.instrumentation.Instrumentable; 23 import com.android.settings.core.instrumentation.MetricsFeatureProvider; 24 import com.android.settings.core.instrumentation.VisibilityLoggerMixin; 25 import com.android.settings.core.lifecycle.ObservablePreferenceFragment; 26 import com.android.settings.overlay.FeatureFactory; 27 import com.android.settings.survey.SurveyMixin; 28 29 /** 30 * Instrumented fragment that logs visibility state. 31 */ 32 public abstract class InstrumentedPreferenceFragment extends ObservablePreferenceFragment 33 implements Instrumentable { 34 35 protected MetricsFeatureProvider mMetricsFeatureProvider; 36 37 // metrics placeholder value. Only use this for development. 38 protected final int PLACEHOLDER_METRIC = 10000; 39 40 private final VisibilityLoggerMixin mVisibilityLoggerMixin; 41 42 public InstrumentedPreferenceFragment() { 43 // Mixin that logs visibility change for activity. 44 mVisibilityLoggerMixin = new VisibilityLoggerMixin(getMetricsCategory()); 45 getLifecycle().addObserver(mVisibilityLoggerMixin); 46 getLifecycle().addObserver(new SurveyMixin(this, getClass().getSimpleName())); 47 } 48 49 @Override 50 public void onAttach(Context context) { 51 super.onAttach(context); 52 mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); 53 } 54 55 @Override 56 public void onResume() { 57 mVisibilityLoggerMixin.setSourceMetricsCategory(getActivity()); 58 super.onResume(); 59 } 60 61 @Override 62 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 63 } 64 65 protected final Context getPrefContext() { 66 return getPreferenceManager().getContext(); 67 } 68 } 69