1 /*
2  * Copyright (C) 2023 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 static android.os.UserHandle.USER_CURRENT;
20 
21 import android.content.Context;
22 import android.content.om.IOverlayManager;
23 import android.os.RemoteException;
24 import android.os.ServiceManager;
25 
26 import androidx.preference.Preference;
27 import androidx.preference.TwoStatePreference;
28 
29 import com.android.internal.R;
30 import com.android.internal.annotations.VisibleForTesting;
31 import com.android.settings.core.PreferenceControllerMixin;
32 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
33 
34 public class TransparentNavigationBarPreferenceController
35         extends DeveloperOptionsPreferenceController
36         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
37 
38     private static final String TRANSPARENT_NAVIGATION_BAR_KEY =
39             "transparent_navigation_bar";
40 
41     private static final String OVERLAY_PACKAGE_NAME =
42             "com.android.internal.systemui.navbar.transparent";
43 
44     private final IOverlayManager mOverlayManager;
45 
TransparentNavigationBarPreferenceController(Context context)46     public TransparentNavigationBarPreferenceController(Context context) {
47         super(context);
48         mOverlayManager = IOverlayManager.Stub.asInterface(
49                 ServiceManager.getService(Context.OVERLAY_SERVICE));
50     }
51 
52     @Override
getPreferenceKey()53     public String getPreferenceKey() {
54         return TRANSPARENT_NAVIGATION_BAR_KEY;
55     }
56 
57     @Override
onPreferenceChange(Preference preference, Object newValue)58     public boolean onPreferenceChange(Preference preference, Object newValue) {
59         setEnabled((boolean) newValue);
60         return true;
61     }
62 
63     @Override
updateState(Preference preference)64     public void updateState(Preference preference) {
65         ((TwoStatePreference) mPreference).setChecked(isEnabled());
66     }
67 
68     @Override
onDeveloperOptionsSwitchDisabled()69     protected void onDeveloperOptionsSwitchDisabled() {
70         super.onDeveloperOptionsSwitchDisabled();
71         ((TwoStatePreference) mPreference).setChecked(false);
72         final boolean enabled = isEnabled();
73         if (!enabled) {
74             setEnabled(false);
75         }
76     }
77 
78     @VisibleForTesting
isEnabled()79     protected boolean isEnabled() {
80         return mContext.getResources().getBoolean(R.bool.config_navBarDefaultTransparent);
81     }
82 
83     @VisibleForTesting
setEnabled(boolean enabled)84     protected void setEnabled(boolean enabled) {
85         try {
86             mOverlayManager.setEnabled(OVERLAY_PACKAGE_NAME, enabled, USER_CURRENT);
87         } catch (RemoteException e) {
88             throw e.rethrowFromSystemServer();
89         }
90     }
91 }
92