Browse Source

Added support for the new Torrent Search module private sites feature (to download a torrent via the Torrent Search module).

pull/148/merge
Eric Kok 11 years ago
parent
commit
a7f12a3e11
  1. 1
      core/.classpath
  2. 34
      core/src/org/transdroid/core/app/search/SearchHelper.java
  3. 8
      core/src/org/transdroid/core/app/search/SearchSite.java
  4. 67
      core/src/org/transdroid/core/gui/TorrentsActivity.java
  5. 7
      core/src/org/transdroid/core/gui/search/SearchResultsFragment.java

1
core/.classpath

@ -10,6 +10,5 @@
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/> <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/Transdroid Torrent Connect"/>
<classpathentry kind="output" path="bin/classes"/> <classpathentry kind="output" path="bin/classes"/>
</classpath> </classpath>

34
core/src/org/transdroid/core/app/search/SearchHelper.java

@ -16,6 +16,10 @@
*/ */
package org.transdroid.core.app.search; 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.ArrayList;
import java.util.List; import java.util.List;
@ -44,6 +48,7 @@ public class SearchHelper {
static final int CURSOR_SITE_CODE = 1; static final int CURSOR_SITE_CODE = 1;
static final int CURSOR_SITE_NAME = 2; static final int CURSOR_SITE_NAME = 2;
static final int CURSOR_SITE_RSSURL = 3; static final int CURSOR_SITE_RSSURL = 3;
static final int CURSOR_SITE_ISPRIVATE = 4;
@RootContext @RootContext
protected Context context; protected Context context;
@ -76,12 +81,18 @@ public class SearchHelper {
// Query the available in-app torrent search sites // Query the available in-app torrent search sites
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); 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()) { if (cursor.moveToFirst()) {
List<SearchSite> sites = new ArrayList<SearchSite>(); List<SearchSite> sites = new ArrayList<SearchSite>();
do { do {
// Read the cursor fields into the SearchSite object // Read the cursor fields into the SearchSite object
sites.add(new SearchSite(cursor.getInt(CURSOR_SITE_ID), cursor.getString(CURSOR_SITE_CODE), cursor 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()); } while (cursor.moveToNext());
cursor.close(); cursor.close();
return sites; 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;
}
}
} }

8
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 key;
private final String name; private final String name;
private final String rssFeedUrl; 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.id = id;
this.key = key; this.key = key;
this.name = name; this.name = name;
this.rssFeedUrl = rssFeedUrl; this.rssFeedUrl = rssFeedUrl;
this.isPrivate = isPrivate;
} }
public int getId() { public int getId() {
@ -58,5 +60,9 @@ public class SearchSite implements SimpleListItem, SearchSetting {
public String getBaseUrl() { public String getBaseUrl() {
return rssFeedUrl; return rssFeedUrl;
} }
public boolean isPrivate() {
return isPrivate;
}
} }

67
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.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie; import org.apache.http.impl.cookie.BasicClientCookie;
import org.transdroid.core.R; 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.*;
import org.transdroid.core.app.settings.WebsearchSetting; import org.transdroid.core.app.settings.WebsearchSetting;
import org.transdroid.core.gui.lists.LocalTorrent; import org.transdroid.core.gui.lists.LocalTorrent;
@ -228,7 +228,7 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi
// No server settings yet; // No server settings yet;
return; return;
} }
Torrent startTorrent = null; Torrent openTorrent = null;
if (getIntent().getAction() != null && getIntent().getAction().equals(ListWidgetProvider.INTENT_STARTSERVER) if (getIntent().getAction() != null && getIntent().getAction().equals(ListWidgetProvider.INTENT_STARTSERVER)
&& getIntent().getExtras() == null && getIntent().hasExtra(ListWidgetProvider.EXTRA_SERVER)) { && getIntent().getExtras() == null && getIntent().hasExtra(ListWidgetProvider.EXTRA_SERVER)) {
// A server settings order ID was provided in this org.transdroid.START_SERVER action intent // 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 { } else {
lastUsed = applicationSettings.getServerSetting(serverId); lastUsed = applicationSettings.getServerSetting(serverId);
if (getIntent().hasExtra(ListWidgetProvider.EXTRA_TORRENT)) 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 // Force first torrents refresh
filterSelected(lastUsed, true); filterSelected(lastUsed, true);
// Handle any start up intents // Handle any start up intents
if (firstStart && startTorrent != null) { if (openTorrent != null) {
openDetails(startTorrent); openDetails(openTorrent);
startTorrent = null; openTorrent = null;
} else if (firstStart && getIntent() != null) { } else if (getIntent() != null) {
handleStartIntent(); handleStartIntent();
} }
} else { } else {
@ -483,14 +483,19 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi
String action = intent.getAction(); String action = intent.getAction();
// Adding multiple torrents at the same time (as found in the Intent extras Bundle) // Adding multiple torrents at the same time (as found in the Intent extras Bundle)
if (action != null && action.equals("org.transdroid.ADD_MULTIPLE")) { if (action != null && action.equals("org.transdroid.ADD_MULTIPLE")) {
// Intent should have some extras pointing to possibly multiple torrents // Intent should have some extras pointing to possibly multiple torrents
String[] urls = intent.getStringArrayExtra("TORRENT_URLS"); String[] urls = intent.getStringArrayExtra("TORRENT_URLS");
String[] titles = intent.getStringArrayExtra("TORRENT_TITLES"); String[] titles = intent.getStringArrayExtra("TORRENT_TITLES");
if (urls != null) { if (urls != null) {
for (int i = 0; i < urls.length; i++) { 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; return;
@ -513,29 +518,40 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi
// Adding a torrent from http or https URL // Adding a torrent from http or https URL
if (dataUri.getScheme().equals("http") || dataUri.getScheme().equals("https")) { 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 String privateSource = getIntent().getStringExtra("PRIVATE_SOURCE");
List<WebsearchSetting> websearches = applicationSettings.getWebsearchSettings();
WebsearchSetting match = null; WebsearchSetting match = null;
for (WebsearchSetting setting : websearches) { if (privateSource == null) {
Uri uri = Uri.parse(setting.getBaseUrl()); // Check if the target URL is also defined as a web search in the user's settings
if (uri.getHost() != null && uri.getHost().equals(dataUri.getHost())) { List<WebsearchSetting> websearches = applicationSettings.getWebsearchSettings();
match = setting; for (WebsearchSetting setting : websearches) {
break; 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 // 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 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) { if (match != null && match.getCookies() != null) {
addTorrentFromWeb(data, match); addTorrentFromWeb(data, match);
} else { } 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); String title = NavigationHelper.extractNameFromUri(dataUri);
if (intent.hasExtra("TORRENT_TITLE")) { if (intent.hasExtra("TORRENT_TITLE")) {
title = intent.getStringExtra("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; 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 @Background
protected void addTorrentFromWeb(String url, WebsearchSetting websearchSetting) { protected void addTorrentFromWeb(String url, WebsearchSetting websearchSetting) {

7
core/src/org/transdroid/core/gui/search/SearchResultsFragment.java

@ -63,6 +63,8 @@ public class SearchResultsFragment extends SherlockFragment {
@InstanceState @InstanceState
protected ArrayList<SearchResult> results = null; protected ArrayList<SearchResult> results = null;
@InstanceState
protected String resultsSource;
@Bean @Bean
protected SearchHelper searchHelper; protected SearchHelper searchHelper;
@ -107,6 +109,7 @@ public class SearchResultsFragment extends SherlockFragment {
@Background @Background
protected void performSearch(String query, SearchSite site) { protected void performSearch(String query, SearchSite site) {
results = searchHelper.search(query, site, SearchSortOrder.BySeeders); results = searchHelper.search(query, site, SearchSortOrder.BySeeders);
resultsSource = site.isPrivate()? site.getKey(): null;
showResults(); showResults();
} }
@ -133,6 +136,8 @@ public class SearchResultsFragment extends SherlockFragment {
Intent i = TorrentsActivity_.intent(getActivity()).get(); Intent i = TorrentsActivity_.intent(getActivity()).get();
i.setData(Uri.parse(item.getTorrentUrl())); i.setData(Uri.parse(item.getTorrentUrl()));
i.putExtra("TORRENT_TITLE", item.getName()); i.putExtra("TORRENT_TITLE", item.getName());
if (resultsSource != null)
i.putExtra("PRIVATE_SOURCE", resultsSource);
startActivity(i); startActivity(i);
} }
@ -176,6 +181,8 @@ public class SearchResultsFragment extends SherlockFragment {
} }
intent.putExtra("TORRENT_URLS", urls); intent.putExtra("TORRENT_URLS", urls);
intent.putExtra("TORRENT_TITLES", titles); intent.putExtra("TORRENT_TITLES", titles);
if (resultsSource != null)
intent.putExtra("PRIVATE_SOURCE", resultsSource);
startActivity(intent); startActivity(intent);
mode.finish(); mode.finish();
return true; return true;

Loading…
Cancel
Save