2013-02-11 05:30:46 +01:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
2013-04-06 21:47:24 +02:00
|
|
|
package com.thejoshwa.ultrasonic.androidapp.activity;
|
2013-02-11 05:30:46 +01:00
|
|
|
|
2013-05-02 08:53:33 +02:00
|
|
|
import android.content.Intent;
|
2013-02-11 05:30:46 +01:00
|
|
|
import android.content.SharedPreferences;
|
2013-05-16 09:59:55 +02:00
|
|
|
import android.os.Build;
|
2013-02-11 05:30:46 +01:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.CheckBoxPreference;
|
|
|
|
import android.preference.EditTextPreference;
|
|
|
|
import android.preference.ListPreference;
|
|
|
|
import android.preference.Preference;
|
|
|
|
import android.preference.PreferenceActivity;
|
2013-05-05 03:44:39 +02:00
|
|
|
import android.preference.PreferenceCategory;
|
2013-05-05 03:57:38 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2013-02-11 05:30:46 +01:00
|
|
|
import android.preference.PreferenceScreen;
|
|
|
|
import android.provider.SearchRecentSuggestions;
|
2013-05-05 03:44:39 +02:00
|
|
|
import android.text.InputType;
|
2013-02-11 05:30:46 +01:00
|
|
|
import android.util.Log;
|
2013-05-02 08:53:33 +02:00
|
|
|
import android.view.MenuItem;
|
2013-04-27 11:52:25 +02:00
|
|
|
import android.view.View;
|
2013-05-02 08:53:33 +02:00
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.widget.TextView;
|
2013-04-27 11:52:25 +02:00
|
|
|
|
2013-04-06 21:47:24 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.R;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.provider.SearchSuggestionProvider;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.DownloadService;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.DownloadServiceImpl;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.MusicService;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.MusicServiceFactory;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.Constants;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.ErrorDialog;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.FileUtil;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.ModalBackgroundTask;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.Util;
|
2013-02-11 05:30:46 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
2013-05-02 08:53:33 +02:00
|
|
|
import net.simonvt.menudrawer.MenuDrawer;
|
|
|
|
import net.simonvt.menudrawer.Position;
|
|
|
|
|
|
|
|
public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener, OnClickListener {
|
2013-02-11 05:30:46 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
private ListPreference bufferLength;
|
2013-05-17 09:26:06 +02:00
|
|
|
private ListPreference incrementTime;
|
2013-02-11 05:30:46 +01:00
|
|
|
private ListPreference networkTimeout;
|
|
|
|
private ListPreference maxAlbums;
|
|
|
|
private ListPreference maxSongs;
|
|
|
|
private ListPreference maxArtists;
|
|
|
|
private ListPreference defaultAlbums;
|
|
|
|
private ListPreference defaultSongs;
|
|
|
|
private ListPreference defaultArtists;
|
2013-05-24 10:16:48 +02:00
|
|
|
private ListPreference chatRefreshInterval;
|
2013-05-26 08:06:10 +02:00
|
|
|
private ListPreference directoryCacheTime;
|
2013-02-11 05:30:46 +01:00
|
|
|
private CheckBoxPreference mediaButtonsEnabled;
|
|
|
|
private CheckBoxPreference lockScreenEnabled;
|
2013-05-16 09:59:55 +02:00
|
|
|
private CheckBoxPreference gaplessPlaybackEnabled;
|
2013-05-05 03:44:39 +02:00
|
|
|
private int maxServerCount = 10;
|
|
|
|
private int minServerCount = 0;
|
2013-05-02 08:53:33 +02:00
|
|
|
|
|
|
|
private static final String STATE_MENUDRAWER = "com.thejoshwa.ultrasonic.androidapp.menuDrawer";
|
|
|
|
private static final String STATE_ACTIVE_VIEW_ID = "com.thejoshwa.ultrasonic.androidapp.activeViewId";
|
|
|
|
private static final String STATE_ACTIVE_POSITION = "com.thejoshwa.ultrasonic.androidapp.activePosition";
|
|
|
|
|
|
|
|
public MenuDrawer menuDrawer;
|
|
|
|
private int activePosition = 1;
|
|
|
|
private int menuActiveViewId;
|
2013-05-05 03:44:39 +02:00
|
|
|
private int activeServers = 3;
|
2013-05-02 08:53:33 +02:00
|
|
|
View searchMenuItem = null;
|
|
|
|
View playlistsMenuItem = null;
|
|
|
|
View menuMain = null;
|
2013-05-05 03:44:39 +02:00
|
|
|
PreferenceCategory serversCategory;
|
|
|
|
Preference addServerPreference;
|
2013-05-05 03:57:38 +02:00
|
|
|
SharedPreferences settings;
|
2013-02-11 05:30:46 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2013-05-02 08:53:33 +02:00
|
|
|
applyTheme();
|
2013-02-11 05:30:46 +01:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
addPreferencesFromResource(R.xml.settings);
|
2013-05-02 08:53:33 +02:00
|
|
|
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
activePosition = savedInstanceState.getInt(STATE_ACTIVE_POSITION);
|
|
|
|
menuActiveViewId = savedInstanceState.getInt(STATE_ACTIVE_VIEW_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
menuDrawer = MenuDrawer.attach(this, MenuDrawer.MENU_DRAG_WINDOW, Position.LEFT);
|
|
|
|
menuDrawer.setMenuView(R.layout.menu_main);
|
|
|
|
|
|
|
|
searchMenuItem = findViewById(R.id.menu_search);
|
|
|
|
playlistsMenuItem = findViewById(R.id.menu_playlists);
|
|
|
|
|
|
|
|
findViewById(R.id.menu_home).setOnClickListener(this);
|
|
|
|
findViewById(R.id.menu_browse).setOnClickListener(this);
|
|
|
|
searchMenuItem.setOnClickListener(this);
|
|
|
|
playlistsMenuItem.setOnClickListener(this);
|
2013-05-24 10:16:48 +02:00
|
|
|
findViewById(R.id.menu_chat).setOnClickListener(this);
|
2013-05-02 08:53:33 +02:00
|
|
|
findViewById(R.id.menu_now_playing).setOnClickListener(this);
|
|
|
|
findViewById(R.id.menu_settings).setOnClickListener(this);
|
|
|
|
findViewById(R.id.menu_about).setOnClickListener(this);
|
|
|
|
findViewById(R.id.menu_exit).setOnClickListener(this);
|
|
|
|
|
2013-05-05 03:44:39 +02:00
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
2013-05-02 08:53:33 +02:00
|
|
|
|
|
|
|
View browseMenuItem = findViewById(R.id.menu_settings);
|
|
|
|
menuDrawer.setActiveView(browseMenuItem);
|
|
|
|
|
|
|
|
TextView activeView = (TextView)findViewById(menuActiveViewId);
|
|
|
|
|
|
|
|
if (activeView != null) {
|
|
|
|
menuDrawer.setActiveView(activeView);
|
|
|
|
}
|
2013-02-11 05:30:46 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
bufferLength = (ListPreference) findPreference(Constants.PREFERENCES_KEY_BUFFER_LENGTH);
|
2013-05-17 09:26:06 +02:00
|
|
|
incrementTime = (ListPreference) findPreference(Constants.PREFERENCES_KEY_INCREMENT_TIME);
|
2013-02-11 05:30:46 +01:00
|
|
|
networkTimeout = (ListPreference) findPreference(Constants.PREFERENCES_KEY_NETWORK_TIMEOUT);
|
|
|
|
maxAlbums = (ListPreference) findPreference(Constants.PREFERENCES_KEY_MAX_ALBUMS);
|
|
|
|
maxSongs = (ListPreference) findPreference(Constants.PREFERENCES_KEY_MAX_SONGS);
|
|
|
|
maxArtists = (ListPreference) findPreference(Constants.PREFERENCES_KEY_MAX_ARTISTS);
|
|
|
|
defaultArtists = (ListPreference) findPreference(Constants.PREFERENCES_KEY_DEFAULT_ARTISTS);
|
|
|
|
defaultSongs = (ListPreference) findPreference(Constants.PREFERENCES_KEY_DEFAULT_SONGS);
|
|
|
|
defaultAlbums = (ListPreference) findPreference(Constants.PREFERENCES_KEY_DEFAULT_ALBUMS);
|
2013-05-24 10:16:48 +02:00
|
|
|
chatRefreshInterval = (ListPreference) findPreference(Constants.PREFERENCES_KEY_CHAT_REFRESH_INTERVAL);
|
2013-05-26 08:06:10 +02:00
|
|
|
directoryCacheTime = (ListPreference) findPreference(Constants.PREFERENCES_KEY_DIRECTORY_CACHE_TIME);
|
2013-02-11 05:30:46 +01:00
|
|
|
mediaButtonsEnabled = (CheckBoxPreference) findPreference(Constants.PREFERENCES_KEY_MEDIA_BUTTONS);
|
|
|
|
lockScreenEnabled = (CheckBoxPreference) findPreference(Constants.PREFERENCES_KEY_SHOW_LOCK_SCREEN_CONTROLS);
|
2013-05-16 09:59:55 +02:00
|
|
|
gaplessPlaybackEnabled = (CheckBoxPreference) findPreference(Constants.PREFERENCES_KEY_GAPLESS_PLAYBACK);
|
2013-02-11 05:30:46 +01:00
|
|
|
|
2013-05-05 03:44:39 +02:00
|
|
|
findPreference(Constants.PREFERENCES_KEY_CLEAR_SEARCH_HISTORY).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
2013-02-11 05:30:46 +01:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
2013-05-05 03:44:39 +02:00
|
|
|
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(SettingsActivity.this, SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);
|
|
|
|
suggestions.clearHistory();
|
|
|
|
Util.toast(SettingsActivity.this, R.string.settings_search_history_cleared);
|
2013-02-11 05:30:46 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2013-05-05 03:44:39 +02:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
if (Build.VERSION.SDK_INT < 14) {
|
|
|
|
gaplessPlaybackEnabled.setChecked(false);
|
|
|
|
gaplessPlaybackEnabled.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
2013-05-05 03:57:38 +02:00
|
|
|
settings = PreferenceManager.getDefaultSharedPreferences(this );
|
|
|
|
activeServers = settings.getInt(Constants.PREFERENCES_KEY_ACTIVE_SERVERS, 3);
|
2013-05-05 03:44:39 +02:00
|
|
|
|
|
|
|
serversCategory = (PreferenceCategory) findPreference(Constants.PREFERENCES_KEY_SERVERS_KEY);
|
|
|
|
|
|
|
|
addServerPreference = new Preference(this);
|
|
|
|
addServerPreference.setKey(Constants.PREFERENCES_KEY_ADD_SERVER);
|
|
|
|
addServerPreference.setPersistent(false);
|
|
|
|
addServerPreference.setTitle(getResources().getString(R.string.settings_server_add_server));
|
|
|
|
addServerPreference.setEnabled(activeServers < maxServerCount);
|
|
|
|
serversCategory.addPreference(addServerPreference);
|
|
|
|
|
|
|
|
for (int i = 1; i <= activeServers; i++) {
|
|
|
|
final int instanceValue = i;
|
|
|
|
|
|
|
|
serversCategory.addPreference(addServer(i));
|
|
|
|
|
|
|
|
findPreference(Constants.PREFERENCES_KEY_TEST_CONNECTION + i).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
2013-02-11 05:30:46 +01:00
|
|
|
|
2013-05-05 03:44:39 +02:00
|
|
|
testConnection(instanceValue);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
String instance = String.valueOf(i);
|
|
|
|
serverSettings.put(instance, new ServerSettings(instance));
|
|
|
|
}
|
|
|
|
|
|
|
|
addServerPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
2013-02-11 05:30:46 +01:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
2013-05-05 03:44:39 +02:00
|
|
|
if (activeServers == maxServerCount) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
activeServers++;
|
2013-05-05 03:57:38 +02:00
|
|
|
|
|
|
|
SharedPreferences.Editor prefEditor = settings.edit();
|
|
|
|
prefEditor.putInt(Constants.PREFERENCES_KEY_ACTIVE_SERVERS, activeServers);
|
|
|
|
prefEditor.commit();
|
2013-05-05 03:44:39 +02:00
|
|
|
|
|
|
|
Preference addServerPreference = findPreference(Constants.PREFERENCES_KEY_ADD_SERVER);
|
|
|
|
serversCategory.removePreference(addServerPreference);
|
|
|
|
serversCategory.addPreference(addServer(activeServers));
|
|
|
|
serversCategory.addPreference(addServerPreference);
|
|
|
|
|
2013-05-05 03:57:38 +02:00
|
|
|
String instance = String.valueOf(activeServers);
|
2013-05-05 03:44:39 +02:00
|
|
|
serverSettings.put(instance, new ServerSettings(instance));
|
|
|
|
|
|
|
|
addServerPreference.setEnabled(activeServers < maxServerCount);
|
|
|
|
applyTheme();
|
|
|
|
|
|
|
|
return true;
|
2013-02-11 05:30:46 +01:00
|
|
|
}
|
|
|
|
});
|
2013-05-05 03:44:39 +02:00
|
|
|
|
|
|
|
SharedPreferences prefs = Util.getPreferences(this);
|
|
|
|
prefs.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
|
|
|
|
getActionBar().setSubtitle(R.string.menu_settings);
|
2013-02-11 05:30:46 +01:00
|
|
|
|
2013-05-05 03:44:39 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
private PreferenceScreen addServer(final int instance) {
|
|
|
|
final PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(this);
|
|
|
|
screen.setTitle(R.string.settings_server_unused);
|
|
|
|
screen.setKey(Constants.PREFERENCES_KEY_SERVER + instance);
|
|
|
|
|
|
|
|
final EditTextPreference serverNamePreference = new EditTextPreference(this);
|
|
|
|
serverNamePreference.setKey(Constants.PREFERENCES_KEY_SERVER_NAME + instance);
|
|
|
|
serverNamePreference.setDefaultValue(getResources().getString(R.string.settings_server_unused));
|
|
|
|
serverNamePreference.setTitle(R.string.settings_server_name);
|
|
|
|
|
|
|
|
if (serverNamePreference.getText() == null) {
|
|
|
|
serverNamePreference.setText(getResources().getString(R.string.settings_server_unused));
|
|
|
|
}
|
|
|
|
|
|
|
|
serverNamePreference.setSummary(serverNamePreference.getText());
|
|
|
|
|
|
|
|
final EditTextPreference serverUrlPreference = new EditTextPreference(this);
|
|
|
|
serverUrlPreference.setKey(Constants.PREFERENCES_KEY_SERVER_URL + instance);
|
|
|
|
serverUrlPreference.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_URI);
|
|
|
|
serverUrlPreference.setDefaultValue("http://yourhost");
|
|
|
|
serverUrlPreference.setTitle(R.string.settings_server_address);
|
|
|
|
|
|
|
|
if (serverUrlPreference.getText() == null) {
|
|
|
|
serverUrlPreference.setText("http://yourhost");
|
|
|
|
}
|
|
|
|
|
|
|
|
serverUrlPreference.setSummary(serverUrlPreference.getText());
|
|
|
|
|
|
|
|
screen.setSummary(serverUrlPreference.getText());
|
|
|
|
|
|
|
|
final EditTextPreference serverUsernamePreference = new EditTextPreference(this);
|
|
|
|
serverUsernamePreference.setKey(Constants.PREFERENCES_KEY_USERNAME + instance);
|
|
|
|
serverUsernamePreference.setTitle(R.string.settings_server_username);
|
|
|
|
|
|
|
|
final EditTextPreference serverPasswordPreference = new EditTextPreference(this);
|
|
|
|
serverPasswordPreference.setKey(Constants.PREFERENCES_KEY_PASSWORD + instance);
|
|
|
|
serverPasswordPreference.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
|
|
|
|
serverPasswordPreference.setSummary("***");
|
|
|
|
serverPasswordPreference.setTitle(R.string.settings_server_password);
|
|
|
|
|
|
|
|
final CheckBoxPreference serverEnabledPreference = new CheckBoxPreference(this);
|
|
|
|
serverEnabledPreference.setDefaultValue(true);
|
|
|
|
serverEnabledPreference.setKey(Constants.PREFERENCES_KEY_SERVER_ENABLED + instance);
|
|
|
|
serverEnabledPreference.setTitle(R.string.equalizer_enabled);
|
|
|
|
|
|
|
|
Preference serverRemoveServerPreference = new Preference(this);
|
|
|
|
serverRemoveServerPreference.setKey(Constants.PREFERENCES_KEY_REMOVE_SERVER + instance);
|
|
|
|
serverRemoveServerPreference.setPersistent(false);
|
|
|
|
serverRemoveServerPreference.setTitle(R.string.settings_server_remove_server);
|
|
|
|
|
|
|
|
serverRemoveServerPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
2013-02-11 05:30:46 +01:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
2013-05-05 03:44:39 +02:00
|
|
|
|
|
|
|
if (activeServers == minServerCount) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset values to null so when we ask for them again they are new
|
|
|
|
serverNamePreference.setText(null);
|
|
|
|
serverUrlPreference.setText(null);
|
|
|
|
serverUsernamePreference.setText(null);
|
|
|
|
serverPasswordPreference.setText(null);
|
|
|
|
serverEnabledPreference.setChecked(true);
|
|
|
|
|
2013-05-09 09:21:07 +02:00
|
|
|
if (instance < activeServers) {
|
|
|
|
|
|
|
|
int activeServer = Util.getActiveServer(SettingsActivity.this);
|
|
|
|
for (int i = instance; i <= activeServers; i++) {
|
|
|
|
Util.removeInstanceName(SettingsActivity.this, i, activeServer);
|
|
|
|
}
|
2013-05-05 07:29:14 +02:00
|
|
|
}
|
2013-05-09 09:21:07 +02:00
|
|
|
|
2013-05-05 03:44:39 +02:00
|
|
|
activeServers--;
|
2013-05-09 09:21:07 +02:00
|
|
|
|
2013-05-05 03:44:39 +02:00
|
|
|
serversCategory.removePreference(screen);
|
2013-05-05 03:57:38 +02:00
|
|
|
|
|
|
|
SharedPreferences.Editor prefEditor = settings.edit();
|
|
|
|
prefEditor.putInt(Constants.PREFERENCES_KEY_ACTIVE_SERVERS, activeServers);
|
|
|
|
prefEditor.commit();
|
|
|
|
|
2013-05-05 03:44:39 +02:00
|
|
|
addServerPreference.setEnabled(activeServers < maxServerCount);
|
|
|
|
screen.getDialog().dismiss();
|
|
|
|
|
|
|
|
return true;
|
2013-02-11 05:30:46 +01:00
|
|
|
}
|
|
|
|
});
|
2013-05-05 03:44:39 +02:00
|
|
|
|
|
|
|
Preference serverTestConnectionPreference = new Preference(this);
|
|
|
|
serverTestConnectionPreference.setKey(Constants.PREFERENCES_KEY_TEST_CONNECTION + instance);
|
|
|
|
serverTestConnectionPreference.setPersistent(false);
|
|
|
|
serverTestConnectionPreference.setTitle(R.string.settings_test_connection_title);
|
|
|
|
serverTestConnectionPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
2013-02-11 05:30:46 +01:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
2013-05-05 03:44:39 +02:00
|
|
|
|
|
|
|
testConnection(instance);
|
2013-02-11 05:30:46 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2013-05-05 03:44:39 +02:00
|
|
|
|
|
|
|
screen.addPreference(serverNamePreference);
|
|
|
|
screen.addPreference(serverUrlPreference);
|
|
|
|
screen.addPreference(serverUsernamePreference);
|
|
|
|
screen.addPreference(serverPasswordPreference);
|
|
|
|
screen.addPreference(serverEnabledPreference);
|
|
|
|
screen.addPreference(serverRemoveServerPreference);
|
|
|
|
screen.addPreference(serverTestConnectionPreference);
|
|
|
|
|
|
|
|
return screen;
|
2013-02-11 05:30:46 +01:00
|
|
|
}
|
2013-05-02 08:53:33 +02:00
|
|
|
|
|
|
|
private void applyTheme() {
|
|
|
|
String theme = Util.getTheme(this);
|
|
|
|
|
2013-05-05 03:44:39 +02:00
|
|
|
// Support the old fullscreen themes as well, for upgrade purposes
|
2013-05-02 08:53:33 +02:00
|
|
|
if ("dark".equalsIgnoreCase(theme) || "fullscreen".equalsIgnoreCase(theme)) {
|
|
|
|
setTheme(R.style.UltraSonicTheme);
|
|
|
|
} else if ("light".equalsIgnoreCase(theme) || "fullscreenlight".equalsIgnoreCase(theme)) {
|
|
|
|
setTheme(R.style.UltraSonicTheme_Light);
|
|
|
|
}
|
|
|
|
}
|
2013-05-05 03:44:39 +02:00
|
|
|
|
2013-02-11 05:30:46 +01:00
|
|
|
@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;
|
|
|
|
}
|
2013-05-02 08:53:33 +02:00
|
|
|
|
2013-02-11 05:30:46 +01:00
|
|
|
theme.setSummary(theme.getEntry());
|
|
|
|
maxBitrateWifi.setSummary(maxBitrateWifi.getEntry());
|
|
|
|
maxBitrateMobile.setSummary(maxBitrateMobile.getEntry());
|
|
|
|
cacheSize.setSummary(cacheSize.getEntry());
|
|
|
|
cacheLocation.setSummary(cacheLocation.getText());
|
|
|
|
preloadCount.setSummary(preloadCount.getEntry());
|
|
|
|
bufferLength.setSummary(bufferLength.getEntry());
|
2013-05-17 09:26:06 +02:00
|
|
|
incrementTime.setSummary(incrementTime.getEntry());
|
2013-02-11 05:30:46 +01:00
|
|
|
networkTimeout.setSummary(networkTimeout.getEntry());
|
|
|
|
maxAlbums.setSummary(maxAlbums.getEntry());
|
|
|
|
maxArtists.setSummary(maxArtists.getEntry());
|
|
|
|
maxSongs.setSummary(maxSongs.getEntry());
|
|
|
|
defaultAlbums.setSummary(defaultAlbums.getEntry());
|
|
|
|
defaultArtists.setSummary(defaultArtists.getEntry());
|
|
|
|
defaultSongs.setSummary(defaultSongs.getEntry());
|
2013-05-24 10:16:48 +02:00
|
|
|
chatRefreshInterval.setSummary(chatRefreshInterval.getEntry());
|
2013-05-26 08:06:10 +02:00
|
|
|
directoryCacheTime.setSummary(directoryCacheTime.getEntry());
|
2013-02-11 05:30:46 +01:00
|
|
|
|
|
|
|
if (!mediaButtonsEnabled.isChecked()) {
|
|
|
|
lockScreenEnabled.setChecked(false);
|
|
|
|
lockScreenEnabled.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ServerSettings ss : serverSettings.values()) {
|
|
|
|
ss.update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setHideMedia(boolean hide) {
|
2013-04-06 21:47:24 +02:00
|
|
|
File nomediaDir = new File(FileUtil.getUltraSonicDirectory(), ".nomedia");
|
2013-02-11 05:30:46 +01:00
|
|
|
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) {
|
|
|
|
lockScreenEnabled.setEnabled(true);
|
|
|
|
Util.registerMediaButtonEventReceiver(this);
|
|
|
|
} else {
|
|
|
|
lockScreenEnabled.setEnabled(false);
|
|
|
|
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;
|
2013-05-05 03:44:39 +02:00
|
|
|
private CheckBoxPreference enabled;
|
2013-02-11 05:30:46 +01:00
|
|
|
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);
|
2013-05-05 03:44:39 +02:00
|
|
|
enabled = (CheckBoxPreference) findPreference(Constants.PREFERENCES_KEY_SERVER_ENABLED + instance);
|
2013-02-11 05:30:46 +01:00
|
|
|
|
|
|
|
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() {
|
2013-05-05 03:44:39 +02:00
|
|
|
serverName.setSummary(serverName.getText());
|
|
|
|
serverUrl.setSummary(serverUrl.getText());
|
|
|
|
username.setSummary(username.getText());
|
|
|
|
screen.setSummary(serverUrl.getText());
|
|
|
|
screen.setTitle(serverName.getText());
|
2013-02-11 05:30:46 +01:00
|
|
|
}
|
|
|
|
}
|
2013-05-02 08:53:33 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
menuDrawer.toggleMenu();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onRestoreInstanceState(Bundle inState) {
|
|
|
|
super.onRestoreInstanceState(inState);
|
|
|
|
menuDrawer.restoreState(inState.getParcelable(STATE_MENUDRAWER));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
outState.putParcelable(STATE_MENUDRAWER, menuDrawer.saveState());
|
|
|
|
outState.putInt(STATE_ACTIVE_VIEW_ID, menuActiveViewId);
|
|
|
|
outState.putInt(STATE_ACTIVE_POSITION, activePosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
final int drawerState = menuDrawer.getDrawerState();
|
|
|
|
|
|
|
|
if (drawerState == MenuDrawer.STATE_OPEN || drawerState == MenuDrawer.STATE_OPENING) {
|
|
|
|
menuDrawer.closeMenu();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
super.onBackPressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
menuActiveViewId = v.getId();
|
|
|
|
|
|
|
|
Intent intent;
|
|
|
|
|
|
|
|
switch (menuActiveViewId) {
|
|
|
|
case R.id.menu_home:
|
|
|
|
intent = new Intent(this, MainActivity.class);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
Util.startActivityWithoutTransition(this, intent);
|
|
|
|
break;
|
|
|
|
case R.id.menu_browse:
|
|
|
|
intent = new Intent(this, SelectArtistActivity.class);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
Util.startActivityWithoutTransition(this, intent);
|
|
|
|
break;
|
|
|
|
case R.id.menu_search:
|
|
|
|
intent = new Intent(this, SearchActivity.class);
|
|
|
|
intent.putExtra(Constants.INTENT_EXTRA_REQUEST_SEARCH, true);
|
|
|
|
Util.startActivityWithoutTransition(this, intent);
|
|
|
|
break;
|
|
|
|
case R.id.menu_playlists:
|
|
|
|
intent = new Intent(this, SelectPlaylistActivity.class);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
Util.startActivityWithoutTransition(this, intent);
|
|
|
|
break;
|
2013-05-22 07:47:40 +02:00
|
|
|
case R.id.menu_chat:
|
|
|
|
Util.startActivityWithoutTransition(this, ChatActivity.class);
|
|
|
|
break;
|
2013-05-02 08:53:33 +02:00
|
|
|
case R.id.menu_now_playing:
|
|
|
|
Util.startActivityWithoutTransition(this, DownloadActivity.class);
|
|
|
|
break;
|
|
|
|
case R.id.menu_settings:
|
|
|
|
Util.startActivityWithoutTransition(this, SettingsActivity.class);
|
|
|
|
break;
|
|
|
|
case R.id.menu_about:
|
|
|
|
Util.startActivityWithoutTransition(this, HelpActivity.class);
|
|
|
|
break;
|
|
|
|
case R.id.menu_exit:
|
|
|
|
intent = new Intent(this, MainActivity.class);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
intent.putExtra(Constants.INTENT_EXTRA_NAME_EXIT, true);
|
|
|
|
Util.startActivityWithoutTransition(this, intent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
menuDrawer.closeMenu();
|
|
|
|
}
|
2012-02-26 21:25:13 +01:00
|
|
|
}
|