From 8f60f8926c47b7c461cae5fd4e7d42843bbf64bd Mon Sep 17 00:00:00 2001 From: Adamantcheese Date: Wed, 26 Feb 2020 19:53:06 -0800 Subject: [PATCH] Adjustments for #742 per deeper analysis of other DataSource implementations --- .../cache/stream/WebmStreamingDataSource.java | 16 +++++++++++----- .../core/cache/stream/WebmStreamingSource.kt | 6 ++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/cache/stream/WebmStreamingDataSource.java b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/cache/stream/WebmStreamingDataSource.java index 1d30e7294a..6e4504518c 100644 --- a/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/cache/stream/WebmStreamingDataSource.java +++ b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/cache/stream/WebmStreamingDataSource.java @@ -318,7 +318,7 @@ public long open(DataSpec dataSpec) } transferStarted(dataSpec); - + opened = true; return bytesRemaining; } @@ -345,7 +345,7 @@ public int read(byte[] buffer, int offset, int readLength) throws IOException { if (readLength == 0) { return 0; - } else if (bytesRemaining() == 0) { + } else if (bytesRemaining() == 0 || dataSource == null) { return C.RESULT_END_OF_INPUT; } @@ -389,11 +389,10 @@ public void cacheComplete() { BackgroundUtils.runOnMainThread(() -> { for (Callback c : listeners) { - if (c != null) c.dataSourceAddedFile(innerFile); + c.dataSourceAddedFile(innerFile); } - listeners.clear(); - partialFileCache.clearListeners(); + clearListeners(); }); } @@ -401,6 +400,11 @@ public void addListener(Callback c) { if (c != null) listeners.add(c); } + public void clearListeners() { + listeners.clear(); + partialFileCache.clearListeners(); + } + @Nullable @Override public Uri getUri() { @@ -411,9 +415,11 @@ public Uri getUri() { public void close() throws IOException { Logger.i(TAG, "close"); + clearListeners(); try { if (dataSource != null) { dataSource.close(); + dataSource = null; } } finally { if (opened) { diff --git a/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/cache/stream/WebmStreamingSource.kt b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/cache/stream/WebmStreamingSource.kt index 176ca7da16..53ec192ca7 100644 --- a/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/cache/stream/WebmStreamingSource.kt +++ b/Kuroba/app/src/main/java/com/github/adamantcheese/chan/core/cache/stream/WebmStreamingSource.kt @@ -27,10 +27,8 @@ class WebmStreamingSource( val fileCacheSource = WebmStreamingDataSource(uri, rawFile, fileManager) fileCacheSource.addListener { file -> - run { - BackgroundUtils.ensureMainThread() - cacheHandler.fileWasAdded(file.length()) - } + BackgroundUtils.ensureMainThread() + cacheHandler.fileWasAdded(file.length()) } if (alreadyExists