Support for Google Play expansion files

This commit is contained in:
pelya
2014-11-09 23:24:45 +02:00
parent bc138b83b3
commit a6ca9e6bef
2 changed files with 90 additions and 33 deletions

View File

@@ -284,22 +284,28 @@ class DataDownloader extends Thread
catch( FileNotFoundException e ) {}
catch( IOException e ) {};
HttpResponse response = null, responseError = null;
HttpGet request;
HttpResponse response = null, responseError = null;
long totalLen = 0;
long partialDownloadLen = 0;
CountingInputStream stream;
byte[] buf = new byte[16384];
boolean DoNotUnzip = false;
boolean FileInAssets = false;
boolean FileInExpansion = false;
String url = "";
long partialDownloadLen = 0;
int downloadUrlIndex = 1;
while( downloadUrlIndex < downloadUrls.length )
{
Log.i("SDL", "Processing download " + downloadUrls[downloadUrlIndex]);
url = new String(downloadUrls[downloadUrlIndex]);
DoNotUnzip = false;
FileInAssets = false;
FileInExpansion = false;
partialDownloadLen = 0;
totalLen = 0;
url = new String(downloadUrls[downloadUrlIndex]);
if(url.indexOf(":") == 0)
{
path = getOutFilePath(url.substring( 1, url.indexOf(":", 1) ));
@@ -311,7 +317,24 @@ class DataDownloader extends Thread
partialDownloadLen = partialDownload.length();
}
Status.setText( downloadCount + "/" + downloadTotal + ": " + res.getString(R.string.connecting_to, url) );
if( url.indexOf("http://") == -1 && url.indexOf("https://") == -1 ) // File inside assets
if( url.indexOf("obb:") == 0 ) // APK expansion file provided by Google Play
{
FileInExpansion = true;
url = url.substring("obb:".length());
url = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/obb/" +
Parent.getPackageName() + "/" + url + "." + Parent.getPackageName() + ".obb";
Log.i("SDL", "Fetching file from OBB: " + url);
InputStream stream1 = null;
try {
stream1 = new FileInputStream(url);
stream1.close();
} catch( Exception e ) {
Log.i("SDL", "Failed to open file: " + url);
downloadUrlIndex++;
continue;
}
}
else if( url.indexOf("http://") == -1 && url.indexOf("https://") == -1 ) // File inside assets
{
InputStream stream1 = null;
try {
@@ -362,7 +385,22 @@ class DataDownloader extends Thread
}
}
}
if( FileInAssets )
if( FileInExpansion )
{
try {
stream = new CountingInputStream(new FileInputStream(url), 8192);
while( stream.skip(65536) > 0 ) { };
totalLen = stream.getBytesRead();
stream.close();
stream = new CountingInputStream(new FileInputStream(url), 8192);
} catch( IOException e ) {
Log.i("SDL", "Unpacking from filesystem '" + url + "' - error: " + e.toString());
Status.setText( res.getString(R.string.error_dl_from, url) );
return false;
}
}
else if( FileInAssets )
{
int multipartCounter = 0;
InputStream multipart = null;
@@ -393,7 +431,7 @@ class DataDownloader extends Thread
try {
stream = new CountingInputStream(Parent.getAssets().open(url), 8192);
while( stream.skip(65536) > 0 ) { };
totalLen += stream.getBytesRead();
totalLen = stream.getBytesRead();
stream.close();
stream = new CountingInputStream(Parent.getAssets().open(url), 8192);
} catch( IOException e ) {
@@ -421,9 +459,49 @@ class DataDownloader extends Thread
return false;
}
}
if( !copyUnpackFileStream(stream, path, url, DoNotUnzip, FileInAssets, FileInExpansion, totalLen, partialDownloadLen, response, downloadCount, downloadTotal) )
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( res.getString(R.string.error_write, path) + ": " + e.getMessage() );
return false;
};
Status.setText( downloadCount + "/" + downloadTotal + ": " + res.getString(R.string.dl_finished) );
try {
stream.close();
if( FileInExpansion )
{
Writer writer = new OutputStreamWriter(new FileOutputStream(url), "UTF-8");
writer.write("Extracted and cleared\n");
writer.close();
}
} catch( java.io.IOException e ) {
};
return true;
};
// Moved part of code to a separate method, because Android imposes a stupid limit on Java method size
private boolean copyUnpackFileStream( CountingInputStream stream, String path, String url,
boolean DoNotUnzip, boolean FileInAssets, boolean FileInExpansion,
long totalLen, long partialDownloadLen, HttpResponse response,
int downloadCount, int downloadTotal)
{
long updateStatusTime = 0;
byte[] buf = new byte[16384];
Resources res = Parent.getResources();
if(DoNotUnzip)
{
Log.i("SDL", "Saving file '" + path + "'");
@@ -651,30 +729,9 @@ class DataDownloader extends Thread
Log.i("SDL", "Saving file '" + path + "' done");
}
};
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( res.getString(R.string.error_write, path) + ": " + e.getMessage() );
return false;
};
Status.setText( downloadCount + "/" + downloadTotal + ": " + res.getString(R.string.dl_finished) );
try {
stream.close();
} catch( java.io.IOException e ) {
};
return true;
};
}
private void initParent()
{
class Callback implements Runnable