From 865385ec787b3186b432932deb946b91a6332aaa Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Thu, 16 May 2013 17:14:44 +0200 Subject: [PATCH] Allow filtering (with text entry) on torrents list.\nUpgraded to Eclipse ADT tools v22. --- android/.classpath | 1 + .../org/transdroid/gui/TorrentsFragment.java | 4 +- core/.classpath | 3 +- .../core/gui/FilterEntryDialog.java | 39 ++++++++++++ .../transdroid/core/gui/TorrentsActivity.java | 19 ++++-- .../transdroid/core/gui/TorrentsFragment.java | 62 ++++++++++++++----- .../core/gui/search/UrlEntryDialog.java | 4 ++ external/ColorPickerPreference/.classpath | 3 +- external/Crouton/library/.classpath | 3 +- .../library/.classpath | 1 + full/.classpath | 3 +- lite/.classpath | 3 +- 12 files changed, 117 insertions(+), 28 deletions(-) create mode 100644 core/src/org/transdroid/core/gui/FilterEntryDialog.java diff --git a/android/.classpath b/android/.classpath index 6b9842fa..98efdebb 100644 --- a/android/.classpath +++ b/android/.classpath @@ -5,5 +5,6 @@ + diff --git a/android/src/org/transdroid/gui/TorrentsFragment.java b/android/src/org/transdroid/gui/TorrentsFragment.java index d92653b7..a9ae176e 100644 --- a/android/src/org/transdroid/gui/TorrentsFragment.java +++ b/android/src/org/transdroid/gui/TorrentsFragment.java @@ -1749,7 +1749,7 @@ public class TorrentsFragment extends SherlockFragment implements IDaemonCallbac // Sort the new list of torrents allTorrents = ((RetrieveTaskSuccessResult) result).getTorrents(); - Collections.sort(allTorrents, new TorrentsComparator(daemon, sortSetting, sortReversed)); + Collections.sort(allTorrents, new TorrentsComparator(daemon.getType(), sortSetting, sortReversed)); // Sort the new list of labels allLabels = ((RetrieveTaskSuccessResult) result).getLabels(); @@ -1936,7 +1936,7 @@ public class TorrentsFragment extends SherlockFragment implements IDaemonCallbac if (!(getTorrentListAdapter() == null || getTorrentListAdapter().getCount() == 0)) { // Sort the shown list of torrents using the new sortBy criteria - Collections.sort(allTorrents, new TorrentsComparator(daemon, sortSetting, sortReversed)); + Collections.sort(allTorrents, new TorrentsComparator(daemon.getType(), sortSetting, sortReversed)); updateTorrentsView(true); } diff --git a/core/.classpath b/core/.classpath index 08d23ddb..68860191 100644 --- a/core/.classpath +++ b/core/.classpath @@ -3,12 +3,13 @@ - + + diff --git a/core/src/org/transdroid/core/gui/FilterEntryDialog.java b/core/src/org/transdroid/core/gui/FilterEntryDialog.java new file mode 100644 index 00000000..7d97759a --- /dev/null +++ b/core/src/org/transdroid/core/gui/FilterEntryDialog.java @@ -0,0 +1,39 @@ +package org.transdroid.core.gui; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.support.v4.app.DialogFragment; +import android.text.InputType; +import android.view.inputmethod.InputMethodManager; +import android.widget.EditText; + +public class FilterEntryDialog { + + /** + * Opens a dialog that allows entry of a filter string, which (on confirmation) will be used to filter the list of + * torrents. + * @param activity The activity that opens (and owns) this dialog + */ + public static void startFilterEntry(final TorrentsActivity activity) { + new DialogFragment() { + public android.app.Dialog onCreateDialog(android.os.Bundle savedInstanceState) { + final EditText filterInput = new EditText(activity); + filterInput.setInputType(InputType.TYPE_TEXT_VARIATION_FILTER); + ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput( + InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); + return new AlertDialog.Builder(activity).setView(filterInput) + .setPositiveButton(android.R.string.ok, new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + String filterText = filterInput.getText().toString(); + if (activity != null) + activity.filterTorrents(filterText); + } + }).setNegativeButton(android.R.string.cancel, null).create(); + }; + }.show(activity.getSupportFragmentManager(), "filterentry"); + } + +} diff --git a/core/src/org/transdroid/core/gui/TorrentsActivity.java b/core/src/org/transdroid/core/gui/TorrentsActivity.java index 9d5ffb7c..8a208786 100644 --- a/core/src/org/transdroid/core/gui/TorrentsActivity.java +++ b/core/src/org/transdroid/core/gui/TorrentsActivity.java @@ -307,7 +307,7 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi // Status type or label selection - both of which are navigation filters if (item instanceof NavigationFilter) { currentFilter = (NavigationFilter) item; - fragmentTorrents.applyFilter(currentFilter); + fragmentTorrents.applyNavigationFilter(currentFilter); navigationSpinnerAdapter.updateCurrentFilter(currentFilter); // Clear the details view if (fragmentDetails != null) { @@ -444,10 +444,6 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi updateTurtleMode(false); } - @OptionsItem(resName = "action_filter") - protected void filterList() { - } - @OptionsItem(resName = "action_settings") protected void openSettings() { MainSettingsActivity_.intent(this).start(); @@ -488,6 +484,19 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi fragmentTorrents.sortBy(TorrentsSortBy.Ratio); } + @OptionsItem(resName = "action_filter") + protected void startFilterEntryDialog() { + FilterEntryDialog.startFilterEntry(this); + } + + /** + * Redirect the newly entered list filter to the torrents fragment. + * @param newFilterText The newly entered filter (or empty to clear the current filter). + */ + public void filterTorrents(String newFilterText) { + fragmentTorrents.applyTextFilter(newFilterText); + } + /** * Shows the a details fragment for the given torrent, either in the dedicated details fragment pane, in the same * pane as the torrent list was displayed or by starting a details activity. diff --git a/core/src/org/transdroid/core/gui/TorrentsFragment.java b/core/src/org/transdroid/core/gui/TorrentsFragment.java index bc9df79f..fa71b2e2 100644 --- a/core/src/org/transdroid/core/gui/TorrentsFragment.java +++ b/core/src/org/transdroid/core/gui/TorrentsFragment.java @@ -2,7 +2,9 @@ package org.transdroid.core.gui; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; +import java.util.Locale; import org.androidannotations.annotations.AfterViews; import org.androidannotations.annotations.Bean; @@ -40,12 +42,14 @@ public class TorrentsFragment extends SherlockFragment { @InstanceState protected ArrayList torrents = null; @InstanceState - protected NavigationFilter currentFilter = null; + protected NavigationFilter currentNavigationFilter = null; @InstanceState protected TorrentsSortBy currentSortOrder = TorrentsSortBy.Alphanumeric; @InstanceState protected boolean currentSortDescending = false; @InstanceState + protected String currentTextFilter = null; + @InstanceState protected boolean hasAConnection = false; @InstanceState protected boolean isLoading = true; @@ -80,7 +84,7 @@ public class TorrentsFragment extends SherlockFragment { */ public void updateTorrents(ArrayList newTorrents) { torrents = newTorrents; - applyFilter(null); // Resets the filter and shown list of torrents + applyNavigationFilter(null); // Resets the filter and shown list of torrents } /** @@ -88,7 +92,9 @@ public class TorrentsFragment extends SherlockFragment { */ public void clear() { this.connectionErrorMessage = null; - updateTorrents(null); + this.currentTextFilter = null; + this.currentNavigationFilter = null; + applyAllFilters(); } /** @@ -111,27 +117,51 @@ public class TorrentsFragment extends SherlockFragment { Collections.sort(this.torrents, new TorrentsComparator(serverType, this.currentSortOrder, this.currentSortDescending)); // Show the new resorted list - applyFilter(this.currentFilter); + applyAllFilters(); + } + + public void applyTextFilter(String newTextFilter) { + this.currentTextFilter = newTextFilter; + // TODO: Actually apply text filter + // Show the new filtered list + applyAllFilters(); } /** * Apply a filter on the current list of all torrents, showing the appropriate sublist of torrents only * @param newFilter The new filter to apply to the local list of torrents */ - public void applyFilter(NavigationFilter newFilter) { - this.currentFilter = newFilter; - if (torrents != null && newFilter != null) { - // Build a local list of torrents that match the selected navigation filter - ArrayList filteredTorrents = new ArrayList(); - for (Torrent torrent : torrents) { - if (newFilter.matches(torrent)) - filteredTorrents.add(torrent); + public void applyNavigationFilter(NavigationFilter newFilter) { + this.currentNavigationFilter = newFilter; + applyAllFilters(); + } + + private void applyAllFilters() { + + // No torrents? Directly update views accordingly + if (torrents == null) { + updateViewVisibility(); + return; + } + + // Filter the list of torrents to show according to navigation and text filters + ArrayList filteredTorrents = torrents; + if (torrents != null && currentNavigationFilter != null) { + // Remove torrents that do not match the selected navigation filter + for (Iterator torrentIter = torrents.iterator(); torrentIter.hasNext();) { + if (!currentNavigationFilter.matches(torrentIter.next())) + torrentIter.remove(); + } + } + if (torrents != null && currentTextFilter != null) { + // Remove torrent that do not contain the text filter string + for (Iterator torrentIter = torrents.iterator(); torrentIter.hasNext();) { + if (!torrentIter.next().getName().toLowerCase(Locale.getDefault()) + .contains(currentTextFilter.toLowerCase(Locale.getDefault()))) + torrentIter.remove(); } - ((TorrentsAdapter) torrentsList.getAdapter()).update(filteredTorrents); - } else if (torrents != null) { - // No need to filter any torrents; directly show the full list - ((TorrentsAdapter) torrentsList.getAdapter()).update(torrents); } + ((TorrentsAdapter) torrentsList.getAdapter()).update(filteredTorrents); updateViewVisibility(); } diff --git a/core/src/org/transdroid/core/gui/search/UrlEntryDialog.java b/core/src/org/transdroid/core/gui/search/UrlEntryDialog.java index db0f7eb4..0c00cb2a 100644 --- a/core/src/org/transdroid/core/gui/search/UrlEntryDialog.java +++ b/core/src/org/transdroid/core/gui/search/UrlEntryDialog.java @@ -3,11 +3,13 @@ package org.transdroid.core.gui.search; import org.transdroid.core.gui.TorrentsActivity; import android.app.AlertDialog; +import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.support.v4.app.DialogFragment; import android.text.InputType; import android.text.TextUtils; +import android.view.inputmethod.InputMethodManager; import android.widget.EditText; public class UrlEntryDialog { @@ -22,6 +24,8 @@ public class UrlEntryDialog { public android.app.Dialog onCreateDialog(android.os.Bundle savedInstanceState) { final EditText urlInput = new EditText(activity); urlInput.setInputType(InputType.TYPE_TEXT_VARIATION_URI); + ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput( + InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); return new AlertDialog.Builder(activity).setView(urlInput) .setPositiveButton(android.R.string.ok, new OnClickListener() { @Override diff --git a/external/ColorPickerPreference/.classpath b/external/ColorPickerPreference/.classpath index a4763d1e..7bc01d9a 100644 --- a/external/ColorPickerPreference/.classpath +++ b/external/ColorPickerPreference/.classpath @@ -3,6 +3,7 @@ - + + diff --git a/external/Crouton/library/.classpath b/external/Crouton/library/.classpath index a4763d1e..7bc01d9a 100644 --- a/external/Crouton/library/.classpath +++ b/external/Crouton/library/.classpath @@ -3,6 +3,7 @@ - + + diff --git a/external/JakeWharton-ActionBarSherlock/library/.classpath b/external/JakeWharton-ActionBarSherlock/library/.classpath index 570891c6..c8e53d5f 100644 --- a/external/JakeWharton-ActionBarSherlock/library/.classpath +++ b/external/JakeWharton-ActionBarSherlock/library/.classpath @@ -4,5 +4,6 @@ + diff --git a/full/.classpath b/full/.classpath index a4763d1e..7bc01d9a 100644 --- a/full/.classpath +++ b/full/.classpath @@ -3,6 +3,7 @@ - + + diff --git a/lite/.classpath b/lite/.classpath index a4763d1e..7bc01d9a 100644 --- a/lite/.classpath +++ b/lite/.classpath @@ -3,6 +3,7 @@ - + +