diff --git a/library/src/main/java/com/danikula/videocache/ErrorCacheListener.java b/library/src/main/java/com/danikula/videocache/ErrorCacheListener.java new file mode 100644 index 0000000..9e8ea56 --- /dev/null +++ b/library/src/main/java/com/danikula/videocache/ErrorCacheListener.java @@ -0,0 +1,13 @@ +package com.danikula.videocache; + +import java.io.File; + +/** + * Listener for cache error. + * + * @author Sasha Karamyshev (sheckspir88@gmail.com) + */ +public interface ErrorCacheListener { + + void onCacheUnavailable(Throwable throwable); +} diff --git a/library/src/main/java/com/danikula/videocache/HttpProxyCache.java b/library/src/main/java/com/danikula/videocache/HttpProxyCache.java index 010354f..f5aa763 100644 --- a/library/src/main/java/com/danikula/videocache/HttpProxyCache.java +++ b/library/src/main/java/com/danikula/videocache/HttpProxyCache.java @@ -23,7 +23,8 @@ class HttpProxyCache extends ProxyCache { private final HttpUrlSource source; private final FileCache cache; - private CacheListener listener; + private CacheListener cacheListener; + private ErrorCacheListener errorListener; public HttpProxyCache(HttpUrlSource source, FileCache cache) { super(source, cache); @@ -32,7 +33,11 @@ public HttpProxyCache(HttpUrlSource source, FileCache cache) { } public void registerCacheListener(CacheListener cacheListener) { - this.listener = cacheListener; + this.cacheListener = cacheListener; + } + + public void registerErrorListener(ErrorCacheListener errorCacheListener) { + this.errorListener = errorCacheListener; } public void processRequest(GetRequest request, Socket socket) throws IOException, ProxyCacheException { @@ -105,8 +110,16 @@ private String format(String pattern, Object... args) { @Override protected void onCachePercentsAvailableChanged(int percents) { - if (listener != null) { - listener.onCacheAvailable(cache.file, source.getUrl(), percents); + if (cacheListener != null) { + cacheListener.onCacheAvailable(cache.file, source.getUrl(), percents); + } + } + + @Override + protected void onErrorCache(Throwable throwable) { + super.onErrorCache(throwable); + if (errorListener != null) { + errorListener.onCacheUnavailable(throwable); } } } diff --git a/library/src/main/java/com/danikula/videocache/ProxyCache.java b/library/src/main/java/com/danikula/videocache/ProxyCache.java index eeea971..bb27c40 100644 --- a/library/src/main/java/com/danikula/videocache/ProxyCache.java +++ b/library/src/main/java/com/danikula/videocache/ProxyCache.java @@ -101,6 +101,10 @@ private void notifyNewCacheDataAvailable(long cacheAvailable, long sourceAvailab } } + private void notifyCacheError(Throwable throwable){ + onErrorCache(throwable); + } + protected void onCacheAvailable(long cacheAvailable, long sourceLength) { boolean zeroLengthSource = sourceLength == 0; int percents = zeroLengthSource ? 100 : (int) ((float) cacheAvailable / sourceLength * 100); @@ -115,6 +119,10 @@ protected void onCacheAvailable(long cacheAvailable, long sourceLength) { protected void onCachePercentsAvailableChanged(int percentsAvailable) { } + protected void onErrorCache(Throwable throwable) { + // do nothing + } + private void readSource() { long sourceAvailable = -1; long offset = 0; @@ -139,9 +147,12 @@ private void readSource() { } catch (Throwable e) { readSourceErrorsCount.incrementAndGet(); onError(e); + notifyCacheError(e); } finally { closeSource(); - notifyNewCacheDataAvailable(offset, sourceAvailable); + synchronized (wc) { + wc.notifyAll(); + } } }