add support for changing the API URL

This commit is contained in:
caneleex 2022-01-28 22:38:43 +01:00
parent 4c7f21f5a9
commit b809ac6111
4 changed files with 88 additions and 2 deletions

View File

@ -1,7 +1,9 @@
package pl.jakubweg;
import static fi.razerman.youtube.XGlobals.debug;
import static pl.jakubweg.SponsorBlockSettings.DEFAULT_API_URL;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_API_URL;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_BROWSER_BUTTON;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_COUNT_SKIPS;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_MIN_DURATION;
@ -25,6 +27,7 @@ import static pl.jakubweg.StringRef.str;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
@ -35,9 +38,14 @@ import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.text.Editable;
import android.text.Html;
import android.text.InputType;
import android.util.Patterns;
import android.widget.EditText;
import android.widget.Toast;
import java.lang.ref.WeakReference;
import java.text.DecimalFormat;
import java.util.ArrayList;
@ -50,6 +58,7 @@ import pl.jakubweg.requests.SBRequester;
public class SponsorBlockPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final DecimalFormat FORMATTER = new DecimalFormat("#,###,###");
public static final String SAVED_TEMPLATE = "%dh %.1f %s";
private static final APIURLChangeListener API_URL_CHANGE_LISTENER = new APIURLChangeListener();
private final ArrayList<Preference> preferencesToDisableWhenSBDisabled = new ArrayList<>();
@Override
@ -347,6 +356,73 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
screen.addPreference(preference);
preferencesToDisableWhenSBDisabled.add(preference);
}
{
Preference preference = new Preference(context);
String title = str("general_api_url");
preference.setTitle(title);
preference.setSummary(Html.fromHtml(str("general_api_url_sum")));
preference.setOnPreferenceClickListener(preference1 -> {
EditText editText = new EditText(context);
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
editText.setText(SponsorBlockSettings.apiUrl);
API_URL_CHANGE_LISTENER.setEditTextRef(editText);
new AlertDialog.Builder(context)
.setTitle(title)
.setView(editText)
.setNegativeButton(android.R.string.cancel, null)
.setNeutralButton(str("reset"), API_URL_CHANGE_LISTENER)
.setPositiveButton(android.R.string.ok, API_URL_CHANGE_LISTENER)
.show();
return true;
});
screen.addPreference(preference);
preferencesToDisableWhenSBDisabled.add(preference);
}
}
private static class APIURLChangeListener implements DialogInterface.OnClickListener {
private WeakReference<EditText> editTextRef;
@Override
public void onClick(DialogInterface dialog, int which) {
EditText editText = editTextRef.get();
if (editText == null)
return;
Context context = ((AlertDialog) dialog).getContext();
Context applicationContext = context.getApplicationContext();
SharedPreferences preferences = SponsorBlockSettings.getPreferences(context);
switch (which) {
case DialogInterface.BUTTON_NEUTRAL:
preferences.edit().putString(PREFERENCES_KEY_API_URL, DEFAULT_API_URL).apply();
Toast.makeText(applicationContext, str("api_url_reset"), Toast.LENGTH_SHORT).show();
break;
case DialogInterface.BUTTON_POSITIVE:
Editable text = editText.getText();
Toast invalidToast = Toast.makeText(applicationContext, str("api_url_invalid"), Toast.LENGTH_SHORT);
if (text == null) {
invalidToast.show();
}
else {
String textAsString = text.toString();
if (textAsString.isEmpty() || !Patterns.WEB_URL.matcher(textAsString).matches()) {
invalidToast.show();
}
else {
preferences.edit().putString(PREFERENCES_KEY_API_URL, textAsString).apply();
Toast.makeText(applicationContext, str("api_url_changed"), Toast.LENGTH_SHORT).show();
}
}
break;
}
}
public void setEditTextRef(EditText editText) {
editTextRef = new WeakReference<>(editText);
}
}
@Override

View File

@ -33,8 +33,10 @@ public class SponsorBlockSettings {
public static final String PREFERENCES_KEY_SHOW_TIME_WITHOUT_SEGMENTS = "sb-length-without-segments";
public static final String PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX = "_color";
public static final String PREFERENCES_KEY_BROWSER_BUTTON = "sb-browser-button";
public static final String PREFERENCES_KEY_API_URL = "sb-api-url";
public static final SegmentBehaviour DefaultBehaviour = SegmentBehaviour.SKIP_AUTOMATICALLY;
public static final String DEFAULT_API_URL = "https://sponsor.ajay.app/api/";
public static boolean isSponsorBlockEnabled = false;
public static boolean seenGuidelinesPopup = false;
@ -46,6 +48,7 @@ public class SponsorBlockSettings {
public static int adjustNewSegmentMillis = 150;
public static float minDuration = 0f;
public static String uuid = "<invalid>";
public static String apiUrl = DEFAULT_API_URL;
public static String sponsorBlockUrlCategories = "[]";
public static int skippedSegments;
public static long skippedTime;
@ -144,6 +147,8 @@ public class SponsorBlockSettings {
countSkips = preferences.getBoolean(PREFERENCES_KEY_COUNT_SKIPS, countSkips);
showTimeWithoutSegments = preferences.getBoolean(PREFERENCES_KEY_SHOW_TIME_WITHOUT_SEGMENTS, showTimeWithoutSegments);
apiUrl = preferences.getString(PREFERENCES_KEY_API_URL, DEFAULT_API_URL);
uuid = preferences.getString(PREFERENCES_KEY_UUID, null);
if (uuid == null) {
uuid = (UUID.randomUUID().toString() +

View File

@ -31,7 +31,6 @@ import pl.jakubweg.objects.SponsorSegment;
import pl.jakubweg.objects.UserStats;
public class SBRequester {
private static final String SPONSORBLOCK_API_URL = "https://sponsor.ajay.app/api/";
private static final String TIME_TEMPLATE = "%.3f";
private SBRequester() {}
@ -201,7 +200,7 @@ public class SBRequester {
// helpers
private static HttpURLConnection getConnectionFromRoute(Route route, String... params) throws IOException {
return Requester.getConnectionFromRoute(SPONSORBLOCK_API_URL, route, params);
return Requester.getConnectionFromRoute(SponsorBlockSettings.apiUrl, route, params);
}
private static JSONObject getJSONObject(Route route, String... params) throws Exception {

View File

@ -157,6 +157,8 @@
<string name="general_uuid_sum">This should be kept private. This is like a password and should not be shared with anyone. If someone has this, they can impersonate you</string>
<string name="settings_ie">Import/Export settings</string>
<string name="settings_ie_sum">This is your entire configuration that is applicable in the desktop extension in JSON. This includes your userID, so be sure to share this wisely.</string>
<string name="general_api_url">Change API URL</string>
<string name="general_api_url_sum">The address SponsorBlock uses to make calls to the server. &lt;b>Don\'t change this unless you know what you\'re doing.&lt;/b></string>
<string name="settings_import_successful">Settings were successfully imported</string>
<string name="settings_import_failed">Failed to import settings</string>
<string name="settings_export_failed">Failed to export settings</string>
@ -350,6 +352,10 @@
<string name="action_segments">Segments</string>
<string name="action_browser">SB Browser</string>
<string name="api_url_changed">API URL changed</string>
<string name="api_url_reset">API URL reset</string>
<string name="api_url_invalid">Provided API URL is invalid</string>
<string name="vanced_video_ad_settings_title">Video ad settings</string>
<string name="vanced_videoadwhitelisting_title">Video ad whitelisting</string>
<string name="vanced_videoadwhitelisting_summary_off">Video ad whitelisting is turned off</string>