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