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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/*
This file is part of Subsonic.
Subsonic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Subsonic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2009 (C) Sindre Mehus
*/
package github.daneren2005.subphonic.activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.SearchRecentSuggestions;
import android.util.Log;
import github.daneren2005.subphonic.R;
import github.daneren2005.subphonic.provider.SearchSuggestionProvider1;
import github.daneren2005.subphonic.service.DownloadService;
import github.daneren2005.subphonic.service.DownloadServiceImpl;
import github.daneren2005.subphonic.service.MusicService;
import github.daneren2005.subphonic.service.MusicServiceFactory;
import github.daneren2005.subphonic.util.Constants;
import github.daneren2005.subphonic.util.ErrorDialog;
import github.daneren2005.subphonic.util.FileUtil;
import github.daneren2005.subphonic.util.ModalBackgroundTask;
import github.daneren2005.subphonic.util.Util;
import java.io.File;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;
public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = SettingsActivity.class.getSimpleName();
private final Map<String, ServerSettings> serverSettings = new LinkedHashMap<String, ServerSettings>();
private boolean testingConnection;
private ListPreference theme;
private ListPreference maxBitrateWifi;
private ListPreference maxBitrateMobile;
private ListPreference cacheSize;
private EditTextPreference cacheLocation;
private ListPreference preloadCount;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
theme = (ListPreference) findPreference(Constants.PREFERENCES_KEY_THEME);
maxBitrateWifi = (ListPreference) findPreference(Constants.PREFERENCES_KEY_MAX_BITRATE_WIFI);
maxBitrateMobile = (ListPreference) findPreference(Constants.PREFERENCES_KEY_MAX_BITRATE_MOBILE);
cacheSize = (ListPreference) findPreference(Constants.PREFERENCES_KEY_CACHE_SIZE);
cacheLocation = (EditTextPreference) findPreference(Constants.PREFERENCES_KEY_CACHE_LOCATION);
preloadCount = (ListPreference) findPreference(Constants.PREFERENCES_KEY_PRELOAD_COUNT);
findPreference("testConnection1").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
testConnection(1);
return false;
}
});
findPreference("testConnection2").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
testConnection(2);
return false;
}
});
findPreference("testConnection3").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
testConnection(3);
return false;
}
});
findPreference("clearSearchHistory").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(SettingsActivity.this, SearchSuggestionProvider1.AUTHORITY, SearchSuggestionProvider1.MODE);
suggestions.clearHistory();
Util.toast(SettingsActivity.this, R.string.settings_search_history_cleared);
return false;
}
});
for (int i = 1; i <= 3; i++) {
String instance = String.valueOf(i);
serverSettings.put(instance, new ServerSettings(instance));
}
SharedPreferences prefs = Util.getPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
update();
}
@Override
protected void onDestroy() {
super.onDestroy();
SharedPreferences prefs = Util.getPreferences(this);
prefs.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.d(TAG, "Preference changed: " + key);
update();
if (Constants.PREFERENCES_KEY_HIDE_MEDIA.equals(key)) {
setHideMedia(sharedPreferences.getBoolean(key, false));
}
else if (Constants.PREFERENCES_KEY_MEDIA_BUTTONS.equals(key)) {
setMediaButtonsEnabled(sharedPreferences.getBoolean(key, true));
}
else if (Constants.PREFERENCES_KEY_CACHE_LOCATION.equals(key)) {
setCacheLocation(sharedPreferences.getString(key, ""));
}
}
private void update() {
if (testingConnection) {
return;
}
theme.setSummary(theme.getEntry());
maxBitrateWifi.setSummary(maxBitrateWifi.getEntry());
maxBitrateMobile.setSummary(maxBitrateMobile.getEntry());
cacheSize.setSummary(cacheSize.getEntry());
cacheLocation.setSummary(cacheLocation.getText());
preloadCount.setSummary(preloadCount.getEntry());
for (ServerSettings ss : serverSettings.values()) {
ss.update();
}
}
private void setHideMedia(boolean hide) {
File nomediaDir = new File(FileUtil.getSubsonicDirectory(), ".nomedia");
if (hide && !nomediaDir.exists()) {
if (!nomediaDir.mkdir()) {
Log.w(TAG, "Failed to create " + nomediaDir);
}
} else if (nomediaDir.exists()) {
if (!nomediaDir.delete()) {
Log.w(TAG, "Failed to delete " + nomediaDir);
}
}
Util.toast(this, R.string.settings_hide_media_toast, false);
}
private void setMediaButtonsEnabled(boolean enabled) {
if (enabled) {
Util.registerMediaButtonEventReceiver(this);
} else {
Util.unregisterMediaButtonEventReceiver(this);
}
}
private void setCacheLocation(String path) {
File dir = new File(path);
if (!FileUtil.ensureDirectoryExistsAndIsReadWritable(dir)) {
Util.toast(this, R.string.settings_cache_location_error, false);
// Reset it to the default.
String defaultPath = FileUtil.getDefaultMusicDirectory().getPath();
if (!defaultPath.equals(path)) {
SharedPreferences prefs = Util.getPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(Constants.PREFERENCES_KEY_CACHE_LOCATION, defaultPath);
editor.commit();
cacheLocation.setSummary(defaultPath);
cacheLocation.setText(defaultPath);
}
// Clear download queue.
DownloadService downloadService = DownloadServiceImpl.getInstance();
downloadService.clear();
}
}
private void testConnection(final int instance) {
ModalBackgroundTask<Boolean> task = new ModalBackgroundTask<Boolean>(this, false) {
private int previousInstance;
@Override
protected Boolean doInBackground() throws Throwable {
updateProgress(R.string.settings_testing_connection);
previousInstance = Util.getActiveServer(SettingsActivity.this);
testingConnection = true;
Util.setActiveServer(SettingsActivity.this, instance);
try {
MusicService musicService = MusicServiceFactory.getMusicService(SettingsActivity.this);
musicService.ping(SettingsActivity.this, this);
return musicService.isLicenseValid(SettingsActivity.this, null);
} finally {
Util.setActiveServer(SettingsActivity.this, previousInstance);
testingConnection = false;
}
}
@Override
protected void done(Boolean licenseValid) {
if (licenseValid) {
Util.toast(SettingsActivity.this, R.string.settings_testing_ok);
} else {
Util.toast(SettingsActivity.this, R.string.settings_testing_unlicensed);
}
}
@Override
protected void cancel() {
super.cancel();
Util.setActiveServer(SettingsActivity.this, previousInstance);
}
@Override
protected void error(Throwable error) {
Log.w(TAG, error.toString(), error);
new ErrorDialog(SettingsActivity.this, getResources().getString(R.string.settings_connection_failure) +
" " + getErrorMessage(error), false);
}
};
task.execute();
}
private class ServerSettings {
private EditTextPreference serverName;
private EditTextPreference serverUrl;
private EditTextPreference username;
private PreferenceScreen screen;
private ServerSettings(String instance) {
screen = (PreferenceScreen) findPreference("server" + instance);
serverName = (EditTextPreference) findPreference(Constants.PREFERENCES_KEY_SERVER_NAME + instance);
serverUrl = (EditTextPreference) findPreference(Constants.PREFERENCES_KEY_SERVER_URL + instance);
username = (EditTextPreference) findPreference(Constants.PREFERENCES_KEY_USERNAME + instance);
serverUrl.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
try {
String url = (String) value;
new URL(url);
if (!url.equals(url.trim()) || url.contains("@")) {
throw new Exception();
}
} catch (Exception x) {
new ErrorDialog(SettingsActivity.this, R.string.settings_invalid_url, false);
return false;
}
return true;
}
});
username.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String username = (String) value;
if (username == null || !username.equals(username.trim())) {
new ErrorDialog(SettingsActivity.this, R.string.settings_invalid_username, false);
return false;
}
return true;
}
});
}
public void update() {
serverName.setSummary(serverName.getText());
serverUrl.setSummary(serverUrl.getText());
username.setSummary(username.getText());
screen.setSummary(serverUrl.getText());
screen.setTitle(serverName.getText());
}
}
}
|