Skip to content
Permalink
Browse files

Merge branch 'enhance-rom-search'

  • Loading branch information
littleguy77 committed Jan 18, 2015
2 parents 6b0b7ab + 4caf203 commit b6bc32677fbc1f94095d89e919476aef4582e5b2
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/linearLayout1"
android:orientation="vertical" >

</ListView>

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:padding="16dp" >

<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select a ROM file or an entire folder to scan"
android:textAppearance="?android:attr/textAppearanceMedium" />

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search zip files" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download cover art" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clear gallery before adding" />
</LinearLayout>

</RelativeLayout>
@@ -28,8 +28,8 @@
import org.mupen64plusae.v3.alpha.R;

import paulscode.android.mupen64plusae.dialog.ChangeLog;
import paulscode.android.mupen64plusae.dialog.Prompt;
import paulscode.android.mupen64plusae.dialog.Prompt.PromptFileListener;
import paulscode.android.mupen64plusae.dialog.ScanRomsDialog;
import paulscode.android.mupen64plusae.dialog.ScanRomsDialog.ScanRomsDialogListener;
import paulscode.android.mupen64plusae.input.DiagnosticActivity;
import paulscode.android.mupen64plusae.persistent.AppData;
import paulscode.android.mupen64plusae.persistent.ConfigFile;
@@ -252,29 +252,41 @@ private void promptSearchPath( File startDir )
if( startDir == null || !startDir.exists() )
startDir = new File( Environment.getExternalStorageDirectory().getAbsolutePath() );

Prompt.promptFile( this, startDir.getPath(), null, startDir, true, true, false, true,
new PromptFileListener()
ScanRomsDialog dialog = new ScanRomsDialog( this, startDir,
mUserPrefs.getSearchZips(),
mUserPrefs.getDownloadArt(),
mUserPrefs.getClearGallery(),
new ScanRomsDialogListener()
{
@Override
public void onDialogClosed( File file, int which )
public void onDialogClosed( File file, int which, boolean searchZips,
boolean downloadArt, boolean clearGallery )
{
mUserPrefs.putSearchZips( searchZips );
mUserPrefs.putDownloadArt( downloadArt );
mUserPrefs.putClearGallery( clearGallery );
if( which == DialogInterface.BUTTON_POSITIVE )
{
refreshRoms( file );
}
else if( file != null )
{
promptSearchPath( file );
{
if( file.isDirectory() )
promptSearchPath( file );
else
refreshRoms( file );
}
}
} );
dialog.show();
}

private void refreshRoms( final File startDir )
{
// Asynchronously search for ROMs
mCacheRomInfoTask = new CacheRomInfoTask( this, startDir, mAppData.mupen64plus_ini, mUserPrefs.romInfoCache_cfg,
mUserPrefs.coverArtDir, mUserPrefs.unzippedRomsDir, this );
mCacheRomInfoTask = new CacheRomInfoTask( this, startDir,
mAppData.mupen64plus_ini, mUserPrefs.romInfoCache_cfg, mUserPrefs.coverArtDir, mUserPrefs.unzippedRomsDir,
mUserPrefs.getSearchZips(), mUserPrefs.getDownloadArt(), mUserPrefs.getClearGallery(),this );
mCacheRomInfoTask.execute();
}

@@ -0,0 +1,125 @@
package paulscode.android.mupen64plusae.dialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mupen64plusae.v3.alpha.R;

import paulscode.android.mupen64plusae.util.FileUtil;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;

public class ScanRomsDialog implements OnClickListener, OnItemClickListener
{
public interface ScanRomsDialogListener
{
/**
* Called when the dialog is dismissed and should be used to process the file selected by
* the user.
*
* @param file The file selected by the user, or null if the user clicks the dialog's
* negative button.
* @param which The DialogInterface button pressed by the user.
*/
public void onDialogClosed( File file, int which, boolean searchZips, boolean downloadArt, boolean clearGallery );
}

private final ScanRomsDialogListener mListener;
private final List<CharSequence> mNames;
private final List<String> mPaths;
private final CheckBox mCheckBox1;
private final CheckBox mCheckBox2;
private final CheckBox mCheckBox3;
private final AlertDialog mDialog;
private final File mStartPath;

@SuppressLint( "InflateParams" )
public ScanRomsDialog( Activity activity, File startPath, boolean searchZips, boolean downloadArt, boolean clearGallery,
ScanRomsDialogListener listener )
{
mListener = listener;

// Pick the root of the storage directory by default
if( startPath == null || !startPath.exists() )
startPath = new File( Environment.getExternalStorageDirectory().getAbsolutePath() );
mStartPath = startPath;

// Get the filenames and absolute paths
mNames = new ArrayList<CharSequence>();
mPaths = new ArrayList<String>();
FileUtil.populate( startPath, true, true, true, mNames, mPaths );

// Inflate layout
final LayoutInflater inflater = (LayoutInflater) activity
.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View layout = inflater.inflate( R.layout.scan_roms_dialog, null );

// Set checkbox state
mCheckBox1 = (CheckBox) layout.findViewById( R.id.checkBox1 );
mCheckBox2 = (CheckBox) layout.findViewById( R.id.checkBox2 );
mCheckBox3 = (CheckBox) layout.findViewById( R.id.checkBox3 );
mCheckBox1.setChecked( searchZips );
mCheckBox2.setChecked( downloadArt );
mCheckBox3.setChecked( clearGallery );

// Populate the file list
ListView listView1 = (ListView) layout.findViewById( R.id.listView1 );
ArrayAdapter<String> adapter = Prompt.createFilenameAdapter( activity, mPaths, mNames );
listView1.setAdapter( adapter );
listView1.setOnItemClickListener( this );

// Create the dialog
Builder builder = new Builder( activity ).setTitle( startPath.getPath() )
.setCancelable( false ).setView( layout )
.setPositiveButton( android.R.string.ok, this )
.setNegativeButton( android.R.string.cancel, this );

mDialog = builder.create();
}

public void show()
{
mDialog.show();
}

public void dismiss()
{
mDialog.dismiss();
}

@Override
public void onItemClick( AdapterView<?> parent, View view, int position, long id )
{
onClick( null, position );
}

@Override
public void onClick( DialogInterface dlg, int which )
{
boolean check1 = mCheckBox1.isChecked();
boolean check2 = mCheckBox2.isChecked();
boolean check3 = mCheckBox3.isChecked();
dismiss();

if( which >= 0 && which < mNames.size() )
mListener.onDialogClosed( new File( mPaths.get( which ) ), which, check1, check2, check3 );
else if( which == DialogInterface.BUTTON_POSITIVE )
mListener.onDialogClosed( mStartPath, which, check1, check2, check3 );
else
mListener.onDialogClosed( null, which, check1, check2, check3 );
}
}
@@ -226,6 +226,9 @@
private static final String KEYTEMPLATE_PAK_TYPE = "inputPakType%1$d";
private static final String KEY_PLAYER_MAP_REMINDER = "playerMapReminder";
private static final String KEY_LOCALE_OVERRIDE = "localeOverride";
private static final String KEY_SEARCH_ZIPS = "searchZips";
private static final String KEY_DOWNLOAD_ART = "downloadArt";
private static final String KEY_CLEAR_GALLERY = "clearGallery";
// ... add more as needed

// Shared preferences default values
@@ -235,6 +238,9 @@
public static final int DEFAULT_PAK_TYPE = NativeConstants.PAK_TYPE_MEMORY;
public static final boolean DEFAULT_PLAYER_MAP_REMINDER = true;
public static final String DEFAULT_LOCALE_OVERRIDE = "";
public static final boolean DEFAULT_SEARCH_ZIPS = true;
public static final boolean DEFAULT_DOWNLOAD_ART = true;
public static final boolean DEFAULT_CLEAR_GALLERY = true;
// ... add more as needed

private final SharedPreferences mPreferences;
@@ -565,6 +571,21 @@ public boolean getPlayerMapReminder()
return getBoolean( KEY_PLAYER_MAP_REMINDER, DEFAULT_PLAYER_MAP_REMINDER );
}

public boolean getSearchZips()
{
return getBoolean( KEY_SEARCH_ZIPS, DEFAULT_SEARCH_ZIPS );
}

public boolean getDownloadArt()
{
return getBoolean( KEY_DOWNLOAD_ART, DEFAULT_DOWNLOAD_ART );
}

public boolean getClearGallery()
{
return getBoolean( KEY_CLEAR_GALLERY, DEFAULT_CLEAR_GALLERY );
}

public void putEmulationProfileDefault( String value )
{
putString( KEY_EMULATION_PROFILE_DEFAULT, value );
@@ -590,6 +611,21 @@ public void putPlayerMapReminder( boolean value )
putBoolean( KEY_PLAYER_MAP_REMINDER, value );
}

public void putSearchZips( boolean value )
{
putBoolean( KEY_SEARCH_ZIPS, value );
}

public void putDownloadArt( boolean value )
{
putBoolean( KEY_DOWNLOAD_ART, value );
}

public void putClearGallery( boolean value )
{
putBoolean( KEY_CLEAR_GALLERY, value );
}

private boolean getBoolean( String key, boolean defaultValue )
{
return mPreferences.getBoolean( key, defaultValue );
@@ -58,7 +58,9 @@
public void onCacheRomInfoFinished( ConfigFile file, boolean canceled );
}

public CacheRomInfoTask( Activity activity, File searchPath, String databasePath, String configPath, String artDir, String unzipDir, CacheRomInfoListener listener )
public CacheRomInfoTask( Activity activity, File searchPath, String databasePath, String configPath,
String artDir, String unzipDir, boolean searchZips, boolean downloadArt, boolean clearGallery,
CacheRomInfoListener listener )
{
if( searchPath == null )
throw new IllegalArgumentException( "Root path cannot be null" );
@@ -80,6 +82,9 @@ public CacheRomInfoTask( Activity activity, File searchPath, String databasePath
mConfigPath = configPath;
mArtDir = artDir;
mUnzipDir = unzipDir;
mSearchZips = searchZips;
mDownloadArt = downloadArt;
mClearGallery = clearGallery;
mListener = listener;

CharSequence title = activity.getString( R.string.scanning_title );
@@ -93,6 +98,9 @@ public CacheRomInfoTask( Activity activity, File searchPath, String databasePath
private final String mConfigPath;
private final String mArtDir;
private final String mUnzipDir;
private final boolean mSearchZips;
private final boolean mDownloadArt;
private final boolean mClearGallery;
private final CacheRomInfoListener mListener;
private final ProgressDialog mProgress;

@@ -110,7 +118,8 @@ protected ConfigFile doInBackground( Void... params )
final List<File> files = getAllFiles( mSearchPath );
final RomDatabase database = new RomDatabase( mDatabasePath );
final ConfigFile config = new ConfigFile( mConfigPath );
config.clear();
if (mClearGallery)
config.clear();

mProgress.setMaxProgress( files.size() );
for( final File file : files )
@@ -126,7 +135,7 @@ protected ConfigFile doInBackground( Void... params )
{
cacheFile( file, database, config );
}
else if( header.isZip )
else if( header.isZip && mSearchZips )
{
Log.i( "CacheRomInfoTask", "Found zip file " + file.getName() );
try
@@ -221,14 +230,17 @@ private void cacheFile( File file, RomDatabase database, ConfigFile config )
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( mDownloadArt )
{
if( isCancelled() ) return;
mProgress.setMessage( "Downloading cover art…" );
downloadFile( detail.artUrl, artPath );
}

if( isCancelled() ) return;
mProgress.setMessage( "Refreshing UI…" );

0 comments on commit b6bc326

Please sign in to comment.
You can’t perform that action at this time.