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  * Copyright (C) 2023 The Android Open Source Project
18  *
19  * Licensed under the Apache License, Version 2.0 (the "License");
20  * you may not use this file except in compliance with the License.
21  * You may obtain a copy of the License at
22  *
23  *      http://www.apache.org/licenses/LICENSE-2.0
24  *
25  * Unless required by applicable law or agreed to in writing, software
26  * distributed under the License is distributed on an "AS IS" BASIS,
27  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28  * See the License for the specific language governing permissions and
29  * limitations under the License.
30  */
31 
32 package com.android.settings.development;
33 
34 import android.content.Context;
35 import android.content.om.IOverlayManager;
36 import android.content.pm.UserInfo;
37 import android.os.RemoteException;
38 import android.os.ServiceManager;
39 import android.os.UserManager;
40 
41 import androidx.preference.Preference;
42 import androidx.preference.TwoStatePreference;
43 
44 import com.android.internal.R;
45 import com.android.internal.annotations.VisibleForTesting;
46 import com.android.settings.core.PreferenceControllerMixin;
47 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
48 
49 public class ForceEnableNotesRolePreferenceController
50         extends DeveloperOptionsPreferenceController
51         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
52 
53 
54     @VisibleForTesting
55     static final String OVERLAY_PACKAGE_NAME =
56             "com.android.role.notes.enabled";
57 
58     private static final String NOTES_ROLE_ENABLED_KEY =
59             "force_enable_notes_role";
60 
61     private final IOverlayManager mOverlayManager;
62     private final UserManager mUserManager;
63 
ForceEnableNotesRolePreferenceController(Context context)64     public ForceEnableNotesRolePreferenceController(Context context) {
65         super(context);
66         mOverlayManager = IOverlayManager.Stub.asInterface(
67                 ServiceManager.getService(Context.OVERLAY_SERVICE));
68         mUserManager = context.getSystemService(UserManager.class);
69     }
70 
71     @Override
getPreferenceKey()72     public String getPreferenceKey() {
73         return NOTES_ROLE_ENABLED_KEY;
74     }
75 
76     @Override
onPreferenceChange(Preference preference, Object newValue)77     public boolean onPreferenceChange(Preference preference, Object newValue) {
78         setEnabled((boolean) newValue);
79         return true;
80     }
81 
82     @Override
updateState(Preference preference)83     public void updateState(Preference preference) {
84         ((TwoStatePreference) mPreference).setChecked(isEnabled());
85     }
86 
87     @Override
onDeveloperOptionsSwitchDisabled()88     protected void onDeveloperOptionsSwitchDisabled() {
89         super.onDeveloperOptionsSwitchDisabled();
90         ((TwoStatePreference) mPreference).setChecked(false);
91         setEnabled(false);
92     }
93 
94     @VisibleForTesting
isEnabled()95     protected boolean isEnabled() {
96         return mContext.getResources().getBoolean(R.bool.config_enableDefaultNotes);
97     }
98 
99     @VisibleForTesting
setEnabled(boolean enabled)100     protected void setEnabled(boolean enabled) {
101         try {
102             for (UserInfo user : mUserManager.getUsers()) {
103                 if (user.isFull() || user.isProfile()) {
104                     mOverlayManager.setEnabled(OVERLAY_PACKAGE_NAME, enabled, user.id);
105                 }
106             }
107         } catch (RemoteException e) {
108             throw e.rethrowFromSystemServer();
109         }
110     }
111 }
112