project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Downloader/DownloadAsyncTask.java
author Xeli
Thu, 01 Sep 2011 14:55:31 +0200
branchhedgeroid
changeset 5671 ba4c3a4c8b09
parent 5621 ea796c83ea47
permissions -rw-r--r--
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
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
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    22
import java.io.BufferedInputStream;
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    23
import java.io.File;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    24
import java.io.FileNotFoundException;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    25
import java.io.FileOutputStream;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    26
import java.io.IOException;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    27
import java.net.HttpURLConnection;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    28
import java.net.URL;
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    29
import java.security.MessageDigest;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    30
import java.security.NoSuchAlgorithmException;
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    31
import java.util.zip.ZipEntry;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    32
import java.util.zip.ZipInputStream;
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
import android.os.AsyncTask;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    35
import android.util.Log;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    36
/**
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    37
 * 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
    38
 * 
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    39
 *  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
    40
 * @author Xeli
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
 */
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    43
public class DownloadAsyncTask extends AsyncTask<String, Object, Long> {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    44
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    45
	private final static String URL_WITHOUT_SUFFIX = "http://hedgewars.googlecode.com/files/data_5631.";
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    46
	//private final static String URL_WITHOUT_SUFFIX = "http://www.xelification.com/tmp/firebutton.";
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    47
	private final static String URL_ZIP_SUFFIX = "zip";
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    48
	private final static String URL_HASH_SUFFIX = "hash";
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    49
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    50
	private DownloadService service;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    51
	private long lastUpdateMillis = 0;
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    52
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    53
	public DownloadAsyncTask(DownloadService _service){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    54
		service = _service;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    55
	}
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    56
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    57
	/**
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    58
	 * 
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    59
	 * @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
    60
	 */
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    61
	protected Long doInBackground(String... params) {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    62
		HttpURLConnection conn = null;
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    63
		MessageDigest digester = null;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    64
		String rootZipDest = params[0];
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    65
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    66
		File rootDest = new File(rootZipDest);//TODO check for nullpointer, it hints to the absence of an sdcard
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    67
		rootDest.mkdir();
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    68
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    69
		try {
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    70
			URL url = new URL(URL_WITHOUT_SUFFIX + URL_ZIP_SUFFIX);
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    71
			conn = (HttpURLConnection)url.openConnection();
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    72
		} catch (IOException e) {
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    73
			e.printStackTrace();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    74
			return -1l;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    75
		}
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    76
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    77
		String contentType = conn.getContentType();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    78
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    79
		if(contentType == null || contentType.contains("zip")){ //Seeing as we provide the url if the contentType is unknown lets assume zips
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    80
			int bytesDecompressed = 0;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    81
			ZipEntry entry = null;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    82
			ZipInputStream input = null;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    83
			int kbytesToProcess = conn.getContentLength()/1024;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    84
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    85
			byte[] buffer = new byte[1024];
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    86
			service.start(kbytesToProcess);
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    87
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    88
			try {
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    89
				digester = MessageDigest.getInstance("MD5");
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    90
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    91
			} catch (NoSuchAlgorithmException e1) {
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    92
				e1.printStackTrace();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    93
			}
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    94
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    95
			try{
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    96
				input = new ZipInputStream(conn.getInputStream());
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    97
				entry = input.getNextEntry();	
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    98
			}catch(IOException e){
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
    99
				e.printStackTrace();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   100
				if(conn != null) conn.disconnect();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   101
				return -1l;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   102
			}
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   103
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   104
			while(entry != null){
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   105
				if(isCancelled()) break;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   106
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   107
				String fileName = entry.getName();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   108
				File f = new File(rootZipDest + fileName);
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   109
				bytesDecompressed += entry.getCompressedSize();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   110
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   111
				if(entry.isDirectory()){
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   112
					f.mkdir();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   113
				}else{
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   114
					if(f.exists()){
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   115
						f.delete();
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   116
					}
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   117
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   118
					FileOutputStream output = null;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   119
					try {
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   120
						f.createNewFile();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   121
						output = new FileOutputStream(f);
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   122
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   123
						int count = 0;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   124
						while((count = input.read(buffer)) != -1){
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   125
							output.write(buffer, 0, count);
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   126
							digester.update(buffer, 0, count);
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   127
							if(System.currentTimeMillis() - lastUpdateMillis > 1000){
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   128
								lastUpdateMillis = System.currentTimeMillis();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   129
								publishProgress(bytesDecompressed, kbytesToProcess, fileName);
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   130
							}
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   131
						}
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   132
						output.flush();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   133
						input.closeEntry();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   134
					} catch (FileNotFoundException e) {
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   135
						e.printStackTrace();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   136
						if(conn != null) conn.disconnect();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   137
						return -1l;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   138
					} catch (IOException e) {
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   139
						e.printStackTrace();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   140
						if(conn != null) conn.disconnect();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   141
						return -1l;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   142
					}finally{
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   143
						try {
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   144
							if( output != null) output.close();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   145
						} catch (IOException e) {}
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   146
					}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   147
				}
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   148
				try{
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   149
					entry = input.getNextEntry();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   150
				}catch(IOException e){
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   151
					e.printStackTrace();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   152
					if(conn != null) conn.disconnect();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   153
					return -1l;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   154
				}
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   155
			}//end while(entry != null)
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   156
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   157
			try {
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   158
				input.close();
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   159
			} catch (IOException e) {}
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   160
		}//end if contentType == "zip"
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   161
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   162
		if(conn != null) conn.disconnect();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   163
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   164
		if(checkMD5(digester))return 0l;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   165
		else return -1l;
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   166
	}
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   167
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   168
	//TODO proper result handling
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   169
	protected void onPostExecute(Long result){
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   170
		service.done(result > -1l);
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   171
	}
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   172
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   173
	protected void onProgressUpdate(Object...objects){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   174
		service.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   175
	}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   176
5671
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   177
	private boolean checkMD5(MessageDigest digester){
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   178
		if(digester != null) {
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   179
			byte[] messageDigest = digester.digest();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   180
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   181
			try {
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   182
				URL url = new URL(URL_WITHOUT_SUFFIX + URL_HASH_SUFFIX);
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   183
				HttpURLConnection conn = (HttpURLConnection)url.openConnection();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   184
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   185
				byte[] buffer = new byte[1024];//size is large enough to hold the entire hash
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   186
				BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   187
				int bytesRead = bis.read(buffer);
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   188
				if(bytesRead > -1){
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   189
					String hash = new String(buffer, 0, bytesRead);
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   190
					StringBuffer sb = new StringBuffer();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   191
					Integer tmp = 0;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   192
					for(int i = 0; i < messageDigest.length; i++){
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   193
						tmp = 0xFF & messageDigest[i];
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   194
						if(tmp < 0xF) sb.append('0');
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   195
						sb.append(Integer.toHexString(tmp));
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   196
					}
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   197
					sb.append('\n');//add newline to become identical with the hash file
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   198
					
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   199
					return hash.equals(sb.toString());
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   200
				}
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   201
				return false;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   202
			} catch (IOException e) {
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   203
				e.printStackTrace();
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   204
				return false;
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   205
			}
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   206
		}else{
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   207
			return false;	
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   208
		}
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   209
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   210
	}
ba4c3a4c8b09 Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents: 5621
diff changeset
   211
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   212
}