OSDN Git Service

Overhaul vibrate and silent settings to match hard keys.
[android-x86/packages-apps-Settings.git] / src / com / android / settings / DreamSettings.java
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             if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) {
73                 final int padding = activity.getResources().getDimensionPixelSize(
74                         R.dimen.action_bar_switch_padding);
75                 mEnableSwitch.setPadding(0, 0, padding, 0);
76                 activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
77                         ActionBar.DISPLAY_SHOW_CUSTOM);
78                 activity.getActionBar().setCustomView(mEnableSwitch, new ActionBar.LayoutParams(
79                         ActionBar.LayoutParams.WRAP_CONTENT,
80                         ActionBar.LayoutParams.WRAP_CONTENT,
81                         Gravity.CENTER_VERTICAL | Gravity.RIGHT));
82                 activity.getActionBar().setTitle(R.string.screensaver_settings_title);
83             }
84         }
85
86         mEnabler = new Enabler(activity, mEnableSwitch);
87     }
88
89     public static boolean isScreenSaverEnabled(Context context) {
90         return 0 != Settings.Secure.getInt(
91                     context.getContentResolver(), SCREENSAVER_ENABLED, 1);
92     }
93
94     public static void setScreenSaverEnabled(Context context, boolean enabled) {
95         Settings.Secure.putInt(
96                 context.getContentResolver(), SCREENSAVER_ENABLED, enabled ? 1 : 0);
97     }
98
99     public static class Enabler implements CompoundButton.OnCheckedChangeListener  {
100         private final Context mContext;
101         private Switch mSwitch;
102
103         public Enabler(Context context, Switch switch_) {
104             mContext = context;
105             setSwitch(switch_);
106         }
107         @Override
108         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
109             setScreenSaverEnabled(mContext, isChecked);
110         }
111         public void setSwitch(Switch switch_) {
112             if (mSwitch == switch_) return;
113             if (mSwitch != null) mSwitch.setOnCheckedChangeListener(null);
114             mSwitch = switch_;
115             mSwitch.setOnCheckedChangeListener(this);
116
117             final boolean enabled = isScreenSaverEnabled(mContext);
118             mSwitch.setChecked(enabled);
119         }
120         public void pause() {
121             mSwitch.setOnCheckedChangeListener(null);
122         }
123         public void resume() {
124             mSwitch.setOnCheckedChangeListener(this);
125         }
126     }
127
128     @Override
129     public void onResume() {
130         if (mEnabler != null) {
131             mEnabler.resume();
132         }
133
134         final boolean currentActivateOnDock = 0 != Settings.Secure.getInt(getContentResolver(),
135                 SCREENSAVER_ACTIVATE_ON_DOCK, 1);
136         mActivateOnDockPreference.setChecked(currentActivateOnDock);
137         super.onResume();
138     }
139
140     @Override
141     public void onPause() {
142         if (mEnabler != null) {
143             mEnabler.pause();
144         }
145
146         super.onPause();
147     }
148
149     @Override
150     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
151         if (preference == mActivateOnDockPreference) {
152             Settings.Secure.putInt(getContentResolver(),
153                     SCREENSAVER_ACTIVATE_ON_DOCK, 
154                     mActivateOnDockPreference.isChecked() ? 1 : 0);
155         }
156         return super.onPreferenceTreeClick(preferenceScreen, preference);
157     }
158 }