• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import android.support.annotation.XmlRes;
22 import android.support.v7.preference.PreferenceScreen;
23 import android.text.TextUtils;
24 import android.util.Log;
25 
26 import com.android.settings.overlay.FeatureFactory;
27 import com.android.settings.survey.SurveyMixin;
28 import com.android.settingslib.core.instrumentation.Instrumentable;
29 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
30 import com.android.settingslib.core.instrumentation.VisibilityLoggerMixin;
31 import com.android.settingslib.core.lifecycle.ObservablePreferenceFragment;
32 
33 /**
34  * Instrumented fragment that logs visibility state.
35  */
36 public abstract class InstrumentedPreferenceFragment extends ObservablePreferenceFragment
37         implements Instrumentable {
38 
39     private static final String TAG = "InstrumentedPrefFrag";
40 
41 
42     protected MetricsFeatureProvider mMetricsFeatureProvider;
43 
44     // metrics placeholder value. Only use this for development.
45     protected final int PLACEHOLDER_METRIC = 10000;
46 
47     private VisibilityLoggerMixin mVisibilityLoggerMixin;
48 
49     @Override
onAttach(Context context)50     public void onAttach(Context context) {
51         mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
52         // Mixin that logs visibility change for activity.
53         mVisibilityLoggerMixin = new VisibilityLoggerMixin(getMetricsCategory(),
54                 mMetricsFeatureProvider);
55         getLifecycle().addObserver(mVisibilityLoggerMixin);
56         getLifecycle().addObserver(new SurveyMixin(this, getClass().getSimpleName()));
57         super.onAttach(context);
58     }
59 
60     @Override
onResume()61     public void onResume() {
62         mVisibilityLoggerMixin.setSourceMetricsCategory(getActivity());
63         super.onResume();
64     }
65 
66     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)67     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
68         final int resId = getPreferenceScreenResId();
69         if (resId > 0) {
70             addPreferencesFromResource(resId);
71         }
72     }
73 
74     @Override
addPreferencesFromResource(@mlRes int preferencesResId)75     public void addPreferencesFromResource(@XmlRes int preferencesResId) {
76         super.addPreferencesFromResource(preferencesResId);
77         updateActivityTitleWithScreenTitle(getPreferenceScreen());
78     }
79 
getPrefContext()80     protected final Context getPrefContext() {
81         return getPreferenceManager().getContext();
82     }
83 
getVisibilityLogger()84     protected final VisibilityLoggerMixin getVisibilityLogger() {
85         return mVisibilityLoggerMixin;
86     }
87 
88     /**
89      * Get the res id for static preference xml for this fragment.
90      */
getPreferenceScreenResId()91     protected int getPreferenceScreenResId() {
92         return -1;
93     }
94 
updateActivityTitleWithScreenTitle(PreferenceScreen screen)95     private void updateActivityTitleWithScreenTitle(PreferenceScreen screen) {
96         if (screen != null) {
97             final CharSequence title = screen.getTitle();
98             if (!TextUtils.isEmpty(title)) {
99                 getActivity().setTitle(title);
100             } else {
101                 Log.w(TAG, "Screen title missing for fragment " + this.getClass().getName());
102             }
103         }
104     }
105 
106 }
107