Skip to content

Commit

Permalink
Merge branch 'tweak-async-tasks'
Browse files Browse the repository at this point in the history
  • Loading branch information
littleguy77 committed Dec 31, 2014
2 parents a38051d + a03ad22 commit 5fdb319
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 304 deletions.
33 changes: 21 additions & 12 deletions src/paulscode/android/mupen64plusae/GalleryActivity.java
Expand Up @@ -39,8 +39,6 @@
import paulscode.android.mupen64plusae.task.CacheRomInfoTask.CacheRomInfoListener;
import paulscode.android.mupen64plusae.task.ComputeMd5Task;
import paulscode.android.mupen64plusae.task.ComputeMd5Task.ComputeMd5Listener;
import paulscode.android.mupen64plusae.task.FindRomsTask;
import paulscode.android.mupen64plusae.task.FindRomsTask.FindRomsListener;
import paulscode.android.mupen64plusae.util.ChangeLog;
import paulscode.android.mupen64plusae.util.DeviceUtil;
import paulscode.android.mupen64plusae.util.Notifier;
Expand All @@ -63,7 +61,7 @@
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class GalleryActivity extends Activity implements OnItemClickListener, ComputeMd5Listener, FindRomsListener, CacheRomInfoListener
public class GalleryActivity extends Activity implements OnItemClickListener, ComputeMd5Listener, CacheRomInfoListener
{
// App data and user preferences
private AppData mAppData = null;
Expand All @@ -72,6 +70,9 @@ public class GalleryActivity extends Activity implements OnItemClickListener, Co
// Widgets
private GridView mGridView;

// Background tasks
private CacheRomInfoTask mCacheRomInfoTask = null;

@Override
protected void onNewIntent( Intent intent )
{
Expand Down Expand Up @@ -135,6 +136,18 @@ protected void onCreate( Bundle savedInstanceState )
}
}

protected void onStop()
{
super.onStop();

// Cancel long-running background tasks
if( mCacheRomInfoTask != null )
{
mCacheRomInfoTask.cancel( false );
mCacheRomInfoTask = null;
}
}

@Override
public boolean onCreateOptionsMenu( Menu menu )
{
Expand Down Expand Up @@ -261,13 +274,8 @@ private void refreshRoms( final File startDir )
{
// Asynchronously search for ROMs
Notifier.showToast( this, "Searching for ROMs in " + startDir.getName() );
new FindRomsTask( startDir, this ).execute();
}

@Override
public void onFindRomsFinished( List<File> result )
{
new CacheRomInfoTask( result, mAppData.mupen64plus_ini, mUserPrefs.romInfoCache_cfg, mUserPrefs.galleryDataDir, this ).execute();
mCacheRomInfoTask = new CacheRomInfoTask( startDir, mAppData.mupen64plus_ini, mUserPrefs.romInfoCache_cfg, mUserPrefs.galleryDataDir, this );
mCacheRomInfoTask.execute();
}

@Override
Expand All @@ -277,9 +285,10 @@ public void onCacheRomInfoProgress( ConfigSection section )
}

@Override
public void onCacheRomInfoFinished( ConfigFile config )
public void onCacheRomInfoFinished( ConfigFile config, boolean canceled )
{
Notifier.showToast( this, "Finished" );
mCacheRomInfoTask = null;
Notifier.showToast( this, canceled ? "Canceled" : "Finished" );
refreshGrid( config );
}

Expand Down
119 changes: 109 additions & 10 deletions src/paulscode/android/mupen64plusae/task/CacheRomInfoTask.java
Expand Up @@ -20,12 +20,18 @@
*/
package paulscode.android.mupen64plusae.task;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import paulscode.android.mupen64plusae.persistent.ConfigFile;
import paulscode.android.mupen64plusae.persistent.ConfigFile.ConfigSection;
import paulscode.android.mupen64plusae.util.FileUtil;
import paulscode.android.mupen64plusae.util.RomDatabase;
import paulscode.android.mupen64plusae.util.RomDatabase.RomDetail;
import android.os.AsyncTask;
Expand All @@ -36,13 +42,16 @@ public class CacheRomInfoTask extends AsyncTask<Void, ConfigSection, ConfigFile>
public interface CacheRomInfoListener
{
public void onCacheRomInfoProgress( ConfigSection section );
public void onCacheRomInfoFinished( ConfigFile file );

public void onCacheRomInfoFinished( ConfigFile file, boolean canceled );
}

public CacheRomInfoTask( List<File> files, String databasePath, String configPath, String artDir, CacheRomInfoListener listener )
public CacheRomInfoTask( File searchPath, String databasePath, String configPath, String artDir, CacheRomInfoListener listener )
{
if( files == null )
throw new IllegalArgumentException( "File list cannot be null" );
if( searchPath == null )
throw new IllegalArgumentException( "Root path cannot be null" );
if( !searchPath.exists() )
throw new IllegalArgumentException( "Root path does not exist: " + searchPath.getAbsolutePath() );
if( TextUtils.isEmpty( databasePath ) )
throw new IllegalArgumentException( "ROM database path cannot be null or empty" );
if( TextUtils.isEmpty( configPath ) )
Expand All @@ -52,14 +61,14 @@ public CacheRomInfoTask( List<File> files, String databasePath, String configPat
if( listener == null )
throw new IllegalArgumentException( "Listener cannot be null" );

mFiles = files;
mSearchPath = searchPath;
mDatabasePath = databasePath;
mConfigPath = configPath;
mArtDir = artDir;
mListener = listener;
}

private final List<File> mFiles;
private final File mSearchPath;
private final String mDatabasePath;
private final String mConfigPath;
private final String mArtDir;
Expand All @@ -68,20 +77,27 @@ public CacheRomInfoTask( List<File> files, String databasePath, String configPat
@Override
protected ConfigFile doInBackground( Void... params )
{
final List<File> files = getRomFiles( mSearchPath );
final RomDatabase database = new RomDatabase( mDatabasePath );
final ConfigFile config = new ConfigFile( mConfigPath );
config.clear();

for( final File file : mFiles )
for( final File file : files )
{
if( isCancelled() ) break;
String md5 = ComputeMd5Task.computeMd5( file );

if( isCancelled() ) break;
RomDetail detail = database.lookupByMd5WithFallback( md5, file );

if( isCancelled() ) break;
String artPath = mArtDir + "/" + detail.artName;
config.put( md5, "goodName", detail.goodName );
config.put( md5, "romPath", file.getAbsolutePath() );
config.put( md5, "artPath", artPath );
FileUtil.downloadFile( detail.artUrl, artPath );
downloadFile( detail.artUrl, artPath );

if( isCancelled() ) break;
this.publishProgress( config.get( md5 ) );
}
config.save();
Expand All @@ -97,6 +113,89 @@ protected void onProgressUpdate( ConfigSection... values )
@Override
protected void onPostExecute( ConfigFile result )
{
mListener.onCacheRomInfoFinished( result );
mListener.onCacheRomInfoFinished( result, false );
}

@Override
protected void onCancelled( ConfigFile result )
{
mListener.onCacheRomInfoFinished( result, true );
}

private List<File> getRomFiles( File searchPath )
{
List<File> result = new ArrayList<File>();
if( searchPath.isDirectory() )
{
for( File file : searchPath.listFiles() )
{
if( isCancelled() ) break;
result.addAll( getRomFiles( file ) );
}
}
else
{
String name = searchPath.getName().toLowerCase( Locale.US );
if( name.matches( ".*\\.(n64|v64|z64|zip)$" ) )
result.add( searchPath );
}
return result;
}

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;
try
{
url = new URL( sourceUrl );
input = new DataInputStream( url.openStream() );
fos = new FileOutputStream( destPath );
output = new DataOutputStream( fos );

int contentLength = url.openConnection().getContentLength();
byte[] buffer = new byte[contentLength];
input.readFully( buffer );
output.write( buffer );
output.flush();
}
catch( Throwable error )
{
return error;
}
finally
{
if( output != null )
try
{
output.close();
}
catch( IOException ignored )
{
}
if( fos != null )
try
{
fos.close();
}
catch( IOException ignored )
{
}
if( input != null )
try
{
input.close();
}
catch( IOException ignored )
{
}
}
return null;
}
}
128 changes: 0 additions & 128 deletions src/paulscode/android/mupen64plusae/task/DownloadFileTask.java

This file was deleted.

0 comments on commit 5fdb319

Please sign in to comment.