1 /*
2  * Copyright (C) 2015 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 package com.android.systemui.tuner;
17 
18 import android.app.AlertDialog;
19 import android.app.Dialog;
20 import android.app.DialogFragment;
21 import android.content.DialogInterface;
22 import android.hardware.display.AmbientDisplayConfiguration;
23 import android.os.Build;
24 import android.os.Bundle;
25 import android.provider.Settings;
26 import android.view.Menu;
27 import android.view.MenuInflater;
28 import android.view.MenuItem;
29 
30 import androidx.preference.Preference;
31 import androidx.preference.PreferenceFragment;
32 
33 import com.android.internal.logging.MetricsLogger;
34 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
35 import com.android.systemui.R;
36 import com.android.systemui.shared.plugins.PluginPrefs;
37 
38 public class TunerFragment extends PreferenceFragment {
39 
40     private static final String TAG = "TunerFragment";
41 
42     private static final String KEY_BATTERY_PCT = "battery_pct";
43     private static final String KEY_PLUGINS = "plugins";
44     private static final CharSequence KEY_DOZE = "doze";
45 
46     public static final String SETTING_SEEN_TUNER_WARNING = "seen_tuner_warning";
47 
48     private static final String WARNING_TAG = "tuner_warning";
49     private static final String[] DEBUG_ONLY = new String[] {
50             "nav_bar",
51             "lockscreen",
52             "picture_in_picture",
53     };
54 
55     private static final int MENU_REMOVE = Menu.FIRST + 1;
56 
57     @Override
onCreate(Bundle savedInstanceState)58     public void onCreate(Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60 
61         setHasOptionsMenu(true);
62     }
63 
64     @Override
onActivityCreated(Bundle savedInstanceState)65     public void onActivityCreated(Bundle savedInstanceState) {
66         super.onActivityCreated(savedInstanceState);
67         getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
68     }
69 
70     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)71     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
72         addPreferencesFromResource(R.xml.tuner_prefs);
73         if (!PluginPrefs.hasPlugins(getContext())) {
74             getPreferenceScreen().removePreference(findPreference(KEY_PLUGINS));
75         }
76         if (!alwaysOnAvailable()) {
77             getPreferenceScreen().removePreference(findPreference(KEY_DOZE));
78         }
79         if (!Build.IS_DEBUGGABLE) {
80             for (int i = 0; i < DEBUG_ONLY.length; i++) {
81                 Preference preference = findPreference(DEBUG_ONLY[i]);
82                 if (preference != null) getPreferenceScreen().removePreference(preference);
83             }
84         }
85 
86         if (Settings.Secure.getInt(getContext().getContentResolver(), SETTING_SEEN_TUNER_WARNING,
87                 0) == 0) {
88             if (getFragmentManager().findFragmentByTag(WARNING_TAG) == null) {
89                 new TunerWarningFragment().show(getFragmentManager(), WARNING_TAG);
90             }
91         }
92     }
93 
alwaysOnAvailable()94     private boolean alwaysOnAvailable() {
95         return new AmbientDisplayConfiguration(getContext()).alwaysOnAvailable();
96     }
97 
98     @Override
onResume()99     public void onResume() {
100         super.onResume();
101         getActivity().setTitle(R.string.system_ui_tuner);
102 
103         MetricsLogger.visibility(getContext(), MetricsEvent.TUNER, true);
104     }
105 
106     @Override
onPause()107     public void onPause() {
108         super.onPause();
109 
110         MetricsLogger.visibility(getContext(), MetricsEvent.TUNER, false);
111     }
112 
113     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)114     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
115         menu.add(Menu.NONE, MENU_REMOVE, Menu.NONE, R.string.remove_from_settings);
116     }
117 
118     @Override
onOptionsItemSelected(MenuItem item)119     public boolean onOptionsItemSelected(MenuItem item) {
120         switch (item.getItemId()) {
121             case android.R.id.home:
122                 getActivity().finish();
123                 return true;
124             case MENU_REMOVE:
125                 TunerService.showResetRequest(getContext(), new Runnable() {
126                     @Override
127                     public void run() {
128                         if (getActivity() != null) {
129                             getActivity().finish();
130                         }
131                     }
132                 });
133                 return true;
134         }
135         return super.onOptionsItemSelected(item);
136     }
137 
138     public static class TunerWarningFragment extends DialogFragment {
139         @Override
onCreateDialog(Bundle savedInstanceState)140         public Dialog onCreateDialog(Bundle savedInstanceState) {
141             return new AlertDialog.Builder(getContext())
142                     .setTitle(R.string.tuner_warning_title)
143                     .setMessage(R.string.tuner_warning)
144                     .setPositiveButton(R.string.got_it, new DialogInterface.OnClickListener() {
145                         @Override
146                         public void onClick(DialogInterface dialog, int which) {
147                             Settings.Secure.putInt(getContext().getContentResolver(),
148                                     SETTING_SEEN_TUNER_WARNING, 1);
149                         }
150                     }).show();
151         }
152     }
153 }
154