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