1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/sync/setup/activities/SendTabData.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,58 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.sync.setup.activities; 1.9 + 1.10 +import java.util.Arrays; 1.11 +import java.util.List; 1.12 + 1.13 +import android.content.Intent; 1.14 +import android.os.Bundle; 1.15 + 1.16 +/** 1.17 + * A static factory that extracts (title, uri) pairs suitable for send tab from 1.18 + * Android intent instances. 1.19 + * <p> 1.20 + * Takes some care to extract likely "Web URLs" in preference to general URIs. 1.21 + */ 1.22 +public class SendTabData { 1.23 + public final String title; 1.24 + public final String uri; 1.25 + 1.26 + public SendTabData(String title, String uri) { 1.27 + this.title = title; 1.28 + this.uri = uri; 1.29 + } 1.30 + 1.31 + public static SendTabData fromIntent(Intent intent) { 1.32 + if (intent == null) { 1.33 + throw new IllegalArgumentException("intent must not be null"); 1.34 + } 1.35 + 1.36 + return fromBundle(intent.getExtras()); 1.37 + } 1.38 + 1.39 + protected static SendTabData fromBundle(Bundle bundle) { 1.40 + if (bundle == null) { 1.41 + throw new IllegalArgumentException("bundle must not be null"); 1.42 + } 1.43 + 1.44 + String text = bundle.getString(Intent.EXTRA_TEXT); 1.45 + String subject = bundle.getString(Intent.EXTRA_SUBJECT); 1.46 + String title = bundle.getString(Intent.EXTRA_TITLE); 1.47 + 1.48 + // For title, prefer EXTRA_SUBJECT but accept EXTRA_TITLE. 1.49 + String theTitle = subject; 1.50 + if (theTitle == null) { 1.51 + theTitle = title; 1.52 + } 1.53 + 1.54 + // For URL, take first URL from EXTRA_TEXT, EXTRA_SUBJECT, and EXTRA_TITLE 1.55 + // (in that order). 1.56 + List<String> strings = Arrays.asList(text, subject, title); 1.57 + String theUri = new WebURLFinder(strings).bestWebURL(); 1.58 + 1.59 + return new SendTabData(theTitle, theUri); 1.60 + } 1.61 +}