Home | History | Annotate | Download | only in spare_parts
      1 /* //device/apps/Settings/src/com/android/settings/Keyguard.java
      2 **
      3 ** Copyright 2006, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 package com.android.spare_parts;
     19 
     20 import android.app.ActivityManagerNative;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.SharedPreferences;
     24 import android.content.pm.ApplicationInfo;
     25 import android.content.pm.PackageManager;
     26 import android.content.pm.ResolveInfo;
     27 import android.content.pm.PackageManager.NameNotFoundException;
     28 import android.content.res.Configuration;
     29 import android.os.RemoteException;
     30 import android.os.ServiceManager;
     31 import android.preference.CheckBoxPreference;
     32 import android.preference.ListPreference;
     33 import android.preference.Preference;
     34 import android.preference.PreferenceActivity;
     35 import android.preference.PreferenceGroup;
     36 import android.preference.PreferenceScreen;
     37 import android.provider.Settings;
     38 import android.provider.Settings.SettingNotFoundException;
     39 import android.os.Bundle;
     40 import android.util.Log;
     41 import android.view.IWindowManager;
     42 
     43 import java.util.List;
     44 
     45 public class SpareParts extends PreferenceActivity
     46         implements Preference.OnPreferenceChangeListener,
     47         SharedPreferences.OnSharedPreferenceChangeListener {
     48     private static final String TAG = "SpareParts";
     49 
     50     private static final String BATTERY_HISTORY_PREF = "battery_history_settings";
     51     private static final String BATTERY_INFORMATION_PREF = "battery_information_settings";
     52     private static final String USAGE_STATISTICS_PREF = "usage_statistics_settings";
     53 
     54     private static final String WINDOW_ANIMATIONS_PREF = "window_animations";
     55     private static final String TRANSITION_ANIMATIONS_PREF = "transition_animations";
     56     private static final String FANCY_IME_ANIMATIONS_PREF = "fancy_ime_animations";
     57     private static final String HAPTIC_FEEDBACK_PREF = "haptic_feedback";
     58     private static final String FONT_SIZE_PREF = "font_size";
     59     private static final String END_BUTTON_PREF = "end_button";
     60     private static final String KEY_COMPATIBILITY_MODE = "compatibility_mode";
     61 
     62     private final Configuration mCurConfig = new Configuration();
     63 
     64     private ListPreference mWindowAnimationsPref;
     65     private ListPreference mTransitionAnimationsPref;
     66     private CheckBoxPreference mFancyImeAnimationsPref;
     67     private CheckBoxPreference mHapticFeedbackPref;
     68     private ListPreference mFontSizePref;
     69     private ListPreference mEndButtonPref;
     70     private CheckBoxPreference mCompatibilityMode;
     71 
     72     private IWindowManager mWindowManager;
     73 
     74     public static boolean updatePreferenceToSpecificActivityOrRemove(Context context,
     75             PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) {
     76 
     77         Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
     78         if (preference == null) {
     79             return false;
     80         }
     81 
     82         Intent intent = preference.getIntent();
     83         if (intent != null) {
     84             // Find the activity that is in the system image
     85             PackageManager pm = context.getPackageManager();
     86             List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
     87             int listSize = list.size();
     88             for (int i = 0; i < listSize; i++) {
     89                 ResolveInfo resolveInfo = list.get(i);
     90                 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
     91                         != 0) {
     92 
     93                     // Replace the intent with this specific activity
     94                     preference.setIntent(new Intent().setClassName(
     95                             resolveInfo.activityInfo.packageName,
     96                             resolveInfo.activityInfo.name));
     97 
     98                     return true;
     99                 }
    100             }
    101         }
    102 
    103         // Did not find a matching activity, so remove the preference
    104         parentPreferenceGroup.removePreference(preference);
    105 
    106         return true;
    107     }
    108 
    109     @Override
    110     public void onCreate(Bundle icicle) {
    111         super.onCreate(icicle);
    112         addPreferencesFromResource(R.xml.spare_parts);
    113 
    114         PreferenceScreen prefSet = getPreferenceScreen();
    115 
    116         mWindowAnimationsPref = (ListPreference) prefSet.findPreference(WINDOW_ANIMATIONS_PREF);
    117         mWindowAnimationsPref.setOnPreferenceChangeListener(this);
    118         mTransitionAnimationsPref = (ListPreference) prefSet.findPreference(TRANSITION_ANIMATIONS_PREF);
    119         mTransitionAnimationsPref.setOnPreferenceChangeListener(this);
    120         mFancyImeAnimationsPref = (CheckBoxPreference) prefSet.findPreference(FANCY_IME_ANIMATIONS_PREF);
    121         mHapticFeedbackPref = (CheckBoxPreference) prefSet.findPreference(HAPTIC_FEEDBACK_PREF);
    122         mFontSizePref = (ListPreference) prefSet.findPreference(FONT_SIZE_PREF);
    123         mFontSizePref.setOnPreferenceChangeListener(this);
    124         mEndButtonPref = (ListPreference) prefSet.findPreference(END_BUTTON_PREF);
    125         mEndButtonPref.setOnPreferenceChangeListener(this);
    126         mCompatibilityMode = (CheckBoxPreference) findPreference(KEY_COMPATIBILITY_MODE);
    127         mCompatibilityMode.setPersistent(false);
    128         mCompatibilityMode.setChecked(Settings.Global.getInt(getContentResolver(),
    129                 Settings.Global.COMPATIBILITY_MODE, 1) != 0);
    130 
    131         mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
    132 
    133         final PreferenceGroup parentPreference = getPreferenceScreen();
    134         updatePreferenceToSpecificActivityOrRemove(this, parentPreference,
    135                 BATTERY_HISTORY_PREF, 0);
    136         updatePreferenceToSpecificActivityOrRemove(this, parentPreference,
    137                 BATTERY_INFORMATION_PREF, 0);
    138         updatePreferenceToSpecificActivityOrRemove(this, parentPreference,
    139                 USAGE_STATISTICS_PREF, 0);
    140 
    141         parentPreference.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    142     }
    143 
    144     private void updateToggles() {
    145         mFancyImeAnimationsPref.setChecked(Settings.Global.getInt(
    146                 getContentResolver(),
    147                 Settings.Global.FANCY_IME_ANIMATIONS, 0) != 0);
    148         mHapticFeedbackPref.setChecked(Settings.System.getInt(
    149                 getContentResolver(),
    150                 Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) != 0);
    151     }
    152 
    153     public boolean onPreferenceChange(Preference preference, Object objValue) {
    154         if (preference == mWindowAnimationsPref) {
    155             writeAnimationPreference(0, objValue);
    156         } else if (preference == mTransitionAnimationsPref) {
    157             writeAnimationPreference(1, objValue);
    158         } else if (preference == mFontSizePref) {
    159             writeFontSizePreference(objValue);
    160         } else if (preference == mEndButtonPref) {
    161             writeEndButtonPreference(objValue);
    162         }
    163         // always let the preference setting proceed.
    164         return true;
    165     }
    166 
    167     @Override
    168     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    169         if (preference == mCompatibilityMode) {
    170             Settings.Global.putInt(getContentResolver(),
    171                     Settings.Global.COMPATIBILITY_MODE,
    172                     mCompatibilityMode.isChecked() ? 1 : 0);
    173             return true;
    174         }
    175         return false;
    176     }
    177 
    178     public void writeAnimationPreference(int which, Object objValue) {
    179         try {
    180             float val = Float.parseFloat(objValue.toString());
    181             mWindowManager.setAnimationScale(which, val);
    182         } catch (NumberFormatException e) {
    183         } catch (RemoteException e) {
    184         }
    185     }
    186 
    187     public void writeFontSizePreference(Object objValue) {
    188         try {
    189             mCurConfig.fontScale = Float.parseFloat(objValue.toString());
    190             ActivityManagerNative.getDefault().updateConfiguration(mCurConfig);
    191         } catch (RemoteException e) {
    192         }
    193     }
    194 
    195     public void writeEndButtonPreference(Object objValue) {
    196         try {
    197             int val = Integer.parseInt(objValue.toString());
    198             Settings.System.putInt(getContentResolver(),
    199                     Settings.System.END_BUTTON_BEHAVIOR, val);
    200         } catch (NumberFormatException e) {
    201         }
    202     }
    203 
    204     int floatToIndex(float val, int resid) {
    205         String[] indices = getResources().getStringArray(resid);
    206         float lastVal = Float.parseFloat(indices[0]);
    207         for (int i=1; i<indices.length; i++) {
    208             float thisVal = Float.parseFloat(indices[i]);
    209             if (val < (lastVal + (thisVal-lastVal)*.5f)) {
    210                 return i-1;
    211             }
    212             lastVal = thisVal;
    213         }
    214         return indices.length-1;
    215     }
    216 
    217     public void readAnimationPreference(int which, ListPreference pref) {
    218         try {
    219             float scale = mWindowManager.getAnimationScale(which);
    220             pref.setValueIndex(floatToIndex(scale,
    221                     R.array.entryvalues_animations));
    222         } catch (RemoteException e) {
    223         }
    224     }
    225 
    226     public void readFontSizePreference(ListPreference pref) {
    227         try {
    228             mCurConfig.updateFrom(
    229                 ActivityManagerNative.getDefault().getConfiguration());
    230         } catch (RemoteException e) {
    231         }
    232         pref.setValueIndex(floatToIndex(mCurConfig.fontScale,
    233                 R.array.entryvalues_font_size));
    234     }
    235 
    236     public void readEndButtonPreference(ListPreference pref) {
    237         try {
    238             pref.setValueIndex(Settings.System.getInt(getContentResolver(),
    239                     Settings.System.END_BUTTON_BEHAVIOR));
    240         } catch (SettingNotFoundException e) {
    241         }
    242     }
    243 
    244     public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
    245         if (FANCY_IME_ANIMATIONS_PREF.equals(key)) {
    246             Settings.Global.putInt(getContentResolver(),
    247                     Settings.Global.FANCY_IME_ANIMATIONS,
    248                     mFancyImeAnimationsPref.isChecked() ? 1 : 0);
    249         } else if (HAPTIC_FEEDBACK_PREF.equals(key)) {
    250             Settings.System.putInt(getContentResolver(),
    251                     Settings.System.HAPTIC_FEEDBACK_ENABLED,
    252                     mHapticFeedbackPref.isChecked() ? 1 : 0);
    253         }
    254     }
    255 
    256     @Override
    257     public void onResume() {
    258         super.onResume();
    259         readAnimationPreference(0, mWindowAnimationsPref);
    260         readAnimationPreference(1, mTransitionAnimationsPref);
    261         readFontSizePreference(mFontSizePref);
    262         readEndButtonPreference(mEndButtonPref);
    263         updateToggles();
    264     }
    265 }
    266