/* * 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 static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; import android.app.ActivityManagerNative; import android.app.Dialog; import android.app.admin.DevicePolicyManager; import android.content.BroadcastReceiver; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.res.Configuration; import android.content.res.Resources; import android.database.ContentObserver; import android.hardware.display.DisplayManager; import android.hardware.display.WifiDisplayStatus; import android.os.Bundle; import android.os.Handler; import android.os.RemoteException; import android.preference.CheckBoxPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceCategory; import android.preference.PreferenceScreen; import android.provider.Settings; import android.util.Log; import com.android.internal.view.RotationPolicy; import com.android.settings.cyanogenmod.DisplayRotation; import java.util.ArrayList; public class DisplaySettings extends SettingsPreferenceFragment implements Preference.OnPreferenceChangeListener, OnPreferenceClickListener { 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_WIFI_DISPLAY = "wifi_display"; private static final String KEY_DISPLAY_ROTATION = "display_rotation"; private static final String KEY_WAKEUP_CATEGORY = "category_wakeup_options"; private static final String KEY_VOLUME_WAKE = "pref_volume_wake"; // Strings used for building the summary private static final String ROTATION_ANGLE_0 = "0"; private static final String ROTATION_ANGLE_90 = "90"; private static final String ROTATION_ANGLE_180 = "180"; private static final String ROTATION_ANGLE_270 = "270"; private static final String ROTATION_ANGLE_DELIM = ", "; private static final String ROTATION_ANGLE_DELIM_FINAL = " & "; private static final int DLG_GLOBAL_CHANGE_WARNING = 1; private DisplayManager mDisplayManager; private CheckBoxPreference mVolumeWake; private PreferenceScreen mDisplayRotationPreference; private WarnedListPreference mFontSizePref; private final Configuration mCurConfig = new Configuration(); private ListPreference mScreenTimeoutPreference; private Preference mScreenSaverPreference; private WifiDisplayStatus mWifiDisplayStatus; private Preference mWifiDisplayPreference; private ContentObserver mAccelerometerRotationObserver = new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { updateDisplayRotationPreferenceDescription(); } }; private final RotationPolicy.RotationPolicyListener mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() { @Override public void onChange() { updateDisplayRotationPreferenceDescription(); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ContentResolver resolver = getActivity().getContentResolver(); addPreferencesFromResource(R.xml.display_settings); mDisplayRotationPreference = (PreferenceScreen) findPreference(KEY_DISPLAY_ROTATION); 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); updateDisplayRotationPreferenceDescription(); mFontSizePref = (WarnedListPreference) findPreference(KEY_FONT_SIZE); mFontSizePref.setOnPreferenceChangeListener(this); mFontSizePref.setOnPreferenceClickListener(this); mDisplayManager = (DisplayManager)getActivity().getSystemService( Context.DISPLAY_SERVICE); mWifiDisplayStatus = mDisplayManager.getWifiDisplayStatus(); mWifiDisplayPreference = (Preference)findPreference(KEY_WIFI_DISPLAY); if (mWifiDisplayStatus.getFeatureState() == WifiDisplayStatus.FEATURE_STATE_UNAVAILABLE) { getPreferenceScreen().removePreference(mWifiDisplayPreference); mWifiDisplayPreference = null; } mVolumeWake = (CheckBoxPreference) findPreference(KEY_VOLUME_WAKE); if (mVolumeWake != null) { if (!getResources().getBoolean(R.bool.config_show_volumeRockerWake)) { getPreferenceScreen().removePreference(mVolumeWake); getPreferenceScreen().removePreference((PreferenceCategory) findPreference(KEY_WAKEUP_CATEGORY)); } else { mVolumeWake.setChecked(Settings.System.getInt(resolver, Settings.System.VOLUME_WAKE_SCREEN, 0) == 1); } } } private void updateDisplayRotationPreferenceDescription() { if (mDisplayRotationPreference == null) { // The preference was removed, do nothing return; } // We have a preference, lets update the summary StringBuilder summary = new StringBuilder(); Boolean rotationEnabled = Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) != 0; int mode = Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION_ANGLES, DisplayRotation.ROTATION_0_MODE|DisplayRotation.ROTATION_90_MODE|DisplayRotation.ROTATION_270_MODE); if (!rotationEnabled) { summary.append(getString(R.string.display_rotation_disabled)); } else { ArrayList rotationList = new ArrayList(); String delim = ""; summary.append(getString(R.string.display_rotation_enabled) + " "); if ((mode & DisplayRotation.ROTATION_0_MODE) != 0) { rotationList.add(ROTATION_ANGLE_0); } if ((mode & DisplayRotation.ROTATION_90_MODE) != 0) { rotationList.add(ROTATION_ANGLE_90); } if ((mode & DisplayRotation.ROTATION_180_MODE) != 0) { rotationList.add(ROTATION_ANGLE_180); } if ((mode & DisplayRotation.ROTATION_270_MODE) != 0) { rotationList.add(ROTATION_ANGLE_270); } for(int i=0;i= 2 && (rotationList.size() - 2) == i) { delim = " " + ROTATION_ANGLE_DELIM_FINAL + " "; } else { delim = ROTATION_ANGLE_DELIM + " "; } } summary.append(" " + getString(R.string.display_rotation_unit)); } mDisplayRotationPreference.setSummary(summary); } 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) { screenTimeoutPreference.setEntries( revisedEntries.toArray(new CharSequence[revisedEntries.size()])); screenTimeoutPreference.setEntryValues( revisedValues.toArray(new CharSequence[revisedValues.size()])); final int userPreference = Integer.parseInt(screenTimeoutPreference.getValue()); if (userPreference <= maxTimeout) { screenTimeoutPreference.setValue(String.valueOf(userPreference)); } 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