Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/preferences/TextMessageWithLinkAndIconPreference.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/TextMessageWithLinkAndIconPreference.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/TextMessageWithLinkAndIconPreference.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7455322cc2801994e46ae1dcb5039c61b2fe5461 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/TextMessageWithLinkAndIconPreference.java |
| @@ -0,0 +1,82 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
|
newt (away)
2016/04/02 01:24:03
2014?! ;)
msramek
2016/04/04 16:23:12
Done. I guess I should write a presubmit check for
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.preferences; |
| + |
| +import android.content.Context; |
| +import android.content.Intent; |
| +import android.net.Uri; |
| +import android.text.SpannableString; |
| +import android.text.TextPaint; |
| +import android.text.method.LinkMovementMethod; |
| +import android.text.style.ClickableSpan; |
| +import android.util.AttributeSet; |
| +import android.view.View; |
| +import android.view.ViewGroup; |
| +import android.widget.TextView; |
| + |
| +import org.chromium.chrome.R; |
| +import org.chromium.ui.text.SpanApplier; |
| + |
| +/** |
| + * A text message whose summary can contain links. |
| + */ |
| +public class TextMessageWithLinkAndIconPreference extends TextMessagePreference { |
| + private String mUrl; |
| + |
| + /** |
| + * Constructor for inflating from XML. |
| + */ |
| + public TextMessageWithLinkAndIconPreference(Context context, AttributeSet attrs) { |
| + super(context, attrs); |
| + setLayoutResource(R.layout.text_message_with_link_and_icon_preference); |
| + } |
| + |
| + /** |
| + * Sets the URL which should be opened after clicking on the link. |
| + * @param url The URL to be opened. |
| + */ |
| + public void setUrl(String url) { |
|
newt (away)
2016/04/02 01:24:03
This method is fine, but just FYI, another option
msramek
2016/04/04 16:23:12
Hmm, yes, I agree it's more elegant that way. But
|
| + mUrl = url; |
| + } |
| + |
| + @Override |
| + public void setSummary(CharSequence summary) { |
| + // If there is no link in the summary, invoke the default behavior. |
| + String summaryString = summary.toString(); |
| + if (!summaryString.contains("<link>") || !summaryString.contains("</link>")) { |
| + super.setSummary(summary); |
| + return; |
| + } |
| + |
| + // Linkify <link></link> span. |
| + final SpannableString summaryWithLink = SpanApplier.applySpans(summaryString, |
|
newt (away)
2016/04/02 01:24:03
Thought:
Putting the link opening logic inside th
msramek
2016/04/04 16:23:12
In ClearBrowsingDataPreferences, I have three inst
newt (away)
2016/04/04 21:09:10
This is fine with me :) (I was envisioning that y
|
| + new SpanApplier.SpanInfo("<link>", "</link>", new ClickableSpan() { |
| + @Override |
| + public void onClick(View widget) { |
| + if (mUrl == null) return; |
| + |
| + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mUrl)); |
|
newt (away)
2016/04/02 01:24:03
We want to ensure that this intent is opened in Ch
msramek
2016/04/04 16:23:12
Done. Thanks! I didn't know about TabDelegate.
|
| + intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| + getContext().startActivity(intent); |
| + } |
| + // Change link formatting to use no underline |
| + @Override |
| + public void updateDrawState(TextPaint textPaint) { |
|
newt (away)
2016/04/02 01:24:03
use NoUnderlineClickableSpan and you won't need th
msramek
2016/04/04 16:23:12
Done.
|
| + textPaint.setColor(textPaint.linkColor); |
| + textPaint.setUnderlineText(false); |
| + } |
| + })); |
| + |
| + super.setSummary(summaryWithLink); |
| + } |
| + |
| + @Override |
| + public View onCreateView(ViewGroup parent) { |
| + View view = super.onCreateView(parent); |
| + ((TextView) view.findViewById(android.R.id.summary)).setMovementMethod( |
| + LinkMovementMethod.getInstance()); |
| + return view; |
| + } |
| +} |