2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import static android.provider.Settings.Secure.SCREENSAVER_ENABLED;
20 import static android.provider.Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK;
22 import android.app.ActionBar;
23 import android.app.Activity;
24 import android.app.ActivityManagerNative;
25 import android.app.admin.DevicePolicyManager;
26 import android.content.ContentResolver;
27 import android.content.Context;
28 import android.content.res.Configuration;
29 import android.database.ContentObserver;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.os.RemoteException;
33 import android.os.ServiceManager;
34 import android.preference.CheckBoxPreference;
35 import android.preference.ListPreference;
36 import android.preference.Preference;
37 import android.preference.PreferenceActivity;
38 import android.preference.PreferenceScreen;
39 import android.provider.Settings;
40 import android.util.Log;
41 import android.view.Gravity;
42 import android.view.IWindowManager;
43 import android.widget.CompoundButton;
44 import android.widget.Switch;
46 import java.util.ArrayList;
48 public class DreamSettings extends SettingsPreferenceFragment {
49 private static final String TAG = "DreamSettings";
51 private static final String KEY_ACTIVATE_ON_DOCK = "activate_on_dock";
53 private CheckBoxPreference mActivateOnDockPreference;
55 private Switch mEnableSwitch;
56 private Enabler mEnabler;
59 public void onActivityCreated(Bundle savedInstanceState) {
60 super.onActivityCreated(savedInstanceState);
62 addPreferencesFromResource(R.xml.dream_settings);
64 mActivateOnDockPreference = (CheckBoxPreference) findPreference(KEY_ACTIVATE_ON_DOCK);
66 final Activity activity = getActivity();
68 mEnableSwitch = new Switch(activity);
70 if (activity instanceof PreferenceActivity) {
71 PreferenceActivity preferenceActivity = (PreferenceActivity) activity;
72 // note: we do not check onIsHidingHeaders() or onIsMultiPane() because there's no
73 // switch in the left-hand pane to control this; we need to show the ON/OFF in our
74 // fragment every time
75 final int padding = activity.getResources().getDimensionPixelSize(
76 R.dimen.action_bar_switch_padding);
77 mEnableSwitch.setPadding(0, 0, padding, 0);
78 activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
79 ActionBar.DISPLAY_SHOW_CUSTOM);
80 activity.getActionBar().setCustomView(mEnableSwitch, new ActionBar.LayoutParams(
81 ActionBar.LayoutParams.WRAP_CONTENT,
82 ActionBar.LayoutParams.WRAP_CONTENT,
83 Gravity.CENTER_VERTICAL | Gravity.RIGHT));
84 activity.getActionBar().setTitle(R.string.screensaver_settings_title);
87 mEnabler = new Enabler(activity, mEnableSwitch);
90 public static boolean isScreenSaverEnabled(Context context) {
91 return 0 != Settings.Secure.getInt(
92 context.getContentResolver(), SCREENSAVER_ENABLED, 1);
95 public static void setScreenSaverEnabled(Context context, boolean enabled) {
96 Settings.Secure.putInt(
97 context.getContentResolver(), SCREENSAVER_ENABLED, enabled ? 1 : 0);
100 public static class Enabler implements CompoundButton.OnCheckedChangeListener {
101 private final Context mContext;
102 private Switch mSwitch;
104 public Enabler(Context context, Switch switch_) {
109 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
110 setScreenSaverEnabled(mContext, isChecked);
112 public void setSwitch(Switch switch_) {
113 if (mSwitch == switch_) return;
114 if (mSwitch != null) mSwitch.setOnCheckedChangeListener(null);
116 mSwitch.setOnCheckedChangeListener(this);
118 final boolean enabled = isScreenSaverEnabled(mContext);
119 mSwitch.setChecked(enabled);
121 public void pause() {
122 mSwitch.setOnCheckedChangeListener(null);
124 public void resume() {
125 mSwitch.setOnCheckedChangeListener(this);
130 public void onResume() {
131 if (mEnabler != null) {
135 final boolean currentActivateOnDock = 0 != Settings.Secure.getInt(getContentResolver(),
136 SCREENSAVER_ACTIVATE_ON_DOCK, 1);
137 mActivateOnDockPreference.setChecked(currentActivateOnDock);
142 public void onPause() {
143 if (mEnabler != null) {
151 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
152 if (preference == mActivateOnDockPreference) {
153 Settings.Secure.putInt(getContentResolver(),
154 SCREENSAVER_ACTIVATE_ON_DOCK,
155 mActivateOnDockPreference.isChecked() ? 1 : 0);
157 return super.onPreferenceTreeClick(preferenceScreen, preference);