summaryrefslogtreecommitdiff
path: root/vdrmanager/src/de/bjusystems/vdrmanager/gui/VdrPreferencesActivity.java
blob: 8bde79ceea5dc5555689d1cf530b2c8ea89fa4d5 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package de.bjusystems.vdrmanager.gui;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import de.bjusystems.vdrmanager.R;
import de.bjusystems.vdrmanager.app.Intents;
import de.bjusystems.vdrmanager.data.Preferences;
import de.bjusystems.vdrmanager.data.Vdr;
import de.bjusystems.vdrmanager.data.VdrSharedPreferences;

public class VdrPreferencesActivity extends BasePreferencesActivity implements
		OnSharedPreferenceChangeListener, OnPreferenceClickListener {

	Vdr vdr;
	VdrSharedPreferences pref;

	int id = -1;

	@Override
	public SharedPreferences getSharedPreferences(String name, int mode) {
		return this.pref;
	}

	@Override
	public Preference findPreference(CharSequence key) {
		return super.findPreference(key);
	}

	private void initVDR() {
		id = getIntent().getIntExtra(Intents.VDR_ID, -1);
		if (id == -1) {// new vdr
			vdr = new Vdr();
			pref = new VdrSharedPreferences();
			pref.instance = vdr;
		} else {// edit
			Vdr v = getHelper().getVdrDAO().queryForId(id);
			if (v != null) {
				vdr = v;
				pref = new VdrSharedPreferences(vdr);
			} else {
				vdr = new Vdr();
				pref = new VdrSharedPreferences();
				pref.instance = vdr;
				id = -1;
			}
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		initVDR();

		// this.getPreferenceManager().setSharedPreferencesName(Preferences.getPreferenceFile(this));

		pref.instance = vdr;
		pref.dao = getHelper().getVdrDAO();
		pref.registerOnSharedPreferenceChangeListener(this);

		this.addPreferencesFromResource(R.xml.vdr_prefs);

		updateChildPreferences();

		findPreference(getString(R.string.wakeup_wol_mac_key))
				.setOnPreferenceClickListener(this);

	}

	public void onSharedPreferenceChanged(SharedPreferences arg0, String key) {
		updateChildPreferences();
		Preference p = findPreference(key);
		updateSummary(p);
	}

	@Override
	protected void onResume() {
		super.onResume();
		// Set up a listener whenever a key changes
		pref.registerOnSharedPreferenceChangeListener(this);

	}

	@Override
	protected void onPause() {
		super.onPause();
		// Unregister the listener whenever a key changes
		pref.unregisterOnSharedPreferenceChangeListener(this);
		Preferences.reloadVDR();
	}

	private void enableWolPreferences() {
		Preference p = findPreference(getString(R.string.wakeup_wol_mac_key));
		if (p != null)

			p.setEnabled(true);
		p = findPreference(getString(R.string.wakeup_wol_custom_broadcast_key));
		if (p != null) {
			p.setEnabled(true);
		}
	}

	private void disableWolPreferences() {
		Preference p = findPreference(getString(R.string.wakeup_wol_mac_key));
		if (p != null)
			p.setEnabled(false);

		p = findPreference(getString(R.string.wakeup_wol_custom_broadcast_key));
		if (p != null)
			p.setEnabled(false);

	}

	private void disableWakeupUrlPreferences() {
		Preference p = findPreference(getString(R.string.wakeup_url_key));
		if (p != null) {
			p.setEnabled(false);
		}
		p = findPreference(getString(R.string.wakeup_password_key));
		if (p != null) {
			p.setEnabled(false);
		}

		p = findPreference(getString(R.string.wakeup_user_key));
		if (p != null) {
			p.setEnabled(false);
		}
	}

	private void enableWakeupUrlPrefenreces() {
		Preference p = findPreference(getString(R.string.wakeup_url_key));
		if (p != null) {
			p.setEnabled(true);
		}

		p = findPreference(getString(R.string.wakeup_password_key));
		if (p != null) {
			p.setEnabled(true);
		}
		p = findPreference(getString(R.string.wakeup_user_key));
		if (p != null) {
			p.setEnabled(true);
		}
	}

	private void updateChildPreferences() {
		String wakup = pref.getString(getString(R.string.wakeup_method_key),
				"wol");

		if (wakup.equals("url")) {
			disableWolPreferences();
			enableWakeupUrlPrefenreces();
		} else {// remote url
			disableWakeupUrlPreferences();
			enableWolPreferences();
		}

		for (String key : pref.getAll().keySet()) {
			Preference p = findPreference(key);
			updateSummary(p);
		}

	}

	public boolean onPreferenceClick(Preference arg0) {

		return false;
	}

	@Override
	public void onBackPressed() {
		if (id != -1) {// no new devices
			setResult(RESULT_OK);
			finish();
			return;
		}
		if (pref.commits < 2) {// user has not changed anything
			getHelper().getVdrDAO().delete(pref.instance);
			finish();
			return;
		}
		super.onBackPressed();
	}

}