Skip to content
This repository was archived by the owner on Jan 21, 2024. It is now read-only.

Commit cbcb0f7

Browse files
SponsorBlock: removed deprecated AsyncTask and improved performance
1 parent e50a40b commit cbcb0f7

File tree

3 files changed

+93
-97
lines changed

3 files changed

+93
-97
lines changed

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@
103103
import org.schabi.newpipe.util.NavigationHelper;
104104
import org.schabi.newpipe.util.PermissionHelper;
105105
import org.schabi.newpipe.util.ShareUtils;
106+
import org.schabi.newpipe.util.SponsorBlockUtils;
106107
import org.schabi.newpipe.util.ThemeHelper;
108+
import org.schabi.newpipe.util.VideoSegment;
107109
import org.schabi.newpipe.views.AnimatedProgressBar;
108110
import org.schabi.newpipe.views.LargeTextMovementMethod;
109111

@@ -192,6 +194,7 @@ public final class VideoDetailFragment
192194
private int selectedVideoStreamIndex = -1;
193195
private BottomSheetBehavior<FrameLayout> bottomSheetBehavior;
194196
private BroadcastReceiver broadcastReceiver;
197+
private VideoSegment[] videoSegments;
195198

196199
/*//////////////////////////////////////////////////////////////////////////
197200
// Views
@@ -262,6 +265,8 @@ public void onServiceConnected(final VideoPlayerImpl connectedPlayer,
262265
final MainPlayer connectedPlayerService,
263266
final boolean playAfterConnect) {
264267
player = connectedPlayer;
268+
player.setVideoSegments(videoSegments);
269+
265270
playerService = connectedPlayerService;
266271

267272
// It will do nothing if the player is not in fullscreen mode
@@ -945,7 +950,43 @@ private void startLoading(final boolean forceLoad, final boolean addToBackStack)
945950

946951
private void runWorker(final boolean forceLoad, final boolean addToBackStack) {
947952
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
953+
954+
final String apiUrl = prefs.getString(getContext()
955+
.getString(R.string.sponsor_block_api_url_key), null);
956+
final boolean isSponsorBlockEnabled = prefs.getBoolean(getContext()
957+
.getString(R.string.sponsor_block_enable_key), false);
958+
final boolean includeSponsorCategory = prefs.getBoolean(getContext()
959+
.getString(R.string.sponsor_block_category_sponsor_key), false);
960+
final boolean includeIntroCategory = prefs.getBoolean(getContext()
961+
.getString(R.string.sponsor_block_category_intro_key), false);
962+
final boolean includeOutroCategory = prefs.getBoolean(getContext()
963+
.getString(R.string.sponsor_block_category_outro_key), false);
964+
final boolean includeInteractionCategory = prefs.getBoolean(getContext()
965+
.getString(R.string.sponsor_block_category_interaction_key), false);
966+
final boolean includeSelfPromoCategory = prefs.getBoolean(getContext()
967+
.getString(R.string.sponsor_block_category_self_promo_key), false);
968+
final boolean includeMusicCategory = prefs.getBoolean(getContext()
969+
.getString(R.string.sponsor_block_category_non_music_key), false);
970+
948971
currentWorker = ExtractorHelper.getStreamInfo(serviceId, url, forceLoad)
972+
.flatMap(streamInfo -> Single.fromCallable(() -> {
973+
if (isSponsorBlockEnabled
974+
&& streamInfo.getUrl().startsWith("https://www.youtube.com")
975+
&& apiUrl != null
976+
&& !apiUrl.isEmpty()) {
977+
this.videoSegments = SponsorBlockUtils.getYouTubeVideoSegments(
978+
apiUrl,
979+
streamInfo.getId(),
980+
includeSponsorCategory,
981+
includeIntroCategory,
982+
includeOutroCategory,
983+
includeInteractionCategory,
984+
includeSelfPromoCategory,
985+
includeMusicCategory);
986+
}
987+
988+
return streamInfo;
989+
}))
949990
.subscribeOn(Schedulers.io())
950991
.observeOn(AndroidSchedulers.mainThread())
951992
.subscribe(result -> {

app/src/main/java/org/schabi/newpipe/player/BasePlayer.java

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import org.schabi.newpipe.DownloaderImpl;
6060
import org.schabi.newpipe.MainActivity;
6161
import org.schabi.newpipe.R;
62-
import org.schabi.newpipe.SponsorBlockApiTask;
6362
import org.schabi.newpipe.extractor.stream.StreamInfo;
6463
import org.schabi.newpipe.local.history.HistoryRecordManager;
6564
import org.schabi.newpipe.player.helper.AudioReactor;
@@ -118,6 +117,7 @@ public abstract class BasePlayer implements
118117
@NonNull
119118
protected final SharedPreferences mPrefs;
120119

120+
private boolean wereSponsorsMarked;
121121
private VideoSegment[] videoSegments;
122122

123123
/*//////////////////////////////////////////////////////////////////////////
@@ -1211,65 +1211,11 @@ protected void onMetadataChanged(@NonNull final MediaSourceTag tag) {
12111211
context.getString(R.string.sponsor_block_whitelist_key), null);
12121212

12131213
if (uploaderWhitelist != null && uploaderWhitelist.contains(info.getUploaderName())) {
1214-
sponsorBlockMode = SponsorBlockMode.IGNORE;
1214+
setSponsorBlockMode(SponsorBlockMode.IGNORE);
12151215
} else {
1216-
sponsorBlockMode = isSponsorBlockEnabled
1216+
setSponsorBlockMode(isSponsorBlockEnabled
12171217
? SponsorBlockMode.ENABLED
1218-
: SponsorBlockMode.DISABLED;
1219-
}
1220-
1221-
if (info.getUrl().startsWith("https://www.youtube.com")) {
1222-
final String apiUrl = mPrefs
1223-
.getString(context.getString(R.string.sponsor_block_api_url_key), null);
1224-
1225-
if (apiUrl != null && !apiUrl.isEmpty() && isSponsorBlockEnabled) {
1226-
try {
1227-
final boolean includeSponsorCategory =
1228-
mPrefs.getBoolean(
1229-
context.getString(
1230-
R.string.sponsor_block_category_sponsor_key),
1231-
false);
1232-
1233-
final boolean includeIntroCategory =
1234-
mPrefs.getBoolean(
1235-
context.getString(
1236-
R.string.sponsor_block_category_intro_key),
1237-
false);
1238-
1239-
final boolean includeOutroCategory =
1240-
mPrefs.getBoolean(
1241-
context.getString(
1242-
R.string.sponsor_block_category_outro_key),
1243-
false);
1244-
1245-
final boolean includeInteractionCategory =
1246-
mPrefs.getBoolean(
1247-
context.getString(
1248-
R.string.sponsor_block_category_interaction_key),
1249-
false);
1250-
final boolean includeSelfPromoCategory =
1251-
mPrefs.getBoolean(
1252-
context.getString(
1253-
R.string.sponsor_block_category_self_promo_key),
1254-
false);
1255-
final boolean includeMusicCategory =
1256-
mPrefs.getBoolean(
1257-
context.getString(
1258-
R.string.sponsor_block_category_non_music_key),
1259-
false);
1260-
1261-
videoSegments = new SponsorBlockApiTask(apiUrl)
1262-
.getYouTubeVideoSegments(info.getId(),
1263-
includeSponsorCategory,
1264-
includeIntroCategory,
1265-
includeOutroCategory,
1266-
includeInteractionCategory,
1267-
includeSelfPromoCategory,
1268-
includeMusicCategory);
1269-
} catch (final Exception e) {
1270-
Log.e("SPONSOR_BLOCK", "Error getting YouTube video segments.", e);
1271-
}
1272-
}
1218+
: SponsorBlockMode.DISABLED);
12731219
}
12741220
}
12751221

@@ -1822,4 +1768,15 @@ private boolean isPlaybackResumeEnabled() {
18221768
public VideoSegment[] getVideoSegments() {
18231769
return videoSegments;
18241770
}
1771+
1772+
public void setVideoSegments(final VideoSegment[] videoSegments) {
1773+
// use a flag to ignore null values later (i.e. when the video goes fullscreen)
1774+
// TODO: there's probably a better way to deal with stuff like that
1775+
if (wereSponsorsMarked) {
1776+
return;
1777+
}
1778+
1779+
this.videoSegments = videoSegments;
1780+
wereSponsorsMarked = true;
1781+
}
18251782
}

app/src/main/java/org/schabi/newpipe/SponsorBlockApiTask.java renamed to app/src/main/java/org/schabi/newpipe/util/SponsorBlockUtils.java

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
1-
package org.schabi.newpipe;
1+
package org.schabi.newpipe.util;
22

33
import android.app.Application;
44
import android.content.Context;
55
import android.net.ConnectivityManager;
6-
import android.os.AsyncTask;
76
import android.text.TextUtils;
87
import android.util.Log;
98

109
import com.grack.nanojson.JsonArray;
1110
import com.grack.nanojson.JsonObject;
1211
import com.grack.nanojson.JsonParser;
1312

14-
import org.schabi.newpipe.util.VideoSegment;
13+
import org.schabi.newpipe.App;
14+
import org.schabi.newpipe.DownloaderImpl;
15+
import org.schabi.newpipe.MainActivity;
1516

1617
import java.io.UnsupportedEncodingException;
1718
import java.net.URLEncoder;
1819
import java.nio.charset.StandardCharsets;
1920
import java.security.MessageDigest;
2021
import java.util.ArrayList;
21-
import java.util.concurrent.ExecutionException;
2222

23-
public class SponsorBlockApiTask extends AsyncTask<String, Void, JsonArray> {
23+
public final class SponsorBlockUtils {
2424
private static final Application APP = App.getApp();
25-
private String apiUrl;
26-
private static final String TAG = SponsorBlockApiTask.class.getSimpleName();
25+
private static final String TAG = SponsorBlockUtils.class.getSimpleName();
2726
private static final boolean DEBUG = MainActivity.DEBUG;
2827

29-
public SponsorBlockApiTask(final String apiUrl) {
30-
this.apiUrl = apiUrl;
28+
private SponsorBlockUtils() {
3129
}
3230

33-
public VideoSegment[] getYouTubeVideoSegments(final String videoId,
31+
@SuppressWarnings("CheckStyle")
32+
public static VideoSegment[] getYouTubeVideoSegments(final String apiUrl,
33+
final String videoId,
3434
final boolean includeSponsorCategory,
3535
final boolean includeIntroCategory,
3636
final boolean includeOutroCategory,
3737
final boolean includeInteractionCategory,
3838
final boolean includeSelfPromoCategory,
3939
final boolean includeMusicCategory)
40-
throws ExecutionException, InterruptedException, UnsupportedEncodingException {
41-
40+
throws UnsupportedEncodingException {
4241
final ArrayList<String> categoryParamList = new ArrayList<>();
4342

4443
if (includeSponsorCategory) {
@@ -76,7 +75,30 @@ public VideoSegment[] getYouTubeVideoSegments(final String videoId,
7675
final String params = "skipSegments/" + videoIdHash.substring(0, 4)
7776
+ "?categories=" + categoryParams;
7877

79-
final JsonArray responseArray = execute(params).get();
78+
if (!isConnected()) {
79+
return null;
80+
}
81+
82+
JsonArray responseArray = null;
83+
84+
try {
85+
final String responseBody =
86+
DownloaderImpl
87+
.getInstance()
88+
.get(apiUrl + params)
89+
.responseBody();
90+
91+
responseArray = JsonParser.array().from(responseBody);
92+
93+
} catch (final Exception ex) {
94+
if (DEBUG) {
95+
Log.w(TAG, Log.getStackTraceString(ex));
96+
}
97+
}
98+
99+
if (responseArray == null) {
100+
return null;
101+
}
80102

81103
final ArrayList<VideoSegment> result = new ArrayList<>();
82104

@@ -113,38 +135,14 @@ public VideoSegment[] getYouTubeVideoSegments(final String videoId,
113135
return result.toArray(new VideoSegment[0]);
114136
}
115137

116-
@Override
117-
protected JsonArray doInBackground(final String... strings) {
118-
if (isCancelled() || !isConnected()) {
119-
return null;
120-
}
121-
122-
try {
123-
final String responseBody =
124-
DownloaderImpl
125-
.getInstance()
126-
.get(apiUrl + strings[0])
127-
.responseBody();
128-
129-
return JsonParser.array().from(responseBody);
130-
131-
} catch (final Exception ex) {
132-
if (DEBUG) {
133-
Log.w(TAG, Log.getStackTraceString(ex));
134-
}
135-
}
136-
137-
return null;
138-
}
139-
140-
private boolean isConnected() {
138+
private static boolean isConnected() {
141139
final ConnectivityManager cm =
142140
(ConnectivityManager) APP.getSystemService(Context.CONNECTIVITY_SERVICE);
143141
return cm.getActiveNetworkInfo() != null
144142
&& cm.getActiveNetworkInfo().isConnected();
145143
}
146144

147-
private String toSha256(final String videoId) {
145+
private static String toSha256(final String videoId) {
148146
try {
149147
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
150148
final byte[] bytes = digest.digest(videoId.getBytes(StandardCharsets.UTF_8));

0 commit comments

Comments
 (0)