Fixed data downloader not downloading optional packages

This commit is contained in:
pelya
2010-08-27 15:42:44 +03:00
parent 1330aec692
commit 6bd7d4126f
14 changed files with 233 additions and 230 deletions

View File

@@ -1,5 +1,5 @@
// This string is autogenerated by ChangeAppSettings.sh, do not change spaces amount
package com.googlecode.opentyrian;
package com.sourceforge.sc2;
import android.app.Activity;
import android.content.Context;
@@ -130,12 +130,12 @@ class DataDownloader extends Thread
public DataDownloader( MainActivity _Parent, TextView _Status )
{
Parent = _Parent;
DownloadComplete = false;
Status = new StatusWriter( _Status, _Parent );
//Status.setText( "Connecting to " + Globals.DataDownloadUrl );
outFilesDir = Parent.getFilesDir().getAbsolutePath();
if( Globals.DownloadToSdcard )
outFilesDir = "/sdcard/app-data/" + Globals.class.getPackage().getName();
DownloadComplete = false;
this.start();
}
@@ -154,14 +154,18 @@ class DataDownloader extends Thread
for( int i = 0; i < downloadFiles.length; i++ )
{
if( downloadFiles[i].length() > 0 && Globals.OptionalDataDownload.length > i && Globals.OptionalDataDownload[i] )
DownloadDataFile(downloadFiles[i], "libsdl-DownloadFinished-" + String.valueOf(i) + ".flag");
if( ! DownloadDataFile(downloadFiles[i], "libsdl-DownloadFinished-" + String.valueOf(i) + ".flag") )
return;
}
DownloadComplete = true;
initParent();
}
public void DownloadDataFile(final String DataDownloadUrl, final String DownloadFlagFileName)
public boolean DownloadDataFile(final String DataDownloadUrl, final String DownloadFlagFileName)
{
String [] downloadUrls = DataDownloadUrl.split("[|]");
if( downloadUrls.length < 2 )
return;
return false;
String path = getOutFilePath(DownloadFlagFileName);
InputStream checkFile = null;
@@ -187,14 +191,11 @@ class DataDownloader extends Thread
if( ! matched )
throw new IOException();
Status.setText( "No need to download" );
DownloadComplete = true;
initParent();
return;
return true;
} catch ( IOException e ) {};
}
checkFile = null;
int downloadUrlIndex = 1;
// Create output directory (not necessary for phone storage)
if( Globals.DownloadToSdcard )
{
@@ -203,68 +204,140 @@ class DataDownloader extends Thread
} catch( SecurityException e ) { };
}
downloading:
while(true)
HttpResponse response = null;
HttpGet request;
long totalLen;
CountingInputStream stream;
byte[] buf = new byte[16384];
boolean DoNotUnzip = false;
String url = "";
int downloadUrlIndex = 1;
while( downloadUrlIndex < downloadUrls.length )
{
HttpResponse response = null;
HttpGet request;
long totalLen;
CountingInputStream stream;
byte[] buf = new byte[16384];
boolean DoNotUnzip = false;
String url = "";
while( downloadUrlIndex < downloadUrls.length && response == null )
System.out.println("Processing download " + downloadUrls[downloadUrlIndex]);
url = new String(downloadUrls[downloadUrlIndex]);
DoNotUnzip = false;
if(url.indexOf(":") == 0)
{
System.out.println("Connecting to " + downloadUrls[downloadUrlIndex]);
Status.setText( "Connecting to " + downloadUrls[downloadUrlIndex] );
url = new String(downloadUrls[downloadUrlIndex]);
if(url.indexOf(":") == 0)
{
url = url.substring( url.indexOf(":", 1) + 1 );
DoNotUnzip = true;
}
request = new HttpGet(url);
request.addHeader("Accept", "*/*");
try {
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setBooleanParameter("http.protocol.handle-redirects", true);
response = client.execute(request);
} catch (IOException e) {
System.out.println("Failed to connect to " + downloadUrls[downloadUrlIndex]);
downloadUrlIndex++;
};
if( response != null )
{
if( response.getStatusLine().getStatusCode() != 200 )
{
response = null;
System.out.println("Failed to connect to " + url);
downloadUrlIndex++;
}
}
url = url.substring( url.indexOf(":", 1) + 1 );
DoNotUnzip = true;
}
if( response == null )
{
System.out.println("Error connecting to " + url);
Status.setText( "Error connecting to " + url );
return;
}
Status.setText( "Downloading data from " + url );
totalLen = response.getEntity().getContentLength();
System.out.println("Connecting to " + url);
Status.setText( "Connecting to " + url);
request = new HttpGet(url);
request.addHeader("Accept", "*/*");
try {
stream = new CountingInputStream(response.getEntity().getContent());
} catch( java.io.IOException e ) {
Status.setText( "Error downloading data from " + Globals.DataDownloadUrl );
return;
}
if(DoNotUnzip)
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setBooleanParameter("http.protocol.handle-redirects", true);
response = client.execute(request);
} catch (IOException e) {
System.out.println("Failed to connect to " + downloadUrls[downloadUrlIndex]);
downloadUrlIndex++;
};
if( response != null )
{
path = getOutFilePath(downloadUrls[downloadUrlIndex].substring( 1,
downloadUrls[downloadUrlIndex].indexOf(":", 1) ));
if( response.getStatusLine().getStatusCode() != 200 )
{
response = null;
System.out.println("Failed to connect to " + url);
downloadUrlIndex++;
}
}
}
if( response == null )
{
System.out.println("Error connecting to " + url);
Status.setText( "Error connecting to " + url );
return false;
}
Status.setText( "Downloading data from " + url );
totalLen = response.getEntity().getContentLength();
try {
stream = new CountingInputStream(response.getEntity().getContent());
} catch( java.io.IOException e ) {
Status.setText( "Error downloading data from " + Globals.DataDownloadUrl );
return false;
}
if(DoNotUnzip)
{
path = getOutFilePath(downloadUrls[downloadUrlIndex].substring( 1,
downloadUrls[downloadUrlIndex].indexOf(":", 1) ));
OutputStream out = null;
try {
out = new FileOutputStream( path );
} catch( FileNotFoundException e ) {
} catch( SecurityException e ) { };
if( out == null )
{
Status.setText( "Error writing to " + path );
return false;
}
try {
int len = stream.read(buf);
while (len >= 0)
{
if(len > 0)
out.write(buf, 0, len);
len = stream.read(buf);
String percent = "";
if( totalLen > 0 )
percent = String.valueOf(stream.getBytesRead() * 100 / totalLen) + "%: ";
Status.setText( percent + "writing file " + path );
}
out.flush();
out.close();
out = null;
} catch( java.io.IOException e ) {
Status.setText( "Error writing file " + path + " from URL " + url );
return false;
}
}
else
{
ZipInputStream zip = new ZipInputStream(stream);
while(true)
{
ZipEntry entry = null;
try {
entry = zip.getNextEntry();
} catch( java.io.IOException e ) {
Status.setText( "Error downloading data from " + url );
return false;
}
if( entry == null )
break;
if( entry.isDirectory() )
{
try {
(new File( getOutFilePath(entry.getName()) )).mkdirs();
} catch( SecurityException e ) { };
continue;
}
OutputStream out = null;
path = getOutFilePath(entry.getName());
try {
CheckedInputStream check = new CheckedInputStream( new FileInputStream(path), new CRC32() );
while( check.read(buf, 0, buf.length) > 0 ) {};
check.close();
if( check.getChecksum().getValue() != entry.getCrc() )
{
File ff = new File(path);
ff.delete();
throw new Exception();
}
continue;
} catch( Exception e )
{
}
try {
out = new FileOutputStream( path );
} catch( FileNotFoundException e ) {
@@ -272,18 +345,23 @@ class DataDownloader extends Thread
if( out == null )
{
Status.setText( "Error writing to " + path );
return;
return false;
}
String percent = "";
if( totalLen > 0 )
percent = String.valueOf(stream.getBytesRead() * 100 / totalLen) + "%: ";
Status.setText( percent + "writing file " + path );
try {
int len = stream.read(buf);
int len = zip.read(buf);
while (len >= 0)
{
if(len > 0)
out.write(buf, 0, len);
len = stream.read(buf);
len = zip.read(buf);
String percent = "";
percent = "";
if( totalLen > 0 )
percent = String.valueOf(stream.getBytesRead() * 100 / totalLen) + "%: ";
Status.setText( percent + "writing file " + path );
@@ -293,136 +371,48 @@ class DataDownloader extends Thread
out = null;
} catch( java.io.IOException e ) {
Status.setText( "Error writing file " + path + " from URL " + url );
return;
return false;
}
}
else
{
ZipInputStream zip = new ZipInputStream(stream);
while(true)
try {
CheckedInputStream check = new CheckedInputStream( new FileInputStream(path), new CRC32() );
while( check.read(buf, 0, buf.length) > 0 ) {};
check.close();
if( check.getChecksum().getValue() != entry.getCrc() )
{
File ff = new File(path);
ff.delete();
throw new Exception();
}
} catch( Exception e )
{
ZipEntry entry = null;
try {
entry = zip.getNextEntry();
} catch( java.io.IOException e ) {
Status.setText( "Error downloading data from " + url );
return;
}
if( entry == null )
break;
if( entry.isDirectory() )
{
try {
(new File( getOutFilePath(entry.getName()) )).mkdirs();
} catch( SecurityException e ) { };
continue;
}
OutputStream out = null;
path = getOutFilePath(entry.getName());
try {
CheckedInputStream check = new CheckedInputStream( new FileInputStream(path), new CRC32() );
while( check.read(buf, 0, buf.length) > 0 ) {};
check.close();
if( check.getChecksum().getValue() != entry.getCrc() )
{
File ff = new File(path);
ff.delete();
throw new Exception();
}
continue;
} catch( Exception e )
{
}
try {
out = new FileOutputStream( path );
} catch( FileNotFoundException e ) {
} catch( SecurityException e ) { };
if( out == null )
{
Status.setText( "Error writing to " + path + " from URL " + url );
return;
}
String percent = "";
if( totalLen > 0 )
percent = String.valueOf(stream.getBytesRead() * 100 / totalLen) + "%: ";
Status.setText( percent + "writing file " + path );
try {
int len = zip.read(buf);
while (len >= 0)
{
if(len > 0)
out.write(buf, 0, len);
len = zip.read(buf);
percent = "";
if( totalLen > 0 )
percent = String.valueOf(stream.getBytesRead() * 100 / totalLen) + "%: ";
Status.setText( percent + "writing file " + path );
}
out.flush();
out.close();
out = null;
} catch( java.io.IOException e ) {
Status.setText( "Error writing file " + path + " from URL " + url );
return;
}
try {
CheckedInputStream check = new CheckedInputStream( new FileInputStream(path), new CRC32() );
while( check.read(buf, 0, buf.length) > 0 ) {};
check.close();
if( check.getChecksum().getValue() != entry.getCrc() )
{
File ff = new File(path);
ff.delete();
throw new Exception();
}
} catch( Exception e )
{
Status.setText( "CRC32 check failed for file " + path );
continue downloading; // Start download over from the same URL
//return;
}
Status.setText( "CRC32 check failed for file " + path );
return false;
}
}
};
OutputStream out = null;
path = getOutFilePath(DownloadFlagFileName);
try {
out = new FileOutputStream( path );
out.write(downloadUrls[downloadUrlIndex].getBytes("UTF-8"));
out.flush();
out.close();
} catch( FileNotFoundException e ) {
} catch( SecurityException e ) {
} catch( java.io.IOException e ) {
Status.setText( "Error writing file " + path );
return;
};
if( out == null )
{
Status.setText( "Error writing to " + path );
return;
}
Status.setText( "Finished" );
DownloadComplete = true;
try {
stream.close();
} catch( java.io.IOException e ) {
};
initParent();
break;
}
OutputStream out = null;
path = getOutFilePath(DownloadFlagFileName);
try {
out = new FileOutputStream( path );
out.write(downloadUrls[downloadUrlIndex].getBytes("UTF-8"));
out.flush();
out.close();
} catch( FileNotFoundException e ) {
} catch( SecurityException e ) {
} catch( java.io.IOException e ) {
Status.setText( "Error writing file " + path );
return false;
};
Status.setText( "Finished" );
try {
stream.close();
} catch( java.io.IOException e ) {
};
return true;
};
private void initParent()
@@ -448,8 +438,8 @@ class DataDownloader extends Thread
return outFilesDir + "/" + filename;
};
public boolean DownloadComplete;
public StatusWriter Status;
public boolean DownloadComplete = false;
private MainActivity Parent;
private String outFilesDir = null;
}