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.GAME_DRIVER_ALL_APPS;
20 import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableForAllAppsPreferenceController.GAME_DRIVER_DEFAULT;
21 import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableForAllAppsPreferenceController.GAME_DRIVER_OFF;
22 import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableForAllAppsPreferenceController.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 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.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         mGraphicsDriverContentObserver.register(mContentResolver);
73     }
74 
75     @Override
onStop()76     public void onStop() {
77         mSwitchWidgetController.stopListening();
78         mGraphicsDriverContentObserver.unregister(mContentResolver);
79     }
80 
81     @Override
onSwitchToggled(boolean isChecked)82     public boolean onSwitchToggled(boolean isChecked) {
83         final int graphicsDriverGlobalOption = Settings.Global.getInt(
84                 mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT);
85 
86         if (isChecked
87                 && (graphicsDriverGlobalOption == GAME_DRIVER_DEFAULT
88                         || graphicsDriverGlobalOption == GAME_DRIVER_ALL_APPS
89                         || graphicsDriverGlobalOption == GAME_DRIVER_PRERELEASE_ALL_APPS)) {
90             return true;
91         }
92 
93         if (!isChecked && graphicsDriverGlobalOption == GAME_DRIVER_OFF) {
94             return true;
95         }
96 
97         Settings.Global.putInt(mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS,
98                 isChecked ? GAME_DRIVER_DEFAULT : GAME_DRIVER_OFF);
99 
100         return true;
101     }
102 
103     @Override
onGraphicsDriverContentChanged()104     public void onGraphicsDriverContentChanged() {
105         mSwitchWidgetController.setChecked(
106                 Settings.Global.getInt(
107                         mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT)
108                 != GAME_DRIVER_OFF);
109     }
110 }
111