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.graphicsdriver; 18 19 import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableForAllAppsPreferenceController.UPDATABLE_DRIVER_DEFAULT; 20 import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableForAllAppsPreferenceController.UPDATABLE_DRIVER_OFF; 21 import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableForAllAppsPreferenceController.UPDATABLE_DRIVER_PRERELEASE_ALL_APPS; 22 import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableForAllAppsPreferenceController.UPDATABLE_DRIVER_PRODUCTION_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 updatable driver. 40 */ 41 public class GraphicsDriverGlobalSwitchBarController 42 implements SwitchWidgetController.OnSwitchChangeListener, 43 GraphicsDriverContentObserver.OnGraphicsDriverContentChangedListener, 44 LifecycleObserver, OnStart, OnStop { 45 46 private final Context mContext; 47 private final ContentResolver mContentResolver; 48 @VisibleForTesting 49 SwitchWidgetController mSwitchWidgetController; 50 @VisibleForTesting 51 GraphicsDriverContentObserver mGraphicsDriverContentObserver; 52 GraphicsDriverGlobalSwitchBarController( Context context, SwitchWidgetController switchWidgetController)53 GraphicsDriverGlobalSwitchBarController( 54 Context context, SwitchWidgetController switchWidgetController) { 55 mContext = context; 56 mContentResolver = context.getContentResolver(); 57 mGraphicsDriverContentObserver = 58 new GraphicsDriverContentObserver(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.UPDATABLE_DRIVER_ALL_APPS, 65 UPDATABLE_DRIVER_DEFAULT) 66 != UPDATABLE_DRIVER_OFF); 67 mSwitchWidgetController.setListener(this); 68 } 69 70 @Override onStart()71 public void onStart() { 72 mSwitchWidgetController.startListening(); 73 mGraphicsDriverContentObserver.register(mContentResolver); 74 } 75 76 @Override onStop()77 public void onStop() { 78 mSwitchWidgetController.stopListening(); 79 mGraphicsDriverContentObserver.unregister(mContentResolver); 80 } 81 82 @Override onSwitchToggled(boolean isChecked)83 public boolean onSwitchToggled(boolean isChecked) { 84 final int graphicsDriverGlobalOption = Settings.Global.getInt( 85 mContentResolver, Settings.Global.UPDATABLE_DRIVER_ALL_APPS, 86 UPDATABLE_DRIVER_DEFAULT); 87 88 if (isChecked 89 && (graphicsDriverGlobalOption == UPDATABLE_DRIVER_DEFAULT 90 || graphicsDriverGlobalOption == UPDATABLE_DRIVER_PRODUCTION_ALL_APPS 91 || graphicsDriverGlobalOption == UPDATABLE_DRIVER_PRERELEASE_ALL_APPS)) { 92 return true; 93 } 94 95 if (!isChecked && graphicsDriverGlobalOption == UPDATABLE_DRIVER_OFF) { 96 return true; 97 } 98 99 Settings.Global.putInt(mContentResolver, Settings.Global.UPDATABLE_DRIVER_ALL_APPS, 100 isChecked ? UPDATABLE_DRIVER_DEFAULT : UPDATABLE_DRIVER_OFF); 101 102 return true; 103 } 104 105 @Override onGraphicsDriverContentChanged()106 public void onGraphicsDriverContentChanged() { 107 mSwitchWidgetController.setChecked( 108 Settings.Global.getInt( 109 mContentResolver, Settings.Global.UPDATABLE_DRIVER_ALL_APPS, 110 UPDATABLE_DRIVER_DEFAULT) 111 != UPDATABLE_DRIVER_OFF); 112 } 113 } 114