Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.sync.setup.activities; |
michael@0 | 6 | |
michael@0 | 7 | import java.util.Arrays; |
michael@0 | 8 | import java.util.List; |
michael@0 | 9 | |
michael@0 | 10 | import android.content.Intent; |
michael@0 | 11 | import android.os.Bundle; |
michael@0 | 12 | |
michael@0 | 13 | /** |
michael@0 | 14 | * A static factory that extracts (title, uri) pairs suitable for send tab from |
michael@0 | 15 | * Android intent instances. |
michael@0 | 16 | * <p> |
michael@0 | 17 | * Takes some care to extract likely "Web URLs" in preference to general URIs. |
michael@0 | 18 | */ |
michael@0 | 19 | public class SendTabData { |
michael@0 | 20 | public final String title; |
michael@0 | 21 | public final String uri; |
michael@0 | 22 | |
michael@0 | 23 | public SendTabData(String title, String uri) { |
michael@0 | 24 | this.title = title; |
michael@0 | 25 | this.uri = uri; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | public static SendTabData fromIntent(Intent intent) { |
michael@0 | 29 | if (intent == null) { |
michael@0 | 30 | throw new IllegalArgumentException("intent must not be null"); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | return fromBundle(intent.getExtras()); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | protected static SendTabData fromBundle(Bundle bundle) { |
michael@0 | 37 | if (bundle == null) { |
michael@0 | 38 | throw new IllegalArgumentException("bundle must not be null"); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | String text = bundle.getString(Intent.EXTRA_TEXT); |
michael@0 | 42 | String subject = bundle.getString(Intent.EXTRA_SUBJECT); |
michael@0 | 43 | String title = bundle.getString(Intent.EXTRA_TITLE); |
michael@0 | 44 | |
michael@0 | 45 | // For title, prefer EXTRA_SUBJECT but accept EXTRA_TITLE. |
michael@0 | 46 | String theTitle = subject; |
michael@0 | 47 | if (theTitle == null) { |
michael@0 | 48 | theTitle = title; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | // For URL, take first URL from EXTRA_TEXT, EXTRA_SUBJECT, and EXTRA_TITLE |
michael@0 | 52 | // (in that order). |
michael@0 | 53 | List<String> strings = Arrays.asList(text, subject, title); |
michael@0 | 54 | String theUri = new WebURLFinder(strings).bestWebURL(); |
michael@0 | 55 | |
michael@0 | 56 | return new SendTabData(theTitle, theUri); |
michael@0 | 57 | } |
michael@0 | 58 | } |