mirror of
https://github.com/AntennaPod/AntennaPod.git
synced 2025-02-06 05:23:30 +01:00
use full URL for custom gpodder server (#4932)
This commit is contained in:
parent
1cd680a2af
commit
519d6ce9d6
@ -18,7 +18,7 @@ public class GpodnetPodcastSearcher implements PodcastSearcher {
|
||||
return Single.create((SingleOnSubscribe<List<PodcastSearchResult>>) subscriber -> {
|
||||
try {
|
||||
GpodnetService service = new GpodnetService(AntennapodHttpClient.getHttpClient(),
|
||||
GpodnetPreferences.getHostname());
|
||||
GpodnetPreferences.getHosturl());
|
||||
List<GpodnetPodcast> gpodnetPodcasts = service.searchPodcasts(query, 0);
|
||||
List<PodcastSearchResult> results = new ArrayList<>();
|
||||
for (GpodnetPodcast podcast : gpodnetPodcasts) {
|
||||
|
@ -71,7 +71,7 @@ public abstract class PodcastListFragment extends Fragment {
|
||||
protected List<GpodnetPodcast> doInBackground(Void... params) {
|
||||
try {
|
||||
GpodnetService service = new GpodnetService(AntennapodHttpClient.getHttpClient(),
|
||||
GpodnetPreferences.getHostname());
|
||||
GpodnetPreferences.getHosturl());
|
||||
return loadPodcastData(service);
|
||||
} catch (GpodnetServiceException e) {
|
||||
exception = e;
|
||||
|
@ -54,7 +54,7 @@ public class TagListFragment extends ListFragment {
|
||||
@Override
|
||||
protected List<GpodnetTag> doInBackground(Void... params) {
|
||||
GpodnetService service = new GpodnetService(AntennapodHttpClient.getHttpClient(),
|
||||
GpodnetPreferences.getHostname());
|
||||
GpodnetPreferences.getHosturl());
|
||||
try {
|
||||
return service.getTopTags(COUNT);
|
||||
} catch (GpodnetServiceException e) {
|
||||
|
@ -82,8 +82,9 @@ public class GpodderAuthenticationFragment extends DialogFragment {
|
||||
final Button selectHost = view.findViewById(R.id.chooseHostButton);
|
||||
final RadioGroup serverRadioGroup = view.findViewById(R.id.serverRadioGroup);
|
||||
final EditText serverUrlText = view.findViewById(R.id.serverUrlText);
|
||||
if (!GpodnetService.DEFAULT_BASE_HOST.equals(GpodnetPreferences.getHostname())) {
|
||||
serverUrlText.setText(GpodnetPreferences.getHostname());
|
||||
|
||||
if (!GpodnetService.DEFAULT_BASE_HOST.equals(GpodnetPreferences.getHosturl())) {
|
||||
serverUrlText.setText(GpodnetPreferences.getHosturl());
|
||||
}
|
||||
final TextInputLayout serverUrlTextInput = view.findViewById(R.id.serverUrlTextInput);
|
||||
serverRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
|
||||
@ -91,12 +92,12 @@ public class GpodderAuthenticationFragment extends DialogFragment {
|
||||
});
|
||||
selectHost.setOnClickListener(v -> {
|
||||
if (serverRadioGroup.getCheckedRadioButtonId() == R.id.customServerRadio) {
|
||||
GpodnetPreferences.setHostname(serverUrlText.getText().toString());
|
||||
GpodnetPreferences.setHosturl(serverUrlText.getText().toString());
|
||||
} else {
|
||||
GpodnetPreferences.setHostname(GpodnetService.DEFAULT_BASE_HOST);
|
||||
GpodnetPreferences.setHosturl(GpodnetService.DEFAULT_BASE_HOST);
|
||||
}
|
||||
service = new GpodnetService(AntennapodHttpClient.getHttpClient(), GpodnetPreferences.getHostname());
|
||||
getDialog().setTitle(GpodnetPreferences.getHostname());
|
||||
service = new GpodnetService(AntennapodHttpClient.getHttpClient(), GpodnetPreferences.getHosturl());
|
||||
getDialog().setTitle(GpodnetPreferences.getHosturl());
|
||||
advance();
|
||||
});
|
||||
}
|
||||
@ -108,10 +109,14 @@ public class GpodderAuthenticationFragment extends DialogFragment {
|
||||
final TextView txtvError = view.findViewById(R.id.credentialsError);
|
||||
final ProgressBar progressBar = view.findViewById(R.id.progBarLogin);
|
||||
final TextView createAccount = view.findViewById(R.id.createAccountButton);
|
||||
final TextView createAccountWarning = view.findViewById(R.id.createAccountWarning);
|
||||
|
||||
createAccount.setPaintFlags(createAccount.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
|
||||
createAccount.setOnClickListener(v -> IntentUtils.openInBrowser(getContext(), "https://gpodder.net/register/"));
|
||||
|
||||
if (GpodnetPreferences.getHosturl().startsWith("http://")) {
|
||||
createAccountWarning.setVisibility(View.VISIBLE);
|
||||
}
|
||||
password.setOnEditorActionListener((v, actionID, event) ->
|
||||
actionID == EditorInfo.IME_ACTION_GO && login.performClick());
|
||||
|
||||
|
@ -29,6 +29,15 @@
|
||||
android:text="@string/create_account"/>
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/createAccountWarning"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/gpodnetauth_encryption_warning"
|
||||
android:textColor="#F44336"
|
||||
android:textStyle="bold"
|
||||
android:visibility="invisible" />
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
|
||||
android:layout_width="match_parent"
|
||||
@ -94,4 +103,4 @@
|
||||
android:text="@string/gpodnetauth_login_butLabel"/>
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
@ -26,7 +26,7 @@ public class GpodnetPreferences {
|
||||
private static String username;
|
||||
private static String password;
|
||||
private static String deviceID;
|
||||
private static String hostname;
|
||||
private static String hosturl;
|
||||
|
||||
private static boolean preferencesLoaded = false;
|
||||
|
||||
@ -40,7 +40,7 @@ public class GpodnetPreferences {
|
||||
username = prefs.getString(PREF_GPODNET_USERNAME, null);
|
||||
password = prefs.getString(PREF_GPODNET_PASSWORD, null);
|
||||
deviceID = prefs.getString(PREF_GPODNET_DEVICEID, null);
|
||||
hostname = checkGpodnetHostname(prefs.getString(PREF_GPODNET_HOSTNAME, GpodnetService.DEFAULT_BASE_HOST));
|
||||
hosturl = prefs.getString(PREF_GPODNET_HOSTNAME, GpodnetService.DEFAULT_BASE_HOST);
|
||||
|
||||
preferencesLoaded = true;
|
||||
}
|
||||
@ -82,17 +82,16 @@ public class GpodnetPreferences {
|
||||
writePreference(PREF_GPODNET_DEVICEID, deviceID);
|
||||
}
|
||||
|
||||
public static String getHostname() {
|
||||
public static String getHosturl() {
|
||||
ensurePreferencesLoaded();
|
||||
return hostname;
|
||||
return hosturl;
|
||||
}
|
||||
|
||||
public static void setHostname(String value) {
|
||||
value = checkGpodnetHostname(value);
|
||||
if (!value.equals(hostname)) {
|
||||
public static void setHosturl(String value) {
|
||||
if (!value.equals(hosturl)) {
|
||||
logout();
|
||||
writePreference(PREF_GPODNET_HOSTNAME, value);
|
||||
hostname = value;
|
||||
hosturl = value;
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,13 +112,4 @@ public class GpodnetPreferences {
|
||||
UserPreferences.setGpodnetNotificationsEnabled();
|
||||
}
|
||||
|
||||
private static String checkGpodnetHostname(String value) {
|
||||
int startIndex = 0;
|
||||
if (value.startsWith("http://")) {
|
||||
startIndex = "http://".length();
|
||||
} else if (value.startsWith("https://")) {
|
||||
startIndex = "https://".length();
|
||||
}
|
||||
return value.substring(startIndex);
|
||||
}
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ public class SyncService extends Worker {
|
||||
if (!GpodnetPreferences.loggedIn()) {
|
||||
return Result.success();
|
||||
}
|
||||
syncServiceImpl = new GpodnetService(AntennapodHttpClient.getHttpClient(), GpodnetPreferences.getHostname());
|
||||
syncServiceImpl = new GpodnetService(AntennapodHttpClient.getHttpClient(), GpodnetPreferences.getHosturl());
|
||||
SharedPreferences.Editor prefs = getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
.edit();
|
||||
prefs.putLong(PREF_LAST_SYNC_ATTEMPT_TIMESTAMP, System.currentTimeMillis()).apply();
|
||||
|
@ -42,24 +42,58 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Communicates with the gpodder.net service.
|
||||
*/
|
||||
public class GpodnetService implements ISyncService {
|
||||
public static final String TAG = "GpodnetService";
|
||||
public static final String DEFAULT_BASE_HOST = "gpodder.net";
|
||||
private static final String BASE_SCHEME = "https";
|
||||
private static final int PORT = 443;
|
||||
private static final int UPLOAD_BULK_SIZE = 30;
|
||||
private static final MediaType TEXT = MediaType.parse("plain/text; charset=utf-8");
|
||||
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
private final String baseHost;
|
||||
private String baseScheme;
|
||||
private String baseHost;
|
||||
private int basePort;
|
||||
|
||||
private final OkHttpClient httpClient;
|
||||
private String username = null;
|
||||
|
||||
public GpodnetService(OkHttpClient httpClient, String baseHost) {
|
||||
// split into schema, host and port - missing parts are null
|
||||
private static Pattern urlsplit_regex = Pattern.compile("(?:(https?)://)?([^:]+)(?::(\\d+))?");
|
||||
|
||||
public GpodnetService(OkHttpClient httpClient, String baseHosturl) {
|
||||
this.httpClient = httpClient;
|
||||
this.baseHost = baseHost;
|
||||
|
||||
Matcher m = urlsplit_regex.matcher(baseHosturl);
|
||||
if (m.matches()) {
|
||||
this.baseScheme = m.group(1);
|
||||
this.baseHost = m.group(2);
|
||||
if (m.group(3) == null) {
|
||||
this.basePort = -1;
|
||||
} else {
|
||||
this.basePort = Integer.parseInt(m.group(3)); // regex -> can only be digits
|
||||
}
|
||||
} else {
|
||||
// URL does not match regex: use it anyway -> this will cause an exception on connect
|
||||
this.baseScheme = "https";
|
||||
this.baseHost = baseHosturl;
|
||||
this.basePort = 443;
|
||||
}
|
||||
|
||||
if (this.baseScheme == null) { // assume https
|
||||
this.baseScheme = "https";
|
||||
}
|
||||
|
||||
if (this.baseScheme.equals("https") && this.basePort == -1) {
|
||||
this.basePort = 443;
|
||||
}
|
||||
|
||||
if (this.baseScheme.equals("http") && this.basePort == -1) {
|
||||
this.basePort = 80;
|
||||
}
|
||||
}
|
||||
|
||||
private void requireLoggedIn() {
|
||||
@ -74,7 +108,7 @@ public class GpodnetService implements ISyncService {
|
||||
public List<GpodnetTag> getTopTags(int count) throws GpodnetServiceException {
|
||||
URL url;
|
||||
try {
|
||||
url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format(Locale.US, "/api/2/tags/%d.json", count), null, null).toURL();
|
||||
} catch (MalformedURLException | URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
@ -108,7 +142,7 @@ public class GpodnetService implements ISyncService {
|
||||
public List<GpodnetPodcast> getPodcastsForTag(@NonNull GpodnetTag tag, int count)
|
||||
throws GpodnetServiceException {
|
||||
try {
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format(Locale.US, "/api/2/tag/%s/%d.json", tag.getTag(), count), null, null).toURL();
|
||||
Request.Builder request = new Request.Builder().url(url);
|
||||
String response = executeRequest(request);
|
||||
@ -134,7 +168,7 @@ public class GpodnetService implements ISyncService {
|
||||
}
|
||||
|
||||
try {
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format(Locale.US, "/toplist/%d.json", count), null, null).toURL();
|
||||
Request.Builder request = new Request.Builder().url(url);
|
||||
String response = executeRequest(request);
|
||||
@ -166,7 +200,7 @@ public class GpodnetService implements ISyncService {
|
||||
}
|
||||
|
||||
try {
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format(Locale.US, "/suggestions/%d.json", count), null, null).toURL();
|
||||
Request.Builder request = new Request.Builder().url(url);
|
||||
String response = executeRequest(request);
|
||||
@ -192,7 +226,7 @@ public class GpodnetService implements ISyncService {
|
||||
.format(Locale.US, "q=%s&scale_logo=%d", query, scaledLogoSize) : String
|
||||
.format("q=%s", query);
|
||||
try {
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT, "/search.json",
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort, "/search.json",
|
||||
parameters, null).toURL();
|
||||
Request.Builder request = new Request.Builder().url(url);
|
||||
String response = executeRequest(request);
|
||||
@ -219,7 +253,7 @@ public class GpodnetService implements ISyncService {
|
||||
public List<GpodnetDevice> getDevices() throws GpodnetServiceException {
|
||||
requireLoggedIn();
|
||||
try {
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format("/api/2/devices/%s.json", username), null, null).toURL();
|
||||
Request.Builder request = new Request.Builder().url(url);
|
||||
String response = executeRequest(request);
|
||||
@ -241,7 +275,7 @@ public class GpodnetService implements ISyncService {
|
||||
public List<List<String>> getSynchronizedDevices() throws GpodnetServiceException {
|
||||
requireLoggedIn();
|
||||
try {
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format("/api/2/sync-devices/%s.json", username), null, null).toURL();
|
||||
Request.Builder request = new Request.Builder().url(url);
|
||||
String response = executeRequest(request);
|
||||
@ -282,7 +316,7 @@ public class GpodnetService implements ISyncService {
|
||||
throws GpodnetServiceException {
|
||||
requireLoggedIn();
|
||||
try {
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format("/api/2/devices/%s/%s.json", username, deviceId), null, null).toURL();
|
||||
String content;
|
||||
if (caption != null || type != null) {
|
||||
@ -316,7 +350,7 @@ public class GpodnetService implements ISyncService {
|
||||
public void linkDevices(@NonNull List<String> deviceIds) throws GpodnetServiceException {
|
||||
requireLoggedIn();
|
||||
try {
|
||||
final URL url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
final URL url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format("/api/2/sync-devices/%s.json", username), null, null).toURL();
|
||||
JSONObject jsonContent = new JSONObject();
|
||||
JSONArray group = new JSONArray();
|
||||
@ -351,7 +385,7 @@ public class GpodnetService implements ISyncService {
|
||||
public String getSubscriptionsOfDevice(@NonNull String deviceId) throws GpodnetServiceException {
|
||||
requireLoggedIn();
|
||||
try {
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format("/subscriptions/%s/%s.opml", username, deviceId), null, null).toURL();
|
||||
Request.Builder request = new Request.Builder().url(url);
|
||||
return executeRequest(request);
|
||||
@ -373,7 +407,7 @@ public class GpodnetService implements ISyncService {
|
||||
public String getSubscriptionsOfUser() throws GpodnetServiceException {
|
||||
requireLoggedIn();
|
||||
try {
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format("/subscriptions/%s.opml", username), null, null).toURL();
|
||||
Request.Builder request = new Request.Builder().url(url);
|
||||
return executeRequest(request);
|
||||
@ -398,7 +432,7 @@ public class GpodnetService implements ISyncService {
|
||||
throws GpodnetServiceException {
|
||||
requireLoggedIn();
|
||||
try {
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format("/subscriptions/%s/%s.txt", username, deviceId), null, null).toURL();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (String s : subscriptions) {
|
||||
@ -432,7 +466,7 @@ public class GpodnetService implements ISyncService {
|
||||
@NonNull Collection<String> removed) throws GpodnetServiceException {
|
||||
requireLoggedIn();
|
||||
try {
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format("/api/2/subscriptions/%s/%s.json", username, deviceId), null, null).toURL();
|
||||
|
||||
final JSONObject requestObject = new JSONObject();
|
||||
@ -468,7 +502,7 @@ public class GpodnetService implements ISyncService {
|
||||
String params = String.format(Locale.US, "since=%d", timestamp);
|
||||
String path = String.format("/api/2/subscriptions/%s/%s.json", username, deviceId);
|
||||
try {
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT, path, params, null).toURL();
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort, path, params, null).toURL();
|
||||
Request.Builder request = new Request.Builder().url(url);
|
||||
|
||||
String response = executeRequest(request);
|
||||
@ -510,7 +544,7 @@ public class GpodnetService implements ISyncService {
|
||||
throws SyncServiceException {
|
||||
try {
|
||||
Log.d(TAG, "Uploading partial actions " + from + " to " + to + " of " + episodeActions.size());
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format("/api/2/episodes/%s.json", username), null, null).toURL();
|
||||
|
||||
final JSONArray list = new JSONArray();
|
||||
@ -549,7 +583,7 @@ public class GpodnetService implements ISyncService {
|
||||
String params = String.format(Locale.US, "since=%d", timestamp);
|
||||
String path = String.format("/api/2/episodes/%s.json", username);
|
||||
try {
|
||||
URL url = new URI(BASE_SCHEME, null, baseHost, PORT, path, params, null).toURL();
|
||||
URL url = new URI(baseScheme, null, baseHost, basePort, path, params, null).toURL();
|
||||
Request.Builder request = new Request.Builder().url(url);
|
||||
|
||||
String response = executeRequest(request);
|
||||
@ -575,7 +609,7 @@ public class GpodnetService implements ISyncService {
|
||||
public void authenticate(@NonNull String username, @NonNull String password) throws GpodnetServiceException {
|
||||
URL url;
|
||||
try {
|
||||
url = new URI(BASE_SCHEME, null, baseHost, PORT,
|
||||
url = new URI(baseScheme, null, baseHost, basePort,
|
||||
String.format("/api/2/auth/%s/login.json", username), null, null).toURL();
|
||||
} catch (MalformedURLException | URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -654,6 +654,7 @@
|
||||
<string name="gpodnet_search_hint">Search gpodder.net</string>
|
||||
<string name="gpodnetauth_login_title">Login</string>
|
||||
<string name="gpodnetauth_login_butLabel">Login</string>
|
||||
<string name="gpodnetauth_encryption_warning">Password and data are not encrypted!</string>
|
||||
<string name="create_account">Create account</string>
|
||||
<string name="username_label">Username</string>
|
||||
<string name="password_label">Password</string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user