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.annotation.SuppressLint;
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.app.DialogFragment;
22 import android.content.DialogInterface;
23 import android.hardware.display.AmbientDisplayConfiguration;
24 import android.os.Build;
25 import android.os.Bundle;
26 import android.provider.Settings;
27 import android.view.Menu;
28 import android.view.MenuInflater;
29 import android.view.MenuItem;
30 
31 import androidx.preference.Preference;
32 import androidx.preference.PreferenceFragment;
33 
34 import com.android.internal.logging.MetricsLogger;
35 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
36 import com.android.systemui.res.R;
37 import com.android.systemui.shared.plugins.PluginPrefs;
38 import com.android.tools.r8.keepanno.annotations.KeepTarget;
39 import com.android.tools.r8.keepanno.annotations.UsesReflection;
40 
41 public class TunerFragment extends PreferenceFragment {
42 
43     private static final String TAG = "TunerFragment";
44 
45     private static final String KEY_BATTERY_PCT = "battery_pct";
46     private static final String KEY_PLUGINS = "plugins";
47     private static final CharSequence KEY_DOZE = "doze";
48 
49     public static final String SETTING_SEEN_TUNER_WARNING = "seen_tuner_warning";
50 
51     private static final String WARNING_TAG = "tuner_warning";
52     private static final String[] DEBUG_ONLY = new String[] {
53             "nav_bar",
54             "lockscreen",
55             "picture_in_picture",
56     };
57 
58     private static final int MENU_REMOVE = Menu.FIRST + 1;
59 
60     private final TunerService mTunerService;
61 
62     // We are the only ones who ever call this constructor, so don't worry about the warning
63     @SuppressLint("ValidFragment")
TunerFragment(TunerService tunerService)64     public TunerFragment(TunerService tunerService) {
65         super();
66         mTunerService = tunerService;
67     }
68 
69     @Override
onCreate(Bundle savedInstanceState)70     public void onCreate(Bundle savedInstanceState) {
71         super.onCreate(savedInstanceState);
72 
73         setHasOptionsMenu(true);
74     }
75 
76     @Override
onActivityCreated(Bundle savedInstanceState)77     public void onActivityCreated(Bundle savedInstanceState) {
78         super.onActivityCreated(savedInstanceState);
79         getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
80     }
81 
82     // aapt doesn't generate keep rules for android:fragment references in <Preference> tags, so
83     // explicitly declare references per usage in `R.xml.tuner_prefs`. See b/120445169.
84     @UsesReflection({
85         @KeepTarget(classConstant = LockscreenFragment.class),
86         @KeepTarget(classConstant = NavBarTuner.class),
87         @KeepTarget(classConstant = PluginFragment.class),
88     })
89     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)90     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
91         addPreferencesFromResource(R.xml.tuner_prefs);
92         if (!PluginPrefs.hasPlugins(getContext())) {
93             getPreferenceScreen().removePreference(findPreference(KEY_PLUGINS));
94         }
95         if (!alwaysOnAvailable()) {
96             getPreferenceScreen().removePreference(findPreference(KEY_DOZE));
97         }
98         if (!Build.IS_DEBUGGABLE) {
99             for (int i = 0; i < DEBUG_ONLY.length; i++) {
100                 Preference preference = findPreference(DEBUG_ONLY[i]);
101                 if (preference != null) getPreferenceScreen().removePreference(preference);
102             }
103         }
104 
105         if (Settings.Secure.getInt(getContext().getContentResolver(), SETTING_SEEN_TUNER_WARNING,
106                 0) == 0) {
107             if (getFragmentManager().findFragmentByTag(WARNING_TAG) == null) {
108                 new TunerWarningFragment().show(getFragmentManager(), WARNING_TAG);
109             }
110         }
111     }
112 
alwaysOnAvailable()113     private boolean alwaysOnAvailable() {
114         return new AmbientDisplayConfiguration(getContext()).alwaysOnAvailable();
115     }
116 
117     @Override
onResume()118     public void onResume() {
119         super.onResume();
120         getActivity().setTitle(R.string.system_ui_tuner);
121 
122         MetricsLogger.visibility(getContext(), MetricsEvent.TUNER, true);
123     }
124 
125     @Override
onPause()126     public void onPause() {
127         super.onPause();
128 
129         MetricsLogger.visibility(getContext(), MetricsEvent.TUNER, false);
130     }
131 
132     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)133     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
134         menu.add(Menu.NONE, MENU_REMOVE, Menu.NONE, R.string.remove_from_settings);
135     }
136 
137     @Override
onOptionsItemSelected(MenuItem item)138     public boolean onOptionsItemSelected(MenuItem item) {
139         switch (item.getItemId()) {
140             case android.R.id.home:
141                 getActivity().finish();
142                 return true;
143             case MENU_REMOVE:
144                 mTunerService.showResetRequest(() -> {
145                     if (getActivity() != null) {
146                         getActivity().finish();
147                     }
148                 });
149                 return true;
150         }
151         return super.onOptionsItemSelected(item);
152     }
153 
154     public static class TunerWarningFragment extends DialogFragment {
155         @Override
onCreateDialog(Bundle savedInstanceState)156         public Dialog onCreateDialog(Bundle savedInstanceState) {
157             return new AlertDialog.Builder(getContext())
158                     .setTitle(R.string.tuner_warning_title)
159                     .setMessage(R.string.tuner_warning)
160                     .setPositiveButton(R.string.got_it, new DialogInterface.OnClickListener() {
161                         @Override
162                         public void onClick(DialogInterface dialog, int which) {
163                             Settings.Secure.putInt(getContext().getContentResolver(),
164                                     SETTING_SEEN_TUNER_WARNING, 1);
165                         }
166                     }).show();
167         }
168     }
169 }
170