Make progress towards having download support work

This commit is contained in:
drdev
2014-05-30 04:59:32 +00:00
parent df8d38475a
commit fcbe79ee3f
7 changed files with 74 additions and 67 deletions

View File

@@ -80,6 +80,17 @@ public final class FileUtil {
return (dir.exists() && dir.isDirectory()) || dir.mkdirs(); return (dir.exists() && dir.isDirectory()) || dir.mkdirs();
} }
public static boolean deleteDirectory(File dir) {
if (dir.isDirectory()) {
for (String filename : dir.list()) {
if (!deleteDirectory(new File(dir, filename))) {
return false;
}
}
}
return dir.delete();
}
/** /**
* <p> * <p>
* writeFile. * writeFile.

View File

@@ -8,7 +8,8 @@
android:minSdkVersion="8" android:minSdkVersion="8"
android:targetSdkVersion="19" /> android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application <application
android:allowBackup="true" android:allowBackup="true"

View File

@@ -31,6 +31,7 @@ import forge.assets.FImage;
import forge.error.BugReporter; import forge.error.BugReporter;
import forge.error.ExceptionHandler; import forge.error.ExceptionHandler;
import forge.model.FModel; import forge.model.FModel;
import forge.properties.ForgeConstants;
import forge.properties.ForgePreferences; import forge.properties.ForgePreferences;
import forge.properties.ForgePreferences.FPref; import forge.properties.ForgePreferences.FPref;
import forge.screens.FScreen; import forge.screens.FScreen;
@@ -41,6 +42,7 @@ import forge.toolbox.FContainer;
import forge.toolbox.FDisplayObject; import forge.toolbox.FDisplayObject;
import forge.toolbox.FGestureAdapter; import forge.toolbox.FGestureAdapter;
import forge.toolbox.FOverlay; import forge.toolbox.FOverlay;
import forge.util.FileUtil;
import forge.util.Utils; import forge.util.Utils;
public class Forge implements ApplicationListener { public class Forge implements ApplicationListener {
@@ -76,8 +78,16 @@ public class Forge implements ApplicationListener {
splashScreen = new SplashScreen(); splashScreen = new SplashScreen();
final ForgePreferences prefs = new ForgePreferences(); System.err.println("testing2323");
FSkin.loadLight(prefs.getPref(FPref.UI_SKIN), splashScreen);
String skinName;
if (FileUtil.doesFileExist(ForgeConstants.MAIN_PREFS_FILE)) {
skinName = new ForgePreferences().getPref(FPref.UI_SKIN);
}
else {
skinName = "default"; //use default skin if preferences file doesn't exist yet
}
FSkin.loadLight(skinName, splashScreen);
//load model on background thread (using progress bar to report progress) //load model on background thread (using progress bar to report progress)
FThreads.invokeInBackgroundThread(new Runnable() { FThreads.invokeInBackgroundThread(new Runnable() {
@@ -170,7 +180,7 @@ public class Forge implements ApplicationListener {
Animation.advanceAll(); Animation.advanceAll();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear the screen. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear the screen.
FContainer screen = currentScreen; FContainer screen = currentScreen;
if (screen == null) { if (screen == null) {
screen = splashScreen; screen = splashScreen;

View File

@@ -1,16 +1,14 @@
package forge.assets; package forge.assets;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.nio.channels.Channels; import java.net.URLConnection;
import java.nio.channels.ReadableByteChannel;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
@@ -56,7 +54,6 @@ public class AssetsDownloader {
FThreads.invokeInEdtAndWait(new Runnable() { FThreads.invokeInEdtAndWait(new Runnable() {
@Override @Override
public void run() { public void run() {
splashScreen.getProgressBar().setShowProgressTrail(false);
FSkin.reloadAfterAssetsDownload(splashScreen); FSkin.reloadAfterAssetsDownload(splashScreen);
} }
}); });
@@ -67,94 +64,82 @@ public class AssetsDownloader {
} }
private static void downloadAssets(final FProgressBar progressBar) { private static void downloadAssets(final FProgressBar progressBar) {
FThreads.invokeInEdtLater(new Runnable() { final String destFile = ForgeConstants.ASSETS_DIR + "assets.zip";
@Override
public void run() {
progressBar.setShowProgressTrail(true);
progressBar.setDescription("Updating resource files...\n(This may take several minutes)");
}
});
String url = "http://cardforge.org/android/releases/forge/forge-gui-android/" + Forge.CURRENT_VERSION + "/assets.zip";
final File destDir = new File(ForgeConstants.ASSETS_DIR);
final File fileDest = new File(destDir.getAbsolutePath() + "/assets.zip");
final File resDir = new File(ForgeConstants.RES_DIR);
try { try {
File resDir = new File(ForgeConstants.RES_DIR);
if (resDir.exists()) { if (resDir.exists()) {
resDir.delete(); //attempt to delete previous res directory if to be rebuilt //attempt to delete previous res directory if to be rebuilt
FileUtil.deleteDirectory(resDir);
} }
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
ReadableByteChannel rbc = null; progressBar.reset();
FileOutputStream fos = null; progressBar.setPercentMode(true);
try { progressBar.setDescription("Downloading resource files");
// test for folder existence
if (!destDir.exists() && !destDir.mkdir()) { // create folder if not found
System.out.println("Can't create folder" + destDir.getAbsolutePath());
}
URL imageUrl = new URL(url); try {
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(Proxy.NO_PROXY); URL url = new URL("http://cardforge.org/android/releases/forge/forge-gui-android/" + Forge.CURRENT_VERSION + "/assets.zip");
// don't allow redirections here -- they indicate 'file not found' on the server URLConnection conn = url.openConnection();
conn.setInstanceFollowRedirects(false);
conn.connect(); conn.connect();
if (conn.getResponseCode() != 200) { long contentLength = conn.getContentLength();
conn.disconnect(); progressBar.setMaximum(100);
System.out.println("Could not download assets.zip");
return;
}
rbc = Channels.newChannel(conn.getInputStream()); // input stream to read file - with 8k buffer
fos = new FileOutputStream(fileDest); InputStream input = new BufferedInputStream(url.openStream(), 8192);
fos.getChannel().transferFrom(rbc, 0, 1 << 27);
// output stream to write file
OutputStream output = new FileOutputStream(destFile);
int count;
long total = 0;
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
total += count;
progressBar.setValue((int)(total / contentLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} }
catch (final Exception ex) { catch (final Exception ex) {
Log.error("Assets", "Error downloading assets", ex); Log.error("Assets", "Error downloading assets", ex);
} }
finally {
if (rbc != null) {
try {
rbc.close();
}
catch (IOException e) {
System.out.println("error closing input stream");
}
}
if (fos != null) {
try {
fos.close();
}
catch (IOException e) {
System.out.println("error closing output stream");
}
}
}
//if assets.zip downloaded successfully, unzip into destination folder //if assets.zip downloaded successfully, unzip into destination folder
try { try {
ZipFile zipFile = new ZipFile(fileDest); ZipFile zipFile = new ZipFile(destFile);
Enumeration<? extends ZipEntry> entries = zipFile.entries(); Enumeration<? extends ZipEntry> entries = zipFile.entries();
progressBar.reset();
progressBar.setPercentMode(true);
progressBar.setDescription("Unzipping resource files");
progressBar.setMaximum(zipFile.size());
int count = 0;
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entries.nextElement(); ZipEntry entry = (ZipEntry)entries.nextElement();
String path = ForgeConstants.ASSETS_DIR + entry.getName(); String path = ForgeConstants.ASSETS_DIR + entry.getName();
if (entry.isDirectory()) { if (entry.isDirectory()) {
new File(path).mkdir(); new File(path).mkdir();
progressBar.setValue(++count);
continue; continue;
} }
copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(path))); copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(path)));
progressBar.setValue(++count);
} }
zipFile.close(); zipFile.close();
fileDest.delete(); new File(destFile).delete();
} }
catch (Exception e) { catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@@ -284,9 +284,9 @@ public class FSkin {
//if skins directory doesn't exists, create a minimum directory containing skin files for the splash screen //if skins directory doesn't exists, create a minimum directory containing skin files for the splash screen
FileUtil.ensureDirectoryExists(ForgeConstants.DEFAULT_SKINS_DIR); FileUtil.ensureDirectoryExists(ForgeConstants.DEFAULT_SKINS_DIR);
final FileHandle defaultDir = Gdx.files.absolute(ForgeConstants.DEFAULT_SKINS_DIR); final FileHandle defaultDir = Gdx.files.absolute(ForgeConstants.DEFAULT_SKINS_DIR);
Gdx.files.internal("bg_splash.png").copyTo(defaultDir.child("bg_splash.png")); Gdx.files.internal(ForgeConstants.SPLASH_BG_FILE).copyTo(defaultDir.child(ForgeConstants.SPLASH_BG_FILE));
Gdx.files.internal("bg_texture.jpg").copyTo(defaultDir.child("bg_texture.jpg")); Gdx.files.internal(ForgeConstants.TEXTURE_BG_FILE).copyTo(defaultDir.child(ForgeConstants.TEXTURE_BG_FILE));
Gdx.files.internal("font1.ttf").copyTo(defaultDir.child("font1.ttf")); Gdx.files.internal(ForgeConstants.FONT_FILE).copyTo(defaultDir.child(ForgeConstants.FONT_FILE));
mySkins.add("default"); mySkins.add("default");
needReloadAfterAssetsDownloaded = true; //flag that skins need to be reloaded after assets downloaded needReloadAfterAssetsDownloaded = true; //flag that skins need to be reloaded after assets downloaded
} }

View File

@@ -17,7 +17,7 @@ public class BugReportDialog extends FOptionPane {
private static boolean dialogShown; private static boolean dialogShown;
public static void show(String title, String text, boolean showExitAppBtn) { public static void show(String title, String text, boolean showExitAppBtn) {
if (dialogShown) { return; } if (dialogShown || Forge.getCurrentScreen() == null) { return; } //don't allow showing if Forge not finished initializing yet
dialogShown = true; dialogShown = true;
BugReportDialog dialog = new BugReportDialog(title, text, showExitAppBtn); BugReportDialog dialog = new BugReportDialog(title, text, showExitAppBtn);

View File

@@ -249,7 +249,7 @@ public abstract class GuiDownloadService implements Runnable {
conn.setInstanceFollowRedirects(false); conn.setInstanceFollowRedirects(false);
conn.connect(); conn.connect();
if (conn.getResponseCode() != 200) { if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
conn.disconnect(); conn.disconnect();
System.out.println("Skipped Download for: " + fileDest.getPath()); System.out.println("Skipped Download for: " + fileDest.getPath());
update(++iCard, fileDest); update(++iCard, fileDest);