sanitize URL from clipboard and parse fingerprint (fixes #50)

Instead of just sticking whatever URL is in the clipboard into the "Add
Repo" dialog, this attempts to sanitize the URL in case it has some garbage
or came from a QR Code, and therefore was all uppercase (that makes for
smaller QR Codes).  It also checks if there is a fingerprint in the query
string of the URL, and sticks that into the fingerprint box.

fixes #50 https://gitlab.com/fdroid/fdroidclient/issues/50
This commit is contained in:
Hans-Christoph Steiner 2014-07-11 17:03:40 -04:00
parent a525bca1cf
commit 56933cdbd6

View File

@ -276,7 +276,31 @@ public class ManageReposActivity extends ActionBarActivity {
}
private void showAddRepo() {
showAddRepo(getNewRepoUri(), null);
/*
* If there is text in the clipboard, and it looks like a URL, use that.
* Otherwise use "https://" as default repo string.
*/
ClipboardCompat clipboard = ClipboardCompat.create(this);
String text = clipboard.getText();
String fingerprint = null;
if (!TextUtils.isEmpty(text)) {
try {
new URL(text);
Uri uri = Uri.parse(text);
fingerprint = uri.getQueryParameter("fingerprint");
// uri might contain a QR-style, all uppercase URL:
if (TextUtils.isEmpty(fingerprint))
fingerprint = uri.getQueryParameter("FINGERPRINT");
text = NewRepoConfig.sanitizeRepoUri(uri);
} catch (MalformedURLException e) {
text = null;
}
}
if (TextUtils.isEmpty(text)) {
text = DEFAULT_NEW_REPO_TEXT;
}
showAddRepo(text, fingerprint);
}
private void showAddRepo(String newAddress, String newFingerprint) {
@ -421,27 +445,6 @@ public class ManageReposActivity extends ActionBarActivity {
}
}
/**
* If there is text in the clipboard, and it looks like a URL, use that.
* Otherwise return "https://".
*/
private String getNewRepoUri() {
ClipboardCompat clipboard = ClipboardCompat.create(this);
String text = clipboard.getText();
if (text != null) {
try {
new URL(text);
} catch (MalformedURLException e) {
text = null;
}
}
if (text == null) {
text = DEFAULT_NEW_REPO_TEXT;
}
return text;
}
private void addRepoFromIntent(Intent intent) {
/* an URL from a click, NFC, QRCode scan, etc */
NewRepoConfig newRepoConfig = new NewRepoConfig(this, intent);