Permalink
Browse files

Added search field to the ROMs list

The ROMs list is now searchable! Implementation is pretty basic, but
functional.
  • Loading branch information...
Mike McFadden
Mike McFadden committed Feb 26, 2015
1 parent 6581bc2 commit 058c8485b253412ccbd52ccfb24ec05b3bab86f4
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,9 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
-<GridView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/gridview"
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
- android:layout_margin="@dimen/galleryMargin"
- android:gravity="center"
- android:numColumns="1"
- android:verticalSpacing="@dimen/galleryMargin" />
+ android:orientation="vertical">
+
+ <EditText
+ android:id="@+id/searchview"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:layout_marginLeft="3dp"
+ android:layout_marginRight="3dp"
+ android:layout_marginBottom="5dp"
+ android:inputType="textFilter|textNoSuggestions"
+ android:maxLines="1"
+ android:singleLine="true"
+ android:imeOptions="actionSearch"
+ android:visibility="gone" />
+
+ <GridView
+ android:id="@+id/gridview"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:layout_margin="@dimen/galleryMargin"
+ android:gravity="center"
+ android:numColumns="1"
+ android:verticalSpacing="@dimen/galleryMargin" />
+
+</LinearLayout>
@@ -1,6 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
+ <item
+ android:id="@+id/menuItem_search"
+ android:icon="@drawable/ic_search"
+ android:showAsAction="ifRoom|withText"
+ android:title="@string/menuItem_search"/>
<item
android:id="@+id/menuItem_refreshRoms"
android:icon="@drawable/ic_refresh"
View
@@ -57,6 +57,7 @@
<string name="menuItem_unmapAll">Unmap all</string>
<!-- Menu Activity -->
+ <string name="menuItem_search">Search</string>
<string name="menuItem_play">Play</string>
<string name="menuItem_refreshRoms">Refresh ROMs</string>
<string name="menuItem_settings">Settings</string>
@@ -61,6 +61,15 @@
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
+import android.widget.EditText;
+
+import android.view.Window;
+import android.view.WindowManager;
+import android.view.KeyEvent;
+import android.graphics.Color;
+import android.graphics.drawable.ColorDrawable;
+import android.text.TextWatcher;
+import android.text.Editable;
public class GalleryActivity extends Activity implements OnItemClickListener, ComputeMd5Listener, CacheRomInfoListener
{
@@ -70,6 +79,7 @@
// Widgets
private GridView mGridView;
+ private EditText mSearchView;
// Background tasks
private CacheRomInfoTask mCacheRomInfoTask = null;
@@ -126,6 +136,7 @@ protected void onCreate( Bundle savedInstanceState )
// Lay out the content
setContentView( R.layout.gallery_activity );
mGridView = (GridView) findViewById( R.id.gridview );
+ mSearchView = (EditText) findViewById( R.id.searchview );
refreshGrid( new ConfigFile( mUserPrefs.romInfoCache_cfg ) );
// Popup a warning if the installation appears to be corrupt
@@ -135,6 +146,31 @@ protected void onCreate( Bundle savedInstanceState )
CharSequence message = getText( R.string.invalidInstall_message );
new Builder( this ).setTitle( title ).setMessage( message ).create().show();
}
+
+ if( mUserPrefs.isActionBarAvailable )
+ {
+ ColorDrawable color = new ColorDrawable( Color.parseColor( "#303030" ) );
+ color.setAlpha( mUserPrefs.displayActionBarTransparency );
+ getActionBar().setBackgroundDrawable( color );
+ }
+
+ mSearchView.addTextChangedListener(new TextWatcher()
+ {
+ public void afterTextChanged( Editable s )
+ {
+
+ }
+
+ public void beforeTextChanged( CharSequence s, int start, int count, int after )
+ {
+
+ }
+
+ public void onTextChanged( CharSequence s, int start, int before, int count )
+ {
+ refreshGrid( new ConfigFile( mUserPrefs.romInfoCache_cfg ) );
+ }
+ });
}
protected void onStop()
@@ -149,6 +185,28 @@ protected void onStop()
}
}
+ @SuppressLint( "InlinedApi" )
+ @TargetApi( 11 )
+ @Override
+ public void onBackPressed()
+ {
+ // Only applies to Honeycomb devices
+ if( !AppData.IS_HONEYCOMB )
+ return;
+
+ // Hide the search field
+ if ( mSearchView.getVisibility() == View.VISIBLE )
+ {
+ mSearchView.setVisibility( View.GONE );
+ mGridView.requestFocus();
+ refreshGrid( new ConfigFile( mUserPrefs.romInfoCache_cfg ) );
+ }
+ else
+ {
+ super.onBackPressed();
+ }
+ }
+
@Override
public boolean onCreateOptionsMenu( Menu menu )
{
@@ -161,6 +219,19 @@ public boolean onMenuItemSelected( int featureId, MenuItem item )
{
switch( item.getItemId() )
{
+ case R.id.menuItem_search:
+ if ( mSearchView.getVisibility() == View.VISIBLE )
+ {
+ mSearchView.setVisibility( View.GONE );
+ mGridView.requestFocus();
+ }
+ else
+ {
+ mSearchView.setVisibility( View.VISIBLE );
+ mSearchView.requestFocus();
+ }
+ refreshGrid( new ConfigFile( mUserPrefs.romInfoCache_cfg ) );
+ return true;
case R.id.menuItem_refreshRoms:
promptSearchPath( null );
return true;
@@ -305,15 +376,43 @@ public void onCacheRomInfoFinished( ConfigFile config, boolean canceled )
private void refreshGrid( ConfigFile config )
{
+ String searchText = "";
+ String[] searches = null;
+ if ( mSearchView.getVisibility() == View.VISIBLE )
+ {
+ searchText = mSearchView.getText().toString().toLowerCase();
+ if ( searchText.length() > 0 )
+ searches = searchText.split(" ");
+ }
+
List<GalleryItem> items = new ArrayList<GalleryItem>();
for( String md5 : config.keySet() )
{
if( !ConfigFile.SECTIONLESS_NAME.equals( md5 ) )
{
String goodName = config.get( md5, "goodName" );
- String romPath = config.get( md5, "romPath" );
- String artPath = config.get( md5, "artPath" );
- items.add( new GalleryItem( this, md5, goodName, romPath, artPath ) );
+
+ boolean matchesSearch = true;
+ if ( searches != null && searches.length > 0 )
+ {
+ // Make sure the ROM name contains every token in the query
+ String lowerName = goodName.toLowerCase();
+ for( String search : searches )
+ {
+ if ( search.length() > 0 && !lowerName.contains( search ) )
+ {
+ matchesSearch = false;
+ break;
+ }
+ }
+ }
+
+ if ( matchesSearch )
+ {
+ String romPath = config.get( md5, "romPath" );
+ String artPath = config.get( md5, "artPath" );
+ items.add( new GalleryItem( this, md5, goodName, romPath, artPath ) );
+ }
}
}
Collections.sort( items );

0 comments on commit 058c848

Please sign in to comment.