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.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; 31 import android.view.MenuItem; 32 33 import com.android.internal.logging.MetricsLogger; 34 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 35 import com.android.systemui.DemoMode; 36 import com.android.systemui.R; 37 38 public class DemoModeFragment extends PreferenceFragment implements OnPreferenceChangeListener { 39 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 onCreatePreferences(Bundle savedInstanceState, String rootKey)60 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 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(); 78 contentResolver.registerContentObserver(Settings.Global.getUriFor( 79 DemoMode.DEMO_MODE_ALLOWED), false, mDemoModeObserver); 80 contentResolver.registerContentObserver(Settings.Global.getUriFor(DEMO_MODE_ON), false, 81 mDemoModeObserver); 82 setHasOptionsMenu(true); 83 } 84 85 @Override onOptionsItemSelected(MenuItem item)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); 93 } 94 95 @Override onResume()96 public void onResume() { 97 super.onResume(); 98 MetricsLogger.visibility(getContext(), MetricsEvent.TUNER_DEMO_MODE, true); 99 } 100 101 @Override onPause()102 public void onPause() { 103 super.onPause(); 104 MetricsLogger.visibility(getContext(), MetricsEvent.TUNER_DEMO_MODE, false); 105 } 106 107 @Override onDestroy()108 public void onDestroy() { 109 getContext().getContentResolver().unregisterContentObserver(mDemoModeObserver); 110 super.onDestroy(); 111 } 112 updateDemoModeEnabled()113 private void updateDemoModeEnabled() { 114 boolean enabled = Settings.Global.getInt(getContext().getContentResolver(), 115 DemoMode.DEMO_MODE_ALLOWED, 0) != 0; 116 mEnabledSwitch.setChecked(enabled); 117 mOnSwitch.setEnabled(enabled); 118 } 119 updateDemoModeOn()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 onPreferenceChange(Preference preference, Object newValue)127 public boolean onPreferenceChange(Preference preference, Object newValue) { 128 boolean enabled = newValue == Boolean.TRUE; 129 if (preference == mEnabledSwitch) { 130 if (!enabled) { 131 // Make sure we aren't in demo mode when disabling it. 132 mOnSwitch.setChecked(false); 133 stopDemoMode(); 134 } 135 MetricsLogger.action(getContext(), MetricsEvent.TUNER_DEMO_MODE_ENABLED, enabled); 136 setGlobal(DemoMode.DEMO_MODE_ALLOWED, enabled ? 1 : 0); 137 } else if (preference == mOnSwitch) { 138 MetricsLogger.action(getContext(), MetricsEvent.TUNER_DEMO_MODE_ON, enabled); 139 if (enabled) { 140 startDemoMode(); 141 } else { 142 stopDemoMode(); 143 } 144 } else { 145 return false; 146 } 147 return true; 148 } 149 startDemoMode()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); 157 158 String demoTime = "1010"; // 10:10, a classic choice of horologists 159 try { 160 String[] versionParts = android.os.Build.VERSION.RELEASE.split("\\."); 161 int majorVersion = Integer.valueOf(versionParts[0]); 162 demoTime = String.format("%02d00", majorVersion % 24); 163 } catch (IllegalArgumentException ex) { 164 } 165 intent.putExtra("hhmm", demoTime); 166 getContext().sendBroadcast(intent); 167 168 intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_NETWORK); 169 intent.putExtra("wifi", "show"); 170 intent.putExtra("mobile", "show"); 171 intent.putExtra("sims", "1"); 172 intent.putExtra("nosim", "false"); 173 intent.putExtra("level", "4"); 174 intent.putExtra("datatype", "lte"); 175 getContext().sendBroadcast(intent); 176 177 // Need to send this after so that the sim controller already exists. 178 intent.putExtra("fully", "true"); 179 getContext().sendBroadcast(intent); 180 181 intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_BATTERY); 182 intent.putExtra("level", "100"); 183 intent.putExtra("plugged", "false"); 184 getContext().sendBroadcast(intent); 185 186 intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_STATUS); 187 for (String icon : STATUS_ICONS) { 188 intent.putExtra(icon, "hide"); 189 } 190 getContext().sendBroadcast(intent); 191 192 intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_NOTIFICATIONS); 193 intent.putExtra("visible", "false"); 194 getContext().sendBroadcast(intent); 195 196 setGlobal(DEMO_MODE_ON, 1); 197 } 198 stopDemoMode()199 private void stopDemoMode() { 200 Intent intent = new Intent(DemoMode.ACTION_DEMO); 201 intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_EXIT); 202 getContext().sendBroadcast(intent); 203 setGlobal(DEMO_MODE_ON, 0); 204 } 205 setGlobal(String key, int value)206 private void setGlobal(String key, int value) { 207 Settings.Global.putInt(getContext().getContentResolver(), key, value); 208 } 209 210 private final ContentObserver mDemoModeObserver = 211 new ContentObserver(new Handler(Looper.getMainLooper())) { 212 public void onChange(boolean selfChange) { 213 updateDemoModeEnabled(); 214 updateDemoModeOn(); 215 }; 216 }; 217 } 218