project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/DownloadAsyncTask.java
author Xeli
Tue, 05 Jul 2011 18:27:18 +0200
branchhedgeroid
changeset 5397 4ae1b082e4ba
permissions -rw-r--r--
Added download functionality and changed some icons
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     1
package org.hedgewars.mobile;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     2
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     3
import java.io.File;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     4
import java.io.FileNotFoundException;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     5
import java.io.FileOutputStream;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     6
import java.io.IOException;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     7
import java.net.HttpURLConnection;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     8
import java.net.URL;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     9
import java.util.zip.ZipEntry;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    10
import java.util.zip.ZipInputStream;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    11
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    12
import android.os.AsyncTask;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    13
import android.util.Log;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    14
/**
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    15
 * This is an AsyncTask which will download a zip from an URL and unzip it to a specified path
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    16
 * 
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    17
 *  a typical call to start the task would be new DownloadAsyncTask().execute(getExternalStorage(), "www.hedgewars.org/data.zip");
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    18
 * @author Xeli
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    19
 *
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    20
 */
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    21
public class DownloadAsyncTask extends AsyncTask<String, Object, Long> {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    22
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    23
	private DownloadService service;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    24
	private long lastUpdateMillis = 0;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    25
	
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    26
	public DownloadAsyncTask(DownloadService _service){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    27
		service = _service;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    28
	}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    29
	
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    30
	/**
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    31
	 * 
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    32
	 * @param params - 2 Strings, first is the path where the unzipped files will be stored, second is the URL to download from
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    33
	 */
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    34
	protected Long doInBackground(String... params) {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    35
		HttpURLConnection conn = null;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    36
		try {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    37
			String rootZipDest = params[0];
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    38
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    39
			URL url = new URL(params[1]);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    40
			conn = (HttpURLConnection)url.openConnection();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    41
			String contentType = conn.getContentType();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    42
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    43
			if(contentType == null || contentType.contains("zip")){ //Seeing as we provide the url if the contentType is unknown lets assume zips
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    44
				ZipInputStream input = new ZipInputStream(conn.getInputStream());
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    45
				int bytesDecompressed = 0;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    46
				final int kbytesToProcess = conn.getContentLength()/1024;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    47
				
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    48
				service.start(kbytesToProcess);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    49
				
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    50
				ZipEntry entry = null;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    51
				while((entry = input.getNextEntry()) != null){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    52
					String fileName = entry.getName();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    53
					
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    54
					if(isCancelled()) break;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    55
					else if(System.currentTimeMillis() - lastUpdateMillis > 1000){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    56
						lastUpdateMillis = System.currentTimeMillis();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    57
						publishProgress(bytesDecompressed, kbytesToProcess, fileName);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    58
					}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    59
					
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    60
					bytesDecompressed += entry.getCompressedSize();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    61
					
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    62
					File f = new File(rootZipDest + fileName);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    63
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    64
					if(entry.isDirectory()){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    65
						f.mkdir();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    66
					}else{
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    67
						if(f.exists()){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    68
							f.delete();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    69
						}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    70
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    71
						try {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    72
							f.createNewFile();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    73
							FileOutputStream out = new FileOutputStream(f);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    74
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    75
							byte[] buffer = new byte[1024];
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    76
							int count = 0;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    77
							while((count = input.read(buffer)) != -1){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    78
								out.write(buffer, 0, count);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    79
							}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    80
							out.flush();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    81
							out.close();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    82
							input.closeEntry();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    83
						} catch (FileNotFoundException e) {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    84
							e.printStackTrace();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    85
						} catch (IOException e) {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    86
							e.printStackTrace();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    87
						}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    88
					}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    89
				}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    90
				input.close();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    91
			}else{
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    92
				Log.e("bla", "contenttype = " + contentType);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    93
			}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    94
		} catch (IOException e) {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    95
			e.printStackTrace();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    96
		}finally{
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    97
			if(conn != null) conn.disconnect();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    98
		}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    99
		return null;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   100
	}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   101
	
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   102
	//TODO propper result handling
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   103
	protected void onPostExecute(Long result){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   104
		service.done(true);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   105
	}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   106
	
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   107
	protected void onProgressUpdate(Object...objects){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   108
		service.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   109
	}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   110
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   111
}