From b407ae48247ae40b89534fa2df3fa97599890c38 Mon Sep 17 00:00:00 2001 From: polymorphicshade Date: Sun, 2 Aug 2020 17:40:26 -0600 Subject: [PATCH] SponsorBlock: Made entering the API Url more user-friendly Added a custom preference to make the dialog easier to use (if a bit janky). Also did some related string refactoring. --- .../settings/ContentSettingsFragment.java | 44 ++++++++- .../custom/SponsorBlockApiUrlPreference.java | 99 +++++++++++++++++++ .../layout/dialog_sponsorblock_api_url.xml | 33 +++++++ .../dialog_sponsorblock_api_url_help.xml | 25 +++++ app/src/main/res/values/settings_keys.xml | 2 +- app/src/main/res/values/strings.xml | 5 + app/src/main/res/xml/content_settings.xml | 2 +- 7 files changed, 204 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/settings/custom/SponsorBlockApiUrlPreference.java create mode 100644 app/src/main/res/layout/dialog_sponsorblock_api_url.xml create mode 100644 app/src/main/res/layout/dialog_sponsorblock_api_url_help.xml diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java index 45cc84d2c..ec2a79aa1 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -10,6 +10,7 @@ import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; +import android.view.View; import android.widget.Toast; import androidx.annotation.NonNull; @@ -143,7 +144,8 @@ public class ContentSettingsFragment extends BasePreferenceFragment { Preference sponsorBlockWebsitePreference = findPreference(getString(R.string.sponsorblock_home_page)); sponsorBlockWebsitePreference.setOnPreferenceClickListener((Preference p) -> { - Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://sponsor.ajay.app/")); + Intent i = new Intent(Intent.ACTION_VIEW, + Uri.parse(getString(R.string.sponsorblock_homepage_url))); startActivity(i); return true; }); @@ -152,12 +154,46 @@ public class ContentSettingsFragment extends BasePreferenceFragment { findPreference(getString(R.string.sponsorblock_privacy)); sponsorBlockPrivacyPreference.setOnPreferenceClickListener((Preference p) -> { Intent i = new Intent(Intent.ACTION_VIEW, - Uri.parse( - "https://gist.github.com/ajayyy/aa9f8ded2b573d4f73a3ffa0ef74f796" - + "#requests-sent-to-the-server-while-using-the-extension")); + Uri.parse(getString(R.string.sponsorblock_privacy_policy_url))); startActivity(i); return true; }); + + Preference sponsorBlockApiUrlPreference = + findPreference(getString(R.string.sponsorblock_api_url)); + + // workaround to force dependency updates due to using a custom preference + sponsorBlockApiUrlPreference + .setOnPreferenceChangeListener((preference, newValue) -> { + findPreference(getString(R.string.sponsorblock_enable)) + .onDependencyChanged(preference, + newValue == null || newValue.equals("")); + findPreference(getString(R.string.sponsorblock_notifications)) + .onDependencyChanged(preference, + newValue == null || newValue.equals("")); + return true; + }); + } + + @Override + public void onViewCreated(final View view, @Nullable final Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + // workaround to force dependency updates due to using a custom preference + Preference sponsorBlockApiUrlPreference = + findPreference(getString(R.string.sponsorblock_api_url)); + String sponsorBlockApiUrlPreferenceValue = + getPreferenceManager() + .getSharedPreferences() + .getString(getString(R.string.sponsorblock_api_url), null); + findPreference(getString(R.string.sponsorblock_enable)) + .onDependencyChanged(sponsorBlockApiUrlPreference, + sponsorBlockApiUrlPreferenceValue == null + || sponsorBlockApiUrlPreferenceValue.equals("")); + findPreference(getString(R.string.sponsorblock_notifications)) + .onDependencyChanged(sponsorBlockApiUrlPreference, + sponsorBlockApiUrlPreferenceValue == null + || sponsorBlockApiUrlPreferenceValue.equals("")); } @Override diff --git a/app/src/main/java/org/schabi/newpipe/settings/custom/SponsorBlockApiUrlPreference.java b/app/src/main/java/org/schabi/newpipe/settings/custom/SponsorBlockApiUrlPreference.java new file mode 100644 index 000000000..e7bd64f5c --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/settings/custom/SponsorBlockApiUrlPreference.java @@ -0,0 +1,99 @@ +package org.schabi.newpipe.settings.custom; + +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.net.Uri; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.view.inputmethod.InputMethodManager; +import android.widget.EditText; + +import androidx.appcompat.app.AlertDialog; +import androidx.preference.Preference; + +import org.schabi.newpipe.R; + +public class SponsorBlockApiUrlPreference extends Preference { + public SponsorBlockApiUrlPreference(final Context context, final AttributeSet attrs, + final int defStyleAttr, final int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + public SponsorBlockApiUrlPreference(final Context context, final AttributeSet attrs, + final int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + public SponsorBlockApiUrlPreference(final Context context, final AttributeSet attrs) { + super(context, attrs); + } + + public SponsorBlockApiUrlPreference(final Context context) { + super(context); + } + + @Override + protected void onClick() { + super.onClick(); + + View alertDialogView = LayoutInflater.from(getContext()) + .inflate(R.layout.dialog_sponsorblock_api_url, null); + + EditText editText = alertDialogView.findViewById(R.id.api_url_edit); + editText.setText(getSharedPreferences().getString(getKey(), null)); + editText.setOnFocusChangeListener((v, hasFocus) -> editText.post(() -> { + InputMethodManager inputMethodManager = (InputMethodManager) getContext() + .getSystemService(Context.INPUT_METHOD_SERVICE); + inputMethodManager + .showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); + })); + editText.requestFocus(); + + alertDialogView.findViewById(R.id.icon_api_url_help) + .setOnClickListener(v -> { + Uri privacyPolicyUri = Uri.parse(getContext() + .getString(R.string.sponsorblock_privacy_policy_url)); + View helpDialogView = LayoutInflater.from(getContext()) + .inflate(R.layout.dialog_sponsorblock_api_url_help, null); + View privacyPolicyButton = helpDialogView + .findViewById(R.id.sponsorblock_privacy_policy_button); + privacyPolicyButton.setOnClickListener(v1 -> { + Intent i = new Intent(Intent.ACTION_VIEW, privacyPolicyUri); + getContext().startActivity(i); + }); + + new AlertDialog.Builder(getContext()) + .setView(helpDialogView) + .setPositiveButton("Use Official", (dialog, which) -> { + editText.setText(getContext() + .getString(R.string.sponsorblock_default_api_url)); + dialog.dismiss(); + }) + .setNeutralButton("Close", (dialog, which) -> dialog.dismiss()) + .create() + .show(); + }); + + AlertDialog alertDialog = + new AlertDialog.Builder(getContext()) + .setView(alertDialogView) + .setTitle(getContext().getString(R.string.sponsorblock_api_url_title)) + .setPositiveButton("OK", (dialog, which) -> { + String newValue = editText.getText().toString(); + SharedPreferences.Editor editor = + getPreferenceManager().getSharedPreferences().edit(); + editor.putString(getKey(), newValue); + editor.apply(); + + callChangeListener(newValue); + + dialog.dismiss(); + }) + .setNegativeButton("Cancel", (dialog, which) -> dialog.cancel()) + .create(); + + alertDialog.show(); + } +} diff --git a/app/src/main/res/layout/dialog_sponsorblock_api_url.xml b/app/src/main/res/layout/dialog_sponsorblock_api_url.xml new file mode 100644 index 000000000..a98f090b7 --- /dev/null +++ b/app/src/main/res/layout/dialog_sponsorblock_api_url.xml @@ -0,0 +1,33 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_sponsorblock_api_url_help.xml b/app/src/main/res/layout/dialog_sponsorblock_api_url_help.xml new file mode 100644 index 000000000..3d91c84da --- /dev/null +++ b/app/src/main/res/layout/dialog_sponsorblock_api_url_help.xml @@ -0,0 +1,25 @@ + + + +