1 /*
2  * Copyright (C) 2022 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.util.FeatureFlagUtils.SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS;
20 
21 import android.content.Context;
22 import android.util.FeatureFlagUtils;
23 import android.util.Log;
24 
25 import androidx.preference.Preference;
26 import androidx.preference.TwoStatePreference;
27 
28 import com.android.settings.core.PreferenceControllerMixin;
29 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
30 
31 /**
32  * PreferenceController for MockModem
33  */
34 public class PhantomProcessPreferenceController extends
35         DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener,
36         PreferenceControllerMixin {
37 
38     private static final String TAG = "PhantomProcessPreferenceController";
39     private static final String DISABLE_PHANTOM_PROCESS_MONITOR_KEY =
40             "disable_phantom_process_monitor";
41 
PhantomProcessPreferenceController(Context context)42     public PhantomProcessPreferenceController(Context context) {
43         super(context);
44     }
45 
46     @Override
getPreferenceKey()47     public String getPreferenceKey() {
48         return DISABLE_PHANTOM_PROCESS_MONITOR_KEY;
49     }
50 
51     @Override
onPreferenceChange(Preference preference, Object newValue)52     public boolean onPreferenceChange(Preference preference, Object newValue) {
53         final boolean isEnabled = (Boolean) newValue; // true means we're disabling this flag.
54         try {
55             FeatureFlagUtils.setEnabled(mContext,
56                     SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS,
57                     !isEnabled);
58         } catch (RuntimeException e) {
59             Log.e(TAG, "Fail to set feature flag: " + e.getMessage());
60         }
61         return true;
62     }
63 
64     @Override
updateState(Preference preference)65     public void updateState(Preference preference) {
66         try {
67             final boolean isEnabled = !FeatureFlagUtils.isEnabled(mContext,
68                     SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS);
69             ((TwoStatePreference) mPreference).setChecked(isEnabled);
70         } catch (RuntimeException e) {
71             Log.e(TAG, "Fail to get feature flag: " + e.getMessage());
72         }
73     }
74 
75     @Override
onDeveloperOptionsSwitchDisabled()76     protected void onDeveloperOptionsSwitchDisabled() {
77         super.onDeveloperOptionsSwitchDisabled();
78         try {
79             FeatureFlagUtils.setEnabled(mContext,
80                     SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS,
81                     true /* Enable the monitoring */);
82             ((TwoStatePreference) mPreference).setChecked(false);
83         } catch (RuntimeException e) {
84             Log.e(TAG, "Fail to set feature flag: " + e.getMessage());
85         }
86     }
87 }
88