project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAsyncTask.java
author nemo
Wed, 10 Oct 2012 00:14:01 -0400
changeset 7739 8d5d6b1be9ba
parent 6751 7c75941b499b
child 6842 2e6391f33204
permissions -rw-r--r--
no. still doesn't work right
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     1
/*
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
6700
e04da46ee43c the most important commit of the year
koda
parents: 6446
diff changeset
     3
 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     4
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     8
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    12
 * GNU General Public License for more details.
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    13
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    17
 */
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    18
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    19
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    20
package org.hedgewars.hedgeroid.Downloader;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    21
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    22
import java.io.BufferedInputStream;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    23
import java.io.File;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    24
import java.io.FileNotFoundException;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    25
import java.io.FileOutputStream;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    26
import java.io.IOException;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    27
import java.net.HttpURLConnection;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    28
import java.net.URL;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    29
import java.security.MessageDigest;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    30
import java.security.NoSuchAlgorithmException;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    31
import java.util.zip.ZipEntry;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    32
import java.util.zip.ZipInputStream;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    33
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    34
import org.hedgewars.hedgeroid.Downloader.DownloadService.DownloadTask;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    35
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    36
import android.os.AsyncTask;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    37
/**
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    38
 * This is an AsyncTask which will download a zip from an URL and unzip it to a specified path
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    39
 * 
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    40
 *  a typical call to start the task would be new DownloadAsyncTask().execute(getExternalStorage(), "www.hedgewars.org/data.zip");
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    41
 * @author Xeli
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    42
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    43
 */
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    44
public class DownloadAsyncTask extends AsyncTask<DownloadPackage, Object, Integer> {
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    45
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    46
	//private final static String URL_WITHOUT_SUFFIX = "http://www.xelification.com/tmp/firebutton.";
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6047
diff changeset
    47
	private final static String URL_ZIP_SUFFIX = ".zip";
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6047
diff changeset
    48
	private final static String URL_HASH_SUFFIX = ".hash";
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    49
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    50
	public static final int EXIT_SUCCESS = 0;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    51
	public static final int EXIT_URLFAIL = 1;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    52
	public static final int EXIT_CONNERROR = 2;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    53
	public static final int EXIT_FNF = 3;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    54
	public static final int EXIT_MD5 = 4;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    55
	public static final int EXIT_CANCELLED = 5;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    56
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    57
	private DownloadTask task;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    58
	private long lastUpdateMillis = 0;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    59
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    60
	public DownloadAsyncTask(DownloadTask _task){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    61
		task = _task;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    62
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    63
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    64
	/**
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    65
	 * 
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6047
diff changeset
    66
	 * @param params - A {@link}DownloadTask which gives information about where to download from and store the files to 
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    67
	 */
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    68
	protected Integer doInBackground(DownloadPackage...packages) {
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    69
		DownloadPackage pack = packages[0];//just use one task per execute call for now
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    70
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    71
		HttpURLConnection conn = null;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    72
		MessageDigest digester = null;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    73
		String rootZipDest = pack.getPathToStore();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    74
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    75
		File rootDest = new File(rootZipDest);//TODO check for nullpointer, it hints to the absence of an sdcard
6751
7c75941b499b android: fix finding cache dir on prefroyo devices, it now creates the Hedgewars folder if it wasn't there
Xeli
parents: 6700
diff changeset
    76
		rootDest.mkdirs();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    77
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    78
		try {
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    79
			URL url = new URL(pack.getURL() + URL_ZIP_SUFFIX);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    80
			conn = (HttpURLConnection)url.openConnection();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    81
		} catch (IOException e) {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    82
			e.printStackTrace();
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    83
			return EXIT_URLFAIL;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    84
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    85
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    86
		String contentType = conn.getContentType();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    87
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    88
		if(contentType == null || contentType.contains("zip")){ //Seeing as we provide the url if the contentType is unknown lets assume zips
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    89
			int bytesDecompressed = 0;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    90
			ZipEntry entry = null;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    91
			ZipInputStream input = null;
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    92
			FileOutputStream output = null;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    93
			int kbytesToProcess = conn.getContentLength()/1024;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    94
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    95
			byte[] buffer = new byte[1024];
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    96
			task.start(kbytesToProcess);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    97
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    98
			try {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    99
				digester = MessageDigest.getInstance("MD5");
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   100
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   101
			} catch (NoSuchAlgorithmException e1) {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   102
				e1.printStackTrace();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   103
			}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   104
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   105
			try{
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   106
				input = new ZipInputStream(conn.getInputStream());
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   107
				entry = input.getNextEntry();	
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   108
			}catch(IOException e){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   109
				e.printStackTrace();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   110
				if(conn != null) conn.disconnect();
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   111
				return EXIT_CONNERROR;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   112
			}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   113
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   114
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   115
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   116
			while(entry != null){
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   117
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   118
				if(isCancelled()) break;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   119
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   120
				try {
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   121
					String fileName = entry.getName();
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   122
					File f = new File(rootZipDest + fileName);
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   123
					bytesDecompressed += entry.getCompressedSize();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   124
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   125
					if(entry.isDirectory()){
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   126
						f.mkdir();
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   127
					}else{
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   128
						if(f.exists()){
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   129
							f.delete();
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   130
						}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   131
						f.createNewFile();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   132
						output = new FileOutputStream(f);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   133
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   134
						int count = 0;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   135
						while((count = input.read(buffer)) != -1){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   136
							output.write(buffer, 0, count);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   137
							digester.update(buffer, 0, count);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   138
							if(System.currentTimeMillis() - lastUpdateMillis > 1000){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   139
								lastUpdateMillis = System.currentTimeMillis();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   140
								publishProgress(bytesDecompressed, kbytesToProcess, fileName);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   141
							}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   142
						}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   143
						output.flush();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   144
						input.closeEntry();
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   145
					}//if isDir 
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   146
					entry = input.getNextEntry();
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   147
				} catch (FileNotFoundException e) {
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   148
					e.printStackTrace();
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   149
					if(conn != null) conn.disconnect();
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   150
					return EXIT_FNF;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   151
				} catch (IOException e) {
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   152
					e.printStackTrace();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   153
					if(conn != null) conn.disconnect();
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   154
					return EXIT_CONNERROR;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   155
				}finally{
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   156
					try {
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   157
						if( output != null) output.close();
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   158
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   159
					} catch (IOException e) {}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   160
				}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   161
			}//end while(entry != null)
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   162
			if( input != null)
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   163
				try {
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   164
					input.close();
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   165
				} catch (IOException e) {}
6446
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   166
		}else{//end if contentType == "zip"
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   167
			return EXIT_URLFAIL;
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   168
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   169
		if(conn != null) conn.disconnect();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   170
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   171
		if(checkMD5(digester, pack))return EXIT_SUCCESS;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   172
		else return EXIT_MD5;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   173
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   174
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   175
	//TODO proper result handling
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   176
	protected void onPostExecute(Integer result){
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   177
		task.done(result);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   178
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   179
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   180
	protected void onProgressUpdate(Object...objects){
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   181
		task.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   182
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   183
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   184
	protected void onCancelled(){
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   185
		onPostExecute(EXIT_CANCELLED);
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   186
	}
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   187
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   188
	private boolean checkMD5(MessageDigest digester, DownloadPackage task){
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   189
		if(digester != null) {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   190
			byte[] messageDigest = digester.digest();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   191
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   192
			try {
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6047
diff changeset
   193
				URL url = new URL(task.getURL() + URL_HASH_SUFFIX);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   194
				HttpURLConnection conn = (HttpURLConnection)url.openConnection();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   195
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   196
				byte[] buffer = new byte[1024];//size is large enough to hold the entire hash
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   197
				BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   198
				int bytesRead = bis.read(buffer);
6446
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   199
				String hash = null;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   200
				if(bytesRead > -1){
6446
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   201
					hash = new String(buffer, 0, bytesRead);
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   202
				}
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   203
				StringBuffer sb = new StringBuffer();
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   204
				Integer tmp = 0;
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   205
				for(int i = 0; i < messageDigest.length; i++){
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   206
					tmp = 0xFF & messageDigest[i];
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   207
					if(tmp < 0xF) sb.append('0');
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   208
					sb.append(Integer.toHexString(tmp));
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   209
				}
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   210
				sb.append('\n');//add newline to become identical with the hash file
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   211
6446
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   212
				return hash.equals(sb.toString());
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   213
			} catch (IOException e) {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   214
				e.printStackTrace();
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   215
				return true;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   216
			}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   217
		}else{
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   218
			return true;	
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   219
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   220
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   221
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   222
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   223
}