diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java index 0e456ee3..2f529417 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java @@ -153,7 +153,9 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement entryValues[i] = behaviour.key; } - for (SponsorBlockSettings.SegmentInfo segmentInfo : SponsorBlockSettings.SegmentInfo.valuesWithoutUnsubmitted()) { + SponsorBlockSettings.SegmentInfo[] categories = SponsorBlockSettings.SegmentInfo.valuesWithoutUnsubmitted(); + + for (SponsorBlockSettings.SegmentInfo segmentInfo : categories) { ListPreference preference = new ListPreference(context); preference.setTitle(segmentInfo.getTitleWithDot()); preference.setSummary(segmentInfo.description.toString()); @@ -161,9 +163,27 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement preference.setDefaultValue(defaultValue); preference.setEntries(entries); preference.setEntryValues(entryValues); + category.addPreference(preference); } + Preference colorPreference = new Preference(context); + screen.addPreference(colorPreference); + colorPreference.setTitle(str("color_change")); + + colorPreference.setOnPreferenceClickListener(preference1 -> { + CharSequence[] items = new CharSequence[categories.length]; + for (int i = 0; i < items.length; i++) { + items[i] = categories[i].getTitleWithDot(); + } + + new AlertDialog.Builder(context) + .setTitle(str("color_choose_category")) + .setItems(items, SponsorBlockUtils.categoryColorChangeClickListener) + .show(); + return true; + }); + preferencesToDisableWhenSBDisabled.add(colorPreference); } private void addStatsCategory(Context context, PreferenceScreen screen) { diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java b/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java index b1f0e491..87dc819c 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java @@ -2,6 +2,7 @@ package pl.jakubweg; import android.content.Context; import android.content.SharedPreferences; +import android.graphics.Color; import android.graphics.Paint; import android.text.Html; import android.text.TextUtils; @@ -28,6 +29,7 @@ public class SponsorBlockSettings { public static final String PREFERENCES_KEY_SKIPPED_SEGMENTS = "sb-skipped-segments"; public static final String PREFERENCES_KEY_SKIPPED_SEGMENTS_TIME = "sb-skipped-segments-time"; 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 SegmentBehaviour DefaultBehaviour = SegmentBehaviour.SKIP_AUTOMATICALLY; @@ -93,6 +95,9 @@ public class SponsorBlockSettings { SegmentBehaviour[] possibleBehaviours = SegmentBehaviour.values(); final ArrayList enabledCategories = new ArrayList<>(possibleBehaviours.length); for (SegmentInfo segment : SegmentInfo.valuesWithoutUnsubmitted()) { + String categoryColor = preferences.getString(segment.key + PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX, SponsorBlockUtils.formatColorString(segment.defaultColor)); + segment.setColor(Color.parseColor(categoryColor)); + SegmentBehaviour behaviour = null; String value = preferences.getString(segment.key, null); if (value == null) @@ -191,26 +196,26 @@ public class SponsorBlockSettings { public final StringRef title; public final StringRef skipMessage; public final StringRef description; - public final int color; public final Paint paint; + public final int defaultColor; + public int color; public SegmentBehaviour behaviour; - private CharSequence lazyTitleWithDot; SegmentInfo(String key, StringRef title, StringRef skipMessage, StringRef description, SegmentBehaviour behaviour, - int color) { + int defaultColor) { this.key = key; this.title = title; this.skipMessage = skipMessage; this.description = description; this.behaviour = behaviour; - this.color = color & 0xFFFFFF; - paint = new Paint(); - paint.setColor(color); + this.defaultColor = defaultColor; + this.color = defaultColor; + this.paint = new Paint(); } public static SegmentInfo[] valuesWithoutUnsubmitted() { @@ -221,10 +226,15 @@ public class SponsorBlockSettings { return mValuesMap.get(key); } + public void setColor(int color) { + color = color & 0xFFFFFF; + this.color = color; + paint.setColor(color); + paint.setAlpha(255); + } + public CharSequence getTitleWithDot() { - return (lazyTitleWithDot == null) ? - lazyTitleWithDot = Html.fromHtml(String.format(" %s", color, title)) - : lazyTitleWithDot; + return Html.fromHtml(String.format(" %s", color, title)); } } } diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java index 7eaba756..1bb73bac 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java @@ -5,11 +5,14 @@ import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.SharedPreferences; +import android.graphics.Color; import android.net.Uri; import android.preference.EditTextPreference; import android.preference.Preference; import android.preference.PreferenceCategory; import android.text.Html; +import android.text.InputType; import android.text.TextUtils; import android.util.Log; import android.view.View; @@ -41,6 +44,7 @@ import static pl.jakubweg.PlayerController.getLastKnownVideoTime; import static pl.jakubweg.PlayerController.sponsorSegmentsOfCurrentVideo; import static pl.jakubweg.SponsorBlockPreferenceFragment.FORMATTER; import static pl.jakubweg.SponsorBlockPreferenceFragment.SAVED_TEMPLATE; +import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX; import static pl.jakubweg.SponsorBlockSettings.isSponsorBlockEnabled; import static pl.jakubweg.SponsorBlockSettings.showTimeWithoutSegments; import static pl.jakubweg.SponsorBlockSettings.skippedSegments; @@ -220,6 +224,40 @@ public abstract class SponsorBlockUtils { }) .show(); }; + public static final DialogInterface.OnClickListener categoryColorChangeClickListener = (dialog, which) -> { + SponsorBlockSettings.SegmentInfo segmentInfo = SponsorBlockSettings.SegmentInfo.valuesWithoutUnsubmitted()[which]; + String key = segmentInfo.key + PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX; + + Context context = ((AlertDialog) dialog).getContext(); + EditText editText = new EditText(context); + editText.setInputType(InputType.TYPE_CLASS_TEXT); + editText.setText(formatColorString(segmentInfo.color)); + + Context applicationContext = context.getApplicationContext(); + SharedPreferences preferences = SponsorBlockSettings.getPreferences(context); + + new AlertDialog.Builder(context) + .setView(editText) + .setPositiveButton(str("change"), (dialog1, which1) -> { + try { + int color = Color.parseColor(editText.getText().toString()); + segmentInfo.setColor(color); + Toast.makeText(applicationContext, str("color_changed"), Toast.LENGTH_SHORT).show(); + preferences.edit().putString(key, formatColorString(color)).apply(); + } + catch (Exception ex) { + Toast.makeText(applicationContext, str("color_invalid"), Toast.LENGTH_SHORT).show(); + } + }) + .setNeutralButton(str("reset"), (dialog1, which1) -> { + int defaultColor = segmentInfo.defaultColor; + segmentInfo.setColor(defaultColor); + Toast.makeText(applicationContext, str("color_reset"), Toast.LENGTH_SHORT).show(); + preferences.edit().putString(key, formatColorString(defaultColor)).apply(); + }) + .setNegativeButton(android.R.string.cancel, null) + .show(); + }; private static final Runnable submitRunnable = () -> { messageToToast = null; final String uuid = SponsorBlockSettings.uuid; @@ -445,6 +483,10 @@ public abstract class SponsorBlockUtils { return count; } + public static String formatColorString(int color) { + return String.format("#%06X", color); + } + @SuppressWarnings("deprecation") public static void addUserStats(PreferenceCategory category, Preference loadingPreference, UserStats stats) { category.removePreference(loadingPreference); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2e68d5ec..e1ae50d5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -183,6 +183,14 @@ Show a skip button Don\'t do anything + Change colors + Choose the category + Color changed + Color reset + Invalid hex code + Change + Reset + Stats Loading.. SponsorBlock is disabled