project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Downloader/DownloadAsyncTask.java
author Xeli
Sun, 21 Aug 2011 16:25:18 +0200
branchhedgeroid
changeset 5621 ea796c83ea47
parent 5514 294c92eea729
child 5671 ba4c3a4c8b09
permissions -rw-r--r--
added licenses
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5621
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
     1
/*
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
     3
 * Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com>
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
     4
 *
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
     8
 *
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
    12
 * GNU General Public License for more details.
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
    13
 *
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
    14
 * You should have received a copy of the GNU General Public License
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
    15
 * along with this program; if not, write to the Free Software
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
    17
 */
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
    18
ea796c83ea47 added licenses
Xeli
parents: 5514
diff changeset
    19
5412
ab055114c788 Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents: 5397
diff changeset
    20
package org.hedgewars.mobile.Downloader;
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    21
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    22
import java.io.File;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    23
import java.io.FileNotFoundException;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    24
import java.io.FileOutputStream;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    25
import java.io.IOException;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    26
import java.net.HttpURLConnection;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    27
import java.net.URL;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    28
import java.util.zip.ZipEntry;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    29
import java.util.zip.ZipInputStream;
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
import android.os.AsyncTask;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    32
import android.util.Log;
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
 * 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
    35
 * 
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    36
 *  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
    37
 * @author Xeli
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
 */
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    40
public class DownloadAsyncTask extends AsyncTask<String, Object, Long> {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    41
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    42
	private DownloadService service;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    43
	private long lastUpdateMillis = 0;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    44
	
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    45
	public DownloadAsyncTask(DownloadService _service){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    46
		service = _service;
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
	
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
	 * 
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    51
	 * @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
    52
	 */
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    53
	protected Long doInBackground(String... params) {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    54
		HttpURLConnection conn = null;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    55
		try {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    56
			String rootZipDest = params[0];
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    57
5514
294c92eea729 fixed minor download bug
Xeli
parents: 5412
diff changeset
    58
			File rootDest = new File(rootZipDest);//TODO check for nullpointer, it hints to the absence of an sdcard
5412
ab055114c788 Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents: 5397
diff changeset
    59
			rootDest.mkdir();
ab055114c788 Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents: 5397
diff changeset
    60
			
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    61
			URL url = new URL(params[1]);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    62
			conn = (HttpURLConnection)url.openConnection();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    63
			String contentType = conn.getContentType();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    64
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    65
			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
    66
				ZipInputStream input = new ZipInputStream(conn.getInputStream());
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    67
				int bytesDecompressed = 0;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    68
				final int kbytesToProcess = conn.getContentLength()/1024;
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
				service.start(kbytesToProcess);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    71
				
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    72
				ZipEntry entry = null;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    73
				while((entry = input.getNextEntry()) != null){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    74
					String fileName = entry.getName();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    75
					
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    76
					if(isCancelled()) break;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    77
					else if(System.currentTimeMillis() - lastUpdateMillis > 1000){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    78
						lastUpdateMillis = System.currentTimeMillis();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    79
						publishProgress(bytesDecompressed, kbytesToProcess, fileName);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    80
					}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    81
					
5412
ab055114c788 Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents: 5397
diff changeset
    82
					Log.e("bla", fileName);
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    83
					bytesDecompressed += entry.getCompressedSize();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    84
					
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    85
					File f = new File(rootZipDest + fileName);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    86
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    87
					if(entry.isDirectory()){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    88
						f.mkdir();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    89
					}else{
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    90
						if(f.exists()){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    91
							f.delete();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    92
						}
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
						try {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    95
							f.createNewFile();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    96
							FileOutputStream out = new FileOutputStream(f);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    97
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    98
							byte[] buffer = new byte[1024];
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    99
							int count = 0;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   100
							while((count = input.read(buffer)) != -1){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   101
								out.write(buffer, 0, count);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   102
							}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   103
							out.flush();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   104
							out.close();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   105
							input.closeEntry();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   106
						} catch (FileNotFoundException e) {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   107
							e.printStackTrace();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   108
						} catch (IOException e) {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   109
							e.printStackTrace();
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
					}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   112
				}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   113
				input.close();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   114
			}else{
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   115
				Log.e("bla", "contenttype = " + contentType);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   116
			}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   117
		} catch (IOException e) {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   118
			e.printStackTrace();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   119
		}finally{
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   120
			if(conn != null) conn.disconnect();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   121
		}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   122
		return null;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   123
	}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   124
	
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   125
	//TODO propper result handling
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   126
	protected void onPostExecute(Long result){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   127
		service.done(true);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   128
	}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   129
	
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   130
	protected void onProgressUpdate(Object...objects){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   131
		service.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   132
	}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   133
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   134
}