Bug 799615 - Make Android Send Tab handle Twitter share intents. r=rnewman

This commit is contained in:
Nick Alexander 2013-01-15 11:42:53 -08:00
Родитель a013264976
Коммит 2447dc2700
5 изменённых файлов: 206 добавлений и 9 удалений

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -133,21 +133,21 @@ public class SendTabActivity extends Activity {
return;
}
final String uri = extras.getString(Intent.EXTRA_TEXT);
final String title = extras.getString(Intent.EXTRA_SUBJECT);
final List<String> guids = arrayAdapter.getCheckedGUIDs();
final SendTabData sendTabData = SendTabData.fromBundle(extras);
if (title == null) {
if (sendTabData.title == null) {
Logger.warn(LOG_TAG, "title was null; ignoring and sending tab anyway.");
}
if (uri == null) {
if (sendTabData.uri == null) {
Logger.warn(LOG_TAG, "uri was null; aborting without sending tab.");
notifyAndFinish(false);
return;
}
if (guids == null) {
final List<String> remoteClientGuids = arrayAdapter.getCheckedGUIDs();
if (remoteClientGuids == null) {
// Should never happen.
Logger.warn(LOG_TAG, "guids was null; aborting without sending tab.");
notifyAndFinish(false);
@ -168,8 +168,8 @@ public class SendTabActivity extends Activity {
return false;
}
for (String guid : guids) {
processor.sendURIToClientForDisplay(uri, guid, title, accountGUID, getApplicationContext());
for (String remoteClientGuid : remoteClientGuids) {
processor.sendURIToClientForDisplay(sendTabData.uri, remoteClientGuid, sendTabData.title, accountGUID, getApplicationContext());
}
Logger.info(LOG_TAG, "Requesting immediate clients stage sync.");

Просмотреть файл

@ -0,0 +1,61 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.sync.setup.activities;
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
/**
* A static factory that extracts (title, uri) pairs suitable for send tab from
* Android intent instances.
* <p>
* Takes some care to extract likely "Web URLs" in preference to general URIs.
*/
public class SendTabData {
public final String title;
public final String uri;
public SendTabData(String title, String uri) {
this.title = title;
this.uri = uri;
}
public static SendTabData fromIntent(Intent intent) {
if (intent == null) {
throw new IllegalArgumentException("intent must not be null");
}
return fromBundle(intent.getExtras());
}
protected static SendTabData fromBundle(Bundle bundle) {
if (bundle == null) {
throw new IllegalArgumentException("bundle must not be null");
}
String text = bundle.getString(Intent.EXTRA_TEXT);
String subject = bundle.getString(Intent.EXTRA_SUBJECT);
String title = bundle.getString(Intent.EXTRA_TITLE);
// For title, prefer EXTRA_SUBJECT but accept EXTRA_TITLE.
String theTitle = subject;
if (theTitle == null) {
theTitle = title;
}
// For URL, take first URL from EXTRA_TEXT, EXTRA_SUBJECT, and EXTRA_TITLE
// (in that order).
List<String> strings = new ArrayList<String>();
strings.add(text);
strings.add(subject);
strings.add(title);
String theUri = new WebURLFinder(strings).bestWebURL();
return new SendTabData(theTitle, theUri);
}
}

Просмотреть файл

@ -0,0 +1,134 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.sync.setup.activities;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import android.util.Patterns;
public class WebURLFinder {
public final List<String> candidates;
public WebURLFinder(String string) {
if (string == null) {
throw new IllegalArgumentException("string must not be null");
}
this.candidates = candidateWebURLs(string);
}
public WebURLFinder(List<String> strings) {
if (strings == null) {
throw new IllegalArgumentException("strings must not be null");
}
this.candidates = candidateWebURLs(strings);
}
/**
* Check if string is a Web URL.
* <p>
* A Web URL is a URI that is not a <code>file:</code> or
* <code>javascript:</code> scheme.
*
* @param string
* to check.
* @return <code>true</code> if <code>string</code> is a Web URL.
*/
public static boolean isWebURL(String string) {
try {
new URI(string);
} catch (Exception e) {
return false;
}
if (android.webkit.URLUtil.isFileUrl(string) ||
android.webkit.URLUtil.isJavaScriptUrl(string)) {
return false;
}
return true;
}
/**
* Return best Web URL.
* <p>
* "Best" means a Web URL with a scheme, and failing that, a Web URL without a
* scheme.
*
* @return a Web URL or <code>null</code>.
*/
public String bestWebURL() {
String firstWebURLWithScheme = firstWebURLWithScheme();
if (firstWebURLWithScheme != null) {
return firstWebURLWithScheme;
}
return firstWebURLWithoutScheme();
}
protected static List<String> candidateWebURLs(Collection<String> strings) {
List<String> candidates = new ArrayList<String>();
for (String string : strings) {
if (string == null) {
continue;
}
candidates.addAll(candidateWebURLs(string));
}
return candidates;
}
protected static List<String> candidateWebURLs(String string) {
Matcher matcher = Patterns.WEB_URL.matcher(string);
List<String> matches = new LinkedList<String>();
while (matcher.find()) {
// Remove URLs with bad schemes.
if (!isWebURL(matcher.group())) {
continue;
}
// Remove parts of email addresses.
if (matcher.start() > 0 && (string.charAt(matcher.start() - 1) == '@')) {
continue;
}
matches.add(matcher.group());
}
return matches;
}
protected String firstWebURLWithScheme() {
for (String match : candidates) {
try {
if (new URI(match).getScheme() != null) {
return match;
}
} catch (URISyntaxException e) {
// Ignore: on to the next.
}
}
return null;
}
protected String firstWebURLWithoutScheme() {
if (!candidates.isEmpty()) {
return candidates.get(0);
}
return null;
}
}

Просмотреть файл

@ -196,10 +196,12 @@ sync/setup/activities/ActivityUtils.java
sync/setup/activities/ClientRecordArrayAdapter.java
sync/setup/activities/RedirectToSetupActivity.java
sync/setup/activities/SendTabActivity.java
sync/setup/activities/SendTabData.java
sync/setup/activities/SetupFailureActivity.java
sync/setup/activities/SetupSuccessActivity.java
sync/setup/activities/SetupSyncActivity.java
sync/setup/activities/SyncActivity.java
sync/setup/activities/WebURLFinder.java
sync/setup/activities/WebViewActivity.java
sync/setup/auth/AccountAuthenticator.java
sync/setup/auth/AuthenticateAccountStage.java