From a7f12a3e11d04183ee4148eb2ff7b6e979932206 Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Thu, 6 Feb 2014 10:06:13 +0100 Subject: [PATCH] Added support for the new Torrent Search module private sites feature (to download a torrent via the Torrent Search module). --- core/.classpath | 1 - .../core/app/search/SearchHelper.java | 34 +++++++++- .../core/app/search/SearchSite.java | 8 ++- .../transdroid/core/gui/TorrentsActivity.java | 67 +++++++++++++------ .../gui/search/SearchResultsFragment.java | 7 ++ 5 files changed, 95 insertions(+), 22 deletions(-) diff --git a/core/.classpath b/core/.classpath index 340040a3..1931bb1c 100644 --- a/core/.classpath +++ b/core/.classpath @@ -10,6 +10,5 @@ - diff --git a/core/src/org/transdroid/core/app/search/SearchHelper.java b/core/src/org/transdroid/core/app/search/SearchHelper.java index 84715ec8..d8c2c53f 100644 --- a/core/src/org/transdroid/core/app/search/SearchHelper.java +++ b/core/src/org/transdroid/core/app/search/SearchHelper.java @@ -16,6 +16,10 @@ */ package org.transdroid.core.app.search; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; @@ -44,6 +48,7 @@ public class SearchHelper { static final int CURSOR_SITE_CODE = 1; static final int CURSOR_SITE_NAME = 2; static final int CURSOR_SITE_RSSURL = 3; + static final int CURSOR_SITE_ISPRIVATE = 4; @RootContext protected Context context; @@ -76,12 +81,18 @@ public class SearchHelper { // Query the available in-app torrent search sites Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); + if (cursor == null) { + // The installed Torrent Search version is corrupt or incompatible + return null; + } if (cursor.moveToFirst()) { List sites = new ArrayList(); do { // Read the cursor fields into the SearchSite object sites.add(new SearchSite(cursor.getInt(CURSOR_SITE_ID), cursor.getString(CURSOR_SITE_CODE), cursor - .getString(CURSOR_SITE_NAME), cursor.getString(CURSOR_SITE_RSSURL))); + .getString(CURSOR_SITE_NAME), cursor.getString(CURSOR_SITE_RSSURL), + cursor.getColumnNames().length > 4 ? (cursor.getInt(CURSOR_SITE_ISPRIVATE) == 1 ? true : false) + : false)); } while (cursor.moveToNext()); cursor.close(); return sites; @@ -134,4 +145,25 @@ public class SearchHelper { } + /** + * Asks the Torrent Search module to download a torrent file given the provided url, while using the specifics of + * the supplied torrent search site to do so. This way the Search Module can take care of user credentials, for + * example. + * @param site The unique key of the search site that this url belongs to, which is used to create a connection + * specific to this (private) site + * @param url The full url of the torrent to download + * @return A file input stream handler that points to the locally downloaded file + * @throws FileNotFoundException Thrown when the requested url could not be downloaded or is not locally available + */ + public InputStream getFile(String site, String url) throws FileNotFoundException { + try { + Uri uri = Uri.parse("content://org.transdroid.search.torrentsearchprovider/get/" + site + "/" + + URLEncoder.encode(url, "UTF-8")); + return context.getContentResolver().openInputStream(uri); + } catch (UnsupportedEncodingException e) { + // Ignore + return null; + } + } + } diff --git a/core/src/org/transdroid/core/app/search/SearchSite.java b/core/src/org/transdroid/core/app/search/SearchSite.java index 88f38439..95cd810a 100644 --- a/core/src/org/transdroid/core/app/search/SearchSite.java +++ b/core/src/org/transdroid/core/app/search/SearchSite.java @@ -29,12 +29,14 @@ public class SearchSite implements SimpleListItem, SearchSetting { private final String key; private final String name; private final String rssFeedUrl; + private final boolean isPrivate; - public SearchSite(int id, String key, String name, String rssFeedUrl) { + public SearchSite(int id, String key, String name, String rssFeedUrl, boolean isPrivate) { this.id = id; this.key = key; this.name = name; this.rssFeedUrl = rssFeedUrl; + this.isPrivate = isPrivate; } public int getId() { @@ -58,5 +60,9 @@ public class SearchSite implements SimpleListItem, SearchSetting { public String getBaseUrl() { return rssFeedUrl; } + + public boolean isPrivate() { + return isPrivate; + } } diff --git a/core/src/org/transdroid/core/gui/TorrentsActivity.java b/core/src/org/transdroid/core/gui/TorrentsActivity.java index acb6788a..03c3bdcc 100644 --- a/core/src/org/transdroid/core/gui/TorrentsActivity.java +++ b/core/src/org/transdroid/core/gui/TorrentsActivity.java @@ -44,7 +44,7 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.cookie.BasicClientCookie; import org.transdroid.core.R; -import org.transdroid.core.app.settings.ApplicationSettings; +import org.transdroid.core.app.search.*; import org.transdroid.core.app.settings.*; import org.transdroid.core.app.settings.WebsearchSetting; import org.transdroid.core.gui.lists.LocalTorrent; @@ -228,7 +228,7 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi // No server settings yet; return; } - Torrent startTorrent = null; + Torrent openTorrent = null; if (getIntent().getAction() != null && getIntent().getAction().equals(ListWidgetProvider.INTENT_STARTSERVER) && getIntent().getExtras() == null && getIntent().hasExtra(ListWidgetProvider.EXTRA_SERVER)) { // A server settings order ID was provided in this org.transdroid.START_SERVER action intent @@ -239,7 +239,7 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi } else { lastUsed = applicationSettings.getServerSetting(serverId); if (getIntent().hasExtra(ListWidgetProvider.EXTRA_TORRENT)) - startTorrent = getIntent().getParcelableExtra(ListWidgetProvider.EXTRA_TORRENT); + openTorrent = getIntent().getParcelableExtra(ListWidgetProvider.EXTRA_TORRENT); } } @@ -252,10 +252,10 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi // Force first torrents refresh filterSelected(lastUsed, true); // Handle any start up intents - if (firstStart && startTorrent != null) { - openDetails(startTorrent); - startTorrent = null; - } else if (firstStart && getIntent() != null) { + if (openTorrent != null) { + openDetails(openTorrent); + openTorrent = null; + } else if (getIntent() != null) { handleStartIntent(); } } else { @@ -483,14 +483,19 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi String action = intent.getAction(); // Adding multiple torrents at the same time (as found in the Intent extras Bundle) - if (action != null && action.equals("org.transdroid.ADD_MULTIPLE")) { // Intent should have some extras pointing to possibly multiple torrents String[] urls = intent.getStringArrayExtra("TORRENT_URLS"); String[] titles = intent.getStringArrayExtra("TORRENT_TITLES"); if (urls != null) { for (int i = 0; i < urls.length; i++) { - addTorrentByUrl(urls[i], (titles != null && titles.length >= i ? titles[i] : "Torrent")); + String title = (titles != null && titles.length >= i ? titles[i] : "Torrent"); + if (intent.hasExtra("PRIVATE_SOURCE")) { + // This is marked by the Search Module as being a private source site; get the url locally first + addTorrentFromPrivateSource(urls[i], title, intent.getStringExtra("PRIVATE_SOURCE")); + } else { + addTorrentByUrl(urls[i], title); + } } } return; @@ -513,29 +518,40 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi // Adding a torrent from http or https URL if (dataUri.getScheme().equals("http") || dataUri.getScheme().equals("https")) { - // Check if the target URL is also defined as a web search in the user's settings - List websearches = applicationSettings.getWebsearchSettings(); + String privateSource = getIntent().getStringExtra("PRIVATE_SOURCE"); + WebsearchSetting match = null; - for (WebsearchSetting setting : websearches) { - Uri uri = Uri.parse(setting.getBaseUrl()); - if (uri.getHost() != null && uri.getHost().equals(dataUri.getHost())) { - match = setting; - break; + if (privateSource == null) { + // Check if the target URL is also defined as a web search in the user's settings + List websearches = applicationSettings.getWebsearchSettings(); + for (WebsearchSetting setting : websearches) { + Uri uri = Uri.parse(setting.getBaseUrl()); + if (uri.getHost() != null && uri.getHost().equals(dataUri.getHost())) { + match = setting; + break; + } } } // If the URL is also a web search and it defines cookies, use the cookies by downloading the targeted // torrent file (while supplies the cookies to the HTTP request) instead of sending the URL directly to the - // torrent client + // torrent client. If instead it is marked (by the Torrent Search module) as being form a private site, use + // the Search Module instead to download the url locally first. if (match != null && match.getCookies() != null) { addTorrentFromWeb(data, match); } else { - // Normally send the URL to the torrent client; the title we show is based on the url + // Get torrent title String title = NavigationHelper.extractNameFromUri(dataUri); if (intent.hasExtra("TORRENT_TITLE")) { title = intent.getStringExtra("TORRENT_TITLE"); } - addTorrentByUrl(data, title); + if (privateSource != null) { + // Download locally first before adding the torrent + addTorrentFromPrivateSource(data.toString(), title, privateSource); + } else { + // Normally send the URL to the torrent client + addTorrentByUrl(data, title); + } } return; } @@ -827,6 +843,19 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi } } + @Background + protected void addTorrentFromPrivateSource(String url, String title, String source) { + + try { + InputStream input = SearchHelper_.getInstance_(this).getFile(source, url); + addTorrentFromStream(input); + } catch (Exception e) { + Log.e(this, "Can't download private site torrent " + url + " from " + source + ": " + e.toString()); + Crouton.showText(this, R.string.error_torrentfile, NavigationHelper.CROUTON_ERROR_STYLE); + } + + } + @Background protected void addTorrentFromWeb(String url, WebsearchSetting websearchSetting) { diff --git a/core/src/org/transdroid/core/gui/search/SearchResultsFragment.java b/core/src/org/transdroid/core/gui/search/SearchResultsFragment.java index 8c9ea114..f55b0d0a 100644 --- a/core/src/org/transdroid/core/gui/search/SearchResultsFragment.java +++ b/core/src/org/transdroid/core/gui/search/SearchResultsFragment.java @@ -63,6 +63,8 @@ public class SearchResultsFragment extends SherlockFragment { @InstanceState protected ArrayList results = null; + @InstanceState + protected String resultsSource; @Bean protected SearchHelper searchHelper; @@ -107,6 +109,7 @@ public class SearchResultsFragment extends SherlockFragment { @Background protected void performSearch(String query, SearchSite site) { results = searchHelper.search(query, site, SearchSortOrder.BySeeders); + resultsSource = site.isPrivate()? site.getKey(): null; showResults(); } @@ -133,6 +136,8 @@ public class SearchResultsFragment extends SherlockFragment { Intent i = TorrentsActivity_.intent(getActivity()).get(); i.setData(Uri.parse(item.getTorrentUrl())); i.putExtra("TORRENT_TITLE", item.getName()); + if (resultsSource != null) + i.putExtra("PRIVATE_SOURCE", resultsSource); startActivity(i); } @@ -176,6 +181,8 @@ public class SearchResultsFragment extends SherlockFragment { } intent.putExtra("TORRENT_URLS", urls); intent.putExtra("TORRENT_TITLES", titles); + if (resultsSource != null) + intent.putExtra("PRIVATE_SOURCE", resultsSource); startActivity(intent); mode.finish(); return true;