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