Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2010 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.settings; |
| 18 | |
| 19 | import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; |
| 20 | |
| 21 | import com.android.settings.bluetooth.DockEventReceiver; |
| 22 | |
| 23 | import android.content.BroadcastReceiver; |
| 24 | import android.content.ContentResolver; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
| 28 | import android.media.AudioManager; |
| 29 | import android.os.Bundle; |
| 30 | import android.os.IMountService; |
| 31 | import android.os.RemoteException; |
| 32 | import android.os.ServiceManager; |
| 33 | import android.preference.CheckBoxPreference; |
| 34 | import android.preference.ListPreference; |
| 35 | import android.preference.Preference; |
| 36 | import android.preference.PreferenceActivity; |
| 37 | import android.preference.PreferenceGroup; |
| 38 | import android.preference.PreferenceScreen; |
| 39 | import android.provider.Settings; |
| 40 | import android.provider.Settings.SettingNotFoundException; |
| 41 | import android.telephony.TelephonyManager; |
| 42 | import android.util.Log; |
| 43 | import android.view.IWindowManager; |
| 44 | |
| 45 | public class DisplaySettings extends PreferenceActivity implements |
| 46 | Preference.OnPreferenceChangeListener { |
| 47 | private static final String TAG = "DisplaySettings"; |
| 48 | |
| 49 | /** If there is no setting in the provider, use this. */ |
| 50 | private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000; |
| 51 | |
| 52 | private static final String KEY_SCREEN_TIMEOUT = "screen_timeout"; |
| 53 | private static final String KEY_ANIMATIONS = "animations"; |
| 54 | private static final String KEY_ACCELEROMETER = "accelerometer"; |
| 55 | |
| 56 | private ListPreference mAnimations; |
| 57 | private CheckBoxPreference mAccelerometer; |
| 58 | private float[] mAnimationScales; |
| 59 | |
| 60 | private IWindowManager mWindowManager; |
| 61 | |
| 62 | @Override |
| 63 | protected void onCreate(Bundle savedInstanceState) { |
| 64 | super.onCreate(savedInstanceState); |
| 65 | ContentResolver resolver = getContentResolver(); |
| 66 | mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window")); |
| 67 | |
| 68 | addPreferencesFromResource(R.xml.display_settings); |
| 69 | |
| 70 | mAnimations = (ListPreference) findPreference(KEY_ANIMATIONS); |
| 71 | mAnimations.setOnPreferenceChangeListener(this); |
| 72 | mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER); |
| 73 | mAccelerometer.setPersistent(false); |
| 74 | |
| 75 | ListPreference screenTimeoutPreference = |
| 76 | (ListPreference) findPreference(KEY_SCREEN_TIMEOUT); |
| 77 | screenTimeoutPreference.setValue(String.valueOf(Settings.System.getInt( |
| 78 | resolver, SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE))); |
| 79 | screenTimeoutPreference.setOnPreferenceChangeListener(this); |
| 80 | |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | protected void onResume() { |
| 85 | super.onResume(); |
| 86 | |
| 87 | updateState(true); |
| 88 | } |
| 89 | |
| 90 | private void updateState(boolean force) { |
| 91 | int animations = 0; |
| 92 | try { |
| 93 | mAnimationScales = mWindowManager.getAnimationScales(); |
| 94 | } catch (RemoteException e) { |
| 95 | } |
| 96 | if (mAnimationScales != null) { |
| 97 | if (mAnimationScales.length >= 1) { |
| 98 | animations = ((int)(mAnimationScales[0]+.5f)) % 10; |
| 99 | } |
| 100 | if (mAnimationScales.length >= 2) { |
| 101 | animations += (((int)(mAnimationScales[1]+.5f)) & 0x7) * 10; |
| 102 | } |
| 103 | } |
| 104 | int idx = 0; |
| 105 | int best = 0; |
| 106 | CharSequence[] aents = mAnimations.getEntryValues(); |
| 107 | for (int i=0; i<aents.length; i++) { |
| 108 | int val = Integer.parseInt(aents[i].toString()); |
| 109 | if (val <= animations && val > best) { |
| 110 | best = val; |
| 111 | idx = i; |
| 112 | } |
| 113 | } |
| 114 | mAnimations.setValueIndex(idx); |
| 115 | updateAnimationsSummary(mAnimations.getValue()); |
| 116 | mAccelerometer.setChecked(Settings.System.getInt( |
| 117 | getContentResolver(), |
| 118 | Settings.System.ACCELEROMETER_ROTATION, 0) != 0); |
| 119 | } |
| 120 | |
| 121 | private void updateAnimationsSummary(Object value) { |
| 122 | CharSequence[] summaries = getResources().getTextArray(R.array.animations_summaries); |
| 123 | CharSequence[] values = mAnimations.getEntryValues(); |
| 124 | for (int i=0; i<values.length; i++) { |
| 125 | //Log.i("foo", "Comparing entry "+ values[i] + " to current " |
| 126 | // + mAnimations.getValue()); |
| 127 | if (values[i].equals(value)) { |
| 128 | mAnimations.setSummary(summaries[i]); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
| 136 | if (preference == mAccelerometer) { |
| 137 | Settings.System.putInt(getContentResolver(), |
| 138 | Settings.System.ACCELEROMETER_ROTATION, |
| 139 | mAccelerometer.isChecked() ? 1 : 0); |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | public boolean onPreferenceChange(Preference preference, Object objValue) { |
| 145 | final String key = preference.getKey(); |
| 146 | if (KEY_ANIMATIONS.equals(key)) { |
| 147 | try { |
| 148 | int value = Integer.parseInt((String) objValue); |
| 149 | if (mAnimationScales.length >= 1) { |
| 150 | mAnimationScales[0] = value%10; |
| 151 | } |
| 152 | if (mAnimationScales.length >= 2) { |
| 153 | mAnimationScales[1] = (value/10)%10; |
| 154 | } |
| 155 | try { |
| 156 | mWindowManager.setAnimationScales(mAnimationScales); |
| 157 | } catch (RemoteException e) { |
| 158 | } |
| 159 | updateAnimationsSummary(objValue); |
| 160 | } catch (NumberFormatException e) { |
| 161 | Log.e(TAG, "could not persist animation setting", e); |
| 162 | } |
| 163 | |
| 164 | } |
| 165 | if (KEY_SCREEN_TIMEOUT.equals(key)) { |
| 166 | int value = Integer.parseInt((String) objValue); |
| 167 | try { |
| 168 | Settings.System.putInt(getContentResolver(), |
| 169 | SCREEN_OFF_TIMEOUT, value); |
| 170 | } catch (NumberFormatException e) { |
| 171 | Log.e(TAG, "could not persist screen timeout setting", e); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return true; |
| 176 | } |
| 177 | } |