DebugSettings.java revision b4c7a10840996cba4f185806cd96992974e1a000
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
17package com.android.inputmethod.latin.settings;
18
19import android.content.SharedPreferences;
20import android.os.Bundle;
21import android.os.Process;
22import android.preference.CheckBoxPreference;
23import android.preference.Preference;
24import android.preference.PreferenceFragment;
25import android.preference.PreferenceScreen;
26
27import com.android.inputmethod.keyboard.KeyboardSwitcher;
28import com.android.inputmethod.latin.LatinImeLogger;
29import com.android.inputmethod.latin.R;
30import com.android.inputmethod.latin.debug.ExternalDictionaryGetterForDebug;
31import com.android.inputmethod.latin.utils.ApplicationUtils;
32
33public 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(PREF_USE_ONLY_PERSONALIZATION_DICTIONARY_FOR_DEBUG)) {
117            mServiceNeedsRestart = true;
118        }
119    }
120
121    private void updateDebugMode() {
122        if (mDebugMode == null) {
123            return;
124        }
125        boolean isDebugMode = mDebugMode.isChecked();
126        final String version = getResources().getString(
127                R.string.version_text, ApplicationUtils.getVersionName(getActivity()));
128        if (!isDebugMode) {
129            mDebugMode.setTitle(version);
130            mDebugMode.setSummary("");
131        } else {
132            mDebugMode.setTitle(getResources().getString(R.string.prefs_debug_mode));
133            mDebugMode.setSummary(version);
134        }
135    }
136}
137