• 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 ShowSurfaceUpdatesPreferenceController extends DeveloperOptionsPreferenceController
33         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
34 
35     private static final String SHOW_SCREEN_UPDATES_KEY = "show_screen_updates";
36 
37     private static final int SETTING_VALUE_ON = 1;
38     private static final int SETTING_VALUE_OFF = 0;
39 
40     @VisibleForTesting
41     static final String SURFACE_FLINGER_SERVICE_KEY = "SurfaceFlinger";
42     @VisibleForTesting
43     static final int SURFACE_FLINGER_READ_CODE = 1010;
44 
45     private static final int SURFACE_FLINGER_WRITE_SURFACE_UPDATES_CODE = 1002;
46     private static final String SURFACE_COMPOSER_INTERFACE_KEY = "android.ui.ISurfaceComposer";
47 
48     private final IBinder mSurfaceFlinger;
49 
ShowSurfaceUpdatesPreferenceController(Context context)50     public ShowSurfaceUpdatesPreferenceController(Context context) {
51         super(context);
52         mSurfaceFlinger = ServiceManager.getService(SURFACE_FLINGER_SERVICE_KEY);
53     }
54 
55     @Override
getPreferenceKey()56     public String getPreferenceKey() {
57         return SHOW_SCREEN_UPDATES_KEY;
58     }
59 
60     @Override
onPreferenceChange(Preference preference, Object newValue)61     public boolean onPreferenceChange(Preference preference, Object newValue) {
62         final boolean isEnabled = (Boolean) newValue;
63         writeShowUpdatesSetting(isEnabled);
64         return true;
65     }
66 
67     @Override
updateState(Preference preference)68     public void updateState(Preference preference) {
69         updateShowUpdatesSetting();
70     }
71 
72     @Override
onDeveloperOptionsSwitchDisabled()73     protected void onDeveloperOptionsSwitchDisabled() {
74         super.onDeveloperOptionsSwitchDisabled();
75         final SwitchPreference preference = (SwitchPreference) mPreference;
76         if (preference.isChecked()) {
77             // Writing false to the preference when the setting is already off will have a
78             // side effect of turning on the preference that we wish to avoid
79             writeShowUpdatesSetting(false);
80             preference.setChecked(false);
81         }
82     }
83 
84     @VisibleForTesting
updateShowUpdatesSetting()85     void updateShowUpdatesSetting() {
86         // magic communication with surface flinger.
87         try {
88             if (mSurfaceFlinger != null) {
89                 final Parcel data = Parcel.obtain();
90                 final Parcel reply = Parcel.obtain();
91                 data.writeInterfaceToken(SURFACE_COMPOSER_INTERFACE_KEY);
92                 mSurfaceFlinger.transact(SURFACE_FLINGER_READ_CODE, data, reply, 0 /* flags */);
93                 @SuppressWarnings("unused") final int showCpu = reply.readInt();
94                 @SuppressWarnings("unused") final int enableGL = reply.readInt();
95                 final int showUpdates = reply.readInt();
96                 ((SwitchPreference) mPreference).setChecked(showUpdates != SETTING_VALUE_OFF);
97                 reply.recycle();
98                 data.recycle();
99             }
100         } catch (RemoteException ex) {
101             // intentional no-op
102         }
103     }
104 
105     @VisibleForTesting
writeShowUpdatesSetting(boolean isEnabled)106     void writeShowUpdatesSetting(boolean isEnabled) {
107         try {
108             if (mSurfaceFlinger != null) {
109                 final Parcel data = Parcel.obtain();
110                 data.writeInterfaceToken(SURFACE_COMPOSER_INTERFACE_KEY);
111                 final int showUpdates = isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF;
112                 data.writeInt(showUpdates);
113                 mSurfaceFlinger.transact(SURFACE_FLINGER_WRITE_SURFACE_UPDATES_CODE, data,
114                         null /* reply */, 0 /* flags */);
115                 data.recycle();
116             }
117         } catch (RemoteException ex) {
118             // intentional no-op
119         }
120         updateShowUpdatesSetting();
121     }
122 }
123