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