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 android.content.Context;
20 import android.os.SystemProperties;
21 import android.util.Log;
22 
23 import androidx.annotation.VisibleForTesting;
24 import androidx.preference.Preference;
25 import androidx.preference.TwoStatePreference;
26 
27 import com.android.settings.core.PreferenceControllerMixin;
28 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
29 
30 /**
31  * PreferenceController for MockModem
32  */
33 public class MockModemPreferenceController extends
34         DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener,
35         PreferenceControllerMixin {
36 
37     private static final String TAG = "MockModemPreferenceController";
38     private static final String ALLOW_MOCK_MODEM_KEY =
39             "allow_mock_modem";
40     @VisibleForTesting
41     static final String ALLOW_MOCK_MODEM_PROPERTY =
42             "persist.radio.allow_mock_modem";
43 
MockModemPreferenceController(Context context)44     public MockModemPreferenceController(Context context) {
45         super(context);
46     }
47 
48     @Override
getPreferenceKey()49     public String getPreferenceKey() {
50         return ALLOW_MOCK_MODEM_KEY;
51     }
52 
53     @Override
onPreferenceChange(Preference preference, Object newValue)54     public boolean onPreferenceChange(Preference preference, Object newValue) {
55         final boolean isEnabled = (Boolean) newValue;
56         try {
57             SystemProperties.set(ALLOW_MOCK_MODEM_PROPERTY,
58                     isEnabled ? "true" : "false");
59         } catch (RuntimeException e) {
60             Log.e(TAG, "Fail to set radio system property: " + e.getMessage());
61         }
62         return true;
63     }
64 
65     @Override
updateState(Preference preference)66     public void updateState(Preference preference) {
67         try {
68             final boolean isEnabled = SystemProperties.getBoolean(
69                     ALLOW_MOCK_MODEM_PROPERTY, false /* default */);
70             ((TwoStatePreference) mPreference).setChecked(isEnabled);
71         } catch (RuntimeException e) {
72             Log.e(TAG, "Fail to get radio system property: " + e.getMessage());
73         }
74     }
75 
76     @Override
onDeveloperOptionsSwitchDisabled()77     protected void onDeveloperOptionsSwitchDisabled() {
78         super.onDeveloperOptionsSwitchDisabled();
79         try {
80             SystemProperties.set(ALLOW_MOCK_MODEM_PROPERTY, "false");
81             ((TwoStatePreference) mPreference).setChecked(false);
82         } catch (RuntimeException e) {
83             Log.e(TAG, "Fail to set radio system property: " + e.getMessage());
84         }
85     }
86 }
87