• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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;
18 
19 import android.content.Context;
20 import android.os.IBinder;
21 import android.os.Parcel;
22 import android.os.RemoteException;
23 import android.os.ServiceManager;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.Preference;
27 import androidx.preference.SwitchPreference;
28 
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
31 
32 public class HardwareOverlaysPreferenceController extends DeveloperOptionsPreferenceController
33         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
34 
35     private static final String DISABLE_OVERLAYS_KEY = "disable_overlays";
36 
37     private static final int SETTING_VALUE_ON = 1;
38     private static final int SETTING_VALUE_OFF = 0;
39     private static final String SURFACE_FLINGER_SERVICE_KEY = "SurfaceFlinger";
40 
41     @VisibleForTesting
42     static final int SURFACE_FLINGER_READ_CODE = 1010;
43 
44     private static final int SURFACE_FLINGER_DISABLE_OVERLAYS_CODE = 1008;
45     private static final String SURFACE_COMPOSER_INTERFACE_KEY = "android.ui.ISurfaceComposer";
46 
47     private final IBinder mSurfaceFlinger;
48 
HardwareOverlaysPreferenceController(Context context)49     public HardwareOverlaysPreferenceController(Context context) {
50         super(context);
51         mSurfaceFlinger = ServiceManager.getService(SURFACE_FLINGER_SERVICE_KEY);
52     }
53 
54     @Override
getPreferenceKey()55     public String getPreferenceKey() {
56         return DISABLE_OVERLAYS_KEY;
57     }
58 
59     @Override
onPreferenceChange(Preference preference, Object newValue)60     public boolean onPreferenceChange(Preference preference, Object newValue) {
61         final boolean isEnabled = (Boolean) newValue;
62         writeHardwareOverlaysSetting(isEnabled);
63         return true;
64     }
65 
66     @Override
updateState(Preference preference)67     public void updateState(Preference preference) {
68         updateHardwareOverlaysSetting();
69     }
70 
71     @Override
onDeveloperOptionsSwitchDisabled()72     protected void onDeveloperOptionsSwitchDisabled() {
73         super.onDeveloperOptionsSwitchDisabled();
74         final SwitchPreference switchPreference = (SwitchPreference) mPreference;
75         if (switchPreference.isChecked()) {
76             // Writing false to the preference when the setting is already off will have a
77             // side effect of turning on the preference that we wish to avoid
78             writeHardwareOverlaysSetting(false);
79             switchPreference.setChecked(false);
80         }
81     }
82 
83     @VisibleForTesting
updateHardwareOverlaysSetting()84     void updateHardwareOverlaysSetting() {
85         if (mSurfaceFlinger == null) {
86             return;
87         }
88         // magic communication with surface flinger.
89         try {
90             final Parcel data = Parcel.obtain();
91             final Parcel reply = Parcel.obtain();
92             data.writeInterfaceToken(SURFACE_COMPOSER_INTERFACE_KEY);
93             mSurfaceFlinger.transact(SURFACE_FLINGER_READ_CODE, data, reply, 0 /* flags */);
94             @SuppressWarnings("unused") final int showCpu = reply.readInt();
95             @SuppressWarnings("unused") final int enableGL = reply.readInt();
96             @SuppressWarnings("unused") final int showUpdates = reply.readInt();
97             @SuppressWarnings("unused") final int showBackground = reply.readInt();
98             final int disableOverlays = reply.readInt();
99             ((SwitchPreference) mPreference).setChecked(disableOverlays != SETTING_VALUE_OFF);
100             reply.recycle();
101             data.recycle();
102         } catch (RemoteException ex) {
103             // intentional no-op
104         }
105     }
106 
107     @VisibleForTesting
writeHardwareOverlaysSetting(boolean isEnabled)108     void writeHardwareOverlaysSetting(boolean isEnabled) {
109         if (mSurfaceFlinger == null) {
110             return;
111         }
112         try {
113             final Parcel data = Parcel.obtain();
114             data.writeInterfaceToken(SURFACE_COMPOSER_INTERFACE_KEY);
115             final int disableOverlays = isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF;
116             data.writeInt(disableOverlays);
117             mSurfaceFlinger.transact(SURFACE_FLINGER_DISABLE_OVERLAYS_CODE, data,
118                     null /* reply */, 0 /* flags */);
119             data.recycle();
120         } catch (RemoteException ex) {
121             // intentional no-op
122         }
123         updateHardwareOverlaysSetting();
124     }
125 }
126