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