0
0
Fork 0
mirror of https://github.com/YTVanced/Integrations synced 2024-11-01 00:42:39 +00:00
VancedIntegrations/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java

304 lines
13 KiB
Java
Raw Normal View History

2020-08-24 15:47:57 +00:00
package pl.jakubweg;
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.ListPreference;
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;
2021-07-19 16:54:26 +00:00
import pl.jakubweg.objects.UserStats;
import pl.jakubweg.requests.Requester;
import static android.text.Html.fromHtml;
2020-08-24 15:47:57 +00:00
import static pl.jakubweg.SponsorBlockSettings.DefaultBehaviour;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_COUNT_SKIPS;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_NEW_SEGMENT_ENABLED;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED;
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_UUID;
2021-04-22 16:43:14 +00:00
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_VOTING_ENABLED;
2020-08-24 15:47:57 +00:00
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_NAME;
import static pl.jakubweg.SponsorBlockSettings.adjustNewSegmentMillis;
import static pl.jakubweg.SponsorBlockSettings.countSkips;
import static pl.jakubweg.SponsorBlockSettings.setSeenGuidelines;
2020-08-24 15:47:57 +00:00
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
@SuppressWarnings({"unused", "deprecation"}) // injected
2020-08-24 15:47:57 +00:00
public class SponsorBlockPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
2021-07-19 16:54:26 +00:00
private static final DecimalFormat FORMATTER = new DecimalFormat("#,###,###");
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
});
}
{
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
String defaultValue = DefaultBehaviour.key;
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-06-18 12:46:55 +00:00
for (SponsorBlockSettings.SegmentInfo segmentInfo : SponsorBlockSettings.SegmentInfo.valuesWithoutUnsubmitted()) {
2020-08-24 15:47:57 +00:00
ListPreference preference = new ListPreference(context);
preference.setTitle(segmentInfo.getTitleWithDot());
preference.setSummary(segmentInfo.description.toString());
2020-08-24 15:47:57 +00:00
preference.setKey(segmentInfo.key);
preference.setDefaultValue(defaultValue);
preference.setEntries(entries);
preference.setEntryValues(entryValues);
category.addPreference(preference);
}
}
2021-07-19 16:54:26 +00:00
private void addStatsCategory(Context context, PreferenceScreen screen) {
PreferenceCategory category = new PreferenceCategory(context);
screen.addPreference(category);
category.setTitle("stats");
UserStats stats = Requester.getUserStats();
{
EditTextPreference preference = new EditTextPreference(context);
screen.addPreference(preference);
String userName = stats.getUserName();
preference.setTitle(fromHtml("Your username: <b>" + userName + "</b>"));
preference.setSummary("Click to change your username");
preference.setText(userName);
preference.setOnPreferenceChangeListener((preference1, newUsername) -> {
Requester.setUsername((String) newUsername);
return false;
});
}
{
Preference preference = new Preference(context);
screen.addPreference(preference);
String formatted = FORMATTER.format(stats.getSegmentCount());
preference.setTitle(fromHtml("Submissions: <b>" + formatted + "</b>"));
}
{
Preference preference = new Preference(context);
screen.addPreference(preference);
String formatted = FORMATTER.format(stats.getViewCount());
double saved = stats.getMinutesSaved();
int hoursSaved = (int) (saved / 60);
double minutesSaved = saved % 60;
String formattedSaved = String.format("%dh %.1f minutes", hoursSaved, minutesSaved);
preference.setTitle(fromHtml("You've saved people from <b>" + formatted + "</b> segments."));
preference.setSummary(fromHtml("That's <b>" + formattedSaved + "</b> of their lives."));
}
}
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);
i.setData(Uri.parse("http://sponsor.ajay.app"));
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);
}
{
EditTextPreference preference = new EditTextPreference(context);
preference.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
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);
}
{
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);
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
SponsorBlockSettings.update(getActivity());
}
}