0
0
Fork 0
mirror of https://github.com/YTVanced/Integrations synced 2024-10-31 16:32:39 +00:00
VancedIntegrations/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java

347 lines
16 KiB
Java
Raw Normal View History

2020-08-24 15:47:57 +00:00
package pl.jakubweg;
2022-01-25 21:28:04 +00:00
import static fi.razerman.youtube.XGlobals.debug;
2021-07-30 17:30:00 +00:00
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_COUNT_SKIPS;
2022-01-24 16:22:21 +00:00
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_MIN_DURATION;
2021-07-30 17:30:00 +00:00
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_NEW_SEGMENT_ENABLED;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_SHOW_TIME_WITHOUT_SEGMENTS;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED;
2022-01-25 21:28:04 +00:00
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_SPONSOR_BLOCK_HINT_SHOWN;
2021-07-30 17:30:00 +00:00
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_UUID;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_VOTING_ENABLED;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_NAME;
import static pl.jakubweg.SponsorBlockSettings.adjustNewSegmentMillis;
import static pl.jakubweg.SponsorBlockSettings.countSkips;
2022-01-24 16:22:21 +00:00
import static pl.jakubweg.SponsorBlockSettings.minDuration;
2021-07-30 17:30:00 +00:00
import static pl.jakubweg.SponsorBlockSettings.setSeenGuidelines;
import static pl.jakubweg.SponsorBlockSettings.showTimeWithoutSegments;
import static pl.jakubweg.SponsorBlockSettings.showToastWhenSkippedAutomatically;
import static pl.jakubweg.SponsorBlockSettings.uuid;
import static pl.jakubweg.StringRef.str;
2020-08-24 15:47:57 +00:00
import android.app.Activity;
import android.app.AlertDialog;
2020-08-24 15:47:57 +00:00
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.text.InputType;
import android.widget.Toast;
2021-07-19 16:54:26 +00:00
import java.text.DecimalFormat;
2020-08-24 15:47:57 +00:00
import java.util.ArrayList;
2022-01-17 22:38:25 +00:00
import fi.vanced.libraries.youtube.whitelisting.WhitelistType;
2022-01-25 21:28:04 +00:00
import fi.vanced.utils.SharedPrefUtils;
import pl.jakubweg.objects.EditTextListPreference;
2022-01-17 13:54:11 +00:00
import pl.jakubweg.requests.SBRequester;
2021-07-19 16:54:26 +00:00
@SuppressWarnings({"unused", "deprecation"}) // injected
2020-08-24 15:47:57 +00:00
public class SponsorBlockPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
2021-07-23 12:45:10 +00:00
public static final DecimalFormat FORMATTER = new DecimalFormat("#,###,###");
2021-07-30 17:30:00 +00:00
public static final String SAVED_TEMPLATE = "%dh %.1f %s";
2021-07-19 15:32:06 +00:00
private final ArrayList<Preference> preferencesToDisableWhenSBDisabled = new ArrayList<>();
2020-08-24 15:47:57 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(PREFERENCES_NAME);
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
final Activity context = this.getActivity();
2020-08-24 15:47:57 +00:00
PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(context);
setPreferenceScreen(preferenceScreen);
SponsorBlockSettings.update(context);
2020-08-24 15:47:57 +00:00
{
SwitchPreference preference = new SwitchPreference(context);
preferenceScreen.addPreference(preference);
preference.setKey(PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED);
preference.setDefaultValue(SponsorBlockSettings.isSponsorBlockEnabled);
preference.setChecked(SponsorBlockSettings.isSponsorBlockEnabled);
preference.setTitle(str("enable_sb"));
preference.setSummary(str("enable_sb_sum"));
2021-07-19 15:32:06 +00:00
preference.setOnPreferenceChangeListener((preference1, newValue) -> {
final boolean value = (Boolean) newValue;
enableCategoriesIfNeeded(value);
return true;
2020-08-24 15:47:57 +00:00
});
}
2022-01-25 21:28:04 +00:00
// Clear hint
if (debug) {
SwitchPreference preference = new SwitchPreference(context);
preferenceScreen.addPreference(preference);
preference.setKey(PREFERENCES_KEY_SPONSOR_BLOCK_HINT_SHOWN);
preference.setDefaultValue(false);
preference.setChecked(SharedPrefUtils.getBoolean(context, PREFERENCES_NAME, PREFERENCES_KEY_SPONSOR_BLOCK_HINT_SHOWN));
preference.setTitle("Hint debug");
preference.setSummary("Debug toggle for clearing the hint shown preference");
preference.setOnPreferenceChangeListener((pref, newValue) -> true);
}
2020-08-24 15:47:57 +00:00
{
SwitchPreference preference = new SwitchPreference(context);
preferenceScreen.addPreference(preference);
preference.setKey(PREFERENCES_KEY_NEW_SEGMENT_ENABLED);
preference.setDefaultValue(SponsorBlockSettings.isAddNewSegmentEnabled);
preference.setChecked(SponsorBlockSettings.isAddNewSegmentEnabled);
preference.setTitle(str("enable_segmadding"));
preference.setSummary(str("enable_segmadding_sum"));
2020-08-24 15:47:57 +00:00
preferencesToDisableWhenSBDisabled.add(preference);
2021-07-19 15:32:06 +00:00
preference.setOnPreferenceChangeListener((preference12, o) -> {
final boolean value = (Boolean) o;
if (value && !SponsorBlockSettings.seenGuidelinesPopup) {
new AlertDialog.Builder(preference12.getContext())
.setTitle(str("sb_guidelines_popup_title"))
.setMessage(str("sb_guidelines_popup_content"))
.setNegativeButton(str("sb_guidelines_popup_already_read"), null)
.setPositiveButton(str("sb_guidelines_popup_open"), (dialogInterface, i) -> openGuidelines())
.show();
}
2021-07-19 15:32:06 +00:00
return true;
});
2020-08-24 15:47:57 +00:00
}
2021-04-22 16:43:14 +00:00
{
SwitchPreference preference = new SwitchPreference(context);
preferenceScreen.addPreference(preference);
preference.setTitle(str("enable_voting"));
preference.setSummary(str("enable_voting_sum"));
preference.setKey(PREFERENCES_KEY_VOTING_ENABLED);
preference.setDefaultValue(SponsorBlockSettings.isVotingEnabled);
preference.setChecked(SponsorBlockSettings.isVotingEnabled);
preferencesToDisableWhenSBDisabled.add(preference);
}
2020-08-24 15:47:57 +00:00
addGeneralCategory(context, preferenceScreen);
addSegmentsCategory(context, preferenceScreen);
2021-07-19 16:54:26 +00:00
addStatsCategory(context, preferenceScreen);
2020-08-24 15:47:57 +00:00
addAboutCategory(context, preferenceScreen);
enableCategoriesIfNeeded(SponsorBlockSettings.isSponsorBlockEnabled);
}
private void openGuidelines() {
final Context context = getActivity();
setSeenGuidelines(context);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://github.com/ajayyy/SponsorBlock/wiki/Guidelines"));
context.startActivity(intent);
}
2020-08-24 15:47:57 +00:00
private void enableCategoriesIfNeeded(boolean enabled) {
for (Preference preference : preferencesToDisableWhenSBDisabled)
preference.setEnabled(enabled);
}
@Override
public void onDestroy() {
super.onDestroy();
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
private void addSegmentsCategory(Context context, PreferenceScreen screen) {
PreferenceCategory category = new PreferenceCategory(context);
screen.addPreference(category);
preferencesToDisableWhenSBDisabled.add(category);
category.setTitle(str("diff_segments"));
2020-08-24 15:47:57 +00:00
SponsorBlockSettings.SegmentBehaviour[] segmentBehaviours = SponsorBlockSettings.SegmentBehaviour.values();
String[] entries = new String[segmentBehaviours.length];
String[] entryValues = new String[segmentBehaviours.length];
for (int i = 0, segmentBehavioursLength = segmentBehaviours.length; i < segmentBehavioursLength; i++) {
SponsorBlockSettings.SegmentBehaviour behaviour = segmentBehaviours[i];
entries[i] = behaviour.name.toString();
2020-08-24 15:47:57 +00:00
entryValues[i] = behaviour.key;
}
2021-07-27 17:58:14 +00:00
SponsorBlockSettings.SegmentInfo[] categories = SponsorBlockSettings.SegmentInfo.valuesWithoutUnsubmitted();
for (SponsorBlockSettings.SegmentInfo segmentInfo : categories) {
EditTextListPreference preference = new EditTextListPreference(context);
2020-08-24 15:47:57 +00:00
preference.setTitle(segmentInfo.getTitleWithDot());
preference.setSummary(segmentInfo.description.toString());
2020-08-24 15:47:57 +00:00
preference.setKey(segmentInfo.key);
preference.setDefaultValue(segmentInfo.behaviour.key);
2020-08-24 15:47:57 +00:00
preference.setEntries(entries);
preference.setEntryValues(entryValues);
2021-04-16 17:43:03 +00:00
2020-08-24 15:47:57 +00:00
category.addPreference(preference);
}
2021-07-27 17:58:14 +00:00
Preference colorPreference = new Preference(context); // TODO remove this after the next major update
2021-07-27 17:58:14 +00:00
screen.addPreference(colorPreference);
colorPreference.setTitle(str("color_change"));
colorPreference.setSummary(str("color_change_sum"));
2021-07-27 17:58:14 +00:00
preferencesToDisableWhenSBDisabled.add(colorPreference);
2020-08-24 15:47:57 +00:00
}
2021-07-19 16:54:26 +00:00
private void addStatsCategory(Context context, PreferenceScreen screen) {
PreferenceCategory category = new PreferenceCategory(context);
screen.addPreference(category);
2021-07-19 19:51:26 +00:00
category.setTitle(str("stats"));
preferencesToDisableWhenSBDisabled.add(category);
2021-07-19 16:54:26 +00:00
{
Preference preference = new Preference(context);
2021-07-19 19:51:26 +00:00
category.addPreference(preference);
2021-07-23 12:45:10 +00:00
preference.setTitle(str("stats_loading"));
2021-07-19 16:54:26 +00:00
2022-01-17 13:54:11 +00:00
SBRequester.retrieveUserStats(category, preference);
2021-07-19 16:54:26 +00:00
}
}
2020-08-24 15:47:57 +00:00
private void addAboutCategory(Context context, PreferenceScreen screen) {
PreferenceCategory category = new PreferenceCategory(context);
screen.addPreference(category);
2020-10-15 12:32:34 +00:00
category.setTitle(str("about"));
2020-08-24 15:47:57 +00:00
{
Preference preference = new Preference(context);
screen.addPreference(preference);
preference.setTitle(str("about_api"));
preference.setSummary(str("about_api_sum"));
2021-07-19 15:32:06 +00:00
preference.setOnPreferenceClickListener(preference1 -> {
Intent i = new Intent(Intent.ACTION_VIEW);
2021-07-19 19:51:26 +00:00
i.setData(Uri.parse("https://sponsor.ajay.app"));
2021-07-19 15:32:06 +00:00
preference1.getContext().startActivity(i);
return false;
2020-08-24 15:47:57 +00:00
});
}
{
Preference preference = new Preference(context);
screen.addPreference(preference);
preference.setTitle(str("about_madeby"));
2020-08-24 15:47:57 +00:00
}
}
private void addGeneralCategory(final Context context, PreferenceScreen screen) {
final PreferenceCategory category = new PreferenceCategory(context);
preferencesToDisableWhenSBDisabled.add(category);
screen.addPreference(category);
category.setTitle(str("general"));
2020-08-24 15:47:57 +00:00
{
Preference preference = new Preference(context);
preference.setTitle(str("sb_guidelines_preference_title"));
preference.setSummary(str("sb_guidelines_preference_sum"));
2021-07-19 15:32:06 +00:00
preference.setOnPreferenceClickListener(preference1 -> {
openGuidelines();
return false;
});
screen.addPreference(preference);
}
2020-08-24 15:47:57 +00:00
{
Preference preference = new SwitchPreference(context);
preference.setTitle(str("general_skiptoast"));
preference.setSummary(str("general_skiptoast_sum"));
2020-08-24 15:47:57 +00:00
preference.setKey(PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP);
preference.setDefaultValue(showToastWhenSkippedAutomatically);
2021-07-19 15:32:06 +00:00
preference.setOnPreferenceClickListener(preference12 -> {
Toast.makeText(preference12.getContext(), str("skipped_sponsor"), Toast.LENGTH_SHORT).show();
return false;
2020-08-24 15:47:57 +00:00
});
preferencesToDisableWhenSBDisabled.add(preference);
screen.addPreference(preference);
}
{
Preference preference = new SwitchPreference(context);
preference.setTitle(str("general_skipcount"));
preference.setSummary(str("general_skipcount_sum"));
2020-08-24 15:47:57 +00:00
preference.setKey(PREFERENCES_KEY_COUNT_SKIPS);
preference.setDefaultValue(countSkips);
preferencesToDisableWhenSBDisabled.add(preference);
screen.addPreference(preference);
}
2021-07-23 15:10:56 +00:00
{
Preference preference = new SwitchPreference(context);
2021-07-23 15:13:17 +00:00
preference.setTitle(str("general_time_without_sb"));
2021-07-23 15:10:56 +00:00
preference.setSummary(str("general_time_without_sb_sum"));
preference.setKey(PREFERENCES_KEY_SHOW_TIME_WITHOUT_SEGMENTS);
preference.setDefaultValue(showTimeWithoutSegments);
preferencesToDisableWhenSBDisabled.add(preference);
screen.addPreference(preference);
}
2022-01-17 22:38:25 +00:00
{
Preference preference = new SwitchPreference(context);
preference.setTitle(str("general_whitelisting"));
preference.setSummary(str("general_whitelisting_sum"));
preference.setKey(WhitelistType.SPONSORBLOCK.getPreferenceEnabledName());
preferencesToDisableWhenSBDisabled.add(preference);
screen.addPreference(preference);
}
2020-08-24 15:47:57 +00:00
{
EditTextPreference preference = new EditTextPreference(context);
2022-01-24 16:22:21 +00:00
preference.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
preference.setTitle(str("general_adjusting"));
preference.setSummary(str("general_adjusting_sum"));
2020-08-24 15:47:57 +00:00
preference.setKey(PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP);
preference.setDefaultValue(String.valueOf(adjustNewSegmentMillis));
screen.addPreference(preference);
preferencesToDisableWhenSBDisabled.add(preference);
}
2022-01-24 16:22:21 +00:00
{
EditTextPreference preference = new EditTextPreference(context);
preference.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
preference.setTitle(str("general_min_duration"));
preference.setSummary(str("general_min_duration_sum"));
preference.setKey(PREFERENCES_KEY_MIN_DURATION);
preference.setDefaultValue(String.valueOf(minDuration));
screen.addPreference(preference);
preferencesToDisableWhenSBDisabled.add(preference);
}
2020-08-24 15:47:57 +00:00
{
Preference preference = new EditTextPreference(context);
preference.setTitle(str("general_uuid"));
preference.setSummary(str("general_uuid_sum"));
2020-08-24 15:47:57 +00:00
preference.setKey(PREFERENCES_KEY_UUID);
preference.setDefaultValue(uuid);
screen.addPreference(preference);
preferencesToDisableWhenSBDisabled.add(preference);
}
{
EditTextPreference preference = new EditTextPreference(context);
2021-08-03 14:38:38 +00:00
Context applicationContext = context.getApplicationContext();
preference.setTitle(str("settings_ie"));
preference.setSummary(str("settings_ie_sum"));
2021-08-03 14:38:38 +00:00
preference.setText(SponsorBlockUtils.exportSettings(applicationContext));
preference.setOnPreferenceChangeListener((preference1, newValue) -> {
2021-08-03 14:38:38 +00:00
SponsorBlockUtils.importSettings((String) newValue, applicationContext);
return false;
});
screen.addPreference(preference);
preferencesToDisableWhenSBDisabled.add(preference);
}
2020-08-24 15:47:57 +00:00
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
SponsorBlockSettings.update(getActivity());
}
}