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 package com.android.telephony.imsmedia.config; 17 18 import android.os.Build; 19 import android.os.Bundle; 20 import android.preference.CheckBoxPreference; 21 import android.preference.ListPreference; 22 import android.preference.Preference; 23 import android.preference.PreferenceActivity; 24 import android.util.SparseArray; 25 import android.view.Window; 26 import android.view.WindowManager.LayoutParams; 27 28 import com.android.telephony.imsmedia.JNIImsMediaService; 29 import com.android.telephony.imsmedia.R; 30 import com.android.telephony.imsmedia.util.Log; 31 32 /** 33 * The configuration of logging and test options for the libimsmedia 34 */ 35 public class ConfigPreference extends PreferenceActivity { 36 private static final String LOG_TAG = "ConfigPreference"; 37 private static final String PREFERENCE_ACTION = "com.android.telephony.imsmedia.PREFERENCE"; 38 private static final String KEY_LOG_MODE = "list_log_level"; 39 private static final String KEY_DEBUG_LOG_MODE_SOCKET = "log_mode_socket"; 40 private static final String KEY_DEBUG_LOG_MODE_AUDIO = "log_mode_audio"; 41 private static final String KEY_DEBUG_LOG_MODE_VIDEO = "log_mode_video"; 42 private static final String KEY_DEBUG_LOG_MODE_TEXT = "log_mode_text"; 43 private static final String KEY_DEBUG_LOG_MODE_RTP = "log_mode_rtp"; 44 private static final String KEY_DEBUG_LOG_MODE_PAYLOAD = "log_mode_payload"; 45 private static final String KEY_DEBUG_LOG_MODE_JITTER = "log_mode_jitterbuffer"; 46 private static final String KEY_DEBUG_LOG_MODE_RTCP = "log_mode_rtcp"; 47 private static final String KEY_DEBUG_LOG_MODE_RTPSTACK = "log_mode_rtpstack"; 48 private static final String KEY_DEBUG_LOG_MODE_VIDEO_JITTER = "log_mode_video_jitter"; 49 50 private static final int DEBUG_LOG_MODE_SOCKET = 1 << 0; 51 private static final int DEBUG_LOG_MODE_AUDIO = 1 << 1; 52 private static final int DEBUG_LOG_MODE_VIDEO = 1 << 2; 53 private static final int DEBUG_LOG_MODE_TEXT = 1 << 3; 54 private static final int DEBUG_LOG_MODE_RTP = 1 << 4; 55 private static final int DEBUG_LOG_MODE_PAYLOAD = 1 << 5; 56 private static final int DEBUG_LOG_MODE_JITTER = 1 << 6; 57 private static final int DEBUG_LOG_MODE_RTCP = 1 << 7; 58 private static final int DEBUG_LOG_MODE_RTPSTACK = 1 << 8; 59 private static final int DEBUG_LOG_MODE_VIDEO_JITTER = 1 << 9; 60 61 private static final String[] KEY_LIST_PREFERENCES = { 62 KEY_LOG_MODE 63 }; 64 65 private static final String[] KEY_CHECKBOX_PREFERENCES = { 66 KEY_DEBUG_LOG_MODE_SOCKET, 67 KEY_DEBUG_LOG_MODE_AUDIO, 68 KEY_DEBUG_LOG_MODE_VIDEO, 69 KEY_DEBUG_LOG_MODE_TEXT, 70 KEY_DEBUG_LOG_MODE_RTP, 71 KEY_DEBUG_LOG_MODE_PAYLOAD, 72 KEY_DEBUG_LOG_MODE_JITTER, 73 KEY_DEBUG_LOG_MODE_RTCP, 74 KEY_DEBUG_LOG_MODE_RTPSTACK, 75 KEY_DEBUG_LOG_MODE_VIDEO_JITTER 76 }; 77 78 private static final int[] DEBUG_MODE_ARRAY = { 79 DEBUG_LOG_MODE_SOCKET, 80 DEBUG_LOG_MODE_AUDIO, 81 DEBUG_LOG_MODE_VIDEO, 82 DEBUG_LOG_MODE_TEXT, 83 DEBUG_LOG_MODE_RTP, 84 DEBUG_LOG_MODE_PAYLOAD, 85 DEBUG_LOG_MODE_JITTER, 86 DEBUG_LOG_MODE_RTCP, 87 DEBUG_LOG_MODE_RTPSTACK, 88 DEBUG_LOG_MODE_VIDEO_JITTER 89 }; 90 91 private final SparseArray<ListPreference> mListPrefs = 92 new SparseArray<>(KEY_LIST_PREFERENCES.length); 93 private final SparseArray<CheckBoxPreference> mCheckboxPrefs = 94 new SparseArray<>(KEY_CHECKBOX_PREFERENCES.length); 95 private int mLogMode = 0; 96 private int mDebugLogMode = 0; 97 98 @SuppressWarnings("deprecation") 99 @Override onCreate(Bundle savedInstanceState)100 public void onCreate(Bundle savedInstanceState) { 101 Log.dc(LOG_TAG, "onCreate"); 102 103 super.onCreate(savedInstanceState); 104 105 String action = getIntent().getAction(); 106 107 // The Configuration Menu is blocked for production builds 108 // and access in debug/eng builds is restricted to preference action 109 if (!Build.IS_DEBUGGABLE || !(action != null && action.equals(PREFERENCE_ACTION))) { 110 Log.e(LOG_TAG, "Configuration Menu cannot be launched"); 111 finish(); // Close the current activity 112 return; 113 } 114 115 Window wd = getWindow(); 116 117 if (wd != null) { 118 LayoutParams layoutParams = wd.getAttributes(); 119 wd.setAttributes(layoutParams); 120 } 121 122 addPreferencesFromResource(R.xml.config_preference); 123 initPreferences(); 124 } 125 126 @Override onPause()127 public void onPause() { 128 super.onPause(); 129 mListPrefs.clear(); 130 mCheckboxPrefs.clear(); 131 } 132 133 @Override onResume()134 public void onResume() { 135 super.onResume(); 136 Log.dc(LOG_TAG, "onResume"); 137 initPreferences(); 138 } 139 140 @Override isValidFragment(String fragmentName)141 protected boolean isValidFragment(String fragmentName) { 142 if (ConfigListItemChangeListener.class.getName().equals(fragmentName) 143 || CheckBoxItemChangeListener.class.getName().equals(fragmentName)) { 144 return true; 145 } 146 return false; 147 } 148 initPreferences()149 private void initPreferences() { 150 Log.dc(LOG_TAG, "initPreferences"); 151 for (int i = 0; i < KEY_LIST_PREFERENCES.length; ++i) { 152 Log.dc(LOG_TAG, "initPreferences, key=" + KEY_LIST_PREFERENCES[i]); 153 ListPreference itemList = (ListPreference) findPreference(KEY_LIST_PREFERENCES[i]); 154 mListPrefs.put(i, itemList); 155 if (itemList != null) { 156 itemList.setOnPreferenceChangeListener(new ConfigListItemChangeListener(i)); 157 if (KEY_LIST_PREFERENCES[i].equals(KEY_LOG_MODE)) { 158 mLogMode = parseInt(itemList.getValue(), 0); 159 } 160 } 161 } 162 163 for (int i = 0; i < KEY_CHECKBOX_PREFERENCES.length; ++i) { 164 Log.dc(LOG_TAG, "initPreferences, key=" + KEY_CHECKBOX_PREFERENCES[i]); 165 CheckBoxPreference check = 166 (CheckBoxPreference) findPreference(KEY_CHECKBOX_PREFERENCES[i]); 167 mCheckboxPrefs.put(i, check); 168 if (check != null) { 169 check.setOnPreferenceChangeListener(new CheckBoxItemChangeListener(i)); 170 if (check.isChecked()) { 171 mDebugLogMode |= DEBUG_MODE_ARRAY[i]; 172 } else { 173 mDebugLogMode &= ~DEBUG_MODE_ARRAY[i]; 174 } 175 } 176 } 177 178 Log.dc(LOG_TAG, "initPreferences, LogMode=" + mLogMode + ", DebugLogMode=" + mDebugLogMode); 179 JNIImsMediaService.setLogMode(mLogMode, mDebugLogMode); 180 Log.init(mLogMode); 181 } 182 183 private final class ConfigListItemChangeListener 184 implements Preference.OnPreferenceChangeListener { 185 private int mPrefIndex; ConfigListItemChangeListener(int index)186 ConfigListItemChangeListener(int index) { 187 mPrefIndex = index; 188 } 189 190 @Override onPreferenceChange(Preference preference, Object newValue)191 public boolean onPreferenceChange(Preference preference, Object newValue) { 192 String value = newValue.toString(); 193 Log.dc(LOG_TAG, "onPreferenceChange: key=" + preference.getKey() + ",value=" + value); 194 ListPreference itemList = mListPrefs.valueAt(mPrefIndex); 195 if (itemList != null) { 196 mLogMode = parseInt(value, 0); 197 itemList.setSummary(value); 198 JNIImsMediaService.setLogMode(mLogMode, mDebugLogMode); 199 Log.setLogLevel(mLogMode); 200 } 201 return true; 202 } 203 } 204 205 private final class CheckBoxItemChangeListener 206 implements Preference.OnPreferenceChangeListener { 207 private int mPrefIndex; CheckBoxItemChangeListener(int index)208 CheckBoxItemChangeListener(int index) { 209 mPrefIndex = index; 210 } 211 212 @Override onPreferenceChange(Preference preference, Object newValue)213 public boolean onPreferenceChange(Preference preference, Object newValue) { 214 String value = newValue.toString(); 215 Log.dc(LOG_TAG, "onPreferenceChange: key=" + preference.getKey() + ", value=" + value); 216 boolean boolValue = Boolean.valueOf(value); 217 if (boolValue) { 218 mDebugLogMode |= DEBUG_MODE_ARRAY[mPrefIndex]; 219 } else { 220 mDebugLogMode &= ~DEBUG_MODE_ARRAY[mPrefIndex]; 221 } 222 JNIImsMediaService.setLogMode(mLogMode, mDebugLogMode); 223 Log.setLogLevel(mLogMode); 224 return true; 225 } 226 } 227 parseInt(String value, int defaultValue)228 private static int parseInt(String value, int defaultValue) { 229 int intValue = defaultValue; 230 try { 231 intValue = Integer.parseInt(value); 232 } catch (NumberFormatException e) { 233 Log.e(LOG_TAG, "NumberFormatException: " + e.toString()); 234 } 235 return intValue; 236 } 237 } 238