project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAsyncTask.java
author Wuzzy <almikes@aol.com>
Sat, 30 Sep 2017 23:52:08 +0200
changeset 12627 07fdda8c13a2
parent 10017 de822cd3df3a
permissions -rw-r--r--
TrophyRace: Fix game never eliminating any hogs after a hog skipped or ran out of time Warning: This commit _might_ invalidate past records, but I'm not sure if this is actually the case. Note that only the eliminiation part of the script is touched, not the actual race logic. Even if records are actually broken by this, I and sheepluva have decided that it's more imporant to fix this very, VERY stupid and old bug than to preserve records.
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    40
 *
6047
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
    //private final static String URL_WITHOUT_SUFFIX = "http://www.xelification.com/tmp/firebutton.";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
    private final static String URL_ZIP_SUFFIX = ".zip";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
    public static final int EXIT_SUCCESS = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
    public static final int EXIT_URLFAIL = 1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
    public static final int EXIT_CONNERROR = 2;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
    public static final int EXIT_FNF = 3;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
    public static final int EXIT_MD5 = 4;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
    public static final int EXIT_CANCELLED = 5;
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    57
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
    private DownloadTask task;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
    private long lastUpdateMillis = 0;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    60
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
    public DownloadAsyncTask(DownloadTask _task){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
        task = _task;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    64
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
     *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
     * @param params - A {@link}DownloadTask which gives information about where to download from and store the files to
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
    protected Integer doInBackground(DownloadPackage...packages) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
        HttpURLConnection conn = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
        MessageDigest digester = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
        File rootDest = new File(rootZipDest);//TODO check for nullpointer, it hints to the absence of an sdcard
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
            URL url = new URL(pack.getURL() + URL_ZIP_SUFFIX);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
            conn = (HttpURLConnection)url.openConnection();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
        } catch (IOException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
            e.printStackTrace();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
            return EXIT_URLFAIL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
        }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    86
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
        String contentType = conn.getContentType();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    88
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
        if(contentType == null || contentType.contains("zip")){ //Seeing as we provide the url if the contentType is unknown lets assume zips
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
            int bytesDecompressed = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
            ZipEntry entry = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
            ZipInputStream input = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
            FileOutputStream output = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    94
            int kbytesToProcess = conn.getContentLength()/1024;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    95
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    96
            byte[] buffer = new byte[1024];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    99
            try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   100
                digester = MessageDigest.getInstance("MD5");
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   101
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   102
            } catch (NoSuchAlgorithmException e1) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   103
                e1.printStackTrace();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   104
            }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   105
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   106
            try{
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   107
                input = new ZipInputStream(conn.getInputStream());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   108
                entry = input.getNextEntry();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   109
            }catch(IOException e){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   110
                e.printStackTrace();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   111
                conn.disconnect();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   112
                return EXIT_CONNERROR;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   113
            }
6047
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   119
                if(isCancelled()) break;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   120
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   121
                try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   122
                    String fileName = entry.getName();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   123
                    File f = new File(rootZipDest + fileName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   126
                    if(entry.isDirectory()){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   127
                        f.mkdir();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   128
                    }else{
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   129
                        if(f.exists()){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   130
                            f.delete();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   131
                        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   132
                        f.createNewFile();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   133
                        output = new FileOutputStream(f);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   134
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   135
                        int count = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   136
                        while((count = input.read(buffer)) != -1){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   137
                            output.write(buffer, 0, count);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   138
                            digester.update(buffer, 0, count);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   139
                            if(System.currentTimeMillis() - lastUpdateMillis > 1000){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   140
                                lastUpdateMillis = System.currentTimeMillis();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   141
                                publishProgress(bytesDecompressed, kbytesToProcess, fileName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   142
                            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   143
                        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   144
                        output.flush();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   145
                        input.closeEntry();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   146
                    }//if isDir
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   147
                    entry = input.getNextEntry();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   148
                } catch (FileNotFoundException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   149
                    e.printStackTrace();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   150
                    if(conn != null) conn.disconnect();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   151
                    return EXIT_FNF;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   152
                } catch (IOException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   153
                    e.printStackTrace();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   154
                    if(conn != null) conn.disconnect();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   155
                    return EXIT_CONNERROR;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   156
                }finally{
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   157
                    try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   158
                        if( output != null) output.close();
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   159
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   160
                    } catch (IOException e) {}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   161
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   162
            }//end while(entry != null)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   163
            if( input != null)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   164
                try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   165
                    input.close();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   166
                } catch (IOException e) {}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   167
        }else{//end if contentType == "zip"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   168
            return EXIT_URLFAIL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   169
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   170
        if(conn != null) conn.disconnect();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   171
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   172
        if(checkMD5(digester, pack))return EXIT_SUCCESS;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   173
        else return EXIT_MD5;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   174
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   175
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   176
    //TODO proper result handling
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   177
    protected void onPostExecute(Integer result){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   178
        task.done(result);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   179
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   180
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   181
    protected void onProgressUpdate(Object...objects){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   182
        task.update((Integer)objects[0], (Integer)objects[1], (String)objects[2]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   183
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   184
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   185
    protected void onCancelled(){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   186
        onPostExecute(EXIT_CANCELLED);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   187
    }
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   188
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   189
    private boolean checkMD5(MessageDigest digester, DownloadPackage task){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   190
        if(digester != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   191
            byte[] messageDigest = digester.digest();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   192
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   193
            try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   194
                URL url = new URL(task.getURL() + URL_HASH_SUFFIX);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   195
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   196
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   197
                byte[] buffer = new byte[1024];//size is large enough to hold the entire hash
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   198
                BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   199
                int bytesRead = bis.read(buffer);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   200
                String hash = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   201
                if(bytesRead > -1){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   202
                    hash = new String(buffer, 0, bytesRead);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   203
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   204
                StringBuffer sb = new StringBuffer();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   205
                Integer tmp = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   206
                for(int i = 0; i < messageDigest.length; i++){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   207
                    tmp = 0xFF & messageDigest[i];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   208
                    if(tmp < 0xF) sb.append('0');
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   209
                    sb.append(Integer.toHexString(tmp));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   210
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
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
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   213
                return hash.equals(sb.toString());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   214
            } catch (IOException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   215
                e.printStackTrace();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   216
                return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   217
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   218
        }else{
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   219
            return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   220
        }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   221
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   222
    }
6047
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
}