project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAsyncTask.java
author Medo <smaxein@googlemail.com>
Mon, 20 Aug 2012 21:05:57 +0200
changeset 7584 7831c84cc644
parent 6842 2e6391f33204
child 10017 de822cd3df3a
permissions -rw-r--r--
License change: With the agreement of Xeli, I changed the Hedgeroid license to GPLv2+ (from GPLv2).
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
 *
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 6842
diff changeset
     5
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 6842
diff changeset
     6
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 6842
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 6842
diff changeset
     8
 * of the License, or (at your option) any later version.
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     9
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    10
 * 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
    11
 * 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
    12
 * 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
    13
 * 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
    14
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    15
 * 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
    16
 * along with this program; if not, write to the Free Software
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 6842
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
6047
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
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    21
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
    22
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.BufferedInputStream;
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.File;
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.FileNotFoundException;
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.FileOutputStream;
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.io.IOException;
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.HttpURLConnection;
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.net.URL;
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.MessageDigest;
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.security.NoSuchAlgorithmException;
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.ZipEntry;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    33
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
    34
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    35
import org.hedgewars.hedgeroid.Downloader.DownloadService.DownloadTask;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    36
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    37
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
    38
/**
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    39
 * 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
    40
 * 
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    41
 *  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
    42
 * @author Xeli
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    43
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    44
 */
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    45
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
    46
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    47
	//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
    48
	private final static String URL_ZIP_SUFFIX = ".zip";
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6047
diff changeset
    49
	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
    50
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_SUCCESS = 0;
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_URLFAIL = 1;
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_CONNERROR = 2;
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_FNF = 3;
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_MD5 = 4;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    56
	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
    57
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    58
	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
    59
	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
    60
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    61
	public DownloadAsyncTask(DownloadTask _task){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    62
		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
    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
	/**
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    66
	 * 
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6047
diff changeset
    67
	 * @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
    68
	 */
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    69
	protected Integer doInBackground(DownloadPackage...packages) {
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    70
		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
    71
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    72
		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
    73
		MessageDigest digester = null;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    74
		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
    75
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    76
		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
    77
		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
    78
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    79
		try {
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    80
			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
    81
			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
    82
		} 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
    83
			e.printStackTrace();
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    84
			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
    85
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    86
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    87
		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
    88
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    89
		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
    90
			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
    91
			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
    92
			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
    93
			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
    94
			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
    95
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    96
			byte[] buffer = new byte[1024];
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    97
			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
    98
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    99
			try {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   100
				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
   101
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   102
			} 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
   103
				e1.printStackTrace();
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
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   106
			try{
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   107
				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
   108
				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
   109
			}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
   110
				e.printStackTrace();
6842
2e6391f33204 Removed some unused imports and a redundand nullcheck
Medo <smaxein@googlemail.com>
parents: 6751
diff changeset
   111
				conn.disconnect();
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   112
				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
   113
			}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   114
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   115
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   116
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   117
			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
   118
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   119
				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
   120
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   121
				try {
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   122
					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
   123
					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
   124
					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
   125
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   126
					if(entry.isDirectory()){
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   127
						f.mkdir();
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   128
					}else{
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   129
						if(f.exists()){
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   130
							f.delete();
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   131
						}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   132
						f.createNewFile();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   133
						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
   134
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   135
						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
   136
						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
   137
							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
   138
							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
   139
							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
   140
								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
   141
								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
   142
							}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   143
						}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   144
						output.flush();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   145
						input.closeEntry();
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   146
					}//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
   147
					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
   148
				} catch (FileNotFoundException e) {
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   149
					e.printStackTrace();
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   150
					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
   151
					return EXIT_FNF;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   152
				} 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
   153
					e.printStackTrace();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   154
					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
   155
					return EXIT_CONNERROR;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   156
				}finally{
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   157
					try {
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   158
						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
   159
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   160
					} 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
   161
				}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   162
			}//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
   163
			if( input != null)
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   164
				try {
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   165
					input.close();
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   166
				} 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
   167
		}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
   168
			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
   169
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   170
		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
   171
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   172
		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
   173
		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
   174
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   175
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   176
	//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
   177
	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
   178
		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
   179
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   180
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   181
	protected void onProgressUpdate(Object...objects){
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   182
		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
   183
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   184
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   185
	protected void onCancelled(){
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   186
		onPostExecute(EXIT_CANCELLED);
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   187
	}
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   188
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   189
	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
   190
		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
   191
			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
   192
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   193
			try {
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6047
diff changeset
   194
				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
   195
				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
   196
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   197
				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
   198
				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
   199
				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
   200
				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
   201
				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
   202
					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
   203
				}
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   204
				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
   205
				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
   206
				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
   207
					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
   208
					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
   209
					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
   210
				}
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   211
				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
   212
6446
a49d01b96185 android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents: 6437
diff changeset
   213
				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
   214
			} 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
   215
				e.printStackTrace();
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   216
				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
   217
			}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   218
		}else{
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   219
			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
   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
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   224
}