1/*
2 * Copyright (C) 2009 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.pinyin;
18
19import java.util.List;
20
21import android.os.Bundle;
22import android.preference.CheckBoxPreference;
23import android.preference.Preference;
24import android.preference.PreferenceActivity;
25import android.preference.PreferenceGroup;
26import android.preference.PreferenceManager;
27import android.preference.PreferenceScreen;
28import com.android.inputmethod.pinyin.Settings;
29import android.content.Intent;
30import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
32
33/**
34 * Setting activity of Pinyin IME.
35 */
36public class SettingsActivity extends PreferenceActivity implements
37        Preference.OnPreferenceChangeListener {
38
39    private static String TAG = "SettingsActivity";
40
41    private CheckBoxPreference mKeySoundPref;
42    private CheckBoxPreference mVibratePref;
43    private CheckBoxPreference mPredictionPref;
44
45    @Override
46    protected void onCreate(Bundle savedInstanceState) {
47        super.onCreate(savedInstanceState);
48        addPreferencesFromResource(R.xml.settings);
49
50        PreferenceScreen prefSet = getPreferenceScreen();
51
52        mKeySoundPref = (CheckBoxPreference) prefSet
53                .findPreference(getString(R.string.setting_sound_key));
54        mVibratePref = (CheckBoxPreference) prefSet
55                .findPreference(getString(R.string.setting_vibrate_key));
56        mPredictionPref = (CheckBoxPreference) prefSet
57                .findPreference(getString(R.string.setting_prediction_key));
58
59        prefSet.setOnPreferenceChangeListener(this);
60
61        Settings.getInstance(PreferenceManager
62                .getDefaultSharedPreferences(getApplicationContext()));
63
64        updatePreference(prefSet, getString(R.string.setting_advanced_key));
65
66        updateWidgets();
67    }
68
69    @Override
70    protected void onResume() {
71        super.onResume();
72        updateWidgets();
73    }
74
75    @Override
76    protected void onDestroy() {
77        Settings.releaseInstance();
78        super.onDestroy();
79    }
80
81    @Override
82    protected void onPause() {
83        super.onPause();
84        Settings.setKeySound(mKeySoundPref.isChecked());
85        Settings.setVibrate(mVibratePref.isChecked());
86        Settings.setPrediction(mPredictionPref.isChecked());
87
88        Settings.writeBack();
89    }
90
91    public boolean onPreferenceChange(Preference preference, Object newValue) {
92        return true;
93    }
94
95    private void updateWidgets() {
96        mKeySoundPref.setChecked(Settings.getKeySound());
97        mVibratePref.setChecked(Settings.getVibrate());
98        mPredictionPref.setChecked(Settings.getPrediction());
99    }
100
101    public void updatePreference(PreferenceGroup parentPref, String prefKey) {
102        Preference preference = parentPref.findPreference(prefKey);
103        if (preference == null) {
104            return;
105        }
106        Intent intent = preference.getIntent();
107        if (intent != null) {
108            PackageManager pm = getPackageManager();
109            List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
110            int listSize = list.size();
111            if (listSize == 0)
112                parentPref.removePreference(preference);
113        }
114    }
115}
116