Jason Monk | 295a337 | 2015-05-11 16:57:15 -0400 | [diff] [blame] | 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 | |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 18 | import android.app.AlertDialog; |
Jason Monk | 72def0d | 2015-11-10 16:03:06 -0500 | [diff] [blame] | 19 | import android.app.Dialog; |
| 20 | import android.app.DialogFragment; |
Jason Monk | 11a7744 | 2015-05-12 19:29:02 -0400 | [diff] [blame] | 21 | import android.app.FragmentTransaction; |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 22 | import android.content.DialogInterface; |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 23 | import android.database.ContentObserver; |
| 24 | import android.net.Uri; |
Jason Monk | 295a337 | 2015-05-11 16:57:15 -0400 | [diff] [blame] | 25 | import android.os.Bundle; |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 26 | import android.os.Handler; |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 27 | import android.provider.Settings; |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 28 | import android.provider.Settings.System; |
Jason Monk | 87ccd55 | 2015-12-11 21:39:54 -0500 | [diff] [blame] | 29 | import android.support.v14.preference.PreferenceFragment; |
| 30 | import android.support.v14.preference.SwitchPreference; |
| 31 | import android.support.v7.preference.Preference; |
| 32 | import android.support.v7.preference.Preference.OnPreferenceChangeListener; |
| 33 | import android.support.v7.preference.Preference.OnPreferenceClickListener; |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 34 | import android.view.Menu; |
| 35 | import android.view.MenuInflater; |
Jason Monk | 295a337 | 2015-05-11 16:57:15 -0400 | [diff] [blame] | 36 | import android.view.MenuItem; |
Jason Monk | 87ccd55 | 2015-12-11 21:39:54 -0500 | [diff] [blame] | 37 | |
Jason Monk | a403159 | 2015-07-16 14:15:50 -0400 | [diff] [blame] | 38 | import com.android.internal.logging.MetricsLogger; |
Jason Monk | 295a337 | 2015-05-11 16:57:15 -0400 | [diff] [blame] | 39 | import com.android.systemui.R; |
Jason Monk | fe7c91b | 2015-08-13 15:42:28 -0400 | [diff] [blame] | 40 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 41 | import static com.android.systemui.BatteryMeterDrawable.SHOW_PERCENT_SETTING; |
Jason Monk | 295a337 | 2015-05-11 16:57:15 -0400 | [diff] [blame] | 42 | |
| 43 | public class TunerFragment extends PreferenceFragment { |
| 44 | |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 45 | private static final String TAG = "TunerFragment"; |
| 46 | |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 47 | private static final String KEY_BATTERY_PCT = "battery_pct"; |
| 48 | |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 49 | public static final String SETTING_SEEN_TUNER_WARNING = "seen_tuner_warning"; |
| 50 | |
Jason Monk | 72def0d | 2015-11-10 16:03:06 -0500 | [diff] [blame] | 51 | private static final String WARNING_TAG = "tuner_warning"; |
| 52 | |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 53 | private static final int MENU_REMOVE = Menu.FIRST + 1; |
| 54 | |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 55 | private final SettingObserver mSettingObserver = new SettingObserver(); |
| 56 | |
| 57 | private SwitchPreference mBatteryPct; |
Jason Monk | 11a7744 | 2015-05-12 19:29:02 -0400 | [diff] [blame] | 58 | |
Jason Monk | 87ccd55 | 2015-12-11 21:39:54 -0500 | [diff] [blame] | 59 | @Override |
Jason Monk | 295a337 | 2015-05-11 16:57:15 -0400 | [diff] [blame] | 60 | public void onCreate(Bundle savedInstanceState) { |
| 61 | super.onCreate(savedInstanceState); |
| 62 | |
Jason Monk | 295a337 | 2015-05-11 16:57:15 -0400 | [diff] [blame] | 63 | getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); |
| 64 | setHasOptionsMenu(true); |
Jason Monk | 87ccd55 | 2015-12-11 21:39:54 -0500 | [diff] [blame] | 65 | } |
Jason Monk | 11a7744 | 2015-05-12 19:29:02 -0400 | [diff] [blame] | 66 | |
Jason Monk | 87ccd55 | 2015-12-11 21:39:54 -0500 | [diff] [blame] | 67 | @Override |
| 68 | public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { |
| 69 | addPreferencesFromResource(R.xml.tuner_prefs); |
| 70 | |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 71 | mBatteryPct = (SwitchPreference) findPreference(KEY_BATTERY_PCT); |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 72 | if (Settings.Secure.getInt(getContext().getContentResolver(), SETTING_SEEN_TUNER_WARNING, |
| 73 | 0) == 0) { |
Jason Monk | 72def0d | 2015-11-10 16:03:06 -0500 | [diff] [blame] | 74 | if (getFragmentManager().findFragmentByTag(WARNING_TAG) == null) { |
| 75 | new TunerWarningFragment().show(getFragmentManager(), WARNING_TAG); |
| 76 | } |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 77 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void onResume() { |
| 82 | super.onResume(); |
| 83 | updateBatteryPct(); |
| 84 | getContext().getContentResolver().registerContentObserver( |
| 85 | System.getUriFor(SHOW_PERCENT_SETTING), false, mSettingObserver); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 86 | |
Jason Monk | a403159 | 2015-07-16 14:15:50 -0400 | [diff] [blame] | 87 | MetricsLogger.visibility(getContext(), MetricsLogger.TUNER, true); |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void onPause() { |
| 92 | super.onPause(); |
| 93 | getContext().getContentResolver().unregisterContentObserver(mSettingObserver); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 94 | |
Jason Monk | a403159 | 2015-07-16 14:15:50 -0400 | [diff] [blame] | 95 | MetricsLogger.visibility(getContext(), MetricsLogger.TUNER, false); |
Jason Monk | 5e74517 | 2015-06-02 19:14:44 -0400 | [diff] [blame] | 96 | } |
| 97 | |
Jason Monk | 295a337 | 2015-05-11 16:57:15 -0400 | [diff] [blame] | 98 | @Override |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 99 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 100 | menu.add(Menu.NONE, MENU_REMOVE, Menu.NONE, R.string.remove_from_settings); |
| 101 | } |
| 102 | |
| 103 | @Override |
Jason Monk | 295a337 | 2015-05-11 16:57:15 -0400 | [diff] [blame] | 104 | public boolean onOptionsItemSelected(MenuItem item) { |
| 105 | switch (item.getItemId()) { |
| 106 | case android.R.id.home: |
| 107 | getActivity().finish(); |
| 108 | return true; |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 109 | case MENU_REMOVE: |
| 110 | TunerService.showResetRequest(getContext(), new Runnable() { |
| 111 | @Override |
| 112 | public void run() { |
| 113 | getActivity().finish(); |
| 114 | } |
| 115 | }); |
| 116 | return true; |
Jason Monk | 295a337 | 2015-05-11 16:57:15 -0400 | [diff] [blame] | 117 | } |
| 118 | return super.onOptionsItemSelected(item); |
| 119 | } |
| 120 | |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 121 | private void updateBatteryPct() { |
| 122 | mBatteryPct.setOnPreferenceChangeListener(null); |
| 123 | mBatteryPct.setChecked(System.getInt(getContext().getContentResolver(), |
| 124 | SHOW_PERCENT_SETTING, 0) != 0); |
| 125 | mBatteryPct.setOnPreferenceChangeListener(mBatteryPctChange); |
| 126 | } |
| 127 | |
| 128 | private final class SettingObserver extends ContentObserver { |
| 129 | public SettingObserver() { |
| 130 | super(new Handler()); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void onChange(boolean selfChange, Uri uri, int userId) { |
| 135 | super.onChange(selfChange, uri, userId); |
| 136 | updateBatteryPct(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | private final OnPreferenceChangeListener mBatteryPctChange = new OnPreferenceChangeListener() { |
| 141 | @Override |
| 142 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 143 | final boolean v = (Boolean) newValue; |
Jason Monk | a403159 | 2015-07-16 14:15:50 -0400 | [diff] [blame] | 144 | MetricsLogger.action(getContext(), MetricsLogger.TUNER_BATTERY_PERCENTAGE, v); |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 145 | System.putInt(getContext().getContentResolver(), SHOW_PERCENT_SETTING, v ? 1 : 0); |
| 146 | return true; |
| 147 | } |
| 148 | }; |
Jason Monk | caf3762 | 2015-08-18 12:33:50 -0400 | [diff] [blame] | 149 | |
Jason Monk | 72def0d | 2015-11-10 16:03:06 -0500 | [diff] [blame] | 150 | public static class TunerWarningFragment extends DialogFragment { |
| 151 | @Override |
| 152 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 153 | return new AlertDialog.Builder(getContext()) |
| 154 | .setTitle(R.string.tuner_warning_title) |
| 155 | .setMessage(R.string.tuner_warning) |
| 156 | .setPositiveButton(R.string.got_it, new DialogInterface.OnClickListener() { |
| 157 | @Override |
| 158 | public void onClick(DialogInterface dialog, int which) { |
| 159 | Settings.Secure.putInt(getContext().getContentResolver(), |
| 160 | SETTING_SEEN_TUNER_WARNING, 1); |
| 161 | } |
| 162 | }).show(); |
| 163 | } |
| 164 | } |
Jason Monk | 295a337 | 2015-05-11 16:57:15 -0400 | [diff] [blame] | 165 | } |