revanced-integrations/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java

279 lines
12 KiB
Java
Raw Normal View History

2020-08-24 17:47:57 +02:00
package pl.jakubweg;
import static pl.jakubweg.StringRef.sf;
2020-08-24 17:47:57 +02:00
import android.content.Context;
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;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class SponsorBlockSettings {
public static final String PREFERENCES_NAME = "sponsor-block";
public static final String PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP = "show-toast";
public static final String PREFERENCES_KEY_COUNT_SKIPS = "count-skips";
public static final String PREFERENCES_KEY_UUID = "uuid";
public static final String PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP = "new-segment-step-accuracy";
2022-01-24 17:22:21 +01:00
public static final String PREFERENCES_KEY_MIN_DURATION = "sb-min-duration";
2020-08-24 17:47:57 +02:00
public static final String PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED = "sb-enabled";
2022-01-25 22:28:04 +01:00
public static final String PREFERENCES_KEY_SPONSOR_BLOCK_HINT_SHOWN = "sb_hint_shown";
public static final String PREFERENCES_KEY_SEEN_GUIDELINES = "sb-seen-gl";
2020-08-24 17:47:57 +02:00
public static final String PREFERENCES_KEY_NEW_SEGMENT_ENABLED = "sb-new-segment-enabled";
2021-04-22 18:43:14 +02:00
public static final String PREFERENCES_KEY_VOTING_ENABLED = "sb-voting-enabled";
2021-07-19 21:51:26 +02:00
public static final String PREFERENCES_KEY_SKIPPED_SEGMENTS = "sb-skipped-segments";
public static final String PREFERENCES_KEY_SKIPPED_SEGMENTS_TIME = "sb-skipped-segments-time";
2021-07-23 17:10:56 +02:00
public static final String PREFERENCES_KEY_SHOW_TIME_WITHOUT_SEGMENTS = "sb-length-without-segments";
2021-07-27 17:26:59 +02:00
public static final String PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX = "_color";
2022-01-27 23:05:46 +01:00
public static final String PREFERENCES_KEY_BROWSER_BUTTON = "sb-browser-button";
2022-02-07 16:15:22 +01:00
public static final String PREFERENCES_KEY_IS_VIP = "sb-is-vip";
public static final String PREFERENCES_KEY_LAST_VIP_CHECK = "sb-last-vip-check";
2022-01-28 22:38:43 +01:00
public static final String PREFERENCES_KEY_API_URL = "sb-api-url";
2022-03-25 11:20:56 +01:00
public static final SegmentBehaviour DefaultBehaviour = SegmentBehaviour.IGNORE;
2022-02-07 16:29:42 +01:00
public static final String DEFAULT_SERVER_URL = "https://sponsor.ajay.app";
public static final String DEFAULT_API_URL = DEFAULT_SERVER_URL + "/api/";
2020-08-24 17:47:57 +02:00
public static boolean isSponsorBlockEnabled = false;
public static boolean seenGuidelinesPopup = false;
2020-08-24 17:47:57 +02:00
public static boolean isAddNewSegmentEnabled = false;
2021-04-22 18:43:14 +02:00
public static boolean isVotingEnabled = true;
2020-08-24 17:47:57 +02:00
public static boolean showToastWhenSkippedAutomatically = true;
public static boolean countSkips = true;
2021-07-23 17:10:56 +02:00
public static boolean showTimeWithoutSegments = true;
2022-02-07 16:15:22 +01:00
public static boolean vip = false;
public static long lastVipCheck = 0;
2020-08-24 17:47:57 +02:00
public static int adjustNewSegmentMillis = 150;
2022-01-24 17:22:21 +01:00
public static float minDuration = 0f;
2020-08-24 17:47:57 +02:00
public static String uuid = "<invalid>";
2022-01-28 22:38:43 +01:00
public static String apiUrl = DEFAULT_API_URL;
2021-07-19 17:14:16 +02:00
public static String sponsorBlockUrlCategories = "[]";
2021-07-19 21:51:26 +02:00
public static int skippedSegments;
public static long skippedTime;
2020-08-24 17:47:57 +02:00
@SuppressWarnings("unused")
@Deprecated
public SponsorBlockSettings(Context ignored) {
Log.e("jakubweg.Settings", "Do not call SponsorBlockSettings constructor!");
2020-08-24 17:47:57 +02:00
}
public static SharedPreferences getPreferences(Context context) {
return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
}
public static void setSeenGuidelines(Context context) {
SponsorBlockSettings.seenGuidelinesPopup = true;
getPreferences(context).edit().putBoolean(PREFERENCES_KEY_SEEN_GUIDELINES, true).apply();
}
2020-08-24 17:47:57 +02:00
public static void update(Context context) {
if (context == null) return;
SharedPreferences preferences = getPreferences(context);
2020-08-24 17:47:57 +02:00
isSponsorBlockEnabled = preferences.getBoolean(PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED, isSponsorBlockEnabled);
seenGuidelinesPopup = preferences.getBoolean(PREFERENCES_KEY_SEEN_GUIDELINES, seenGuidelinesPopup);
2020-08-24 17:47:57 +02:00
if (!isSponsorBlockEnabled) {
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
}
isAddNewSegmentEnabled = preferences.getBoolean(PREFERENCES_KEY_NEW_SEGMENT_ENABLED, isAddNewSegmentEnabled);
if (!isAddNewSegmentEnabled) {
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
}
2021-04-22 18:43:14 +02:00
isVotingEnabled = preferences.getBoolean(PREFERENCES_KEY_VOTING_ENABLED, isVotingEnabled);
if (!isVotingEnabled)
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()) {
2021-07-27 17:26:59 +02:00
String categoryColor = preferences.getString(segment.key + PREFERENCES_KEY_CATEGORY_COLOR_SUFFIX, SponsorBlockUtils.formatColorString(segment.defaultColor));
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;
}
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]";
2021-07-19 21:51:26 +02:00
skippedSegments = preferences.getInt(PREFERENCES_KEY_SKIPPED_SEGMENTS, skippedSegments);
skippedTime = preferences.getLong(PREFERENCES_KEY_SKIPPED_SEGMENTS_TIME, skippedTime);
2020-08-24 17:47:57 +02:00
showToastWhenSkippedAutomatically = preferences.getBoolean(PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP, showToastWhenSkippedAutomatically);
2020-08-25 13:46:20 +02:00
String tmp1 = preferences.getString(PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP, null);
if (tmp1 != null)
adjustNewSegmentMillis = Integer.parseInt(tmp1);
2020-08-24 17:47:57 +02:00
2022-01-24 17:22:21 +01:00
String minTmp = preferences.getString(PREFERENCES_KEY_MIN_DURATION, null);
if (minTmp != null)
minDuration = Float.parseFloat(minTmp);
countSkips = preferences.getBoolean(PREFERENCES_KEY_COUNT_SKIPS, countSkips);
2021-07-23 17:10:56 +02:00
showTimeWithoutSegments = preferences.getBoolean(PREFERENCES_KEY_SHOW_TIME_WITHOUT_SEGMENTS, showTimeWithoutSegments);
2022-02-07 16:15:22 +01:00
vip = preferences.getBoolean(PREFERENCES_KEY_IS_VIP, false);
String vipCheckTmp = preferences.getString(PREFERENCES_KEY_LAST_VIP_CHECK, null);
if (vipCheckTmp != null)
lastVipCheck = Long.parseLong(vipCheckTmp);
2020-08-24 17:47:57 +02:00
2022-01-28 22:38:43 +01:00
apiUrl = preferences.getString(PREFERENCES_KEY_API_URL, DEFAULT_API_URL);
2020-08-24 17:47:57 +02:00
uuid = preferences.getString(PREFERENCES_KEY_UUID, null);
if (uuid == null) {
uuid = (UUID.randomUUID().toString() +
UUID.randomUUID().toString() +
UUID.randomUUID().toString())
.replace("-", "");
preferences.edit().putString(PREFERENCES_KEY_UUID, uuid).apply();
}
}
public enum SegmentBehaviour {
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"), DefaultBehaviour, 0xFF00ffff),
OUTRO("outro", sf("segments_endcards"), sf("skipped_endcard"), sf("segments_endcards_sum"), DefaultBehaviour, 0xFF0202ed),
INTERACTION("interaction", sf("segments_subscribe"), sf("skipped_subscribe"), sf("segments_subscribe_sum"), DefaultBehaviour, 0xFFcc00ff),
SELF_PROMO("selfpromo", sf("segments_selfpromo"), sf("skipped_selfpromo"), sf("segments_selfpromo_sum"), DefaultBehaviour, 0xFFffff00),
MUSIC_OFFTOPIC("music_offtopic", sf("segments_nomusic"), sf("skipped_nomusic"), sf("segments_nomusic_sum"), DefaultBehaviour, 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"), SegmentBehaviour.IGNORE, 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
}
}
}