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. 53
      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 @@ @@ -10,6 +10,5 @@
</attributes>
</classpathentry>
<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"/>
</classpath>

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

@ -16,6 +16,10 @@ @@ -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 { @@ -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 { @@ -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<SearchSite> sites = new ArrayList<SearchSite>();
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 { @@ -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 { @@ -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() {
@ -59,4 +61,8 @@ public class SearchSite implements SimpleListItem, SearchSetting { @@ -59,4 +61,8 @@ public class SearchSite implements SimpleListItem, SearchSetting {
return rssFeedUrl;
}
public boolean isPrivate() {
return isPrivate;
}
}

53
core/src/org/transdroid/core/gui/TorrentsActivity.java

@ -44,7 +44,7 @@ import org.apache.http.client.methods.HttpGet; @@ -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 @@ -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 @@ -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 @@ -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 @@ -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,9 +518,12 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi @@ -513,9 +518,12 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi
// Adding a torrent from http or https URL
if (dataUri.getScheme().equals("http") || dataUri.getScheme().equals("https")) {
String privateSource = getIntent().getStringExtra("PRIVATE_SOURCE");
WebsearchSetting match = null;
if (privateSource == null) {
// Check if the target URL is also defined as a web search in the user's settings
List<WebsearchSetting> websearches = applicationSettings.getWebsearchSettings();
WebsearchSetting match = null;
for (WebsearchSetting setting : websearches) {
Uri uri = Uri.parse(setting.getBaseUrl());
if (uri.getHost() != null && uri.getHost().equals(dataUri.getHost())) {
@ -523,20 +531,28 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi @@ -523,20 +531,28 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi
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");
}
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 @@ -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) {

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

@ -63,6 +63,8 @@ public class SearchResultsFragment extends SherlockFragment { @@ -63,6 +63,8 @@ public class SearchResultsFragment extends SherlockFragment {
@InstanceState
protected ArrayList<SearchResult> results = null;
@InstanceState
protected String resultsSource;
@Bean
protected SearchHelper searchHelper;
@ -107,6 +109,7 @@ public class SearchResultsFragment extends SherlockFragment { @@ -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 { @@ -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 { @@ -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;

Loading…
Cancel
Save