1 /* 2 * Copyright 2019 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 17 package com.android.settings.development.gamedriver; 18 19 import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_ALL_APPS; 20 import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_DEFAULT; 21 import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_OFF; 22 import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_PRERELEASE_ALL_APPS; 23 24 import android.content.ContentResolver; 25 import android.content.Context; 26 import android.os.Handler; 27 import android.os.Looper; 28 import android.provider.Settings; 29 30 import androidx.annotation.VisibleForTesting; 31 32 import com.android.settings.widget.SwitchWidgetController; 33 import com.android.settingslib.core.lifecycle.LifecycleObserver; 34 import com.android.settingslib.core.lifecycle.events.OnStart; 35 import com.android.settingslib.core.lifecycle.events.OnStop; 36 import com.android.settingslib.development.DevelopmentSettingsEnabler; 37 38 /** 39 * Controller of global switch bar used to fully turn off Game Driver. 40 */ 41 public class GameDriverGlobalSwitchBarController 42 implements SwitchWidgetController.OnSwitchChangeListener, 43 GameDriverContentObserver.OnGameDriverContentChangedListener, LifecycleObserver, 44 OnStart, OnStop { 45 46 private final Context mContext; 47 private final ContentResolver mContentResolver; 48 @VisibleForTesting 49 SwitchWidgetController mSwitchWidgetController; 50 @VisibleForTesting 51 GameDriverContentObserver mGameDriverContentObserver; 52 GameDriverGlobalSwitchBarController( Context context, SwitchWidgetController switchWidgetController)53 GameDriverGlobalSwitchBarController( 54 Context context, SwitchWidgetController switchWidgetController) { 55 mContext = context; 56 mContentResolver = context.getContentResolver(); 57 mGameDriverContentObserver = 58 new GameDriverContentObserver(new Handler(Looper.getMainLooper()), this); 59 mSwitchWidgetController = switchWidgetController; 60 mSwitchWidgetController.setEnabled( 61 DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(context)); 62 mSwitchWidgetController.setChecked( 63 Settings.Global.getInt( 64 mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT) 65 != GAME_DRIVER_OFF); 66 mSwitchWidgetController.setListener(this); 67 } 68 69 @Override onStart()70 public void onStart() { 71 mSwitchWidgetController.startListening(); 72 mGameDriverContentObserver.register(mContentResolver); 73 } 74 75 @Override onStop()76 public void onStop() { 77 mSwitchWidgetController.stopListening(); 78 mGameDriverContentObserver.unregister(mContentResolver); 79 } 80 81 @Override onSwitchToggled(boolean isChecked)82 public boolean onSwitchToggled(boolean isChecked) { 83 final int gameDriver = Settings.Global.getInt( 84 mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT); 85 86 if (isChecked 87 && (gameDriver == GAME_DRIVER_DEFAULT || gameDriver == GAME_DRIVER_ALL_APPS 88 || gameDriver == GAME_DRIVER_PRERELEASE_ALL_APPS)) { 89 return true; 90 } 91 92 if (!isChecked && gameDriver == GAME_DRIVER_OFF) { 93 return true; 94 } 95 96 Settings.Global.putInt(mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, 97 isChecked ? GAME_DRIVER_DEFAULT : GAME_DRIVER_OFF); 98 99 return true; 100 } 101 102 @Override onGameDriverContentChanged()103 public void onGameDriverContentChanged() { 104 mSwitchWidgetController.setChecked( 105 Settings.Global.getInt( 106 mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT) 107 != GAME_DRIVER_OFF); 108 } 109 } 110