1 /* 2 * Copyright (C) 2010 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.inputmethod.latin; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.content.pm.PackageInfo; 22 import android.content.pm.PackageManager.NameNotFoundException; 23 import android.os.Bundle; 24 import android.os.Process; 25 import android.preference.CheckBoxPreference; 26 import android.preference.Preference; 27 import android.preference.PreferenceFragment; 28 import android.preference.PreferenceScreen; 29 import android.util.Log; 30 31 import com.android.inputmethod.keyboard.KeyboardSwitcher; 32 import com.android.inputmethod.research.ResearchLogger; 33 34 public final class DebugSettings extends PreferenceFragment 35 implements SharedPreferences.OnSharedPreferenceChangeListener { 36 private static final String TAG = DebugSettings.class.getSimpleName(); 37 38 public static final String PREF_DEBUG_MODE = "debug_mode"; 39 public static final String PREF_FORCE_NON_DISTINCT_MULTITOUCH = "force_non_distinct_multitouch"; 40 public static final String PREF_USABILITY_STUDY_MODE = "usability_study_mode"; 41 public static final String PREF_STATISTICS_LOGGING = "enable_logging"; 42 private static final String PREF_READ_EXTERNAL_DICTIONARY = "read_external_dictionary"; 43 private static final boolean SHOW_STATISTICS_LOGGING = false; 44 45 private boolean mServiceNeedsRestart = false; 46 private CheckBoxPreference mDebugMode; 47 private CheckBoxPreference mStatisticsLoggingPref; 48 49 @Override 50 public void onCreate(Bundle icicle) { 51 super.onCreate(icicle); 52 addPreferencesFromResource(R.xml.prefs_for_debug); 53 SharedPreferences prefs = getPreferenceManager().getSharedPreferences(); 54 prefs.registerOnSharedPreferenceChangeListener(this); 55 56 final Preference usabilityStudyPref = findPreference(PREF_USABILITY_STUDY_MODE); 57 if (usabilityStudyPref instanceof CheckBoxPreference) { 58 final CheckBoxPreference checkbox = (CheckBoxPreference)usabilityStudyPref; 59 checkbox.setChecked(prefs.getBoolean(PREF_USABILITY_STUDY_MODE, 60 LatinImeLogger.getUsabilityStudyMode(prefs))); 61 checkbox.setSummary(R.string.settings_warning_researcher_mode); 62 } 63 final Preference statisticsLoggingPref = findPreference(PREF_STATISTICS_LOGGING); 64 if (statisticsLoggingPref instanceof CheckBoxPreference) { 65 mStatisticsLoggingPref = (CheckBoxPreference) statisticsLoggingPref; 66 if (!SHOW_STATISTICS_LOGGING) { 67 getPreferenceScreen().removePreference(statisticsLoggingPref); 68 } 69 } 70 71 PreferenceScreen readExternalDictionary = 72 (PreferenceScreen) findPreference(PREF_READ_EXTERNAL_DICTIONARY); 73 if (null != readExternalDictionary) { 74 readExternalDictionary.setOnPreferenceClickListener( 75 new Preference.OnPreferenceClickListener() { 76 @Override 77 public boolean onPreferenceClick(final Preference arg0) { 78 ExternalDictionaryGetterForDebug.chooseAndInstallDictionary( 79 getActivity()); 80 mServiceNeedsRestart = true; 81 return true; 82 } 83 }); 84 } 85 86 mServiceNeedsRestart = false; 87 mDebugMode = (CheckBoxPreference) findPreference(PREF_DEBUG_MODE); 88 updateDebugMode(); 89 } 90 91 @Override 92 public void onStop() { 93 super.onStop(); 94 if (mServiceNeedsRestart) Process.killProcess(Process.myPid()); 95 } 96 97 @Override 98 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { 99 if (key.equals(PREF_DEBUG_MODE)) { 100 if (mDebugMode != null) { 101 mDebugMode.setChecked(prefs.getBoolean(PREF_DEBUG_MODE, false)); 102 final boolean checked = mDebugMode.isChecked(); 103 if (mStatisticsLoggingPref != null) { 104 if (checked) { 105 getPreferenceScreen().addPreference(mStatisticsLoggingPref); 106 } else { 107 getPreferenceScreen().removePreference(mStatisticsLoggingPref); 108 } 109 } 110 updateDebugMode(); 111 mServiceNeedsRestart = true; 112 } 113 } else if (key.equals(PREF_FORCE_NON_DISTINCT_MULTITOUCH) 114 || key.equals(KeyboardSwitcher.PREF_KEYBOARD_LAYOUT)) { 115 mServiceNeedsRestart = true; 116 } 117 } 118 119 private void updateDebugMode() { 120 if (mDebugMode == null) { 121 return; 122 } 123 boolean isDebugMode = mDebugMode.isChecked(); 124 final String version = getResources().getString( 125 R.string.version_text, Utils.getVersionName(getActivity())); 126 if (!isDebugMode) { 127 mDebugMode.setTitle(version); 128 mDebugMode.setSummary(""); 129 } else { 130 mDebugMode.setTitle(getResources().getString(R.string.prefs_debug_mode)); 131 mDebugMode.setSummary(version); 132 } 133 } 134 } 135