Advertisement
Guest User

DisplaySettings.java

a guest
Mar 2nd, 2014
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.02 KB | None | 0 0
  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 android.app.ActivityManagerNative;
  22. import android.app.Dialog;
  23. import android.app.admin.DevicePolicyManager;
  24. import android.content.ContentResolver;
  25. import android.content.Context;
  26. import android.content.res.Configuration;
  27. import android.content.res.Resources;
  28. import android.os.Bundle;
  29. import android.os.RemoteException;
  30. import android.preference.CheckBoxPreference;
  31. import android.preference.ListPreference;
  32. import android.preference.Preference;
  33. import android.preference.Preference.OnPreferenceClickListener;
  34. import android.preference.PreferenceCategory;
  35. import android.preference.PreferenceScreen;
  36. import android.provider.Settings;
  37. import android.provider.Settings.SettingNotFoundException;
  38. import android.util.Log;
  39.  
  40. import com.android.internal.view.RotationPolicy;
  41. import com.android.settings.DreamSettings;
  42.  
  43. import java.util.ArrayList;
  44.  
  45. public class DisplaySettings extends SettingsPreferenceFragment implements
  46. Preference.OnPreferenceChangeListener, OnPreferenceClickListener {
  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_ACCELEROMETER = "accelerometer";
  54. private static final String KEY_FONT_SIZE = "font_size";
  55. private static final String KEY_LIGHT_OPTIONS = "category_light_options";
  56. private static final String KEY_NOTIFICATION_PULSE = "notification_pulse";
  57. private static final String KEY_BATTERY_LIGHT = "battery_light";
  58. private static final String KEY_SCREEN_SAVER = "screensaver";
  59.  
  60. private static final int DLG_GLOBAL_CHANGE_WARNING = 1;
  61.  
  62. private CheckBoxPreference mAccelerometer;
  63. private WarnedListPreference mFontSizePref;
  64. private PreferenceCategory mLightOptions;
  65. private PreferenceScreen mNotificationPulse;
  66. private PreferenceScreen mBatteryPulse;
  67.  
  68. private final Configuration mCurConfig = new Configuration();
  69.  
  70. private ListPreference mScreenTimeoutPreference;
  71. private Preference mScreenSaverPreference;
  72.  
  73. private final RotationPolicy.RotationPolicyListener mRotationPolicyListener =
  74. new RotationPolicy.RotationPolicyListener() {
  75. @Override
  76. public void onChange() {
  77. updateAccelerometerRotationCheckbox();
  78. }
  79. };
  80.  
  81. @Override
  82. public void onCreate(Bundle savedInstanceState) {
  83. super.onCreate(savedInstanceState);
  84. ContentResolver resolver = getActivity().getContentResolver();
  85.  
  86. addPreferencesFromResource(R.xml.display_settings);
  87.  
  88. PreferenceScreen prefSet = getPreferenceScreen();
  89.  
  90. mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER);
  91. mAccelerometer.setPersistent(false);
  92. if (!RotationPolicy.isRotationSupported(getActivity())
  93. || RotationPolicy.isRotationLockToggleSupported(getActivity())) {
  94. // If rotation lock is supported, then we do not provide this option in
  95. // Display settings. However, is still available in Accessibility settings,
  96. // if the device supports rotation.
  97. getPreferenceScreen().removePreference(mAccelerometer);
  98. }
  99.  
  100. mScreenSaverPreference = findPreference(KEY_SCREEN_SAVER);
  101. if (mScreenSaverPreference != null
  102. && getResources().getBoolean(
  103. com.android.internal.R.bool.config_dreamsSupported) == false) {
  104. getPreferenceScreen().removePreference(mScreenSaverPreference);
  105. }
  106.  
  107. mScreenTimeoutPreference = (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
  108. final long currentTimeout = Settings.System.getLong(resolver, SCREEN_OFF_TIMEOUT,
  109. FALLBACK_SCREEN_TIMEOUT_VALUE);
  110. mScreenTimeoutPreference.setValue(String.valueOf(currentTimeout));
  111. mScreenTimeoutPreference.setOnPreferenceChangeListener(this);
  112. disableUnusableTimeouts(mScreenTimeoutPreference);
  113. updateTimeoutPreferenceDescription(currentTimeout);
  114.  
  115. mFontSizePref = (WarnedListPreference) findPreference(KEY_FONT_SIZE);
  116. mFontSizePref.setOnPreferenceChangeListener(this);
  117. mFontSizePref.setOnPreferenceClickListener(this);
  118.  
  119. mDisplayManager = (DisplayManager)getActivity().getSystemService(
  120. Context.DISPLAY_SERVICE);
  121. mWifiDisplayStatus = mDisplayManager.getWifiDisplayStatus();
  122. mWifiDisplayPreference = (Preference)findPreference(KEY_WIFI_DISPLAY);
  123. if (mWifiDisplayStatus.getFeatureState()
  124. == WifiDisplayStatus.FEATURE_STATE_UNAVAILABLE) {
  125. getPreferenceScreen().removePreference(mWifiDisplayPreference);
  126. mWifiDisplayPreference = null;
  127. }
  128.  
  129. mLightOptions = (PreferenceCategory) prefSet.findPreference(KEY_LIGHT_OPTIONS);
  130. mNotificationPulse = (PreferenceScreen) findPreference(KEY_NOTIFICATION_PULSE);
  131. if (mNotificationPulse != null) {
  132. if (!getResources().getBoolean(
  133. com.android.internal.R.bool.config_intrusiveNotificationLed)) {
  134. mLightOptions.removePreference(mNotificationPulse);
  135. } else {
  136. updateLightPulseDescription();
  137. }
  138. }
  139.  
  140. mBatteryPulse = (PreferenceScreen) findPreference(KEY_BATTERY_LIGHT);
  141. if (mBatteryPulse != null) {
  142. if (getResources().getBoolean(
  143. com.android.internal.R.bool.config_intrusiveBatteryLed) == false) {
  144. mLightOptions.removePreference(mBatteryPulse);
  145. } else {
  146. updateBatteryPulseDescription();
  147. }
  148. }
  149. }
  150.  
  151. private void updateTimeoutPreferenceDescription(long currentTimeout) {
  152. ListPreference preference = mScreenTimeoutPreference;
  153. String summary;
  154. if (currentTimeout < 0) {
  155. // Unsupported value
  156. summary = "";
  157. } else {
  158. final CharSequence[] entries = preference.getEntries();
  159. final CharSequence[] values = preference.getEntryValues();
  160. if (entries == null || entries.length == 0) {
  161. summary = "";
  162. } else {
  163. int best = 0;
  164. for (int i = 0; i < values.length; i++) {
  165. long timeout = Long.parseLong(values[i].toString());
  166. if (currentTimeout >= timeout) {
  167. best = i;
  168. }
  169. }
  170. summary = preference.getContext().getString(R.string.screen_timeout_summary,
  171. entries[best]);
  172. }
  173. }
  174. preference.setSummary(summary);
  175. }
  176.  
  177. private void disableUnusableTimeouts(ListPreference screenTimeoutPreference) {
  178. final DevicePolicyManager dpm =
  179. (DevicePolicyManager) getActivity().getSystemService(
  180. Context.DEVICE_POLICY_SERVICE);
  181. final long maxTimeout = dpm != null ? dpm.getMaximumTimeToLock(null) : 0;
  182. if (maxTimeout == 0) {
  183. return; // policy not enforced
  184. }
  185. final CharSequence[] entries = screenTimeoutPreference.getEntries();
  186. final CharSequence[] values = screenTimeoutPreference.getEntryValues();
  187. ArrayList<CharSequence> revisedEntries = new ArrayList<CharSequence>();
  188. ArrayList<CharSequence> revisedValues = new ArrayList<CharSequence>();
  189. for (int i = 0; i < values.length; i++) {
  190. long timeout = Long.parseLong(values[i].toString());
  191. if (timeout <= maxTimeout) {
  192. revisedEntries.add(entries[i]);
  193. revisedValues.add(values[i]);
  194. }
  195. }
  196. if (revisedEntries.size() != entries.length || revisedValues.size() != values.length) {
  197. final int userPreference = Integer.parseInt(screenTimeoutPreference.getValue());
  198. screenTimeoutPreference.setEntries(
  199. revisedEntries.toArray(new CharSequence[revisedEntries.size()]));
  200. screenTimeoutPreference.setEntryValues(
  201. revisedValues.toArray(new CharSequence[revisedValues.size()]));
  202. if (userPreference <= maxTimeout) {
  203. screenTimeoutPreference.setValue(String.valueOf(userPreference));
  204. } else if (revisedValues.size() > 0
  205. && Long.parseLong(revisedValues.get(revisedValues.size() - 1).toString())
  206. == maxTimeout) {
  207. // If the last one happens to be the same as the max timeout, select that
  208. screenTimeoutPreference.setValue(String.valueOf(maxTimeout));
  209. } else {
  210. // There will be no highlighted selection since nothing in the list matches
  211. // maxTimeout. The user can still select anything less than maxTimeout.
  212. // TODO: maybe append maxTimeout to the list and mark selected.
  213. }
  214. }
  215. screenTimeoutPreference.setEnabled(revisedEntries.size() > 0);
  216. }
  217.  
  218. int floatToIndex(float val) {
  219. String[] indices = getResources().getStringArray(R.array.entryvalues_font_size);
  220. float lastVal = Float.parseFloat(indices[0]);
  221. for (int i=1; i<indices.length; i++) {
  222. float thisVal = Float.parseFloat(indices[i]);
  223. if (val < (lastVal + (thisVal-lastVal)*.5f)) {
  224. return i-1;
  225. }
  226. lastVal = thisVal;
  227. }
  228. return indices.length-1;
  229. }
  230.  
  231. public void readFontSizePreference(ListPreference pref) {
  232. try {
  233. mCurConfig.updateFrom(ActivityManagerNative.getDefault().getConfiguration());
  234. } catch (RemoteException e) {
  235. Log.w(TAG, "Unable to retrieve font size");
  236. }
  237.  
  238. // mark the appropriate item in the preferences list
  239. int index = floatToIndex(mCurConfig.fontScale);
  240. pref.setValueIndex(index);
  241.  
  242. // report the current size in the summary text
  243. final Resources res = getResources();
  244. String[] fontSizeNames = res.getStringArray(R.array.entries_font_size);
  245. pref.setSummary(String.format(res.getString(R.string.summary_font_size),
  246. fontSizeNames[index]));
  247. }
  248.  
  249. @Override
  250. public void onResume() {
  251. super.onResume();
  252.  
  253. RotationPolicy.registerRotationPolicyListener(getActivity(),
  254. mRotationPolicyListener);
  255.  
  256. updateState();
  257. }
  258.  
  259. @Override
  260. public void onPause() {
  261. super.onPause();
  262.  
  263. RotationPolicy.unregisterRotationPolicyListener(getActivity(),
  264. mRotationPolicyListener);
  265. }
  266.  
  267. @Override
  268. public Dialog onCreateDialog(int dialogId) {
  269. if (dialogId == DLG_GLOBAL_CHANGE_WARNING) {
  270. return Utils.buildGlobalChangeWarningDialog(getActivity(),
  271. R.string.global_font_change_title,
  272. new Runnable() {
  273. public void run() {
  274. mFontSizePref.click();
  275. }
  276. });
  277. }
  278. return null;
  279. }
  280.  
  281. private void updateState() {
  282. updateAccelerometerRotationCheckbox();
  283. readFontSizePreference(mFontSizePref);
  284. updateScreenSaverSummary();
  285. }
  286.  
  287. private void updateScreenSaverSummary() {
  288. if (mScreenSaverPreference != null) {
  289. mScreenSaverPreference.setSummary(
  290. DreamSettings.getSummaryTextWithDreamName(getActivity()));
  291. }
  292. }
  293.  
  294. private void updateAccelerometerRotationCheckbox() {
  295. if (getActivity() == null) return;
  296.  
  297. mAccelerometer.setChecked(!RotationPolicy.isRotationLocked(getActivity()));
  298. }
  299.  
  300. public void writeFontSizePreference(Object objValue) {
  301. try {
  302. mCurConfig.fontScale = Float.parseFloat(objValue.toString());
  303. ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
  304. } catch (RemoteException e) {
  305. Log.w(TAG, "Unable to save font size");
  306. }
  307. }
  308.  
  309. private void updateLightPulseDescription() {
  310. if (Settings.System.getInt(getActivity().getContentResolver(),
  311. Settings.System.NOTIFICATION_LIGHT_PULSE, 0) == 1) {
  312. mNotificationPulse.setSummary(getString(R.string.notification_light_enabled));
  313. } else {
  314. mNotificationPulse.setSummary(getString(R.string.notification_light_disabled));
  315. }
  316. }
  317.  
  318. private void updateBatteryPulseDescription() {
  319. if (Settings.System.getInt(getActivity().getContentResolver(),
  320. Settings.System.BATTERY_LIGHT_ENABLED, 1) == 1) {
  321. mBatteryPulse.setSummary(getString(R.string.notification_light_enabled));
  322. } else {
  323. mBatteryPulse.setSummary(getString(R.string.notification_light_disabled));
  324. }
  325. }
  326.  
  327. @Override
  328. public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
  329. if (preference == mAccelerometer) {
  330. RotationPolicy.setRotationLockForAccessibility(
  331. getActivity(), !mAccelerometer.isChecked());
  332. }
  333. return super.onPreferenceTreeClick(preferenceScreen, preference);
  334. }
  335.  
  336. @Override
  337. public boolean onPreferenceChange(Preference preference, Object objValue) {
  338. final String key = preference.getKey();
  339. if (KEY_SCREEN_TIMEOUT.equals(key)) {
  340. int value = Integer.parseInt((String) objValue);
  341. try {
  342. Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, value);
  343. updateTimeoutPreferenceDescription(value);
  344. } catch (NumberFormatException e) {
  345. Log.e(TAG, "could not persist screen timeout setting", e);
  346. }
  347. }
  348. if (KEY_FONT_SIZE.equals(key)) {
  349. writeFontSizePreference(objValue);
  350. }
  351.  
  352. return true;
  353. }
  354.  
  355. @Override
  356. public boolean onPreferenceClick(Preference preference) {
  357. if (preference == mFontSizePref) {
  358. if (Utils.hasMultipleUsers(getActivity())) {
  359. showDialog(DLG_GLOBAL_CHANGE_WARNING);
  360. return true;
  361. } else {
  362. mFontSizePref.click();
  363. }
  364. }
  365. return false;
  366. }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement