diff --git a/app/src/main/java/org/transdroid/core/app/search/GoogleWebSearchBarcodeResolver.java b/app/src/main/java/org/transdroid/core/app/search/GoogleWebSearchBarcodeResolver.java index 93ad08c0..df013b07 100644 --- a/app/src/main/java/org/transdroid/core/app/search/GoogleWebSearchBarcodeResolver.java +++ b/app/src/main/java/org/transdroid/core/app/search/GoogleWebSearchBarcodeResolver.java @@ -17,12 +17,6 @@ */ package org.transdroid.core.app.search; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Locale; - import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; @@ -31,110 +25,69 @@ import org.json.JSONException; import org.json.JSONObject; import org.transdroid.daemon.util.HttpHelper; +import java.io.InputStream; +import java.util.Locale; + public class GoogleWebSearchBarcodeResolver { public static final String apiUrl = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%s"; - + public static String resolveBarcode(String barcode) { - + try { // We use the Google AJAX Search API to get a JSON-formatted list of web search results String callUrl = apiUrl.replace("%s", barcode); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(callUrl); HttpResponse response = httpclient.execute(httpget); - InputStream instream = response.getEntity().getContent(); - String result = HttpHelper.convertStreamToString(instream); + InputStream instream = response.getEntity().getContent(); + String result = HttpHelper.convertStreamToString(instream); JSONArray results = new JSONObject(result).getJSONObject("responseData").getJSONArray("results"); - - // We will combine and filter multiple results, if there are any + + // Use the first result, if any, after cleaning it from special characters if (results.length() < 1) { return null; } - return stripGarbage(results, barcode); + return stripGarbage(results.getJSONObject(0), barcode); } catch (Exception e) { return null; } - + } - private static String stripGarbage(JSONArray results, String barcode) throws JSONException { - - String good = " abcdefghijklmnopqrstuvwxyz"; - final int MAX_TITLE_CONSIDER = 4; - final int MAX_MISSING = 1; - final int MIN_TITLE_CONSIDER = 2; - - // First gather the titles for the first MAX_TITLE_CONSIDER results - List titles = new ArrayList(); - for (int i = 0; i < results.length() && i < MAX_TITLE_CONSIDER; i++) { - - String title = results.getJSONObject(i).getString("titleNoFormatting"); - - // Make string lowercase first - title = title.toLowerCase(Locale.US); - - // Remove the barcode number if it's there - title = title.replace(barcode, ""); - - // Remove unwanted words and HTML special chars - for (String rem : new String[] { "dvd", "blu-ray", "bluray", "&", """, "'", "<", ">" }) { - title = title.replace(rem, ""); - } - - // Remove all non-alphanumeric (and space) characters - String result = ""; - for ( int j = 0; j < title.length(); j++ ) { - if ( good.indexOf(title.charAt(j)) >= 0 ) - result += title.charAt(j); - } - - // Remove double spaces - while (result.contains(" ")) { - result = result.replace(" ", " "); - } - - titles.add(result); - - } + private static String stripGarbage(JSONObject item, String barcode) throws JSONException { - // Only retain the words that are missing in at most one of the search result titles - List allWords = new ArrayList(); - for (String title : titles) { - for (String word : Arrays.asList(title.split(" "))) { - if (!allWords.contains(word)) { - allWords.add(word); - } - } + String good = " abcdefghijklmnopqrstuvwxyz1234567890"; + + // Find the unformatted title + String title = item.getString("titleNoFormatting"); + + // Make string lowercase first + title = title.toLowerCase(Locale.US); + + // Remove the barcode number if it's there + title = title.replace(barcode, ""); + + // Remove unwanted words and HTML special chars + for (String rem : new String[]{"dvd", "blu-ray", "bluray", "&", """, "'", "<", ">"}) { + title = title.replace(rem, ""); } - List remainingWords = new ArrayList(); - int allowMissing = Math.min(MAX_MISSING, Math.max(titles.size() - MIN_TITLE_CONSIDER, 0)); - for (String word : allWords) { - - int missing = 0; - for (String title : titles) { - if (!title.contains(word)) { - // The word is not contained in this result title - missing++; - if (missing > allowMissing) { - // Already misssing more than once, no need to look further - break; - } - } - } - if (missing <= allowMissing) { - // The word was only missing at most once, so we keep it - remainingWords.add(word); + + // Remove all non-alphanumeric (and space) characters + String result = ""; + for (int j = 0; j < title.length(); j++) { + if (good.indexOf(title.charAt(j)) >= 0) { + result += title.charAt(j); } } - - // Now the query is the concatenation of the words remaining; with spaces in between - String query = ""; - for (String word : remainingWords) { - query += " " + word; + + // Remove double spaces + while (result.contains(" ")) { + result = result.replace(" ", " "); } - return query.length() > 0? query.substring(1): null; - + + return result; + } } diff --git a/app/src/main/java/org/transdroid/core/gui/TorrentsActivity.java b/app/src/main/java/org/transdroid/core/gui/TorrentsActivity.java index ee62c0b2..177ab15b 100644 --- a/app/src/main/java/org/transdroid/core/gui/TorrentsActivity.java +++ b/app/src/main/java/org/transdroid/core/gui/TorrentsActivity.java @@ -111,6 +111,7 @@ import android.net.Uri; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; +import android.text.TextUtils; import android.view.Menu; import android.view.MenuItem; import android.view.MenuItem.OnActionExpandListener; @@ -135,7 +136,8 @@ public class TorrentsActivity extends Activity implements OnNavigationListener, RefreshableActivity { private static final int RESULT_DETAILS = 0; - + // Fragment uses this to pause the refresh across restarts + public boolean stopRefresh = false; // Navigation components @Bean protected Log log; @@ -150,19 +152,12 @@ public class TorrentsActivity extends Activity implements OnNavigationListener, protected ServerStatusView serverStatusView; @SystemService protected SearchManager searchManager; - private MenuItem searchMenu = null; - private PullToRefreshAttacher pullToRefreshAttacher = null; - // Settings @Bean protected ApplicationSettings applicationSettings; @Bean protected SystemSettings systemSettings; @InstanceState - boolean firstStart = true; - int skipNextOnNavigationItemSelectedCalls = 2; - private IDaemonAdapter currentConnection = null; - @InstanceState protected NavigationFilter currentFilter = null; @InstanceState protected String preselectNavigationFilter = null; @@ -170,17 +165,29 @@ public class TorrentsActivity extends Activity implements OnNavigationListener, protected boolean turleModeEnabled = false; @InstanceState protected ArrayList