michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync.setup.activities; michael@0: michael@0: import java.util.Arrays; michael@0: import java.util.List; michael@0: michael@0: import android.content.Intent; michael@0: import android.os.Bundle; michael@0: michael@0: /** michael@0: * A static factory that extracts (title, uri) pairs suitable for send tab from michael@0: * Android intent instances. michael@0: *

michael@0: * Takes some care to extract likely "Web URLs" in preference to general URIs. michael@0: */ michael@0: public class SendTabData { michael@0: public final String title; michael@0: public final String uri; michael@0: michael@0: public SendTabData(String title, String uri) { michael@0: this.title = title; michael@0: this.uri = uri; michael@0: } michael@0: michael@0: public static SendTabData fromIntent(Intent intent) { michael@0: if (intent == null) { michael@0: throw new IllegalArgumentException("intent must not be null"); michael@0: } michael@0: michael@0: return fromBundle(intent.getExtras()); michael@0: } michael@0: michael@0: protected static SendTabData fromBundle(Bundle bundle) { michael@0: if (bundle == null) { michael@0: throw new IllegalArgumentException("bundle must not be null"); michael@0: } michael@0: michael@0: String text = bundle.getString(Intent.EXTRA_TEXT); michael@0: String subject = bundle.getString(Intent.EXTRA_SUBJECT); michael@0: String title = bundle.getString(Intent.EXTRA_TITLE); michael@0: michael@0: // For title, prefer EXTRA_SUBJECT but accept EXTRA_TITLE. michael@0: String theTitle = subject; michael@0: if (theTitle == null) { michael@0: theTitle = title; michael@0: } michael@0: michael@0: // For URL, take first URL from EXTRA_TEXT, EXTRA_SUBJECT, and EXTRA_TITLE michael@0: // (in that order). michael@0: List strings = Arrays.asList(text, subject, title); michael@0: String theUri = new WebURLFinder(strings).bestWebURL(); michael@0: michael@0: return new SendTabData(theTitle, theUri); michael@0: } michael@0: }