/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings; import com.android.internal.view.RotationPolicy; import com.android.settings.notification.DropDownPreference; import com.android.settings.notification.DropDownPreference.Callback; import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.Indexable; import static android.provider.Settings.Secure.DOZE_ENABLED; import static android.provider.Settings.Secure.WAKE_GESTURE_ENABLED; import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE; import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL; import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; import android.app.Activity; import android.app.ActivityManagerNative; import android.app.Dialog; import android.app.admin.DevicePolicyManager; import android.content.ContentResolver; import android.content.Context; import android.content.res.Configuration; import android.content.res.Resources; import android.hardware.Sensor; import android.hardware.SensorManager; import android.os.Build; import android.os.Bundle; import android.os.RemoteException; import android.os.SystemProperties; import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceScreen; import android.preference.SwitchPreference; import android.provider.SearchIndexableResource; import android.provider.Settings; import android.text.TextUtils; import android.util.Log; import java.util.ArrayList; import java.util.List; public class DisplaySettings extends SettingsPreferenceFragment implements Preference.OnPreferenceChangeListener, OnPreferenceClickListener, Indexable { private static final String TAG = "DisplaySettings"; /** If there is no setting in the provider, use this. */ private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000; private static final String KEY_SCREEN_TIMEOUT = "screen_timeout"; private static final String KEY_FONT_SIZE = "font_size"; private static final String KEY_SCREEN_SAVER = "screensaver"; private static final String KEY_LIFT_TO_WAKE = "lift_to_wake"; private static final String KEY_DOZE = "doze"; private static final String KEY_AUTO_BRIGHTNESS = "auto_brightness"; private static final String KEY_AUTO_ROTATE = "auto_rotate"; private static final int DLG_GLOBAL_CHANGE_WARNING = 1; private WarnedListPreference mFontSizePref; private final Configuration mCurConfig = new Configuration(); private ListPreference mScreenTimeoutPreference; private Preference mScreenSaverPreference; private SwitchPreference mLiftToWakePreference; private SwitchPreference mDozePreference; private SwitchPreference mAutoBrightnessPreference; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Activity activity = getActivity(); final ContentResolver resolver = activity.getContentResolver(); addPreferencesFromResource(R.xml.display_settings); mScreenSaverPreference = findPreference(KEY_SCREEN_SAVER); if (mScreenSaverPreference != null && getResources().getBoolean( com.android.internal.R.bool.config_dreamsSupported) == false) { getPreferenceScreen().removePreference(mScreenSaverPreference); } mScreenTimeoutPreference = (ListPreference) findPreference(KEY_SCREEN_TIMEOUT); final long currentTimeout = Settings.System.getLong(resolver, SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE); mScreenTimeoutPreference.setValue(String.valueOf(currentTimeout)); mScreenTimeoutPreference.setOnPreferenceChangeListener(this); disableUnusableTimeouts(mScreenTimeoutPreference); updateTimeoutPreferenceDescription(currentTimeout); mFontSizePref = (WarnedListPreference) findPreference(KEY_FONT_SIZE); mFontSizePref.setOnPreferenceChangeListener(this); mFontSizePref.setOnPreferenceClickListener(this); if (isAutomaticBrightnessAvailable(getResources())) { mAutoBrightnessPreference = (SwitchPreference) findPreference(KEY_AUTO_BRIGHTNESS); mAutoBrightnessPreference.setOnPreferenceChangeListener(this); } else { removePreference(KEY_AUTO_BRIGHTNESS); } if (isLiftToWakeAvailable(activity)) { mLiftToWakePreference = (SwitchPreference) findPreference(KEY_LIFT_TO_WAKE); mLiftToWakePreference.setOnPreferenceChangeListener(this); } else { removePreference(KEY_LIFT_TO_WAKE); } if (isDozeAvailable(activity)) { mDozePreference = (SwitchPreference) findPreference(KEY_DOZE); mDozePreference.setOnPreferenceChangeListener(this); } else { removePreference(KEY_DOZE); } if (RotationPolicy.isRotationLockToggleVisible(activity)) { DropDownPreference rotatePreference = (DropDownPreference) findPreference(KEY_AUTO_ROTATE); rotatePreference.addItem(activity.getString(R.string.display_auto_rotate_rotate), false); int rotateLockedResourceId; // The following block sets the string used when rotation is locked. // If the device locks specifically to portrait or landscape (rather than current // rotation), then we use a different string to include this information. if (allowAllRotations(activity)) { rotateLockedResourceId = R.string.display_auto_rotate_stay_in_current; } else { if (RotationPolicy.getRotationLockOrientation(activity) == Configuration.ORIENTATION_PORTRAIT) { rotateLockedResourceId = R.string.display_auto_rotate_stay_in_portrait; } else { rotateLockedResourceId = R.string.display_auto_rotate_stay_in_landscape; } } rotatePreference.addItem(activity.getString(rotateLockedResourceId), true); rotatePreference.setSelectedItem(RotationPolicy.isRotationLocked(activity) ? 1 : 0); rotatePreference.setCallback(new Callback() { @Override public boolean onItemSelected(int pos, Object value) { RotationPolicy.setRotationLock(activity, (Boolean) value); return true; } }); } else { removePreference(KEY_AUTO_ROTATE); } } private static boolean allowAllRotations(Context context) { return Resources.getSystem().getBoolean( com.android.internal.R.bool.config_allowAllRotations); } private static boolean isLiftToWakeAvailable(Context context) { SensorManager sensors = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); return sensors != null && sensors.getDefaultSensor(Sensor.TYPE_WAKE_GESTURE) != null; } private static boolean isDozeAvailable(Context context) { String name = Build.IS_DEBUGGABLE ? SystemProperties.get("debug.doze.component") : null; if (TextUtils.isEmpty(name)) { name = context.getResources().getString( com.android.internal.R.string.config_dozeComponent); } return !TextUtils.isEmpty(name); } private static boolean isAutomaticBrightnessAvailable(Resources res) { return res.getBoolean(com.android.internal.R.bool.config_automatic_brightness_available); } private void updateTimeoutPreferenceDescription(long currentTimeout) { ListPreference preference = mScreenTimeoutPreference; String summary; if (currentTimeout < 0) { // Unsupported value summary = ""; } else { final CharSequence[] entries = preference.getEntries(); final CharSequence[] values = preference.getEntryValues(); if (entries == null || entries.length == 0) { summary = ""; } else { int best = 0; for (int i = 0; i < values.length; i++) { long timeout = Long.parseLong(values[i].toString()); if (currentTimeout >= timeout) { best = i; } } summary = preference.getContext().getString(R.string.screen_timeout_summary, entries[best]); } } preference.setSummary(summary); } private void disableUnusableTimeouts(ListPreference screenTimeoutPreference) { final DevicePolicyManager dpm = (DevicePolicyManager) getActivity().getSystemService( Context.DEVICE_POLICY_SERVICE); final long maxTimeout = dpm != null ? dpm.getMaximumTimeToLock(null) : 0; if (maxTimeout == 0) { return; // policy not enforced } final CharSequence[] entries = screenTimeoutPreference.getEntries(); final CharSequence[] values = screenTimeoutPreference.getEntryValues(); ArrayList revisedEntries = new ArrayList(); ArrayList revisedValues = new ArrayList(); for (int i = 0; i < values.length; i++) { long timeout = Long.parseLong(values[i].toString()); if (timeout <= maxTimeout) { revisedEntries.add(entries[i]); revisedValues.add(values[i]); } } if (revisedEntries.size() != entries.length || revisedValues.size() != values.length) { final int userPreference = Integer.parseInt(screenTimeoutPreference.getValue()); screenTimeoutPreference.setEntries( revisedEntries.toArray(new CharSequence[revisedEntries.size()])); screenTimeoutPreference.setEntryValues( revisedValues.toArray(new CharSequence[revisedValues.size()])); if (userPreference <= maxTimeout) { screenTimeoutPreference.setValue(String.valueOf(userPreference)); } else if (revisedValues.size() > 0 && Long.parseLong(revisedValues.get(revisedValues.size() - 1).toString()) == maxTimeout) { // If the last one happens to be the same as the max timeout, select that screenTimeoutPreference.setValue(String.valueOf(maxTimeout)); } else { // There will be no highlighted selection since nothing in the list matches // maxTimeout. The user can still select anything less than maxTimeout. // TODO: maybe append maxTimeout to the list and mark selected. } } screenTimeoutPreference.setEnabled(revisedEntries.size() > 0); } int floatToIndex(float val) { String[] indices = getResources().getStringArray(R.array.entryvalues_font_size); float lastVal = Float.parseFloat(indices[0]); for (int i=1; i getXmlResourcesToIndex(Context context, boolean enabled) { ArrayList result = new ArrayList(); SearchIndexableResource sir = new SearchIndexableResource(context); sir.xmlResId = R.xml.display_settings; result.add(sir); return result; } @Override public List getNonIndexableKeys(Context context) { ArrayList result = new ArrayList(); if (!context.getResources().getBoolean( com.android.internal.R.bool.config_dreamsSupported)) { result.add(KEY_SCREEN_SAVER); } if (!isAutomaticBrightnessAvailable(context.getResources())) { result.add(KEY_AUTO_BRIGHTNESS); } if (!isLiftToWakeAvailable(context)) { result.add(KEY_LIFT_TO_WAKE); } if (!isDozeAvailable(context)) { result.add(KEY_DOZE); } if (!RotationPolicy.isRotationLockToggleVisible(context)) { result.add(KEY_AUTO_ROTATE); } return result; } }; }