Skip to content
This repository has been archived by the owner on Mar 25, 2022. It is now read-only.

add support for highlight category #51

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 18 additions & 82 deletions app/src/main/java/pl/jakubweg/PlayerController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package pl.jakubweg;

import static pl.jakubweg.SponsorBlockSettings.skippedSegments;
import static pl.jakubweg.SponsorBlockSettings.skippedTime;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
Expand All @@ -25,9 +28,6 @@
import pl.jakubweg.objects.SponsorSegment;
import pl.jakubweg.requests.Requester;

import static pl.jakubweg.SponsorBlockSettings.skippedSegments;
import static pl.jakubweg.SponsorBlockSettings.skippedTime;

@SuppressLint({"LongLogTag"})
public class PlayerController {
public static final String TAG = "jakubweg.PlayerController";
Expand All @@ -45,7 +45,6 @@ public class PlayerController {
private static long currentVideoLength = 1L;
private static long lastKnownVideoTime = -1L;
private static final Runnable findAndSkipSegmentRunnable = () -> {
// Log.d(TAG, "findAndSkipSegmentRunnable");
findAndSkipSegment(false);
};
private static float sponsorBarLeft = 1f;
Expand Down Expand Up @@ -89,7 +88,7 @@ public static void setCurrentVideoId(final String videoId) {
sponsorTimer.schedule(new TimerTask() {
@Override
public void run() {
executeDownloadSegments(currentVideoId);
executeDownloadSegments(currentVideoId, context);
}
}, 0);
}
Expand Down Expand Up @@ -124,8 +123,8 @@ public static void onCreate(final Object o) {
}
}

public static void executeDownloadSegments(String videoId) {
SponsorSegment[] segments = Requester.getSegments(videoId);
public static void executeDownloadSegments(String videoId, Context context) {
SponsorSegment[] segments = Requester.getSegments(videoId, context);
Arrays.sort(segments);

if (VERBOSE)
Expand All @@ -137,69 +136,6 @@ public static void executeDownloadSegments(String videoId) {
// new Handler(Looper.getMainLooper()).post(findAndSkipSegmentRunnable);
}

/**
* Works in 14.x, waits some time of object to me filled with data,
* No longer used, i've found another way to get faster videoId
*/
@Deprecated
public static void asyncGetVideoLinkFromObject(final Object o) {
// code no longer used

// if (currentVideoLink != null) {
// if (VERBOSE)
// Log.w(TAG, "asyncGetVideoLinkFromObject: currentVideoLink != null probably share button was clicked");
// return;
// }
//
// new Thread(new Runnable() {
// @Override
// public void run() {
// try {
// // It used to be "b" in 14.x version, it's "a" in 15.x
// Field b = o.getClass().getDeclaredField("b");
//
// int attempts = 0;
// String videoUrl = null;
// while (true) {
// Object objLink = b.get(o);
// if (objLink == null) {
// if (VERBOSE)
// Log.e(TAG, "asyncGetVideoLinkFromObject: objLink is null");
// } else {
// videoUrl = objLink.toString();
// if (videoUrl.isEmpty())
// videoUrl = null;
// }
//
// if (videoUrl != null)
// break;
//
// if (attempts++ > 5) {
// Log.w(TAG, "asyncGetVideoLinkFromObject: attempts++ > 5");
// return;
// }
// Thread.sleep(50);
// }
//
// if (currentVideoLink == null) {
// currentVideoLink = videoUrl;
// if (VERBOSE)
// Log.d(TAG, "asyncGetVideoLinkFromObject: link set to " + videoUrl);
//
// executeDownloadSegments(substringVideoIdFromLink(videoUrl), false);
// }
//
// } catch (Exception e) {
// Log.e(TAG, "Cannot get link from object", e);
// }
// }
// }).start();
//
// Activity activity = playerActivity.get();
// if (activity != null)
// SponsorBlockUtils.addImageButton(activity);
}

/**
* Called when it's time to update the UI with new second, about once per second, only when playing, also in background
*/
Expand Down Expand Up @@ -269,19 +205,19 @@ public void run() {
}

private static void sendViewRequestAsync(final long millis, final SponsorSegment segment) {
if (segment.category != SponsorBlockSettings.SegmentInfo.UNSUBMITTED) {
Context context = YouTubeTikTokRoot_Application.getAppContext();
if (context != null) {
SharedPreferences preferences = SponsorBlockSettings.getPreferences(context);
long newSkippedTime = skippedTime + (segment.end - segment.start);
preferences.edit().putInt(SponsorBlockSettings.PREFERENCES_KEY_SKIPPED_SEGMENTS, skippedSegments + 1).apply();
preferences.edit().putLong(SponsorBlockSettings.PREFERENCES_KEY_SKIPPED_SEGMENTS_TIME, newSkippedTime).apply();
}
if (segment.category == SponsorBlockSettings.SegmentCategory.UNSUBMITTED) {
return;
}
Context context = YouTubeTikTokRoot_Application.getAppContext();
if (context != null) {
SharedPreferences.Editor editor = SponsorBlockSettings.getPreferences(context).edit();
long newSkippedTime = skippedTime + (segment.end - segment.start);
editor.putInt(SponsorBlockSettings.PREFERENCES_KEY_SKIPPED_SEGMENTS, skippedSegments + 1);
editor.putLong(SponsorBlockSettings.PREFERENCES_KEY_SKIPPED_SEGMENTS_TIME, newSkippedTime);
editor.apply();
}
new Thread(() -> {
if (SponsorBlockSettings.countSkips &&
segment.category != SponsorBlockSettings.SegmentInfo.UNSUBMITTED &&
millis - segment.start < 2000) {
if (SponsorBlockSettings.countSkips && millis - segment.start < 2000) {
// Only skips from the start should count as a view
Requester.sendViewCountRequest(segment);
}
Expand Down Expand Up @@ -509,7 +445,7 @@ private static void skipSegment(SponsorSegment segment, boolean wasClicked) {

skipToMillisecond(segment.end + 2);
SkipSegmentView.hide();
if (segment.category == SponsorBlockSettings.SegmentInfo.UNSUBMITTED) {
if (segment.category == SponsorBlockSettings.SegmentCategory.UNSUBMITTED) {
SponsorSegment[] newSegments = new SponsorSegment[sponsorSegmentsOfCurrentVideo.length - 1];
int i = 0;
for (SponsorSegment sponsorSegment : sponsorSegmentsOfCurrentVideo) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pl.jakubweg;

import static pl.jakubweg.SponsorBlockSettings.DefaultBehaviour;
import static pl.jakubweg.SponsorBlockSettings.DEFAULT_BEHAVIOR;
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;
Expand Down Expand Up @@ -143,7 +143,7 @@ private void addSegmentsCategory(Context context, PreferenceScreen screen) {
preferencesToDisableWhenSBDisabled.add(category);
category.setTitle(str("diff_segments"));

String defaultValue = DefaultBehaviour.key;
String defaultValue = DEFAULT_BEHAVIOR.key;
SponsorBlockSettings.SegmentBehaviour[] segmentBehaviours = SponsorBlockSettings.SegmentBehaviour.values();
String[] entries = new String[segmentBehaviours.length];
String[] entryValues = new String[segmentBehaviours.length];
Expand All @@ -153,13 +153,13 @@ private void addSegmentsCategory(Context context, PreferenceScreen screen) {
entryValues[i] = behaviour.key;
}

SponsorBlockSettings.SegmentInfo[] categories = SponsorBlockSettings.SegmentInfo.valuesWithoutUnsubmitted();
SponsorBlockSettings.SegmentCategory[] categories = SponsorBlockSettings.SegmentCategory.valuesWithoutUnsubmitted();

for (SponsorBlockSettings.SegmentInfo segmentInfo : categories) {
for (SponsorBlockSettings.SegmentCategory segmentCategory : categories) {
ListPreference preference = new ListPreference(context);
preference.setTitle(segmentInfo.getTitleWithDot());
preference.setSummary(segmentInfo.description.toString());
preference.setKey(segmentInfo.key);
preference.setTitle(segmentCategory.title);
preference.setSummary(segmentCategory.description.toString());
preference.setKey(segmentCategory.key);
preference.setDefaultValue(defaultValue);
preference.setEntries(entries);
preference.setEntryValues(entryValues);
Expand All @@ -174,7 +174,7 @@ private void addSegmentsCategory(Context context, PreferenceScreen screen) {
colorPreference.setOnPreferenceClickListener(preference1 -> {
CharSequence[] items = new CharSequence[categories.length];
for (int i = 0; i < items.length; i++) {
items[i] = categories[i].getTitleWithDot();
items[i] = categories[i].title;
}

new AlertDialog.Builder(context)
Expand Down
Loading