revanced-integrations/app/src/main/java/app/revanced/integrations/sponsorblock/SponsorBlockSettings.java

203 lines
8.1 KiB
Java
Raw Normal View History

2022-06-24 00:16:32 +02:00
package app.revanced.integrations.sponsorblock;
2020-08-24 17:47:57 +02:00
2022-06-24 00:16:32 +02:00
import static app.revanced.integrations.sponsorblock.StringRef.sf;
import android.app.Activity;
2020-08-24 17:47:57 +02:00
import android.content.SharedPreferences;
2021-07-27 17:26:59 +02:00
import android.graphics.Color;
2020-08-24 17:47:57 +02:00
import android.graphics.Paint;
import android.text.Html;
import android.text.TextUtils;
2022-06-24 00:16:32 +02:00
2020-08-24 17:47:57 +02:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import app.revanced.integrations.settings.SettingsEnum;
2022-06-24 00:16:32 +02:00
import app.revanced.integrations.utils.SharedPrefHelper;
2020-08-24 17:47:57 +02:00
public class SponsorBlockSettings {
public static final String CATEGORY_COLOR_SUFFIX = "_color";
2022-03-25 11:20:56 +01:00
public static final SegmentBehaviour DefaultBehaviour = SegmentBehaviour.IGNORE;
2021-07-19 17:14:16 +02:00
public static String sponsorBlockUrlCategories = "[]";
public static void update(Activity _activity) {
SharedPreferences preferences = SharedPrefHelper.getPreferences(SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK);
2022-06-24 00:16:32 +02:00
if (!SettingsEnum.SB_ENABLED.getBoolean()) {
2020-08-24 17:47:57 +02:00
SkipSegmentView.hide();
NewSegmentHelperLayout.hide();
SponsorBlockUtils.hideShieldButton();
SponsorBlockUtils.hideVoteButton();
2020-08-24 17:47:57 +02:00
PlayerController.sponsorSegmentsOfCurrentVideo = null;
} else { /*isAddNewSegmentEnabled*/
2021-04-22 18:43:14 +02:00
SponsorBlockUtils.showShieldButton();
2020-08-24 17:47:57 +02:00
}
if (!SettingsEnum.SB_NEW_SEGMENT_ENABLED.getBoolean()) {
2020-08-24 17:47:57 +02:00
NewSegmentHelperLayout.hide();
SponsorBlockUtils.hideShieldButton();
2020-08-24 17:47:57 +02:00
} else {
SponsorBlockUtils.showShieldButton();
2020-08-24 17:47:57 +02:00
}
if (!SettingsEnum.SB_VOTING_ENABLED.getBoolean())
SponsorBlockUtils.hideVoteButton();
else
SponsorBlockUtils.showVoteButton();
2021-04-22 18:43:14 +02:00
2020-08-24 17:47:57 +02:00
SegmentBehaviour[] possibleBehaviours = SegmentBehaviour.values();
final ArrayList<String> enabledCategories = new ArrayList<>(possibleBehaviours.length);
2021-08-09 18:17:19 +02:00
for (SegmentInfo segment : SegmentInfo.values()) {
String categoryColor = preferences.getString(segment.key + CATEGORY_COLOR_SUFFIX, SponsorBlockUtils.formatColorString(segment.defaultColor));
2021-07-27 17:26:59 +02:00
segment.setColor(Color.parseColor(categoryColor));
2020-08-24 17:47:57 +02:00
SegmentBehaviour behaviour = null;
String value = preferences.getString(segment.key, null);
if (value != null) {
2020-08-24 17:47:57 +02:00
for (SegmentBehaviour possibleBehaviour : possibleBehaviours) {
if (possibleBehaviour.key.equals(value)) {
behaviour = possibleBehaviour;
break;
}
}
}
if (behaviour != null) {
segment.behaviour = behaviour;
2022-06-24 00:16:32 +02:00
} else {
behaviour = segment.behaviour;
}
2020-08-24 17:47:57 +02:00
2021-08-09 18:17:19 +02:00
if (behaviour.showOnTimeBar && segment != SegmentInfo.UNSUBMITTED)
2020-08-24 17:47:57 +02:00
enabledCategories.add(segment.key);
}
2021-06-18 14:46:55 +02:00
//"[%22sponsor%22,%22outro%22,%22music_offtopic%22,%22intro%22,%22selfpromo%22,%22interaction%22,%22preview%22]";
if (enabledCategories.isEmpty())
2020-08-24 17:47:57 +02:00
sponsorBlockUrlCategories = "[]";
else
sponsorBlockUrlCategories = "[%22" + TextUtils.join("%22,%22", enabledCategories) + "%22]";
String uuid = SettingsEnum.SB_UUID.getString();
if (uuid == null || uuid.length() == 0) {
2020-08-24 17:47:57 +02:00
uuid = (UUID.randomUUID().toString() +
UUID.randomUUID().toString() +
UUID.randomUUID().toString())
.replace("-", "");
SettingsEnum.SB_UUID.saveValue(uuid);
2020-08-24 17:47:57 +02:00
}
}
public enum SegmentBehaviour {
SKIP_AUTOMATICALLY_ONCE("skip-once", 3, sf("skip_automatically_once"), true, true),
SKIP_AUTOMATICALLY("skip", 2, sf("skip_automatically"), true, true),
MANUAL_SKIP("manual-skip", 1, sf("skip_showbutton"), false, true),
IGNORE("ignore", -1, sf("skip_ignore"), false, false);
2020-08-24 17:47:57 +02:00
public final String key;
public final int desktopKey;
public final StringRef name;
2020-08-24 17:47:57 +02:00
public final boolean skip;
public final boolean showOnTimeBar;
SegmentBehaviour(String key,
int desktopKey,
StringRef name,
2020-08-24 17:47:57 +02:00
boolean skip,
boolean showOnTimeBar) {
this.key = key;
this.desktopKey = desktopKey;
2020-08-24 17:47:57 +02:00
this.name = name;
this.skip = skip;
this.showOnTimeBar = showOnTimeBar;
}
public static SegmentBehaviour byDesktopKey(int desktopKey) {
for (SegmentBehaviour behaviour : values()) {
if (behaviour.desktopKey == desktopKey) {
return behaviour;
}
}
return null;
}
2020-08-24 17:47:57 +02:00
}
public enum SegmentInfo {
2022-03-25 11:20:56 +01:00
SPONSOR("sponsor", sf("segments_sponsor"), sf("skipped_sponsor"), sf("segments_sponsor_sum"), SegmentBehaviour.SKIP_AUTOMATICALLY, 0xFF00d400),
INTRO("intro", sf("segments_intermission"), sf("skipped_intermission"), sf("segments_intermission_sum"), SegmentBehaviour.MANUAL_SKIP, 0xFF00ffff),
OUTRO("outro", sf("segments_endcards"), sf("skipped_endcard"), sf("segments_endcards_sum"), SegmentBehaviour.MANUAL_SKIP, 0xFF0202ed),
INTERACTION("interaction", sf("segments_subscribe"), sf("skipped_subscribe"), sf("segments_subscribe_sum"), SegmentBehaviour.SKIP_AUTOMATICALLY, 0xFFcc00ff),
SELF_PROMO("selfpromo", sf("segments_selfpromo"), sf("skipped_selfpromo"), sf("segments_selfpromo_sum"), SegmentBehaviour.SKIP_AUTOMATICALLY, 0xFFffff00),
MUSIC_OFFTOPIC("music_offtopic", sf("segments_nomusic"), sf("skipped_nomusic"), sf("segments_nomusic_sum"), SegmentBehaviour.MANUAL_SKIP, 0xFFff9900),
PREVIEW("preview", sf("segments_preview"), sf("skipped_preview"), sf("segments_preview_sum"), DefaultBehaviour, 0xFF008fd6),
FILLER("filler", sf("segments_filler"), sf("skipped_filler"), sf("segments_filler_sum"), DefaultBehaviour, 0xFF7300FF),
2021-07-19 21:58:28 +02:00
UNSUBMITTED("unsubmitted", StringRef.empty, sf("skipped_unsubmitted"), StringRef.empty, SegmentBehaviour.SKIP_AUTOMATICALLY, 0xFFFFFFFF);
2021-07-19 17:32:06 +02:00
private static final SegmentInfo[] mValuesWithoutUnsubmitted = new SegmentInfo[]{
SPONSOR,
INTRO,
OUTRO,
INTERACTION,
SELF_PROMO,
MUSIC_OFFTOPIC,
PREVIEW,
FILLER
2020-08-24 17:47:57 +02:00
};
2021-07-19 22:01:51 +02:00
private static final Map<String, SegmentInfo> mValuesMap = new HashMap<>(values().length);
2020-08-24 17:47:57 +02:00
static {
2021-06-18 14:46:55 +02:00
for (SegmentInfo value : valuesWithoutUnsubmitted())
2020-08-24 17:47:57 +02:00
mValuesMap.put(value.key, value);
}
public final String key;
public final StringRef title;
public final StringRef skipMessage;
public final StringRef description;
2020-08-24 17:47:57 +02:00
public final Paint paint;
2021-07-27 17:26:59 +02:00
public final int defaultColor;
2021-04-16 19:43:03 +02:00
public int color;
2020-08-24 17:47:57 +02:00
public SegmentBehaviour behaviour;
SegmentInfo(String key,
StringRef title,
StringRef skipMessage,
StringRef description,
2020-08-24 17:47:57 +02:00
SegmentBehaviour behaviour,
2021-07-27 17:26:59 +02:00
int defaultColor) {
2020-08-24 17:47:57 +02:00
this.key = key;
this.title = title;
this.skipMessage = skipMessage;
this.description = description;
this.behaviour = behaviour;
2021-07-27 17:26:59 +02:00
this.defaultColor = defaultColor;
this.color = defaultColor;
2021-04-16 19:43:03 +02:00
this.paint = new Paint();
2020-08-24 17:47:57 +02:00
}
2021-06-18 14:46:55 +02:00
public static SegmentInfo[] valuesWithoutUnsubmitted() {
return mValuesWithoutUnsubmitted;
2020-08-24 17:47:57 +02:00
}
public static SegmentInfo byCategoryKey(String key) {
return mValuesMap.get(key);
}
2021-04-16 19:43:03 +02:00
public void setColor(int color) {
2021-07-27 17:26:59 +02:00
color = color & 0xFFFFFF;
2021-04-16 19:43:03 +02:00
this.color = color;
paint.setColor(color);
paint.setAlpha(255);
}
2020-08-24 17:47:57 +02:00
public CharSequence getTitleWithDot() {
2021-07-27 17:26:59 +02:00
return Html.fromHtml(String.format("<font color=\"#%06X\">⬤</font> %s", color, title));
2020-08-24 17:47:57 +02:00
}
}
}