diff --git a/app/src/main/java/fi/vanced/libraries/youtube/player/PlayerType.java b/app/src/main/java/fi/vanced/libraries/youtube/player/PlayerType.java index 448ebe0..84eabe4 100644 --- a/app/src/main/java/fi/vanced/libraries/youtube/player/PlayerType.java +++ b/app/src/main/java/fi/vanced/libraries/youtube/player/PlayerType.java @@ -1,9 +1,11 @@ package fi.vanced.libraries.youtube.player; import fi.vanced.libraries.youtube.sponsors.player.ui.SponsorBlockView; +import pl.jakubweg.SponsorBlockUtils; public class PlayerType { public static void playerTypeChanged(String playerType) { SponsorBlockView.playerTypeChanged(playerType); + SponsorBlockUtils.playerTypeChanged(playerType); } } diff --git a/app/src/main/java/pl/jakubweg/PlayerController.java b/app/src/main/java/pl/jakubweg/PlayerController.java index 9dc1a2f..b996415 100644 --- a/app/src/main/java/pl/jakubweg/PlayerController.java +++ b/app/src/main/java/pl/jakubweg/PlayerController.java @@ -56,7 +56,8 @@ public class PlayerController { public static void setCurrentVideoId(final String videoId) { if (videoId == null) { - Log.d(TAG, "setCurrentVideoId: videoId is null"); + currentVideoId = null; + sponsorSegmentsOfCurrentVideo = null; return; } diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java index fb3f667..d4eabff 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java @@ -24,6 +24,7 @@ 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_TIME_WITHOUT_SEGMENTS; 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; @@ -32,6 +33,7 @@ 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; +import static pl.jakubweg.SponsorBlockSettings.showTimeWithoutSegments; import static pl.jakubweg.SponsorBlockSettings.showToastWhenSkippedAutomatically; import static pl.jakubweg.SponsorBlockSettings.uuid; import static pl.jakubweg.StringRef.str; @@ -247,6 +249,16 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement screen.addPreference(preference); } + { + Preference preference = new SwitchPreference(context); + preference.setTitle(str("general_time_without_sb")); + 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); + } + { EditTextPreference preference = new EditTextPreference(context); preference.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED); diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java b/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java index 0210966..985ef64 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java @@ -28,7 +28,7 @@ public class SponsorBlockSettings { public static final String sponsorBlockSkipSegmentsUrl = "https://sponsor.ajay.app/api/skipSegments"; public static final String sponsorBlockViewedUrl = "https://sponsor.ajay.app/api/viewedVideoSponsorTime"; public static final String sponsorBlockVoteUrl = "https://sponsor.ajay.app/api/voteOnSponsorTime"; - + public static final String PREFERENCES_KEY_SHOW_TIME_WITHOUT_SEGMENTS = "sb-length-without-segments"; public static final SegmentBehaviour DefaultBehaviour = SegmentBehaviour.SkipAutomatically; @@ -38,6 +38,7 @@ public class SponsorBlockSettings { public static boolean isVotingEnabled = true; public static boolean showToastWhenSkippedAutomatically = true; public static boolean countSkips = true; + public static boolean showTimeWithoutSegments = true; public static int adjustNewSegmentMillis = 150; public static String uuid = ""; private static String sponsorBlockUrlCategories = "[]"; @@ -140,6 +141,7 @@ public class SponsorBlockSettings { adjustNewSegmentMillis = Integer.parseInt(tmp1); countSkips = preferences.getBoolean(PREFERENCES_KEY_COUNT_SKIPS, countSkips); + showTimeWithoutSegments = preferences.getBoolean(PREFERENCES_KEY_SHOW_TIME_WITHOUT_SEGMENTS, showTimeWithoutSegments); uuid = preferences.getString(PREFERENCES_KEY_UUID, null); if (uuid == null) { diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java index 299992f..e5ef0d5 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java @@ -7,12 +7,16 @@ import android.content.DialogInterface; import android.os.Handler; import android.os.Looper; import android.text.Html; +import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.ImageView; +import android.widget.TextView; import android.widget.Toast; +import com.google.android.apps.youtube.app.YouTubeTikTokRoot_Application; + import org.json.JSONArray; import org.json.JSONObject; @@ -32,6 +36,9 @@ import java.util.Locale; import java.util.Objects; import java.util.TimeZone; +import fi.razerman.youtube.Helpers.XSwipeHelper; +import fi.razerman.youtube.XGlobals; + import static android.view.View.GONE; import static android.view.View.VISIBLE; import static fi.razerman.youtube.XGlobals.debug; @@ -48,8 +55,14 @@ import static pl.jakubweg.StringRef.str; public abstract class SponsorBlockUtils { public static final String TAG = "jakubweg.SponsorBlockUtils"; public static final String DATE_FORMAT = "HH:mm:ss.SSS"; + public static final String WITHOUT_SEGMENTS_FORMAT = " (m:ss)"; + public static final String WITHOUT_SEGMENTS_FORMAT_H = " (H:m:ss)"; @SuppressLint("SimpleDateFormat") public static final SimpleDateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT); + public static final SimpleDateFormat withoutSegmentsFormatter = new SimpleDateFormat(WITHOUT_SEGMENTS_FORMAT); + public static final SimpleDateFormat withoutSegmentsFormatterH = new SimpleDateFormat(WITHOUT_SEGMENTS_FORMAT_H); + private static boolean videoHasSegments = false; + private static String timeWithoutSegments = ""; private static final int sponsorBtnId = 1234; public static final View.OnClickListener sponsorBlockBtnListener = new View.OnClickListener() { @Override @@ -459,6 +472,8 @@ public abstract class SponsorBlockUtils { URL url = new URL(SponsorBlockSettings.getSponsorBlockUrlWithCategories(videoId)); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + videoHasSegments = false; + timeWithoutSegments = ""; switch (connection.getResponseCode()) { default: Log.e(TAG, "Unable to download segments: Status: " + connection.getResponseCode() + " " + connection.getResponseMessage()); @@ -498,6 +513,8 @@ public abstract class SponsorBlockUtils { if (VERBOSE) Log.v(TAG, "Parsing done"); + + videoHasSegments = true; break; } @@ -507,9 +524,16 @@ public abstract class SponsorBlockUtils { Log.e(TAG, "download segments failed", e); } + timeWithoutSegments = getTimeWithoutSegments(sponsorSegments); + return sponsorSegments.toArray(new SponsorSegment[0]); } + private static int getIdentifier(String name, String defType) { + Context context = YouTubeTikTokRoot_Application.getAppContext(); + return context.getResources().getIdentifier(name, defType, context.getPackageName()); + } + public static void sendViewCountRequest(SponsorSegment segment) { try { URL url = new URL(SponsorBlockSettings.getSponsorBlockViewedUrl(segment.UUID)); @@ -562,6 +586,38 @@ public abstract class SponsorBlockUtils { } } + public static String appendTimeWithoutSegments(String totalTime) { + if (videoHasSegments && SponsorBlockSettings.showTimeWithoutSegments && !TextUtils.isEmpty(totalTime)) { + return totalTime + timeWithoutSegments; + } + + return totalTime; + } + + public static String getTimeWithoutSegments(ArrayList sponsorSegmentsOfCurrentVideo) { + if (!SponsorBlockSettings.isSponsorBlockEnabled || !SponsorBlockSettings.showTimeWithoutSegments || sponsorSegmentsOfCurrentVideo == null) { + return ""; + } + long timeWithoutSegments = PlayerController.getCurrentVideoLength(); + for (SponsorSegment segment : sponsorSegmentsOfCurrentVideo) { + timeWithoutSegments -= segment.end - segment.start; + } + Date date = new Date(timeWithoutSegments); + return timeWithoutSegments >= 3600000 ? withoutSegmentsFormatterH.format(date) : withoutSegmentsFormatter.format(date); + } + + public static void playerTypeChanged(String playerType) { + try { + if (videoHasSegments && (playerType.equalsIgnoreCase("NONE"))) { + PlayerController.setCurrentVideoId(null); + return; + } + } + catch (Exception ex) { + Log.e(TAG, "Player type changed caused a crash.", ex); + } + } + private enum VoteOption { UPVOTE(str("vote_upvote")), DOWNVOTE(str("vote_downvote")), diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7b1d5fe..d9d3991 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -151,6 +151,8 @@ Click to see an example toast Skip count tracking This lets SponsorBlock leaderboard system know how much time people have saved. The extension sends a message to the server each time you skip a segment. + Show time without segments + This time appears in brackets next to the current time. This shows the total video duration minus any segments. Adjusting new segment step This is the number of milliseconds you can move when you use the time adjustment buttons while adding new segment Your unique user id