Permalink
Browse files

Merge branch 'enhance-cache-rom-task'

  • Loading branch information...
littleguy77 committed Jan 18, 2015
2 parents 78849f4 + 1da122e commit b6f00c551ba8ac781f04547258ac9c3ee0e4b7ac
@@ -23,6 +23,7 @@
private final AsyncTask<?, ?, ?> mTask;
private final TextView mTextProgress;
private final TextView mTextSubprogress;
private final TextView mTextMessage;
private final ProgressBar mProgressSubtotal;
private final ProgressBar mProgressTotal;
private final AlertDialog mDialog;
@@ -47,11 +48,12 @@ public ProgressDialog( Activity activity, AsyncTask<?, ?, ?> task, CharSequence
Builder builder = getBuilder( activity, title, subtitle, message, cancelable, layout );
mTextProgress = (TextView) layout.findViewById( R.id.textProgress );
mTextSubprogress = (TextView) layout.findViewById( R.id.textSubprogress );
mTextMessage = (TextView) layout.findViewById( R.id.textMessage );
mProgressSubtotal = (ProgressBar) layout.findViewById( R.id.progressSubtotal );
mProgressTotal = (ProgressBar) layout.findViewById( R.id.progressTotal );
mDialog = builder.create();
// Create abort dialog
// Create canceling dialog
subtitle = mActivity.getString( R.string.toast_canceling );
message = mActivity.getString( R.string.toast_pleaseWait );
layout = inflater.inflate( R.layout.progress_dialog, null );
@@ -121,6 +123,18 @@ public void run()
} );
}
public void setMessage( final CharSequence text )
{
mActivity.runOnUiThread( new Runnable()
{
@Override
public void run()
{
mTextMessage.setText( text );
}
} );
}
public void setMaxProgress( final long size )
{
mActivity.runOnUiThread( new Runnable()
@@ -161,8 +175,8 @@ public void run()
if( mMaxProgress > 0 )
{
mProgress += inc;
int pctProgress = Math
.round( ( PROGRESS_PRECISION * mProgress ) / mMaxProgress );
int pctProgress = Math.round( ( PROGRESS_PRECISION * mProgress )
/ mMaxProgress );
mProgressTotal.setProgress( pctProgress );
}
}
@@ -20,13 +20,14 @@
*/
package paulscode.android.mupen64plusae.task;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
@@ -117,6 +118,7 @@ protected ConfigFile doInBackground( Void... params )
mProgress.setMaxSubprogress( 0 );
mProgress.setSubtext( "" );
mProgress.setText( file.getAbsolutePath().substring( mSearchPath.getAbsolutePath().length() ) );
mProgress.setMessage( "Searching…" );
if( isCancelled() ) break;
RomHeader header = new RomHeader( file );
@@ -136,6 +138,7 @@ else if( header.isZip )
{
ZipEntry zipEntry = entries.nextElement();
mProgress.setSubtext( zipEntry.getName() );
mProgress.setMessage( "Searching zip file…" );
if( isCancelled() ) break;
try
@@ -212,23 +215,27 @@ protected void onCancelled( ConfigFile result )
private void cacheFile( File file, RomDatabase database, ConfigFile config )
{
if( isCancelled() ) return;
mProgress.setMessage( "Computing MD5…" );
String md5 = ComputeMd5Task.computeMd5( file );
if( isCancelled() ) return;
mProgress.setMessage( "Searching ROM database…" );
RomDetail detail = database.lookupByMd5WithFallback( md5, file );
if( isCancelled() ) return;
mProgress.setMessage( "Downloading cover art…" );
String artPath = mArtDir + "/" + detail.artName;
config.put( md5, "goodName", detail.goodName );
config.put( md5, "romPath", file.getAbsolutePath() );
config.put( md5, "artPath", artPath );
downloadFile( detail.artUrl, artPath );
if( isCancelled() ) return;
mProgress.setMessage( "Refreshing UI…" );
this.publishProgress( config.get( md5 ) );
}
private File extractRomFile( File destDir, ZipEntry zipEntry, InputStream zipStream )
private File extractRomFile( File destDir, ZipEntry zipEntry, InputStream inStream )
{
if( zipEntry.isDirectory() )
return null;
@@ -237,7 +244,7 @@ private File extractRomFile( File destDir, ZipEntry zipEntry, InputStream zipStr
byte[] buffer = new byte[1024];
try
{
if( zipStream.read( buffer, 0, 4 ) != 4 )
if( inStream.read( buffer, 0, 4 ) != 4 )
return null;
}
catch( IOException e )
@@ -246,31 +253,35 @@ private File extractRomFile( File destDir, ZipEntry zipEntry, InputStream zipStr
return null;
}
// See if this entry is a valid ROM (copy bits in case RomHeader twiddles them)
// See if this entry is a valid ROM (copy array in case RomHeader mutates it)
if( !new RomHeader( new byte[] { buffer[0], buffer[1], buffer[2], buffer[3] } ).isValid )
return null;
// This entry appears to be a valid ROM, extract it
Log.i( "CacheRomInfoTask", "Found zip entry " + zipEntry.getName() );
mProgress.setMessage( "Extracting zip entry…" );
String entryName = new File( zipEntry.getName() ).getName();
File extractedFile = new File( destDir, entryName );
try
{
FileOutputStream outStream = new FileOutputStream( extractedFile );
// Open the output stream (throws exceptions)
OutputStream outStream = new FileOutputStream( extractedFile );
try
{
BufferedOutputStream boutStream = new BufferedOutputStream( outStream );
// Buffer the stream
outStream = new BufferedOutputStream( outStream );
// Write the first four bytes we already peeked at
boutStream.write( buffer, 0, 4 );
// Write the first four bytes we already peeked at (throws exceptions)
outStream.write( buffer, 0, 4 );
// Read/write the remainder of the zip entry
// Read/write the remainder of the zip entry (throws exceptions)
int n;
while( ( n = zipStream.read( buffer ) ) >= 0 )
while( ( n = inStream.read( buffer ) ) >= 0 )
{
boutStream.write( buffer, 0, n );
if( isCancelled() )
return null;
outStream.write( buffer, 0, n );
}
boutStream.close();
return extractedFile;
}
catch( IOException e )
@@ -280,6 +291,7 @@ private File extractRomFile( File destDir, ZipEntry zipEntry, InputStream zipStr
}
finally
{
// Flush output stream and guarantee no memory leaks
outStream.close();
}
}
@@ -292,83 +304,83 @@ private File extractRomFile( File destDir, ZipEntry zipEntry, InputStream zipStr
private static Throwable touchFile( String destPath )
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream( destPath );
}
catch( Throwable error )
{
return error;
OutputStream outStream = new FileOutputStream( destPath );
try
{
outStream.close();
}
catch( IOException e )
{
Log.w( "CacheRomInfoTask", e );
return e;
}
}
finally
catch( FileNotFoundException e )
{
if( fos != null )
try
{
fos.close();
}
catch( IOException ignored )
{
}
Log.w( "CacheRomInfoTask", e );
return e;
}
return null;
}
private static Throwable downloadFile( String sourceUrl, String destPath )
private Throwable downloadFile( String sourceUrl, String destPath )
{
// Be sure destination directory exists
new File( destPath ).getParentFile().mkdirs();
// Download file
URL url = null;
DataInputStream input = null;
FileOutputStream fos = null;
DataOutputStream output = null;
InputStream inStream = null;
OutputStream outStream = null;
try
{
url = new URL( sourceUrl );
input = new DataInputStream( url.openStream() );
fos = new FileOutputStream( destPath );
output = new DataOutputStream( fos );
// Open the streams (throws exceptions)
URL url = new URL( sourceUrl );
inStream = url.openStream();
outStream = new FileOutputStream( destPath );
// Buffer the streams
inStream = new BufferedInputStream( inStream );
outStream = new BufferedOutputStream( outStream );
int contentLength = url.openConnection().getContentLength();
byte[] buffer = new byte[contentLength];
input.readFully( buffer );
output.write( buffer );
output.flush();
// Read/write the streams (throws exceptions)
byte[] buffer = new byte[1024];
int n;
while( ( n = inStream.read( buffer ) ) >= 0 )
{
if( isCancelled() )
return null;
outStream.write( buffer, 0, n );
}
return null;
}
catch( Throwable error )
catch( Throwable e )
{
return error;
Log.w( "CacheRomInfoTask", e );
return e;
}
finally
{
if( output != null )
try
{
output.close();
}
catch( IOException ignored )
{
}
if( fos != null )
// Flush output stream and guarantee no memory leaks
if( outStream != null )
try
{
fos.close();
outStream.close();
}
catch( IOException ignored )
catch( IOException e )
{
Log.w( "CacheRomInfoTask", e );
}
if( input != null )
if( inStream != null )
try
{
input.close();
inStream.close();
}
catch( IOException ignored )
catch( IOException e )
{
Log.w( "CacheRomInfoTask", e );
}
}
return null;
}
}

0 comments on commit b6f00c5

Please sign in to comment.