1 /*
2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
3  * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
4  * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 
21 package org.hedgewars.hedgeroid.Downloader;
22 
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 
27 import org.hedgewars.hedgeroid.MainActivity;
28 import org.hedgewars.hedgeroid.R;
29 import org.hedgewars.hedgeroid.Datastructures.Schemes;
30 import org.hedgewars.hedgeroid.Datastructures.Team;
31 import org.hedgewars.hedgeroid.Datastructures.Weaponsets;
32 import org.hedgewars.hedgeroid.util.FileUtils;
33 
34 import android.content.res.AssetManager;
35 import android.os.AsyncTask;
36 import android.util.Log;
37 
38 public class DownloadAssets extends AsyncTask<Object, Long, Boolean> {
39     private static final String VERSION_FILENAME = "assetsversion.txt";
40     private final MainActivity act;
41 
DownloadAssets(MainActivity act)42     public DownloadAssets(MainActivity act){
43         this.act = act;
44     }
45 
copyFileOrDir(AssetManager assetManager, File target, String assetPath)46     private void copyFileOrDir(AssetManager assetManager, File target, String assetPath) throws IOException {
47         try {
48             FileUtils.writeStreamToFile(assetManager.open(assetPath), target);
49         } catch(FileNotFoundException e) {
50             /*
51              * I can't find a better way to figure out whether an asset entry is
52              * a file or a directory. Checking if assetManager.list(assetPath)
53              * is empty is a bit cleaner, but SLOW.
54              */
55             if (!target.isDirectory() && !target.mkdir()) {
56                 throw new IOException("Unable to create directory "+target);
57             }
58             for (String asset : assetManager.list(assetPath)) {
59                 copyFileOrDir(assetManager, new File(target, asset), assetPath + "/" + asset);
60             }
61         }
62     }
63 
64     @Override
doInBackground(Object... params)65     protected Boolean doInBackground(Object... params) {
66         try {
67             FileUtils.writeStreamToFile(act.getResources().openRawResource(R.raw.schemes_builtin), Schemes.getBuiltinSchemesFile(act));
68             FileUtils.writeStreamToFile(act.getResources().openRawResource(R.raw.weapons_builtin), Weaponsets.getBuiltinWeaponsetsFile(act));
69             FileUtils.resRawToFilesDir(act, R.array.teams, R.array.teamFilenames, Team.DIRECTORY_TEAMS);
70             copyFileOrDir(act.getAssets(), FileUtils.getDataPathFile(act), "Data");
71             copyFileOrDir(act.getAssets(), new File(FileUtils.getCachePath(act), VERSION_FILENAME), VERSION_FILENAME);
72             return Boolean.TRUE;
73         } catch(IOException e) {
74             Log.e("DownloadAssets", e.getMessage(), e);
75             return Boolean.FALSE;
76         }
77     }
78 
79     @Override
onPostExecute(Boolean result)80     protected void onPostExecute(Boolean result){
81         act.onAssetsDownloaded(result);
82     }
83 }
84