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 ContentResolver mContentResolver; 47 private final SwitchWidgetController mSwitchWidgetController; 48 @VisibleForTesting 49 GraphicsDriverContentObserver mGraphicsDriverContentObserver; 50 GraphicsDriverGlobalSwitchBarController( Context context, SwitchWidgetController switchWidgetController)51 GraphicsDriverGlobalSwitchBarController( 52 Context context, SwitchWidgetController switchWidgetController) { 53 mContentResolver = context.getContentResolver(); 54 mGraphicsDriverContentObserver = 55 new GraphicsDriverContentObserver(new Handler(Looper.getMainLooper()), this); 56 mSwitchWidgetController = switchWidgetController; 57 mSwitchWidgetController.setEnabled( 58 DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(context)); 59 mSwitchWidgetController.setChecked( 60 Settings.Global.getInt( 61 mContentResolver, Settings.Global.UPDATABLE_DRIVER_ALL_APPS, 62 UPDATABLE_DRIVER_DEFAULT) 63 != UPDATABLE_DRIVER_OFF); 64 mSwitchWidgetController.setListener(this); 65 } 66 67 @Override onStart()68 public void onStart() { 69 mSwitchWidgetController.startListening(); 70 mGraphicsDriverContentObserver.register(mContentResolver); 71 } 72 73 @Override onStop()74 public void onStop() { 75 mSwitchWidgetController.stopListening(); 76 mGraphicsDriverContentObserver.unregister(mContentResolver); 77 } 78 79 @Override onSwitchToggled(boolean isChecked)80 public boolean onSwitchToggled(boolean isChecked) { 81 final int graphicsDriverGlobalOption = Settings.Global.getInt( 82 mContentResolver, Settings.Global.UPDATABLE_DRIVER_ALL_APPS, 83 UPDATABLE_DRIVER_DEFAULT); 84 85 if (isChecked 86 && (graphicsDriverGlobalOption == UPDATABLE_DRIVER_DEFAULT 87 || graphicsDriverGlobalOption == UPDATABLE_DRIVER_PRODUCTION_ALL_APPS 88 || graphicsDriverGlobalOption == UPDATABLE_DRIVER_PRERELEASE_ALL_APPS)) { 89 return true; 90 } 91 92 if (!isChecked && graphicsDriverGlobalOption == UPDATABLE_DRIVER_OFF) { 93 return true; 94 } 95 96 Settings.Global.putInt(mContentResolver, Settings.Global.UPDATABLE_DRIVER_ALL_APPS, 97 isChecked ? UPDATABLE_DRIVER_DEFAULT : UPDATABLE_DRIVER_OFF); 98 99 return true; 100 } 101 102 @Override onGraphicsDriverContentChanged()103 public void onGraphicsDriverContentChanged() { 104 mSwitchWidgetController.setChecked( 105 Settings.Global.getInt( 106 mContentResolver, Settings.Global.UPDATABLE_DRIVER_ALL_APPS, 107 UPDATABLE_DRIVER_DEFAULT) 108 != UPDATABLE_DRIVER_OFF); 109 } 110 } 111