package com.mick88.dittimetable.pdf_download;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat.Builder;

import com.mick88.dittimetable.R;
import com.mick88.dittimetable.timetable.Timetable;

public class PdfDownloaderService extends Service
{
	private static final int NOTIFICATION_ID = 0;
	public static final String EXTRA_TIMETABLE = "timetable";
	
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		downloadPdf((Timetable) intent.getSerializableExtra(EXTRA_TIMETABLE));
		return START_STICKY;
	}
	
	@Override
	public IBinder onBind(Intent intent)
	{
		// TODO: Return the communication channel to the service.
		throw new UnsupportedOperationException("Not yet implemented");
	}
	
	private void downloadPdf(Timetable timetable)
	{
		new AsyncTask<Timetable, Void, Void>()
		{
			protected void onPreExecute() 
			{
				Builder builder = new Builder(getApplicationContext())
					.setSmallIcon(R.drawable.ic_launcher)
					.setContentTitle("DIT Timetables")
					.setContentText("Downloading PDF");
				startForeground(NOTIFICATION_ID, builder.build());
			}

			@Override
			protected Void doInBackground(Timetable... params)
			{
				Timetable timetable = params[0];
				String url = timetable.getPdfUrl();
				
				return null;
			}
			
			@Override
			protected void onCancelled(Void result) 
			{
				// TODO: clean if necessary
				stopForeground(true);
			}
			
			@Override
			protected void onPostExecute(Void result) 
			{
				stopForeground(true);
				
				Builder builder = new Builder(getApplicationContext())
					.setSmallIcon(R.drawable.ic_launcher)
					.setContentTitle("Dit Timetables")
					.setContentText("Timetable downloaded");
				NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
				notificationManager.notify(NOTIFICATION_ID, builder.build());
						
				stopSelf();
			}
			
		}.execute(timetable);
	}
}

@@ -40,16 +40,213 @@
 nload;%0A%0A
+import java.io.BufferedInputStream;%0Aimport java.io.File;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;%0Aimport java.net.URL;%0A%0A
 import a
@@ -273,24 +273,24 @@
 ionManager;%0A
-
 import andro
@@ -397,16 +397,47 @@
 ncTask;%0A
+import android.os.Environment;%0A
 import a
@@ -512,16 +512,41 @@
 Builder;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -1679,24 +1679,241 @@
 fUrl();%0A%09%09%09%09
+String filename = timetable.getPdfFileName();%0A%09%09%09%09try%0A%09%09%09%09%7B%0A%09%09%09%09%09downloadFile(url, new File(Environment.getExternalStorageDirectory(), filename));%0A%09%09%09%09%7D %0A%09%09%09%09catch (IOException e)%0A%09%09%09%09%7B%0A%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%7D
 %0A%09%09%09%09return 
@@ -2553,16 +2553,16 @@
 %09%09%7D%0A%09%09%09%0A
-
 %09%09%7D.exec
@@ -2580,10 +2580,703 @@
 le);%0A%09%7D%0A
+%09%0A%09private int downloadFile(String url, File outputFile) throws IOException%0A%09%7B%0A%09%09final int BUFFER_SIZE = 1024;%0A%09%09Log.d(%22PDF Downloader%22, %22Downloading file %22+url);%0A%09%09URL pdfUrl = new URL(url);%0A//%09%09URLConnection connection = pdfUrl.openConnection();%0A//%09%09connection.connect();%0A//%09%09int length = connection.getContentLength();%0A%09%0A%09%09InputStream inStream = new BufferedInputStream(pdfUrl.openStream());%0A%09%09OutputStream outStream = new FileOutputStream(outputFile);%0A%09%09%0A%09%09byte %5B%5D data = new byte %5BBUFFER_SIZE%5D;%0A%09%09int count;%0A%09%09int total=0;%0A%09%09while ((count = inStream.read(data)) != -1)%0A%09%09%7B%0A%09%09%09total += count;%0A%09%09%09outStream.write(data);%0A%09%09%7D%0A%09%09%0A%09%09outStream.close();%0A%09%09inStream.close();%0A%09%09%0A%09%09return total;%0A%09%7D%0A
 %7D%0A

Implement method for downloading file