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
17package com.android.inputmethod.latin;
18
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.content.pm.PackageInfo;
22import android.content.pm.PackageManager.NameNotFoundException;
23import android.os.Bundle;
24import android.os.Process;
25import android.preference.CheckBoxPreference;
26import android.preference.PreferenceFragment;
27import android.util.Log;
28
29import com.android.inputmethod.keyboard.KeyboardSwitcher;
30
31public class DebugSettings extends PreferenceFragment
32        implements SharedPreferences.OnSharedPreferenceChangeListener {
33
34    private static final String TAG = DebugSettings.class.getSimpleName();
35    private static final String DEBUG_MODE_KEY = "debug_mode";
36    public static final String FORCE_NON_DISTINCT_MULTITOUCH_KEY = "force_non_distinct_multitouch";
37
38    private boolean mServiceNeedsRestart = false;
39    private CheckBoxPreference mDebugMode;
40
41    @Override
42    public void onCreate(Bundle icicle) {
43        super.onCreate(icicle);
44        addPreferencesFromResource(R.xml.prefs_for_debug);
45        SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
46        prefs.registerOnSharedPreferenceChangeListener(this);
47
48        mServiceNeedsRestart = false;
49        mDebugMode = (CheckBoxPreference) findPreference(DEBUG_MODE_KEY);
50        updateDebugMode();
51    }
52
53    @Override
54    public void onStop() {
55        super.onStop();
56        if (mServiceNeedsRestart) Process.killProcess(Process.myPid());
57    }
58
59    @Override
60    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
61        if (key.equals(DEBUG_MODE_KEY)) {
62            if (mDebugMode != null) {
63                mDebugMode.setChecked(prefs.getBoolean(DEBUG_MODE_KEY, false));
64                updateDebugMode();
65                mServiceNeedsRestart = true;
66            }
67        } else if (key.equals(FORCE_NON_DISTINCT_MULTITOUCH_KEY)
68                || key.equals(KeyboardSwitcher.PREF_KEYBOARD_LAYOUT)) {
69            mServiceNeedsRestart = true;
70        }
71    }
72
73    private void updateDebugMode() {
74        if (mDebugMode == null) {
75            return;
76        }
77        boolean isDebugMode = mDebugMode.isChecked();
78        String version = "";
79        try {
80            final Context context = getActivity();
81            final String packageName = context.getPackageName();
82            PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
83            version = "Version " + info.versionName;
84        } catch (NameNotFoundException e) {
85            Log.e(TAG, "Could not find version info.");
86        }
87        if (!isDebugMode) {
88            mDebugMode.setTitle(version);
89            mDebugMode.setSummary("");
90        } else {
91            mDebugMode.setTitle(getResources().getString(R.string.prefs_debug_mode));
92            mDebugMode.setSummary(version);
93        }
94    }
95}
96