aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2013-12-27 18:16:13 -0800
committerScott Jackson <daneren2005@gmail.com>2013-12-27 18:16:13 -0800
commitb7d51aef6e9cd6ea034741e073ef1573b0ee96e8 (patch)
tree1e0ba7f51ecc831699ae02c62a89a6676b9376d0 /src
parentf012b3222b9341e8380edc79c05eba1b04c2d00a (diff)
downloaddsub-b7d51aef6e9cd6ea034741e073ef1573b0ee96e8.tar.gz
dsub-b7d51aef6e9cd6ea034741e073ef1573b0ee96e8.tar.bz2
dsub-b7d51aef6e9cd6ea034741e073ef1573b0ee96e8.zip
#199 Allow user to define a separate internal ip address
Diffstat (limited to 'src')
-rw-r--r--src/github/daneren2005/dsub/activity/SettingsActivity.java28
-rw-r--r--src/github/daneren2005/dsub/service/CachedMusicService.java10
-rw-r--r--src/github/daneren2005/dsub/service/RESTMusicService.java7
-rw-r--r--src/github/daneren2005/dsub/util/Constants.java1
-rw-r--r--src/github/daneren2005/dsub/util/Util.java68
5 files changed, 84 insertions, 30 deletions
diff --git a/src/github/daneren2005/dsub/activity/SettingsActivity.java b/src/github/daneren2005/dsub/activity/SettingsActivity.java
index 1807237c..c415d164 100644
--- a/src/github/daneren2005/dsub/activity/SettingsActivity.java
+++ b/src/github/daneren2005/dsub/activity/SettingsActivity.java
@@ -317,9 +317,15 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
}
serverUrlPreference.setSummary(serverUrlPreference.getText());
-
screen.setSummary(serverUrlPreference.getText());
+ final EditTextPreference serverInternalUrlPreference = new EditTextPreference(this);
+ serverInternalUrlPreference.setKey(Constants.PREFERENCES_KEY_SERVER_INTERNAL_URL + instance);
+ serverInternalUrlPreference.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_URI);
+ serverInternalUrlPreference.setDefaultValue("http://");
+ serverInternalUrlPreference.setTitle(R.string.settings_server_internal_address);
+ serverInternalUrlPreference.setSummary(serverInternalUrlPreference.getText());
+
final EditTextPreference serverUsernamePreference = new EditTextPreference(this);
serverUsernamePreference.setKey(Constants.PREFERENCES_KEY_USERNAME + instance);
serverUsernamePreference.setTitle(R.string.settings_server_username);
@@ -392,6 +398,7 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
screen.addPreference(serverNamePreference);
screen.addPreference(serverUrlPreference);
+ screen.addPreference(serverInternalUrlPreference);
screen.addPreference(serverUsernamePreference);
screen.addPreference(serverPasswordPreference);
screen.addPreference(serverRemoveServerPreference);
@@ -522,6 +529,7 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
private class ServerSettings {
private EditTextPreference serverName;
private EditTextPreference serverUrl;
+ private EditTextPreference serverInternalUrl;
private EditTextPreference username;
private PreferenceScreen screen;
@@ -530,6 +538,7 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
screen = (PreferenceScreen) findPreference("server" + instance);
serverName = (EditTextPreference) findPreference(Constants.PREFERENCES_KEY_SERVER_NAME + instance);
serverUrl = (EditTextPreference) findPreference(Constants.PREFERENCES_KEY_SERVER_URL + instance);
+ serverInternalUrl = (EditTextPreference) findPreference(Constants.PREFERENCES_KEY_SERVER_INTERNAL_URL + instance);
username = (EditTextPreference) findPreference(Constants.PREFERENCES_KEY_USERNAME + instance);
serverUrl.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@@ -548,6 +557,22 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
return true;
}
});
+ serverInternalUrl.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object value) {
+ try {
+ String url = (String) value;
+ new URL(url);
+ if (url.contains(" ") || url.contains("@") || 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
@@ -565,6 +590,7 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
public void update() {
serverName.setSummary(serverName.getText());
serverUrl.setSummary(serverUrl.getText());
+ serverInternalUrl.setSummary(serverInternalUrl.getText());
username.setSummary(username.getText());
screen.setSummary(serverUrl.getText());
screen.setTitle(serverName.getText());
diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java
index dfd2239e..221ea03c 100644
--- a/src/github/daneren2005/dsub/service/CachedMusicService.java
+++ b/src/github/daneren2005/dsub/service/CachedMusicService.java
@@ -28,6 +28,7 @@ import org.apache.http.HttpResponse;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
+import android.util.Log;
import github.daneren2005.dsub.domain.Bookmark;
import github.daneren2005.dsub.domain.ChatMessage;
@@ -53,11 +54,12 @@ import github.daneren2005.dsub.util.Util;
* @author Sindre Mehus
*/
public class CachedMusicService implements MusicService {
+ private static final String TAG = CachedMusicService.class.getSimpleName();
private static final int MUSIC_DIR_CACHE_SIZE = 20;
private static final int TTL_MUSIC_DIR = 5 * 60; // Five minutes
- private final RESTMusicService musicService;
+ private final RESTMusicService musicService;
private final LruCache<String, TimeLimitedCache<MusicDirectory>> cachedMusicDirectories;
private final TimeLimitedCache<Boolean> cachedLicenseValid = new TimeLimitedCache<Boolean>(120, TimeUnit.SECONDS);
private final TimeLimitedCache<Indexes> cachedIndexes = new TimeLimitedCache<Indexes>(60 * 60, TimeUnit.SECONDS);
@@ -465,16 +467,16 @@ public class CachedMusicService implements MusicService {
}
private String getCacheName(Context context, String name, String id) {
- String s = Util.getRestUrl(context, null) + id;
+ String s = musicService.getRestUrl(context, null, false) + id;
return name + "-" + s.hashCode() + ".ser";
}
private String getCacheName(Context context, String name) {
- String s = Util.getRestUrl(context, null);
+ String s = musicService.getRestUrl(context, null, false);
return name + "-" + s.hashCode() + ".ser";
}
private void checkSettingsChanged(Context context) {
- String newUrl = musicService.getRestUrl(context, null);
+ String newUrl = musicService.getRestUrl(context, null, false);
if (!Util.equals(newUrl, restUrl)) {
cachedMusicFolders.clear();
cachedMusicDirectories.evictAll();
diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java
index 348f3c1c..febabc69 100644
--- a/src/github/daneren2005/dsub/service/RESTMusicService.java
+++ b/src/github/daneren2005/dsub/service/RESTMusicService.java
@@ -1363,10 +1363,13 @@ public class RESTMusicService implements MusicService {
}
public String getRestUrl(Context context, String method) {
+ return getRestUrl(context, method, true);
+ }
+ public String getRestUrl(Context context, String method, boolean allowAltAddress) {
if(instance == null) {
- return Util.getRestUrl(context, method);
+ return Util.getRestUrl(context, method, allowAltAddress);
} else {
- return Util.getRestUrl(context, method, instance);
+ return Util.getRestUrl(context, method, instance, allowAltAddress);
}
}
}
diff --git a/src/github/daneren2005/dsub/util/Constants.java b/src/github/daneren2005/dsub/util/Constants.java
index b4acb2e5..4383a964 100644
--- a/src/github/daneren2005/dsub/util/Constants.java
+++ b/src/github/daneren2005/dsub/util/Constants.java
@@ -75,6 +75,7 @@ public final class Constants {
public static final String PREFERENCES_KEY_SERVER_INSTANCE = "serverInstanceId";
public static final String PREFERENCES_KEY_SERVER_NAME = "serverName";
public static final String PREFERENCES_KEY_SERVER_URL = "serverUrl";
+ public static final String PREFERENCES_KEY_SERVER_INTERNAL_URL = "serverInternalUrl";
public static final String PREFERENCES_KEY_SERVER_VERSION = "serverVersion";
public static final String PREFERENCES_KEY_TEST_CONNECTION = "serverTestConnection";
public static final String PREFERENCES_KEY_OPEN_BROWSER = "openBrowser";
diff --git a/src/github/daneren2005/dsub/util/Util.java b/src/github/daneren2005/dsub/util/Util.java
index ff538f93..b0a2c3b1 100644
--- a/src/github/daneren2005/dsub/util/Util.java
+++ b/src/github/daneren2005/dsub/util/Util.java
@@ -336,36 +336,52 @@ public final class Util {
}
public static String getRestUrl(Context context, String method) {
- SharedPreferences prefs = getPreferences(context);
- int instance = prefs.getInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1);
- return getRestUrl(context, method, prefs, instance);
+ return getRestUrl(context, method, true);
}
+ public static String getRestUrl(Context context, String method, boolean allowAltAddress) {
+ SharedPreferences prefs = getPreferences(context);
+ int instance = prefs.getInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1);
+ return getRestUrl(context, method, prefs, instance, allowAltAddress);
+ }
public static String getRestUrl(Context context, String method, int instance) {
- SharedPreferences prefs = getPreferences(context);
- return getRestUrl(context, method, prefs, instance);
+ return getRestUrl(context, method, instance, true);
}
+ public static String getRestUrl(Context context, String method, int instance, boolean allowAltAddress) {
+ SharedPreferences prefs = getPreferences(context);
+ return getRestUrl(context, method, prefs, instance, allowAltAddress);
+ }
public static String getRestUrl(Context context, String method, SharedPreferences prefs, int instance) {
- StringBuilder builder = new StringBuilder();
-
- String serverUrl = prefs.getString(Constants.PREFERENCES_KEY_SERVER_URL + instance, null);
- String username = prefs.getString(Constants.PREFERENCES_KEY_USERNAME + instance, null);
- String password = prefs.getString(Constants.PREFERENCES_KEY_PASSWORD + instance, null);
+ return getRestUrl(context, method, prefs, instance, true);
+ }
+ public static String getRestUrl(Context context, String method, SharedPreferences prefs, int instance, boolean allowAltAddress) {
+ StringBuilder builder = new StringBuilder();
+
+ String serverUrl = prefs.getString(Constants.PREFERENCES_KEY_SERVER_URL + instance, null);
+ if(allowAltAddress && Util.isWifiConnected(context)) {
+ String internalUrl = prefs.getString(Constants.PREFERENCES_KEY_SERVER_INTERNAL_URL + instance, null);
+ if(internalUrl != null && !"".equals(internalUrl) && !"http://".equals(internalUrl)) {
+ serverUrl = internalUrl;
+ }
+ }
- // Slightly obfuscate password
- password = "enc:" + Util.utf8HexEncode(password);
+ String username = prefs.getString(Constants.PREFERENCES_KEY_USERNAME + instance, null);
+ String password = prefs.getString(Constants.PREFERENCES_KEY_PASSWORD + instance, null);
- builder.append(serverUrl);
- if (builder.charAt(builder.length() - 1) != '/') {
- builder.append("/");
- }
- builder.append("rest/").append(method).append(".view");
- builder.append("?u=").append(username);
- builder.append("&p=").append(password);
- builder.append("&v=").append(Constants.REST_PROTOCOL_VERSION);
- builder.append("&c=").append(Constants.REST_CLIENT_ID);
+ // Slightly obfuscate password
+ password = "enc:" + Util.utf8HexEncode(password);
- return builder.toString();
- }
+ builder.append(serverUrl);
+ if (builder.charAt(builder.length() - 1) != '/') {
+ builder.append("/");
+ }
+ builder.append("rest/").append(method).append(".view");
+ builder.append("?u=").append(username);
+ builder.append("&p=").append(password);
+ builder.append("&v=").append(Constants.REST_PROTOCOL_VERSION);
+ builder.append("&c=").append(Constants.REST_CLIENT_ID);
+
+ return builder.toString();
+ }
public static String getVideoPlayerType(Context context) {
SharedPreferences prefs = getPreferences(context);
@@ -788,6 +804,12 @@ public final class Util {
return connected && (!wifiRequired || wifiConnected);
}
+ public static boolean isWifiConnected(Context context) {
+ ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
+ NetworkInfo networkInfo = manager.getActiveNetworkInfo();
+ boolean connected = networkInfo != null && networkInfo.isConnected();
+ return connected && (networkInfo.getType() == ConnectivityManager.TYPE_WIFI);
+ }
public static boolean isExternalStoragePresent() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());