|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.sync.setup.activities; |
|
6 |
|
7 import java.util.Locale; |
|
8 |
|
9 import org.mozilla.gecko.background.common.GlobalConstants; |
|
10 import org.mozilla.gecko.background.common.log.Logger; |
|
11 import org.mozilla.gecko.sync.SyncConstants; |
|
12 import org.mozilla.gecko.sync.setup.InvalidSyncKeyException; |
|
13 |
|
14 import android.content.Context; |
|
15 import android.content.Intent; |
|
16 import android.net.Uri; |
|
17 import android.text.Html; |
|
18 import android.text.SpannableString; |
|
19 import android.text.Spanned; |
|
20 import android.text.TextPaint; |
|
21 import android.text.method.LinkMovementMethod; |
|
22 import android.text.style.ClickableSpan; |
|
23 import android.text.style.URLSpan; |
|
24 import android.view.View; |
|
25 import android.widget.TextView; |
|
26 |
|
27 public class ActivityUtils { |
|
28 private static final String LOG_TAG = "ActivityUtils"; |
|
29 |
|
30 public static void prepareLogging() { |
|
31 Logger.setThreadLogTag(SyncConstants.GLOBAL_LOG_TAG); |
|
32 } |
|
33 |
|
34 /** |
|
35 * Sync key should be a 26-character string, and can include arbitrary |
|
36 * capitalization and hyphenation. |
|
37 * |
|
38 * @param key |
|
39 * Sync key entered by user in account setup. |
|
40 * @return Sync key in correct format (lower-case, no hyphens). |
|
41 * @throws InvalidSyncKeyException |
|
42 */ |
|
43 public static String validateSyncKey(String key) throws InvalidSyncKeyException { |
|
44 String charKey = key.trim().replace("-", "").toLowerCase(Locale.US); |
|
45 if (!charKey.matches("^[abcdefghijkmnpqrstuvwxyz23456789]{26}$")) { |
|
46 throw new InvalidSyncKeyException(); |
|
47 } |
|
48 return charKey; |
|
49 } |
|
50 |
|
51 /** |
|
52 * Open a URL in Fennec, if one is provided; or just open Fennec. |
|
53 * |
|
54 * @param context Android context. |
|
55 * @param url to visit, or null to just open Fennec. |
|
56 */ |
|
57 public static void openURLInFennec(final Context context, final String url) { |
|
58 Intent intent; |
|
59 if (url != null) { |
|
60 intent = new Intent(Intent.ACTION_VIEW); |
|
61 intent.setData(Uri.parse(url)); |
|
62 } else { |
|
63 intent = new Intent(Intent.ACTION_MAIN); |
|
64 } |
|
65 intent.setClassName(GlobalConstants.BROWSER_INTENT_PACKAGE, GlobalConstants.BROWSER_INTENT_CLASS); |
|
66 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
|
67 context.startActivity(intent); |
|
68 } |
|
69 |
|
70 /** |
|
71 * Open a clicked span in Fennec with the provided URL. |
|
72 */ |
|
73 public static class FennecClickableSpan extends ClickableSpan { |
|
74 private final String url; |
|
75 private final boolean underlining; |
|
76 |
|
77 public FennecClickableSpan(final String url, boolean underlining) { |
|
78 this.url = url; |
|
79 this.underlining = underlining; |
|
80 } |
|
81 |
|
82 @Override |
|
83 public void updateDrawState(TextPaint ds) { |
|
84 super.updateDrawState(ds); |
|
85 if (!this.underlining) { |
|
86 ds.setUnderlineText(false); |
|
87 } |
|
88 } |
|
89 |
|
90 @Override |
|
91 public void onClick(View widget) { |
|
92 openURLInFennec(widget.getContext(), this.url); |
|
93 } |
|
94 } |
|
95 |
|
96 /** |
|
97 * Replace the contents of a plain text view with the provided text wrapped in a link. |
|
98 * TODO: escape the URL! |
|
99 */ |
|
100 public static void linkTextView(TextView view, int text, int link) { |
|
101 final Context context = view.getContext(); |
|
102 linkTextView(view, context.getString(text), context.getString(link)); |
|
103 } |
|
104 |
|
105 /** |
|
106 * Replace the contents of a plain text view with the provided text wrapped in a link. |
|
107 * TODO: escape the URL! |
|
108 */ |
|
109 public static void linkTextView(TextView view, String text, String url) { |
|
110 view.setText("<a href=\"" + url + "\">" + text + "</a>"); |
|
111 linkifyTextView(view, false); |
|
112 } |
|
113 |
|
114 public static void linkifyTextView(TextView textView, boolean underlining) { |
|
115 if (textView == null) { |
|
116 Logger.warn(LOG_TAG, "Could not process links for view."); |
|
117 return; |
|
118 } |
|
119 |
|
120 textView.setMovementMethod(LinkMovementMethod.getInstance()); |
|
121 |
|
122 // Create spans. |
|
123 final Spanned spanned = Html.fromHtml(textView.getText().toString()); |
|
124 |
|
125 // Replace the spans with Fennec-launching links. |
|
126 SpannableString replaced = new SpannableString(spanned); |
|
127 URLSpan[] spans = replaced.getSpans(0, replaced.length(), URLSpan.class); |
|
128 for (URLSpan span : spans) { |
|
129 final int start = replaced.getSpanStart(span); |
|
130 final int end = replaced.getSpanEnd(span); |
|
131 final int flags = replaced.getSpanFlags(span); |
|
132 |
|
133 replaced.removeSpan(span); |
|
134 replaced.setSpan(new FennecClickableSpan(span.getURL(), underlining), start, end, flags); |
|
135 } |
|
136 |
|
137 textView.setText(replaced); |
|
138 } |
|
139 } |