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.Secure.SCREENSAVER_ENABLED; 20 import static android.provider.Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK; 21 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; 45 46 import java.util.ArrayList; 47 48 public class DreamSettings extends SettingsPreferenceFragment { 49 private static final String TAG = "DreamSettings"; 50 51 private static final String KEY_ACTIVATE_ON_DOCK = "activate_on_dock"; 52 53 private CheckBoxPreference mActivateOnDockPreference; 54 55 private Switch mEnableSwitch; 56 private Enabler mEnabler; 57 58 @Override 59 public void onActivityCreated(Bundle savedInstanceState) { 60 super.onActivityCreated(savedInstanceState); 61 62 addPreferencesFromResource(R.xml.dream_settings); 63 64 mActivateOnDockPreference = (CheckBoxPreference) findPreference(KEY_ACTIVATE_ON_DOCK); 65 66 final Activity activity = getActivity(); 67 68 mEnableSwitch = new Switch(activity); 69 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); 85 } 86 87 mEnabler = new Enabler(activity, mEnableSwitch); 88 } 89 90 public static boolean isScreenSaverEnabled(Context context) { 91 return 0 != Settings.Secure.getInt( 92 context.getContentResolver(), SCREENSAVER_ENABLED, 1); 93 } 94 95 public static void setScreenSaverEnabled(Context context, boolean enabled) { 96 Settings.Secure.putInt( 97 context.getContentResolver(), SCREENSAVER_ENABLED, enabled ? 1 : 0); 98 } 99 100 public static class Enabler implements CompoundButton.OnCheckedChangeListener { 101 private final Context mContext; 102 private Switch mSwitch; 103 104 public Enabler(Context context, Switch switch_) { 105 mContext = context; 106 setSwitch(switch_); 107 } 108 @Override 109 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 110 setScreenSaverEnabled(mContext, isChecked); 111 } 112 public void setSwitch(Switch switch_) { 113 if (mSwitch == switch_) return; 114 if (mSwitch != null) mSwitch.setOnCheckedChangeListener(null); 115 mSwitch = switch_; 116 mSwitch.setOnCheckedChangeListener(this); 117 118 final boolean enabled = isScreenSaverEnabled(mContext); 119 mSwitch.setChecked(enabled); 120 } 121 public void pause() { 122 mSwitch.setOnCheckedChangeListener(null); 123 } 124 public void resume() { 125 mSwitch.setOnCheckedChangeListener(this); 126 } 127 } 128 129 @Override 130 public void onResume() { 131 if (mEnabler != null) { 132 mEnabler.resume(); 133 } 134 135 final boolean currentActivateOnDock = 0 != Settings.Secure.getInt(getContentResolver(), 136 SCREENSAVER_ACTIVATE_ON_DOCK, 1); 137 mActivateOnDockPreference.setChecked(currentActivateOnDock); 138 super.onResume(); 139 } 140 141 @Override 142 public void onPause() { 143 if (mEnabler != null) { 144 mEnabler.pause(); 145 } 146 147 super.onPause(); 148 } 149 150 @Override 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); 156 } 157 return super.onPreferenceTreeClick(preferenceScreen, preference); 158 } 159 } 160