@@ -73,10 +73,11 @@ public DownloadManager(Context ctx, boolean use_https, Handler handler) {
7373 }
7474 this .ctx = ctx ;
7575 this .use_https = use_https ;
76- this . legacy_mode = legacy_mode ;
76+
7777 if (BuildConfig .BUILD_TYPE .equals ("release" )) {
7878 logging_enabled = false ;
7979 }
80+
8081 try {
8182 Log .v (OvkApplication .DL_TAG , "Starting DownloadManager..." );
8283 SSLContext sslContext = null ;
@@ -184,6 +185,7 @@ public void downloadPhotosToCache(final ArrayList<PhotoAttachment> photoAttachme
184185
185186 @ Override
186187 public void run () {
188+
187189 try {
188190 File directory = new File (String .format ("%s/%s/photos_cache" ,
189191 ctx .getCacheDir ().getAbsolutePath (), instance ), where );
@@ -193,31 +195,41 @@ public void run() {
193195 } catch (Exception ex ) {
194196 ex .printStackTrace ();
195197 }
198+
196199 for (int i = 0 ; i < photoAttachments .size (); i ++) {
197200 filesize = 0 ;
198201 filename = photoAttachments .get (i ).filename ;
202+
199203 File downloadedFile = new File (String .format ("%s/%s/photos_cache/%s" ,
200204 ctx .getCacheDir ().getAbsolutePath (), instance , where ), filename );
205+
201206 PhotoAttachment photoAttachment = photoAttachments .get (i );
207+
202208 if (photoAttachment .url == null ) {
203209 photoAttachment .url = "" ;
204210 }
211+
205212 Date lastModDate ;
213+
206214 if (downloadedFile .exists ()) {
207215 lastModDate = new Date (downloadedFile .lastModified ());
208216 } else {
209217 lastModDate = new Date (0 );
210218 }
219+
211220 long time_diff = System .currentTimeMillis () - lastModDate .getTime ();
212221 TimeUnit timeUnit = TimeUnit .MILLISECONDS ;
222+
213223 // photo autocaching
214224 if (forceCaching && downloadedFile .exists () && downloadedFile .length () >= 5120 &&
215225 timeUnit .convert (time_diff ,TimeUnit .MILLISECONDS ) >= 360000L &&
216226 timeUnit .convert (time_diff ,TimeUnit .MILLISECONDS ) < 259200000L ) {
217- if (logging_enabled ) Log .e (OvkApplication .DL_TAG , "Duplicated filename. Skipping..." +
218- "\r \n TimeDiff: " + timeUnit .convert (time_diff ,TimeUnit .MILLISECONDS )
227+ if (logging_enabled )
228+ Log .w (OvkApplication .DL_TAG , "Duplicated filename: " + filename + ". Skipping..." +
229+ "\r \n TimeDiff: " + timeUnit .convert (time_diff , TimeUnit .MILLISECONDS )
219230 + " ms | Filesize: " + downloadedFile .length () + " bytes" );
220- } else if (photoAttachment .url .length () == 0 ) {
231+
232+ } else if (photoAttachment .url .isEmpty ()) {
221233 filename = photoAttachment .filename ;
222234 if (logging_enabled ) Log .e (OvkApplication .DL_TAG ,
223235 "Invalid or empty URL. Skipping..." );
@@ -232,15 +244,12 @@ public void run() {
232244 } catch (Exception e ) {
233245 e .printStackTrace ();
234246 }
247+
235248 } else {
236249 try {
237250 filename = photoAttachment .filename ;
238- String short_address = "" ;
239- if (photoAttachments .get (i ).url .length () > 40 ) {
240- short_address = photoAttachments .get (i ).url .substring (0 , 39 );
241- } else {
242- short_address = photoAttachments .get (i ).url ;
243- }
251+ String short_address = url .length () > 60 ? url .substring (0 , 60 ) : url ;
252+
244253 //Log.v("DownloadManager", String.format("Downloading %s (%d/%d)...",
245254 // short_address, i + 1, photoAttachments.size()));
246255 url = photoAttachments .get (i ).url ;
@@ -260,6 +269,7 @@ public void run() {
260269 content_length = response .body ().contentLength ();
261270 downloadedFile = new File (String .format ("%s/%s/photos_cache/%s" ,
262271 ctx .getCacheDir ().getAbsolutePath (), instance , where ), filename );
272+
263273 if (!downloadedFile .exists () || content_length != downloadedFile .length ()) {
264274 FileOutputStream fos = new FileOutputStream (downloadedFile );
265275 int inByte ;
@@ -271,15 +281,19 @@ public void run() {
271281 } else {
272282 if (logging_enabled ) Log .w ("DownloadManager" , "Filesizes match, skipping..." );
273283 }
284+
274285 response .body ().byteStream ().close ();
286+
275287 if (logging_enabled ) Log .d (OvkApplication .DL_TAG ,
276288 String .format ("Downloaded from %s (%s): %d kB (%d/%d)" ,
277289 short_address , response_code , (int ) (filesize / 1024 ), i + 1 ,
278290 photoAttachments .size ()));
291+
279292 } catch (IOException | OutOfMemoryError ex ) {
280293 if (logging_enabled ) Log .e (OvkApplication .DL_TAG ,
281294 String .format ("Download error: %s (%d/%d)" , ex .getMessage (), i + 1 ,
282295 photoAttachments .size ()));
296+
283297 if (ex .getMessage () != null ) {
284298 if (ex .getMessage ().startsWith ("Authorization required" )) {
285299 response_code = 401 ;
@@ -289,13 +303,15 @@ public void run() {
289303 response_code = Integer .parseInt (code_str );
290304 }
291305 }
306+
292307 } catch (Exception e ) {
293308 photoAttachment .error = e .getClass ().getSimpleName ();
294309 if (logging_enabled ) Log .e (OvkApplication .DL_TAG ,
295310 String .format ("Download error: %s (%d/%d)" , e .getMessage (), i + 1 ,
296311 photoAttachments .size ()));
297312 }
298313 }
314+
299315 if (i % 15 == 0 || i == photoAttachments .size () - 1 ) {
300316 switch (where ) {
301317 case "account_avatar" ->
@@ -343,7 +359,10 @@ public void downloadOnePhotoToCache(final String url, final String filename, fin
343359 return ;
344360 }
345361 if (!url .startsWith ("http://" ) && !url .startsWith ("https://" )) {
346- Log .e (OvkApplication .DL_TAG , String .format ("Invalid URL: %s. Download canceled." , url ));
362+ Log .e (
363+ OvkApplication .DL_TAG ,
364+ String .format ("Invalid URL: %s. Download canceled." , url )
365+ );
347366 return ;
348367 }
349368 Runnable httpRunnable = new Runnable () {
@@ -368,22 +387,29 @@ public void run() {
368387 filesize = 0 ;
369388 File downloadedFile = new File (String .format ("%s/%s/photos_cache/%s" ,
370389 ctx .getCacheDir ().getAbsolutePath (), instance , where ), filename );
390+
371391 Date lastModDate ;
372392 if (downloadedFile .exists ()) {
373393 lastModDate = new Date (downloadedFile .lastModified ());
374394 } else {
375395 lastModDate = new Date (0 );
376396 }
397+
377398 long time_diff = System .currentTimeMillis () - lastModDate .getTime ();
378399 TimeUnit timeUnit = TimeUnit .MILLISECONDS ;
400+
379401 if (forceCaching && downloadedFile .exists () && downloadedFile .length () >= 5120 &&
380402 timeUnit .convert (time_diff ,TimeUnit .MILLISECONDS ) >= 360000L &&
381403 timeUnit .convert (time_diff ,TimeUnit .MILLISECONDS ) < 259200000L ) {
382- if (logging_enabled ) Log .e (OvkApplication .DL_TAG , "Duplicated filename. Skipping..." +
404+ if (logging_enabled )
405+ Log .w (OvkApplication .DL_TAG , "Duplicated filename: " + filename + ". Skipping..." +
383406 "\r \n TimeDiff: " + timeUnit .convert (time_diff ,TimeUnit .MILLISECONDS )
384407 + " ms | Filesize: " + downloadedFile .length () + " bytes" );
385- } else if (url .length () == 0 ) {
408+
409+ } else if (url .isEmpty ()) {
410+
386411 if (logging_enabled ) Log .e (OvkApplication .DL_TAG , "Invalid address. Skipping..." );
412+
387413 try {
388414 if (downloadedFile .exists ()) {
389415 FileOutputStream fos = new FileOutputStream (downloadedFile );
@@ -396,19 +422,16 @@ public void run() {
396422 e .printStackTrace ();
397423 }
398424 } else {
399- String short_address = "" ;
400- if (url .length () > 40 ) {
401- short_address = url .substring (0 , 39 );
402- } else {
403- short_address = url ;
404- }
425+ String short_address = url .length () > 60 ? url .substring (0 , 60 ) : url ;
405426
406427 if (logging_enabled ) Log .v ("DownloadManager" ,
407428 String .format ("Downloading %s..." , short_address ));
429+
408430 request = new Request .Builder ()
409431 .url (url )
410432 .addHeader ("User-Agent" , generateUserAgent (ctx ))
411433 .build ();
434+
412435 try {
413436 Response response = httpClient .newCall (request ).execute ();
414437 response_code = response .code ();
@@ -420,9 +443,11 @@ public void run() {
420443 }
421444 fos .close ();
422445 response .body ().byteStream ().close ();
446+
423447 if (response != null ){
424448 response .close ();
425449 }
450+
426451 if (response_code == 200 ) {
427452 if (logging_enabled ) Log .v ("DownloadManager" ,
428453 String .format ("Downloaded from %s (%s): %d kB" , short_address ,
@@ -489,14 +514,11 @@ private void sendMessage(final int message, String response) {
489514 bundle .putString ("response" , response );
490515 bundle .putString ("address" , apiListeners .from );
491516 msg .setData (bundle );
492- handler .post (new Runnable () {
493- @ Override
494- public void run () {
495- if (message < 0 ) {
496- apiListeners .failListener .onAPIFailed (ctx , message , bundle );
497- } else {
498- apiListeners .successListener .onAPISuccess (ctx , message , bundle );
499- }
517+ handler .post (() -> {
518+ if (message < 0 ) {
519+ apiListeners .failListener .onAPIFailed (ctx , message , bundle );
520+ } else {
521+ apiListeners .successListener .onAPISuccess (ctx , message , bundle );
500522 }
501523 });
502524 }
@@ -509,14 +531,11 @@ private void sendMessage(final int message, String response, int id) {
509531 bundle .putInt ("id" , id );
510532 bundle .putString ("address" , apiListeners .from );
511533 msg .setData (bundle );
512- handler .post (new Runnable () {
513- @ Override
514- public void run () {
515- if (message < 0 ) {
516- apiListeners .failListener .onAPIFailed (ctx , message , bundle );
517- } else {
518- apiListeners .successListener .onAPISuccess (ctx , message , bundle );
519- }
534+ handler .post (() -> {
535+ if (message < 0 ) {
536+ apiListeners .failListener .onAPIFailed (ctx , message , bundle );
537+ } else {
538+ apiListeners .successListener .onAPISuccess (ctx , message , bundle );
520539 }
521540 });
522541 }
@@ -556,23 +575,20 @@ public boolean clearCache(File dir) {
556575
557576 public long getCacheSize () {
558577 final long [] size = {0 };
559- Runnable runnable = new Runnable () {
560- @ Override
561- public void run () {
562- long foldersize = 0 ;
563- File [] filelist = new File (ctx .getCacheDir ().getAbsolutePath ()).listFiles ();
564- for (File aFilelist : filelist ) {
565- if (aFilelist .isDirectory ()) {
566- File [] filelist2 = new File (aFilelist .getAbsolutePath ()).listFiles ();
567- for (File aFilelist2 : filelist2 ) {
568- foldersize += aFilelist2 .length ();
569- }
570- } else {
571- foldersize += aFilelist .length ();
578+ Runnable runnable = () -> {
579+ long foldersize = 0 ;
580+ File [] filelist = new File (ctx .getCacheDir ().getAbsolutePath ()).listFiles ();
581+ for (File aFilelist : filelist ) {
582+ if (aFilelist .isDirectory ()) {
583+ File [] filelist2 = new File (aFilelist .getAbsolutePath ()).listFiles ();
584+ for (File aFilelist2 : filelist2 ) {
585+ foldersize += aFilelist2 .length ();
572586 }
587+ } else {
588+ foldersize += aFilelist .length ();
573589 }
574- size [0 ] = foldersize ;
575590 }
591+ size [0 ] = foldersize ;
576592 };
577593 new Thread (runnable ).run ();
578594 return size [0 ];
0 commit comments