Browse Source

Try to parse a name from torrent http and magnet links to show (and solves the 'Add by URL crashes' bug for magnet links).

pull/13/merge
Eric Kok 11 years ago
parent
commit
5ecae7bd38
  1. 5
      core/src/org/transdroid/core/gui/TorrentsActivity.java
  2. 47
      core/src/org/transdroid/core/gui/navigation/NavigationHelper.java
  3. 8
      core/src/org/transdroid/core/gui/search/UrlEntryDialog.java
  4. 2
      core/src/org/transdroid/core/gui/settings/MainSettingsActivity.java

5
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) { 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 just the 'file name' // Normally send the URL to the torrent client; the title we show is based on the url
// TODO: Make a better effort in determining the title (check query string, clean special chars) String title = NavigationHelper.extractNameFromUri(dataUri);
String title = data.substring(data.lastIndexOf("/") + 1);
if (intent.hasExtra("TORRENT_TITLE")) { if (intent.hasExtra("TORRENT_TITLE")) {
title = intent.getStringExtra("TORRENT_TITLE"); title = intent.getStringExtra("TORRENT_TITLE");
} }

47
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.ApplicationInfo;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.style.TypefaceSpan; import android.text.style.TypefaceSpan;
@ -166,4 +167,50 @@ public class NavigationHelper {
return s; 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;
}
} }

8
core/src/org/transdroid/core/gui/search/UrlEntryDialog.java

@ -17,11 +17,13 @@
package org.transdroid.core.gui.search; package org.transdroid.core.gui.search;
import org.transdroid.core.gui.TorrentsActivity; import org.transdroid.core.gui.TorrentsActivity;
import org.transdroid.core.gui.navigation.NavigationHelper;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.text.InputType; import android.text.InputType;
import android.text.TextUtils; import android.text.TextUtils;
@ -48,8 +50,10 @@ public class UrlEntryDialog {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
// Assume text entry box input as URL and treat the filename (after the last /) as title // Assume text entry box input as URL and treat the filename (after the last /) as title
String url = urlInput.getText().toString(); String url = urlInput.getText().toString();
if (activity != null && !TextUtils.isEmpty(url)) if (activity != null && !TextUtils.isEmpty(url)) {
activity.addTorrentByUrl(url, url.substring(url.lastIndexOf("/"))); String title = NavigationHelper.extractNameFromUri(Uri.parse(url));
activity.addTorrentByUrl(url, title);
}
} }
}).setNegativeButton(android.R.string.cancel, null).create(); }).setNegativeButton(android.R.string.cancel, null).create();
}; };

2
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.RssfeedSetting;
import org.transdroid.core.app.settings.ServerSetting; import org.transdroid.core.app.settings.ServerSetting;
import org.transdroid.core.app.settings.WebsearchSetting; 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.navigation.NavigationHelper;
import org.transdroid.core.gui.settings.RssfeedPreference.OnRssfeedClickedListener; import org.transdroid.core.gui.settings.RssfeedPreference.OnRssfeedClickedListener;
import org.transdroid.core.gui.settings.ServerPreference.OnServerClickedListener; import org.transdroid.core.gui.settings.ServerPreference.OnServerClickedListener;

Loading…
Cancel
Save