project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java
author koda
Tue, 21 Jan 2014 22:43:06 +0100
changeset 10017 de822cd3df3a
parent 7584 7831c84cc644
permissions -rw-r--r--
fixwhitespace and dos2unix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     1
/*
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     3
 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     4
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     5
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     6
 * 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: 7508
diff changeset
     7
 * 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: 7508
diff changeset
     8
 * 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: 7508
diff changeset
     9
 * of the License, or (at your option) any later version.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    10
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    11
 * This program is distributed in the hope that it will be useful,
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    14
 * GNU General Public License for more details.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    15
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    16
 * You should have received a copy 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: 7508
diff changeset
    17
 * along with this program; if not, write to the Free Software
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    19
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    20
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    21
package org.hedgewars.hedgeroid.Downloader;
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    22
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    23
import java.io.File;
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6724
diff changeset
    24
import java.io.FileNotFoundException;
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    25
import java.io.IOException;
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    26
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    27
import org.hedgewars.hedgeroid.MainActivity;
6724
03cd33624284 schemes/teams now get replaces on each version update
Xeli
parents: 6485
diff changeset
    28
import org.hedgewars.hedgeroid.R;
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
    29
import org.hedgewars.hedgeroid.Datastructures.Schemes;
6724
03cd33624284 schemes/teams now get replaces on each version update
Xeli
parents: 6485
diff changeset
    30
import org.hedgewars.hedgeroid.Datastructures.Team;
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    31
import org.hedgewars.hedgeroid.Datastructures.Weaponsets;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    32
import org.hedgewars.hedgeroid.util.FileUtils;
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    33
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    34
import android.content.res.AssetManager;
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    35
import android.os.AsyncTask;
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    36
import android.util.Log;
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    37
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
    38
public class DownloadAssets extends AsyncTask<Object, Long, Boolean> {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    39
    private static final String VERSION_FILENAME = "assetsversion.txt";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    40
    private final MainActivity act;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    41
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    42
    public DownloadAssets(MainActivity act){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    43
        this.act = act;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    44
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    45
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
    private void copyFileOrDir(AssetManager assetManager, File target, String assetPath) throws IOException {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
            FileUtils.writeStreamToFile(assetManager.open(assetPath), target);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
        } catch(FileNotFoundException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
            /*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
             * I can't find a better way to figure out whether an asset entry is
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
             * a file or a directory. Checking if assetManager.list(assetPath)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
             * is empty is a bit cleaner, but SLOW.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
             */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
            if (!target.isDirectory() && !target.mkdir()) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
                throw new IOException("Unable to create directory "+target);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
            for (String asset : assetManager.list(assetPath)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
                copyFileOrDir(assetManager, new File(target, asset), assetPath + "/" + asset);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
    protected Boolean doInBackground(Object... params) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
            FileUtils.writeStreamToFile(act.getResources().openRawResource(R.raw.schemes_builtin), Schemes.getBuiltinSchemesFile(act));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
            FileUtils.writeStreamToFile(act.getResources().openRawResource(R.raw.weapons_builtin), Weaponsets.getBuiltinWeaponsetsFile(act));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
            FileUtils.resRawToFilesDir(act, R.array.teams, R.array.teamFilenames, Team.DIRECTORY_TEAMS);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
            copyFileOrDir(act.getAssets(), FileUtils.getDataPathFile(act), "Data");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
            copyFileOrDir(act.getAssets(), new File(FileUtils.getCachePath(act), VERSION_FILENAME), VERSION_FILENAME);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
            return Boolean.TRUE;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
        } catch(IOException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
            Log.e("DownloadAssets", e.getMessage(), e);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
            return Boolean.FALSE;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
    protected void onPostExecute(Boolean result){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
        act.onAssetsDownloaded(result);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
    }
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    83
}