diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java index c1e19c5..79eb4ef 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java @@ -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 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 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 diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java b/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java index 429524d..86522c2 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java @@ -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 = ""; + 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() + diff --git a/app/src/main/java/pl/jakubweg/requests/SBRequester.java b/app/src/main/java/pl/jakubweg/requests/SBRequester.java index 5e939aa..77be145 100644 --- a/app/src/main/java/pl/jakubweg/requests/SBRequester.java +++ b/app/src/main/java/pl/jakubweg/requests/SBRequester.java @@ -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 { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ce3a83c..879bbea 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -157,6 +157,8 @@ 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 Import/Export settings 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. + Change API URL + The address SponsorBlock uses to make calls to the server. <b>Don\'t change this unless you know what you\'re doing.</b> Settings were successfully imported Failed to import settings Failed to export settings @@ -350,6 +352,10 @@ Segments SB Browser + API URL changed + API URL reset + Provided API URL is invalid + Video ad settings Video ad whitelisting Video ad whitelisting is turned off