summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/DreamSettings.java
blob: d9953aafcfbf81a00f7684773626b3b14ce20108 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * 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.Secure.SCREENSAVER_ENABLED;
import static android.provider.Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ActivityManagerNative;
import android.app.admin.DevicePolicyManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Configuration;
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.IWindowManager;
import android.widget.CompoundButton;
import android.widget.Switch;

import java.util.ArrayList;

public class DreamSettings extends SettingsPreferenceFragment {
    private static final String TAG = "DreamSettings";

    private static final String KEY_ACTIVATE_ON_DOCK = "activate_on_dock";

    private CheckBoxPreference mActivateOnDockPreference;

    private Switch mEnableSwitch;
    private Enabler mEnabler;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        addPreferencesFromResource(R.xml.dream_settings);

        mActivateOnDockPreference = (CheckBoxPreference) findPreference(KEY_ACTIVATE_ON_DOCK);

        final Activity activity = getActivity();

        mEnableSwitch = new Switch(activity);

        if (activity instanceof PreferenceActivity) {
            PreferenceActivity preferenceActivity = (PreferenceActivity) activity;
            // note: we do not check onIsHidingHeaders() or onIsMultiPane() because there's no
            // switch in the left-hand pane to control this; we need to show the ON/OFF in our
            // fragment every time
            final int padding = activity.getResources().getDimensionPixelSize(
                    R.dimen.action_bar_switch_padding);
            mEnableSwitch.setPadding(0, 0, padding, 0);
            activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
                    ActionBar.DISPLAY_SHOW_CUSTOM);
            activity.getActionBar().setCustomView(mEnableSwitch, new ActionBar.LayoutParams(
                    ActionBar.LayoutParams.WRAP_CONTENT,
                    ActionBar.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER_VERTICAL | Gravity.RIGHT));
            activity.getActionBar().setTitle(R.string.screensaver_settings_title);
        }

        mEnabler = new Enabler(activity, mEnableSwitch);
    }

    public static boolean isScreenSaverEnabled(Context context) {
        return 0 != Settings.Secure.getInt(
                    context.getContentResolver(), SCREENSAVER_ENABLED, 1);
    }

    public static void setScreenSaverEnabled(Context context, boolean enabled) {
        Settings.Secure.putInt(
                context.getContentResolver(), SCREENSAVER_ENABLED, enabled ? 1 : 0);
    }

    public static class Enabler implements CompoundButton.OnCheckedChangeListener  {
        private final Context mContext;
        private Switch mSwitch;

        public Enabler(Context context, Switch switch_) {
            mContext = context;
            setSwitch(switch_);
        }
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            setScreenSaverEnabled(mContext, isChecked);
        }
        public void setSwitch(Switch switch_) {
            if (mSwitch == switch_) return;
            if (mSwitch != null) mSwitch.setOnCheckedChangeListener(null);
            mSwitch = switch_;
            mSwitch.setOnCheckedChangeListener(this);

            final boolean enabled = isScreenSaverEnabled(mContext);
            mSwitch.setChecked(enabled);
        }
        public void pause() {
            mSwitch.setOnCheckedChangeListener(null);
        }
        public void resume() {
            mSwitch.setOnCheckedChangeListener(this);
        }
    }

    @Override
    public void onResume() {
        if (mEnabler != null) {
            mEnabler.resume();
        }

        final boolean currentActivateOnDock = 0 != Settings.Secure.getInt(getContentResolver(),
                SCREENSAVER_ACTIVATE_ON_DOCK, 1);
        mActivateOnDockPreference.setChecked(currentActivateOnDock);
        super.onResume();
    }

    @Override
    public void onPause() {
        if (mEnabler != null) {
            mEnabler.pause();
        }

        super.onPause();
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        if (preference == mActivateOnDockPreference) {
            Settings.Secure.putInt(getContentResolver(),
                    SCREENSAVER_ACTIVATE_ON_DOCK, 
                    mActivateOnDockPreference.isChecked() ? 1 : 0);
        }
        return super.onPreferenceTreeClick(preferenceScreen, preference);
    }
}