8
8
import com .google .android .apps .muzei .api .Artwork ;
9
9
import com .google .android .apps .muzei .api .RemoteMuzeiArtSource ;
10
10
import com .tasomaniac .muzei .comiccovers .model .Comic ;
11
+ import com .tasomaniac .muzei .comiccovers .util .IOUtil ;
11
12
12
13
import java .util .Random ;
13
14
14
15
import retrofit .ErrorHandler ;
15
16
import retrofit .RequestInterceptor ;
16
17
import retrofit .RestAdapter ;
17
18
import retrofit .RetrofitError ;
19
+ import timber .log .Timber ;
18
20
19
21
public class ComicVineArtSource extends RemoteMuzeiArtSource {
20
22
private static final String SOURCE_NAME = "ComicVineArtSource" ;
21
23
public static final String NUM_OF_TOTAL_RESULTS = "number_of_total_results" ;
22
24
23
- private static final int ROTATE_TIME_MILLIS = 24 * 60 * 60 * 1000 ; // rotate every 3 hours
25
+ private static final int ROTATE_TIME_MILLIS = 24 * 60 * 60 * 1000 ; // rotate every 24 hours
26
+ private static final int NEXT_ON_ERROR_TIME_MILLIS = 60 * 60 * 1000 ; // rotate every 24 hours
24
27
25
28
Random random ;
26
29
@@ -60,14 +63,15 @@ public Throwable handleError(RetrofitError retrofitError) {
60
63
|| (500 <= statusCode && statusCode < 600 )) {
61
64
return new RetryException ();
62
65
}
63
- scheduleUpdate (System .currentTimeMillis () + ROTATE_TIME_MILLIS );
66
+ scheduleUpdate (System .currentTimeMillis () + NEXT_ON_ERROR_TIME_MILLIS );
67
+
68
+ Timber .d (retrofitError , "Error getting the image %s" , retrofitError .getUrl ());
69
+
64
70
return retrofitError ;
65
71
}
66
72
})
67
73
.build ();
68
74
69
-
70
-
71
75
final ComicVineService service = restAdapter .create (ComicVineService .class );
72
76
Comic comic = getNextComic (service );
73
77
if (comic != null ) {
@@ -76,8 +80,10 @@ public Throwable handleError(RetrofitError retrofitError) {
76
80
name .append (comic .getVolume ().getName ());
77
81
}
78
82
if (comic .getName () != null ) {
79
- name .append (" | " )
80
- .append (comic .getName ());
83
+ if (name .length () > 0 ) {
84
+ name .append (" | " );
85
+ }
86
+ name .append (comic .getName ());
81
87
}
82
88
83
89
publishArtwork (new Artwork .Builder ()
@@ -96,26 +102,36 @@ public Throwable handleError(RetrofitError retrofitError) {
96
102
private Comic getNextComic (final ComicVineService service ) throws RetryException {
97
103
String currentToken = (getCurrentArtwork () != null ) ? getCurrentArtwork ().getToken () : null ;
98
104
105
+ String offset = String .valueOf (random .nextInt (max_size ));
99
106
ComicVineService .ComicVineResponse response =
100
- service .getIssues (String . valueOf ( random . nextInt ( max_size )) );
107
+ service .getIssues (offset );
101
108
102
109
if (response == null || response .getResults () == null ) {
110
+ Timber .d ("Error getting the image %s" , offset );
103
111
throw new RetryException ();
104
112
}
105
113
106
114
max_size = response .getNumberOfTotalResult ();
107
115
prefs .edit ().putInt (NUM_OF_TOTAL_RESULTS , max_size ).apply ();
108
116
109
117
if (response .getResults ().size () < 1 ) {
110
- scheduleUpdate (System .currentTimeMillis () + ROTATE_TIME_MILLIS );
118
+ scheduleUpdate (System .currentTimeMillis () + NEXT_ON_ERROR_TIME_MILLIS );
111
119
return null ;
112
120
}
113
121
Comic comic = response .getResults ().get (0 );
114
122
String newToken = String .valueOf (comic .getId ());
115
- if (currentToken == null || !currentToken .equals (newToken )) {
116
- return comic ;
117
- } else {
118
- return getNextComic (service );
123
+ try {
124
+ boolean isContentValid = IOUtil .checkContentType (getApplicationContext (),
125
+ Uri .parse (comic .getImage ().getSuperUrl ()), "image/" );
126
+ if ((currentToken == null || !currentToken .equals (newToken )) && isContentValid ) {
127
+ return comic ;
128
+ } else {
129
+ return getNextComic (service );
130
+ }
131
+ } catch (IOUtil .OpenUriException e ) {
132
+ Timber .e (e , "Error on the image. Will try in an hour." );
133
+ scheduleUpdate (System .currentTimeMillis () + NEXT_ON_ERROR_TIME_MILLIS );
134
+ return null ;
119
135
}
120
136
}
121
137
}
0 commit comments