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 17 package com.android.inputmethod.pinyin; 18 19 import java.util.List; 20 21 import android.os.Bundle; 22 import android.preference.CheckBoxPreference; 23 import android.preference.Preference; 24 import android.preference.PreferenceActivity; 25 import android.preference.PreferenceGroup; 26 import android.preference.PreferenceManager; 27 import android.preference.PreferenceScreen; 28 import com.android.inputmethod.pinyin.Settings; 29 import android.content.Intent; 30 import android.content.pm.PackageManager; 31 import android.content.pm.ResolveInfo; 32 33 /** 34 * Setting activity of Pinyin IME. 35 */ 36 public 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 onCreate(Bundle savedInstanceState)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 onResume()70 protected void onResume() { 71 super.onResume(); 72 updateWidgets(); 73 } 74 75 @Override onDestroy()76 protected void onDestroy() { 77 Settings.releaseInstance(); 78 super.onDestroy(); 79 } 80 81 @Override onPause()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 onPreferenceChange(Preference preference, Object newValue)91 public boolean onPreferenceChange(Preference preference, Object newValue) { 92 return true; 93 } 94 updateWidgets()95 private void updateWidgets() { 96 mKeySoundPref.setChecked(Settings.getKeySound()); 97 mVibratePref.setChecked(Settings.getVibrate()); 98 mPredictionPref.setChecked(Settings.getPrediction()); 99 } 100 updatePreference(PreferenceGroup parentPref, String prefKey)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