22
33import android .os .Handler ;
44import android .os .Looper ;
5+ import android .os .Message ;
56import android .util .Log ;
67
78import java .util .concurrent .atomic .AtomicInteger ;
@@ -25,7 +26,7 @@ public class ProxyCache {
2526 private final Source source ;
2627 private final Cache cache ;
2728 private final Object wc ;
28- private final Handler handler ;
29+ private final ListenerHandler handler ;
2930 private volatile Thread sourceReaderThread ;
3031 private volatile boolean stopped ;
3132 private final AtomicInteger readSourceErrorsCount ;
@@ -37,7 +38,7 @@ public ProxyCache(Source source, Cache cache, boolean logEnabled) {
3738 this .cache = checkNotNull (cache );
3839 this .logEnabled = logEnabled ;
3940 this .wc = new Object ();
40- this .handler = new Handler ( Looper . getMainLooper () );
41+ this .handler = new ListenerHandler ( );
4142 this .readSourceErrorsCount = new AtomicInteger ();
4243 }
4344
@@ -93,7 +94,6 @@ public void shutdown() {
9394 }
9495
9596 private void readSourceAsync () throws ProxyCacheException {
96-
9797 boolean readingInProgress = sourceReaderThread != null && sourceReaderThread .getState () != Thread .State .TERMINATED ;
9898 if (!stopped && !cache .isCompleted () && !readingInProgress ) {
9999 sourceReaderThread = new Thread (new SourceReaderRunnable (), "Source reader for ProxyCache" );
@@ -111,15 +111,8 @@ private void waitForSourceData() throws ProxyCacheException {
111111 }
112112 }
113113
114- private void notifyNewCacheDataAvailable (final int cachePercentage ) {
115- handler .post (new Runnable () {
116- @ Override
117- public void run () {
118- if (cacheListener != null ) {
119- cacheListener .onCacheDataAvailable (cachePercentage );
120- }
121- }
122- });
114+ private void notifyNewCacheDataAvailable (int cachePercentage ) {
115+ handler .deliverCachePercentage (cachePercentage );
123116
124117 synchronized (wc ) {
125118 wc .notifyAll ();
@@ -165,7 +158,7 @@ private void closeSource() {
165158
166159 protected final void onError (final Throwable e ) {
167160 Log .e (LOG_TAG , "ProxyCache error" , e );
168- handler .post ( new ErrorDeliverer ( e ) );
161+ handler .deliverError ( e );
169162 }
170163
171164 protected boolean isLogEnabled () {
@@ -180,22 +173,53 @@ public void run() {
180173 }
181174 }
182175
183- private class ErrorDeliverer implements Runnable {
176+ private final class ListenerHandler extends Handler {
177+
178+ private static final int MSG_ERROR = 1 ;
179+ private static final int MSG_CACHE_PERCENTAGE = 2 ;
184180
185- private final Throwable error ;
181+ public ListenerHandler () {
182+ super (Looper .getMainLooper ());
183+ }
186184
187- public ErrorDeliverer (Throwable error ) {
188- this .error = error ;
185+ public void deliverCachePercentage (int percents ) {
186+ if (cacheListener != null ) {
187+ send (MSG_CACHE_PERCENTAGE , percents , null );
188+ }
189+ }
190+
191+ public void deliverError (Throwable error ) {
192+ if (isFatalError (error ) || cacheListener != null ) {
193+ send (MSG_ERROR , 0 , error );
194+ }
195+ }
196+
197+ private boolean isFatalError (Throwable error ) {
198+ return !(error instanceof ProxyCacheException );
199+ }
200+
201+ private void send (int what , int arg1 , Object data ) {
202+ Message message = obtainMessage (what );
203+ message .arg1 = arg1 ;
204+ message .obj = data ;
205+ sendMessage (message );
189206 }
190207
191208 @ Override
192- public void run () {
193- if (error instanceof ProxyCacheException ) {
194- if (cacheListener != null ) {
209+ public void handleMessage (Message msg ) {
210+ switch (msg .what ) {
211+ case MSG_CACHE_PERCENTAGE :
212+ cacheListener .onCacheDataAvailable (msg .arg1 );
213+ break ;
214+ case MSG_ERROR :
215+ Throwable error = (Throwable ) msg .obj ;
216+ if (isFatalError (error )) {
217+ throw new RuntimeException ("Unexpected error!" , error );
218+ }
195219 cacheListener .onError ((ProxyCacheException ) error );
196- }
197- } else {
198- throw new RuntimeException ("Unexpected error!" , error );
220+ break ;
221+ default :
222+ throw new RuntimeException ("Unknown message " + msg );
199223 }
200224 }
201225 }
0 commit comments