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.SystemProperties; 21 import android.support.v14.preference.SwitchPreference; 22 import android.support.v7.preference.Preference; 23 import android.support.v7.preference.PreferenceScreen; 24 import android.widget.Toast; 25 26 import com.android.internal.annotations.VisibleForTesting; 27 import com.android.settings.core.PreferenceController; 28 import com.android.settings.R; 29 30 public class TelephonyMonitorPreferenceController extends PreferenceController { 31 32 private static final String KEY_TELEPHONY_MONITOR_SWITCH = "telephony_monitor_switch"; 33 @VisibleForTesting 34 static final String BUILD_TYPE = "ro.build.type"; 35 @VisibleForTesting 36 static final String PROPERTY_TELEPHONY_MONITOR = "persist.radio.enable_tel_mon"; 37 38 @VisibleForTesting 39 static final String ENABLED_STATUS = "enabled"; 40 @VisibleForTesting 41 static final String DISABLED_STATUS = "disabled"; 42 @VisibleForTesting 43 static final String USER_ENABLED_STATUS = "user_enabled"; 44 @VisibleForTesting 45 static final String USER_DISABLED_STATUS = "user_disabled"; 46 47 private SwitchPreference mPreference; 48 TelephonyMonitorPreferenceController(Context context)49 public TelephonyMonitorPreferenceController(Context context) { 50 super(context); 51 } 52 53 @Override displayPreference(PreferenceScreen screen)54 public void displayPreference(PreferenceScreen screen) { 55 super.displayPreference(screen); 56 if (isAvailable()) { 57 mPreference = (SwitchPreference) screen.findPreference(KEY_TELEPHONY_MONITOR_SWITCH); 58 mPreference.setChecked(isTelephonyMonitorEnabled()); 59 } 60 } 61 62 @Override getPreferenceKey()63 public String getPreferenceKey() { 64 return KEY_TELEPHONY_MONITOR_SWITCH; 65 } 66 67 @Override isAvailable()68 public boolean isAvailable() { 69 return mContext.getResources().getBoolean(R.bool.config_show_telephony_monitor) && 70 (SystemProperties.get(BUILD_TYPE).equals("userdebug") || 71 SystemProperties.get(BUILD_TYPE).equals("eng")); 72 } 73 74 @Override updateState(Preference preference)75 public void updateState(Preference preference) { 76 updatePreference(); 77 } 78 79 @Override handlePreferenceTreeClick(Preference preference)80 public boolean handlePreferenceTreeClick(Preference preference) { 81 if (KEY_TELEPHONY_MONITOR_SWITCH.equals(preference.getKey())) { 82 final SwitchPreference switchPreference = (SwitchPreference) preference; 83 SystemProperties.set(PROPERTY_TELEPHONY_MONITOR, 84 switchPreference.isChecked() ? USER_ENABLED_STATUS : USER_DISABLED_STATUS); 85 Toast.makeText(mContext, R.string.telephony_monitor_toast, 86 Toast.LENGTH_LONG).show(); 87 return true; 88 } 89 return false; 90 } 91 enablePreference(boolean enabled)92 public void enablePreference(boolean enabled) { 93 if (isAvailable()) { 94 mPreference.setEnabled(enabled); 95 } 96 } 97 updatePreference()98 public boolean updatePreference() { 99 if (!isAvailable()) { 100 return false; 101 } 102 final boolean enabled = isTelephonyMonitorEnabled(); 103 mPreference.setChecked(enabled); 104 return enabled; 105 } 106 isTelephonyMonitorEnabled()107 private boolean isTelephonyMonitorEnabled() { 108 final String tmStatus = SystemProperties.get(PROPERTY_TELEPHONY_MONITOR, DISABLED_STATUS); 109 return ENABLED_STATUS.equals(tmStatus) || USER_ENABLED_STATUS.equals(tmStatus); 110 } 111 112 } 113