aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/wallpapers/photophase/preferences/LayoutPreferenceFragment.java
blob: 46a55e9ebe8651707de120b07e89b308fa9c02ed (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) 2014 The CyanogenMod 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 org.cyanogenmod.wallpapers.photophase.preferences;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.Preference.OnPreferenceChangeListener;
import android.util.Log;

import org.cyanogenmod.wallpapers.photophase.R;
import org.cyanogenmod.wallpapers.photophase.preferences.PreferencesProvider.Preferences;
import org.cyanogenmod.wallpapers.photophase.preferences.SeekBarProgressPreference.OnDisplayProgress;

/**
 * A fragment class with the layout disposition
 */
public class LayoutPreferenceFragment extends PreferenceFragment {

    private static final String TAG = "LayoutPreferenceFragment";

    private static final boolean DEBUG = false;

    private CheckBoxPreference mRandomDispositions;
    SeekBarProgressPreference mRandomDispositionsInterval;
    Preference mPortraitDisposition;
    Preference mLandscapeDisposition;

    boolean mRedrawFlag;
    boolean mDispositionIntervalFlag;

    private final OnPreferenceChangeListener mOnChangeListener = new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(final Preference preference, Object newValue) {
            String key = preference.getKey();
            if (DEBUG) Log.d(TAG, "Preference changed: " + key + "=" + newValue);
            if (key.compareTo("ui_disposition_random") == 0) {
                boolean randomDispositions = ((Boolean)newValue).booleanValue();
                mPortraitDisposition.setEnabled(!randomDispositions);
                mLandscapeDisposition.setEnabled(!randomDispositions);
                mRedrawFlag = true;
            } else if (key.compareTo("ui_disposition_random_interval") == 0) {
                mDispositionIntervalFlag = true;
            }
            return true;
        }
    };

    /**
     * {@inheritDoc}
     */
    @Override
    public void onDestroy() {
        super.onDestroy();

        // Reload the settings
        PreferencesProvider.reload(getActivity());

        // Notify that the settings was changed
        Intent intent = new Intent(PreferencesProvider.ACTION_SETTINGS_CHANGED);
        if (mRedrawFlag) {
            intent.putExtra(PreferencesProvider.EXTRA_FLAG_REDRAW, Boolean.TRUE);
        }
        if (mDispositionIntervalFlag) {
            int interval = Preferences.Layout.getRandomDispositionsInterval();
            intent.putExtra(PreferencesProvider.EXTRA_FLAG_DISPOSITION_INTERVAL_CHANGED, interval);
        }
        getActivity().sendBroadcast(intent);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final String formatSeconds = getString(R.string.format_seconds);
        final String formatMinutes = getString(R.string.format_minutes);
        final String formatHours = getString(R.string.format_hours);
        final String formatDays = getString(R.string.format_days);

        // Change the preference manager
        getPreferenceManager().setSharedPreferencesName(PreferencesProvider.PREFERENCES_FILE);
        getPreferenceManager().setSharedPreferencesMode(Context.MODE_PRIVATE);

        final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
        final Resources res = getActivity().getResources();

        // Add the preferences
        addPreferencesFromResource(R.xml.preferences_layout);

        // -- Random dispositions
        mRandomDispositions = (CheckBoxPreference)findPreference("ui_disposition_random");
        mRandomDispositions.setOnPreferenceChangeListener(mOnChangeListener);

        // -- Interval
        final int[] randomDispositionsIntervals =
                res.getIntArray(R.array.random_dispositions_intervals_values);
        mRandomDispositionsInterval =
                (SeekBarProgressPreference)findPreference("ui_disposition_random_interval");
        mRandomDispositionsInterval.setMax(randomDispositionsIntervals.length - 1);
        int transitionInterval = prefs.getInt("ui_disposition_random_interval",
                Preferences.Layout.DEFAULT_RANDOM_DISPOSITIONS_INTERVAL_INDEX);
        if (transitionInterval > (randomDispositionsIntervals.length - 1)) {
            mRandomDispositionsInterval.setProgress(
                    Preferences.Layout.DEFAULT_RANDOM_DISPOSITIONS_INTERVAL_INDEX);
        }
        mRandomDispositionsInterval.setOnDisplayProgress(new OnDisplayProgress() {
            @Override
            public String onDisplayProgress(int progress) {
                if (randomDispositionsIntervals[progress] < 60000) {
                    // Seconds
                    mRandomDispositionsInterval.setFormat(formatSeconds);
                    return String.valueOf(randomDispositionsIntervals[progress] / 1000);
                } else if (randomDispositionsIntervals[progress] < 3600000) {
                    // Minutes
                    mRandomDispositionsInterval.setFormat(formatMinutes);
                    return String.valueOf(randomDispositionsIntervals[progress] / 1000 / 60);
                } else if (randomDispositionsIntervals[progress] < 86400000) {
                    // Hours
                    mRandomDispositionsInterval.setFormat(formatHours);
                    return String.valueOf(randomDispositionsIntervals[progress] / 1000 / 60 / 60);
                }
                // Days
                mRandomDispositionsInterval.setFormat(formatDays);
                return String.valueOf(randomDispositionsIntervals[progress] / 1000 / 60 / 60 / 24);
            }
        });
        mRandomDispositionsInterval.setOnPreferenceChangeListener(mOnChangeListener);

        // -- Portrait
        mPortraitDisposition = findPreference("ui_disposition_portrait");
        mPortraitDisposition.setEnabled(!Preferences.Layout.isRandomDispositions());

        // -- Landscape
        mLandscapeDisposition = findPreference("ui_disposition_landscape");
        mLandscapeDisposition.setEnabled(!Preferences.Layout.isRandomDispositions());
    }
}