package de.danoeh.antennapod.core.service;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import de.danoeh.antennapod.core.event.FeedMediaEvent;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.NetworkUtils;
import de.greenrobot.event.EventBus;

public class FeedMediaSizeService extends IntentService {

    private final static String TAG = "FeedMediaSizeService";

    public FeedMediaSizeService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d(TAG, "onHandleIntent()");
        if(false == NetworkUtils.networkAvailable(this)) {
            return;
        }
        List<FeedMedia> list = DBReader.getFeedMediaUnknownSize(this);
        for (FeedMedia media : list) {
            if(false == NetworkUtils.networkAvailable(this)) {
                return;
            }
            long size = Integer.MIN_VALUE;
            HttpURLConnection conn = null;
            try {
                URL url = new URL(media.getDownload_url());
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestProperty( "Accept-Encoding", "" );
                conn.setRequestMethod("HEAD");
                size = conn.getContentLength();
                conn.disconnect();
            } catch (IOException e) {
                Log.d(TAG, media.getDownload_url());
                e.printStackTrace();
            } finally {
                if(conn != null) {
                    conn.disconnect();
                }
            }
            media.setSize(size);
            DBWriter.setFeedMedia(this, media);
            EventBus.getDefault().post(FeedMediaEvent.update(media));
        }
    }

}

@@ -128,16 +128,37 @@
 l.Log;%0A%0A
+import java.io.File;%0A
 import j
@@ -1186,16 +1186,255 @@
 _VALUE;%0A
+            if(media.isDownloaded()) %7B%0A                File mediaFile = new File(media.getLocalMediaUrl());%0A                if(mediaFile.exists()) %7B%0A                    size = mediaFile.length();%0A                %7D%0A            %7D else %7B%0A    
         
@@ -1484,14 +1484,22 @@
     
+    
 try %7B%0A
+    
     
@@ -1566,24 +1566,28 @@
             
+    
 conn = (Http
@@ -1631,32 +1631,36 @@
                 
+    
 conn.setRequestP
@@ -1667,17 +1667,16 @@
 roperty(
- 
 %22Accept-
@@ -1688,17 +1688,16 @@
 ing%22, %22%22
- 
 );%0A     
@@ -1699,32 +1699,36 @@
                 
+    
 conn.setRequestM
@@ -1738,24 +1738,28 @@
 od(%22HEAD%22);%0A
+    
             
@@ -1802,32 +1802,36 @@
                 
+  
+  
 conn.disconnect(
@@ -1825,32 +1825,36 @@
 n.disconnect();%0A
+    
             %7D ca
@@ -1871,24 +1871,28 @@
 eption e) %7B%0A
+    
             
@@ -1948,16 +1948,20 @@
         
+    
 e.printS
@@ -1985,16 +1985,20 @@
         
+    
 %7D finall
@@ -2017,18 +2017,23 @@
         
+  
+  
 if
+ 
 (conn !=
@@ -2037,24 +2037,28 @@
  != null) %7B%0A
+    
             
@@ -2076,32 +2076,54 @@
 n.disconnect();%0A
+                    %7D%0A
                 

Use local file size if episode is downloaded