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