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