Home | History | Annotate | Download | only in latin
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * 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.util.Log;
     29 
     30 import com.android.inputmethod.keyboard.KeyboardSwitcher;
     31 import com.android.inputmethod.research.ResearchLogger;
     32 
     33 public final class DebugSettings extends PreferenceFragment
     34         implements SharedPreferences.OnSharedPreferenceChangeListener {
     35 
     36     private static final String TAG = DebugSettings.class.getSimpleName();
     37     private static final String DEBUG_MODE_KEY = "debug_mode";
     38     public static final String FORCE_NON_DISTINCT_MULTITOUCH_KEY = "force_non_distinct_multitouch";
     39     public static final String PREF_USABILITY_STUDY_MODE = "usability_study_mode";
     40 
     41     private boolean mServiceNeedsRestart = false;
     42     private CheckBoxPreference mDebugMode;
     43 
     44     @Override
     45     public void onCreate(Bundle icicle) {
     46         super.onCreate(icicle);
     47         addPreferencesFromResource(R.xml.prefs_for_debug);
     48         SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
     49         prefs.registerOnSharedPreferenceChangeListener(this);
     50 
     51         final Preference usabilityStudyPref = findPreference(PREF_USABILITY_STUDY_MODE);
     52         if (usabilityStudyPref instanceof CheckBoxPreference) {
     53             final CheckBoxPreference checkbox = (CheckBoxPreference)usabilityStudyPref;
     54             checkbox.setChecked(prefs.getBoolean(PREF_USABILITY_STUDY_MODE,
     55                     ResearchLogger.DEFAULT_USABILITY_STUDY_MODE));
     56             checkbox.setSummary(R.string.settings_warning_researcher_mode);
     57         }
     58 
     59         mServiceNeedsRestart = false;
     60         mDebugMode = (CheckBoxPreference) findPreference(DEBUG_MODE_KEY);
     61         updateDebugMode();
     62     }
     63 
     64     @Override
     65     public void onStop() {
     66         super.onStop();
     67         if (mServiceNeedsRestart) Process.killProcess(Process.myPid());
     68     }
     69 
     70     @Override
     71     public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
     72         if (key.equals(DEBUG_MODE_KEY)) {
     73             if (mDebugMode != null) {
     74                 mDebugMode.setChecked(prefs.getBoolean(DEBUG_MODE_KEY, false));
     75                 updateDebugMode();
     76                 mServiceNeedsRestart = true;
     77             }
     78         } else if (key.equals(FORCE_NON_DISTINCT_MULTITOUCH_KEY)
     79                 || key.equals(KeyboardSwitcher.PREF_KEYBOARD_LAYOUT)) {
     80             mServiceNeedsRestart = true;
     81         }
     82     }
     83 
     84     private void updateDebugMode() {
     85         if (mDebugMode == null) {
     86             return;
     87         }
     88         boolean isDebugMode = mDebugMode.isChecked();
     89         String version = "";
     90         try {
     91             final Context context = getActivity();
     92             final String packageName = context.getPackageName();
     93             PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
     94             version = "Version " + info.versionName;
     95         } catch (NameNotFoundException e) {
     96             Log.e(TAG, "Could not find version info.");
     97         }
     98         if (!isDebugMode) {
     99             mDebugMode.setTitle(version);
    100             mDebugMode.setSummary("");
    101         } else {
    102             mDebugMode.setTitle(getResources().getString(R.string.prefs_debug_mode));
    103             mDebugMode.setSummary(version);
    104         }
    105     }
    106 }
    107