Jason Monk | 16ada3d | 2015-06-07 09:58:30 -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 | |
| 18 | import android.content.ContentResolver; |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.database.ContentObserver; |
| 22 | import android.os.Bundle; |
| 23 | import android.os.Handler; |
| 24 | import android.os.Looper; |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 25 | import android.provider.Settings; |
Jason Monk | 87ccd55 | 2015-12-11 21:39:54 -0500 | [diff] [blame] | 26 | import android.support.v14.preference.PreferenceFragment; |
| 27 | import android.support.v14.preference.SwitchPreference; |
| 28 | import android.support.v7.preference.Preference; |
| 29 | import android.support.v7.preference.Preference.OnPreferenceChangeListener; |
| 30 | import android.support.v7.preference.PreferenceScreen; |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 31 | import android.view.MenuItem; |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 32 | |
Jason Monk | a403159 | 2015-07-16 14:15:50 -0400 | [diff] [blame] | 33 | import com.android.internal.logging.MetricsLogger; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 34 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 35 | import com.android.systemui.DemoMode; |
| 36 | import com.android.systemui.R; |
| 37 | |
| 38 | public class DemoModeFragment extends PreferenceFragment implements OnPreferenceChangeListener { |
| 39 | |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 40 | private static final String DEMO_MODE_ON = "sysui_tuner_demo_on"; |
| 41 | |
| 42 | private static final String[] STATUS_ICONS = { |
| 43 | "volume", |
| 44 | "bluetooth", |
| 45 | "location", |
| 46 | "alarm", |
| 47 | "zen", |
| 48 | "sync", |
| 49 | "tty", |
| 50 | "eri", |
| 51 | "mute", |
| 52 | "speakerphone", |
| 53 | "managed_profile", |
| 54 | }; |
| 55 | |
| 56 | private SwitchPreference mEnabledSwitch; |
| 57 | private SwitchPreference mOnSwitch; |
| 58 | |
| 59 | @Override |
Jason Monk | 87ccd55 | 2015-12-11 21:39:54 -0500 | [diff] [blame] | 60 | public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 61 | Context context = getContext(); |
| 62 | mEnabledSwitch = new SwitchPreference(context); |
| 63 | mEnabledSwitch.setTitle(R.string.enable_demo_mode); |
| 64 | mEnabledSwitch.setOnPreferenceChangeListener(this); |
| 65 | mOnSwitch = new SwitchPreference(context); |
| 66 | mOnSwitch.setTitle(R.string.show_demo_mode); |
| 67 | mOnSwitch.setEnabled(false); |
| 68 | mOnSwitch.setOnPreferenceChangeListener(this); |
| 69 | |
| 70 | PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context); |
| 71 | screen.addPreference(mEnabledSwitch); |
| 72 | screen.addPreference(mOnSwitch); |
| 73 | setPreferenceScreen(screen); |
| 74 | |
| 75 | updateDemoModeEnabled(); |
| 76 | updateDemoModeOn(); |
| 77 | ContentResolver contentResolver = getContext().getContentResolver(); |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 78 | contentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 79 | DemoMode.DEMO_MODE_ALLOWED), false, mDemoModeObserver); |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 80 | contentResolver.registerContentObserver(Settings.Global.getUriFor(DEMO_MODE_ON), false, |
| 81 | mDemoModeObserver); |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 82 | setHasOptionsMenu(true); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean onOptionsItemSelected(MenuItem item) { |
| 87 | switch (item.getItemId()) { |
| 88 | case android.R.id.home: |
| 89 | getFragmentManager().popBackStack(); |
| 90 | break; |
| 91 | } |
| 92 | return super.onOptionsItemSelected(item); |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
Jason Monk | a403159 | 2015-07-16 14:15:50 -0400 | [diff] [blame] | 96 | public void onResume() { |
| 97 | super.onResume(); |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 98 | MetricsLogger.visibility(getContext(), MetricsEvent.TUNER_DEMO_MODE, true); |
Jason Monk | a403159 | 2015-07-16 14:15:50 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void onPause() { |
| 103 | super.onPause(); |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 104 | MetricsLogger.visibility(getContext(), MetricsEvent.TUNER_DEMO_MODE, false); |
Jason Monk | a403159 | 2015-07-16 14:15:50 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 108 | public void onDestroy() { |
| 109 | getContext().getContentResolver().unregisterContentObserver(mDemoModeObserver); |
| 110 | super.onDestroy(); |
| 111 | } |
| 112 | |
| 113 | private void updateDemoModeEnabled() { |
| 114 | boolean enabled = Settings.Global.getInt(getContext().getContentResolver(), |
Jason Monk | 431ad73 | 2015-07-16 08:58:15 -0400 | [diff] [blame] | 115 | DemoMode.DEMO_MODE_ALLOWED, 0) != 0; |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 116 | mEnabledSwitch.setChecked(enabled); |
| 117 | mOnSwitch.setEnabled(enabled); |
| 118 | } |
| 119 | |
| 120 | private void updateDemoModeOn() { |
| 121 | boolean enabled = Settings.Global.getInt(getContext().getContentResolver(), |
| 122 | DEMO_MODE_ON, 0) != 0; |
| 123 | mOnSwitch.setChecked(enabled); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Jason Monk | a403159 | 2015-07-16 14:15:50 -0400 | [diff] [blame] | 128 | boolean enabled = newValue == Boolean.TRUE; |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 129 | if (preference == mEnabledSwitch) { |
Jason Monk | a403159 | 2015-07-16 14:15:50 -0400 | [diff] [blame] | 130 | if (!enabled) { |
Jason Monk | 33b60bb | 2015-07-13 10:42:23 -0400 | [diff] [blame] | 131 | // Make sure we aren't in demo mode when disabling it. |
| 132 | mOnSwitch.setChecked(false); |
| 133 | stopDemoMode(); |
| 134 | } |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 135 | MetricsLogger.action(getContext(), MetricsEvent.TUNER_DEMO_MODE_ENABLED, enabled); |
Jason Monk | a403159 | 2015-07-16 14:15:50 -0400 | [diff] [blame] | 136 | setGlobal(DemoMode.DEMO_MODE_ALLOWED, enabled ? 1 : 0); |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 137 | } else if (preference == mOnSwitch) { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 138 | MetricsLogger.action(getContext(), MetricsEvent.TUNER_DEMO_MODE_ON, enabled); |
Jason Monk | a403159 | 2015-07-16 14:15:50 -0400 | [diff] [blame] | 139 | if (enabled) { |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 140 | startDemoMode(); |
| 141 | } else { |
| 142 | stopDemoMode(); |
| 143 | } |
| 144 | } else { |
| 145 | return false; |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | private void startDemoMode() { |
| 151 | Intent intent = new Intent(DemoMode.ACTION_DEMO); |
| 152 | |
| 153 | intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_ENTER); |
| 154 | getContext().sendBroadcast(intent); |
| 155 | |
| 156 | intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_CLOCK); |
Jason Monk | 7abe71c | 2016-05-12 14:05:18 -0400 | [diff] [blame] | 157 | intent.putExtra("hhmm", "0700"); |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 158 | getContext().sendBroadcast(intent); |
| 159 | |
| 160 | intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_NETWORK); |
| 161 | intent.putExtra("wifi", "show"); |
| 162 | intent.putExtra("mobile", "show"); |
| 163 | intent.putExtra("sims", "1"); |
| 164 | intent.putExtra("nosim", "false"); |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 165 | intent.putExtra("level", "4"); |
Kenneth Westin | 5c88ffb | 2013-11-28 16:29:40 +0100 | [diff] [blame^] | 166 | intent.putExtra("datatype", "lte"); |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 167 | getContext().sendBroadcast(intent); |
| 168 | |
Jason Monk | 259e06f | 2015-06-18 14:07:26 -0400 | [diff] [blame] | 169 | // Need to send this after so that the sim controller already exists. |
| 170 | intent.putExtra("fully", "true"); |
| 171 | getContext().sendBroadcast(intent); |
| 172 | |
Jason Monk | 16ada3d | 2015-06-07 09:58:30 -0400 | [diff] [blame] | 173 | intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_BATTERY); |
| 174 | intent.putExtra("level", "100"); |
| 175 | intent.putExtra("plugged", "false"); |
| 176 | getContext().sendBroadcast(intent); |
| 177 | |
| 178 | intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_STATUS); |
| 179 | for (String icon : STATUS_ICONS) { |
| 180 | intent.putExtra(icon, "hide"); |
| 181 | } |
| 182 | getContext().sendBroadcast(intent); |
| 183 | |
| 184 | intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_NOTIFICATIONS); |
| 185 | intent.putExtra("visible", "false"); |
| 186 | getContext().sendBroadcast(intent); |
| 187 | |
| 188 | setGlobal(DEMO_MODE_ON, 1); |
| 189 | } |
| 190 | |
| 191 | private void stopDemoMode() { |
| 192 | Intent intent = new Intent(DemoMode.ACTION_DEMO); |
| 193 | intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_EXIT); |
| 194 | getContext().sendBroadcast(intent); |
| 195 | setGlobal(DEMO_MODE_ON, 0); |
| 196 | } |
| 197 | |
| 198 | private void setGlobal(String key, int value) { |
| 199 | Settings.Global.putInt(getContext().getContentResolver(), key, value); |
| 200 | } |
| 201 | |
| 202 | private final ContentObserver mDemoModeObserver = |
| 203 | new ContentObserver(new Handler(Looper.getMainLooper())) { |
| 204 | public void onChange(boolean selfChange) { |
| 205 | updateDemoModeEnabled(); |
| 206 | updateDemoModeOn(); |
| 207 | }; |
| 208 | }; |
| 209 | } |