From 5ecae7bd38911fa2b48e306ae81590e60ddf751b Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Fri, 4 Oct 2013 18:08:19 +0200 Subject: [PATCH] Try to parse a name from torrent http and magnet links to show (and solves the 'Add by URL crashes' bug for magnet links). --- .../transdroid/core/gui/TorrentsActivity.java | 5 +- .../core/gui/navigation/NavigationHelper.java | 47 +++++++++++++++++++ .../core/gui/search/UrlEntryDialog.java | 8 +++- .../gui/settings/MainSettingsActivity.java | 2 +- 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/core/src/org/transdroid/core/gui/TorrentsActivity.java b/core/src/org/transdroid/core/gui/TorrentsActivity.java index 966fc799..0fc45c09 100644 --- a/core/src/org/transdroid/core/gui/TorrentsActivity.java +++ b/core/src/org/transdroid/core/gui/TorrentsActivity.java @@ -463,9 +463,8 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi if (match != null && match.getCookies() != null) { addTorrentFromWeb(data, match); } else { - // Normally send the URL to the torrent client; the title we show is just the 'file name' - // TODO: Make a better effort in determining the title (check query string, clean special chars) - String title = data.substring(data.lastIndexOf("/") + 1); + // Normally send the URL to the torrent client; the title we show is based on the url + String title = NavigationHelper.extractNameFromUri(dataUri); if (intent.hasExtra("TORRENT_TITLE")) { title = intent.getStringExtra("TORRENT_TITLE"); } diff --git a/core/src/org/transdroid/core/gui/navigation/NavigationHelper.java b/core/src/org/transdroid/core/gui/navigation/NavigationHelper.java index 86f673ac..6c3736ae 100644 --- a/core/src/org/transdroid/core/gui/navigation/NavigationHelper.java +++ b/core/src/org/transdroid/core/gui/navigation/NavigationHelper.java @@ -25,6 +25,7 @@ import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager.NameNotFoundException; +import android.net.Uri; import android.text.Spannable; import android.text.SpannableString; import android.text.style.TypefaceSpan; @@ -166,4 +167,50 @@ public class NavigationHelper { return s; } + /** + * Analyses a torrent or magnet URI and tries to come up with a reasonable human-readable name. + * @param rawTorrentUri The raw http:// or magnet: link to the torrent + * @return A best-guess, reasonably long name for the linked torrent + */ + public static String extractNameFromUri(Uri rawTorrentUri) { + + if (rawTorrentUri.getScheme() == null) { + // Probably an incorrect URI; just return the whole thing + return rawTorrentUri.toString(); + } + + if (rawTorrentUri.getScheme().equals("magnet")) { + // Magnet links might have a dn (display name) parameter + String dn = getQueryParameter(rawTorrentUri, "dn"); + if (dn != null && !dn.equals("")) + return dn; + // If not, try to return the hash that is specified as xt (exact topci) + String xt = getQueryParameter(rawTorrentUri, "xt"); + if (xt != null && !xt.equals("")) + return xt; + } + + if (rawTorrentUri.isHierarchical()) { + String path = rawTorrentUri.getPath(); + if (path != null) { + if (path.contains("/")) + path = path.substring(path.lastIndexOf("/")); + return path; + } + } + + // No idea what to do with this; return as is + return rawTorrentUri.toString(); + } + + private static String getQueryParameter(Uri uri, String parameter) { + int start = uri.toString().indexOf(parameter + "="); + if (start >= 0) { + int begin = start + (parameter + "=").length(); + int end = uri.toString().indexOf("&", begin); + return uri.toString().substring(begin, end >= 0? end: uri.toString().length()); + } + return null; + } + } diff --git a/core/src/org/transdroid/core/gui/search/UrlEntryDialog.java b/core/src/org/transdroid/core/gui/search/UrlEntryDialog.java index 45ac488a..77410540 100644 --- a/core/src/org/transdroid/core/gui/search/UrlEntryDialog.java +++ b/core/src/org/transdroid/core/gui/search/UrlEntryDialog.java @@ -17,11 +17,13 @@ package org.transdroid.core.gui.search; import org.transdroid.core.gui.TorrentsActivity; +import org.transdroid.core.gui.navigation.NavigationHelper; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; +import android.net.Uri; import android.support.v4.app.DialogFragment; import android.text.InputType; import android.text.TextUtils; @@ -48,8 +50,10 @@ public class UrlEntryDialog { public void onClick(DialogInterface dialog, int which) { // Assume text entry box input as URL and treat the filename (after the last /) as title String url = urlInput.getText().toString(); - if (activity != null && !TextUtils.isEmpty(url)) - activity.addTorrentByUrl(url, url.substring(url.lastIndexOf("/"))); + if (activity != null && !TextUtils.isEmpty(url)) { + String title = NavigationHelper.extractNameFromUri(Uri.parse(url)); + activity.addTorrentByUrl(url, title); + } } }).setNegativeButton(android.R.string.cancel, null).create(); }; diff --git a/core/src/org/transdroid/core/gui/settings/MainSettingsActivity.java b/core/src/org/transdroid/core/gui/settings/MainSettingsActivity.java index 66a1da02..7e4ad600 100644 --- a/core/src/org/transdroid/core/gui/settings/MainSettingsActivity.java +++ b/core/src/org/transdroid/core/gui/settings/MainSettingsActivity.java @@ -29,7 +29,7 @@ import org.transdroid.core.app.settings.ApplicationSettings; import org.transdroid.core.app.settings.RssfeedSetting; import org.transdroid.core.app.settings.ServerSetting; import org.transdroid.core.app.settings.WebsearchSetting; -import org.transdroid.core.gui.TorrentsActivity_; +import org.transdroid.core.gui.*; import org.transdroid.core.gui.navigation.NavigationHelper; import org.transdroid.core.gui.settings.RssfeedPreference.OnRssfeedClickedListener; import org.transdroid.core.gui.settings.ServerPreference.OnServerClickedListener;