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.preference.Preference; 26 import android.preference.Preference.OnPreferenceChangeListener; 27 import android.preference.PreferenceFragment; 28 import android.preference.PreferenceScreen; 29 import android.preference.SwitchPreference; 30 import android.provider.Settings; 31 import android.view.MenuItem; 32 33 import com.android.internal.logging.MetricsLogger; 34 import com.android.systemui.DemoMode; 35 import com.android.systemui.R; 36 37 public class DemoModeFragment extends PreferenceFragment implements OnPreferenceChangeListener { 38 39 private static final String DEMO_MODE_ON = "sysui_tuner_demo_on"; 40 41 private static final String[] STATUS_ICONS = { 42 "volume", 43 "bluetooth", 44 "location", 45 "alarm", 46 "zen", 47 "sync", 48 "tty", 49 "eri", 50 "mute", 51 "speakerphone", 52 "managed_profile", 53 }; 54 55 private SwitchPreference mEnabledSwitch; 56 private SwitchPreference mOnSwitch; 57 58 @Override onCreate(Bundle savedInstanceState)59 public void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 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(), MetricsLogger.TUNER_DEMO_MODE, true); 100 } 101 102 @Override onPause()103 public void onPause() { 104 super.onPause(); 105 MetricsLogger.visibility(getContext(), MetricsLogger.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(), MetricsLogger.TUNER_DEMO_MODE_ENABLED, enabled); 137 setGlobal(DemoMode.DEMO_MODE_ALLOWED, enabled ? 1 : 0); 138 } else if (preference == mOnSwitch) { 139 MetricsLogger.action(getContext(), MetricsLogger.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 intent.putExtra("hhmm", "0600"); 159 getContext().sendBroadcast(intent); 160 161 intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_NETWORK); 162 intent.putExtra("wifi", "show"); 163 intent.putExtra("mobile", "show"); 164 intent.putExtra("sims", "1"); 165 intent.putExtra("nosim", "false"); 166 intent.putExtra("level", "4"); 167 intent.putExtra("datatypel", ""); 168 getContext().sendBroadcast(intent); 169 170 // Need to send this after so that the sim controller already exists. 171 intent.putExtra("fully", "true"); 172 getContext().sendBroadcast(intent); 173 174 intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_BATTERY); 175 intent.putExtra("level", "100"); 176 intent.putExtra("plugged", "false"); 177 getContext().sendBroadcast(intent); 178 179 intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_STATUS); 180 for (String icon : STATUS_ICONS) { 181 intent.putExtra(icon, "hide"); 182 } 183 getContext().sendBroadcast(intent); 184 185 intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_NOTIFICATIONS); 186 intent.putExtra("visible", "false"); 187 getContext().sendBroadcast(intent); 188 189 setGlobal(DEMO_MODE_ON, 1); 190 } 191 stopDemoMode()192 private void stopDemoMode() { 193 Intent intent = new Intent(DemoMode.ACTION_DEMO); 194 intent.putExtra(DemoMode.EXTRA_COMMAND, DemoMode.COMMAND_EXIT); 195 getContext().sendBroadcast(intent); 196 setGlobal(DEMO_MODE_ON, 0); 197 } 198 setGlobal(String key, int value)199 private void setGlobal(String key, int value) { 200 Settings.Global.putInt(getContext().getContentResolver(), key, value); 201 } 202 203 private final ContentObserver mDemoModeObserver = 204 new ContentObserver(new Handler(Looper.getMainLooper())) { 205 public void onChange(boolean selfChange) { 206 updateDemoModeEnabled(); 207 updateDemoModeOn(); 208 }; 209 }; 210 } 211