From 062b50f92420e7962877abfcf040563ff2433b20 Mon Sep 17 00:00:00 2001 From: jakweg Date: Tue, 25 Aug 2020 13:46:20 +0200 Subject: [PATCH] Removed useless caching functionality --- .../java/pl/jakubweg/PlayerController.java | 6 +- .../SponsorBlockPreferenceFragment.java | 34 ------- .../pl/jakubweg/SponsorBlockSettings.java | 15 +--- .../java/pl/jakubweg/SponsorBlockUtils.java | 88 +------------------ .../main/java/pl/jakubweg/SponsorSegment.java | 19 ---- app/src/main/res/values/strings.xml | 3 - 6 files changed, 8 insertions(+), 157 deletions(-) diff --git a/app/src/main/java/pl/jakubweg/PlayerController.java b/app/src/main/java/pl/jakubweg/PlayerController.java index 62c6dac..94a3d13 100644 --- a/app/src/main/java/pl/jakubweg/PlayerController.java +++ b/app/src/main/java/pl/jakubweg/PlayerController.java @@ -75,7 +75,7 @@ public class PlayerController { sponsorTimer.schedule(new TimerTask() { @Override public void run() { - executeDownloadSegments(currentVideoId, false); + executeDownloadSegments(currentVideoId); } }, 0); } @@ -114,8 +114,8 @@ public class PlayerController { } } - public static void executeDownloadSegments(String videoId, boolean ignoreCache) { - SponsorSegment[] segments = SponsorBlockUtils.getSegmentsForVideo(videoId, ignoreCache); + public static void executeDownloadSegments(String videoId) { + SponsorSegment[] segments = SponsorBlockUtils.getSegmentsForVideo(videoId); Arrays.sort(segments); if (VERBOSE) diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java index 74f1a1a..c946219 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java @@ -21,7 +21,6 @@ import java.util.ArrayList; import static pl.jakubweg.SponsorBlockSettings.DefaultBehaviour; import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP; -import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_CACHE_SEGMENTS; 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_TOAST_WHEN_SKIP; @@ -29,7 +28,6 @@ import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_SPONSOR_BLOCK_ENA import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_UUID; import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_NAME; import static pl.jakubweg.SponsorBlockSettings.adjustNewSegmentMillis; -import static pl.jakubweg.SponsorBlockSettings.cacheEnabled; import static pl.jakubweg.SponsorBlockSettings.countSkips; import static pl.jakubweg.SponsorBlockSettings.showToastWhenSkippedAutomatically; import static pl.jakubweg.SponsorBlockSettings.uuid; @@ -210,38 +208,6 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement screen.addPreference(preference); preferencesToDisableWhenSBDisabled.add(preference); } - - { - Preference preference = new SwitchPreference(context); - preference.setTitle(str("general_cache")); - preference.setSummary(str("general_cache_sum")); - preference.setKey(PREFERENCES_KEY_CACHE_SEGMENTS); - preference.setDefaultValue(cacheEnabled); - screen.addPreference(preference); - preferencesToDisableWhenSBDisabled.add(preference); - } - - { - Preference preference = new Preference(context); - preference.setTitle(str("general_cache_clear")); - preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - File cacheDirectory = SponsorBlockSettings.cacheDirectory; - if (cacheDirectory != null) { - for (File file : cacheDirectory.listFiles()) { - if (!file.delete()) - return false; - } - Toast.makeText(getActivity(), str("done"), Toast.LENGTH_SHORT).show(); - } - return false; - } - }); - preferencesToDisableWhenSBDisabled.add(preference); - screen.addPreference(preference); - } - } @Override diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java b/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java index f050fa1..71f9f7c 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockSettings.java @@ -22,7 +22,6 @@ public class SponsorBlockSettings { 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_CACHE_SEGMENTS = "cache-enabled"; public static final String PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP = "new-segment-step-accuracy"; public static final String PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED = "sb-enabled"; public static final String PREFERENCES_KEY_NEW_SEGMENT_ENABLED = "sb-new-segment-enabled"; @@ -36,10 +35,8 @@ public class SponsorBlockSettings { public static boolean isAddNewSegmentEnabled = false; public static boolean showToastWhenSkippedAutomatically = true; public static boolean countSkips = true; - public static boolean cacheEnabled = true; public static int adjustNewSegmentMillis = 150; public static String uuid = ""; - public static File cacheDirectory; private static String sponsorBlockUrlCategories = "[]"; @SuppressWarnings("unused") @@ -58,11 +55,6 @@ public class SponsorBlockSettings { public static void update(Context context) { if (context == null) return; - File directory = cacheDirectory = new File(context.getCacheDir(), CACHE_DIRECTORY_NAME); - if (!directory.mkdirs() && !directory.exists()) { - Log.e("jakubweg.Settings", "Unable to create cache directory"); - cacheDirectory = null; - } SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE); isSponsorBlockEnabled = preferences.getBoolean(PREFERENCES_KEY_SPONSOR_BLOCK_ENABLED, isSponsorBlockEnabled); @@ -114,10 +106,9 @@ public class SponsorBlockSettings { showToastWhenSkippedAutomatically = preferences.getBoolean(PREFERENCES_KEY_SHOW_TOAST_WHEN_SKIP, showToastWhenSkippedAutomatically); - cacheEnabled = preferences.getBoolean(PREFERENCES_KEY_CACHE_SEGMENTS, true); - adjustNewSegmentMillis = Integer.parseInt(preferences - .getString(PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP, - String.valueOf(adjustNewSegmentMillis))); + String tmp1 = preferences.getString(PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP, null); + if (tmp1 != null) + adjustNewSegmentMillis = Integer.parseInt(tmp1); uuid = preferences.getString(PREFERENCES_KEY_UUID, null); diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java index c5fc0be..3196881 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockUtils.java @@ -250,7 +250,7 @@ public abstract class SponsorBlockUtils { } if (videoId != null) - PlayerController.executeDownloadSegments(videoId, true); + PlayerController.executeDownloadSegments(videoId); } }; @@ -453,70 +453,9 @@ public abstract class SponsorBlockUtils { } } - public synchronized static SponsorSegment[] getSegmentsForVideo(String videoId, boolean ignoreCache) { + public synchronized static SponsorSegment[] getSegmentsForVideo(String videoId) { newSponsorSegmentEndMillis = newSponsorSegmentStartMillis = -1; - int usageCounter = 0; - if (!ignoreCache && SponsorBlockSettings.cacheEnabled) { - - File cacheDirectory = SponsorBlockSettings.cacheDirectory; - if (cacheDirectory == null) { - Log.w(TAG, "Cache directory is null, cannot read"); - } else { - File file = new File(cacheDirectory, videoId); - try { - RandomAccessFile rwd = new RandomAccessFile(file, "rw"); - rwd.seek(0); - usageCounter = rwd.readInt(); - long now = System.currentTimeMillis(); - long savedTimestamp = rwd.readLong(); - int segmentsSize = rwd.readInt(); - byte maxDaysCache; - if (usageCounter < 2) - maxDaysCache = 0; - else if (usageCounter < 5 || segmentsSize == 0) - maxDaysCache = 2; - else if (usageCounter < 10) - maxDaysCache = 5; - else - maxDaysCache = 10; - - - if (VERBOSE) - Log.d(TAG, String.format("Read cache data about segments, counter=%d, timestamp=%d, now=%d, maxCacheDays=%s, segmentsSize=%d", - usageCounter, savedTimestamp, now, maxDaysCache, segmentsSize)); - - if (savedTimestamp + (((long) maxDaysCache) * 24 * 60 * 60 * 1000) > now) { - if (VERBOSE) - Log.d(TAG, "getSegmentsForVideo: cacheHonored videoId=" + videoId); - - SponsorSegment[] segments = new SponsorSegment[segmentsSize]; - for (int i = 0; i < segmentsSize; i++) { - segments[i] = SponsorSegment.readFrom(rwd); - } - - rwd.seek(0); - rwd.writeInt(usageCounter + 1); - rwd.close(); - if (VERBOSE) - Log.d(TAG, "getSegmentsForVideo: reading from cache and updating usageCounter finished"); - - return segments; - } else { - if (VERBOSE) - Log.d(TAG, "getSegmentsForVideo: cache of video " + videoId + " was not honored, fallback to downloading..."); - } - } catch (FileNotFoundException | EOFException ignored) { - if (VERBOSE) - Log.e(TAG, "FileNotFoundException | EOFException ignored"); - } catch (Exception e) { - //noinspection ResultOfMethodCallIgnored - file.delete(); - Log.e(TAG, "Error while reading cached segments", e); - } - } - } - ArrayList sponsorSegments = new ArrayList<>(); try { if (VERBOSE) @@ -569,29 +508,6 @@ public abstract class SponsorBlockUtils { connection.disconnect(); - if (SponsorBlockSettings.cacheEnabled) { - File cacheDirectory = SponsorBlockSettings.cacheDirectory; - if (cacheDirectory == null) { - Log.w(TAG, "Cache directory is null"); - } else { - File file = new File(cacheDirectory, videoId); - try { - DataOutputStream stream = new DataOutputStream(new FileOutputStream(file)); - stream.writeInt(usageCounter + 1); - stream.writeLong(System.currentTimeMillis()); - stream.writeInt(sponsorSegments.size()); - for (SponsorSegment segment : sponsorSegments) { - segment.writeTo(stream); - } - stream.close(); - } catch (Exception e) { - //noinspection ResultOfMethodCallIgnored - file.delete(); - Log.e(TAG, "Unable to write segments to file", e); - } - } - } - } catch (Exception e) { Log.e(TAG, "download segments failed", e); } diff --git a/app/src/main/java/pl/jakubweg/SponsorSegment.java b/app/src/main/java/pl/jakubweg/SponsorSegment.java index 7f25c0b..f96117d 100644 --- a/app/src/main/java/pl/jakubweg/SponsorSegment.java +++ b/app/src/main/java/pl/jakubweg/SponsorSegment.java @@ -1,9 +1,5 @@ package pl.jakubweg; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.RandomAccessFile; - public class SponsorSegment implements Comparable { public final long start; public final long end; @@ -17,15 +13,6 @@ public class SponsorSegment implements Comparable { this.UUID = UUID; } - public static SponsorSegment readFrom(RandomAccessFile stream) throws IOException { - long start = stream.readLong(); - long end = stream.readLong(); - String categoryName = stream.readUTF(); - String UUID = stream.readUTF(); - SponsorBlockSettings.SegmentInfo category = SponsorBlockSettings.SegmentInfo.valueOf(categoryName); - return new SponsorSegment(start, end, category, UUID); - } - @Override public String toString() { return "SegmentInfo{" + @@ -40,10 +27,4 @@ public class SponsorSegment implements Comparable { return (int) (this.start - o.start); } - public void writeTo(DataOutputStream stream) throws IOException { - stream.writeLong(start); - stream.writeLong(end); - stream.writeUTF(category.name()); - stream.writeUTF(UUID); - } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a0c075a..8b54953 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -153,9 +153,6 @@ This is the number of milliseconds you can move when you use the time adjustment buttons while adding new segment Your unique user id This should be kept private. This is like a password and should not be shared with anyone. If someone has this, they can impersonate you - Cache segments locally - Frequently watched videos (eg. music videos) may store segments in app cache to make skipping segments faster - Clear sponsor block segments cache Sponsor Paid promotion, paid referrals and direct advertisements Intermission/Intro Animation