Index: chrome/android/java/src/org/chromium/chrome/browser/preferences/ClearBrowsingDataTabCheckBoxPreference.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/ClearBrowsingDataTabCheckBoxPreference.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/ClearBrowsingDataTabCheckBoxPreference.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..aa3398a87a994e6f4806b85f023072b07bb376f6 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/ClearBrowsingDataTabCheckBoxPreference.java |
@@ -0,0 +1,127 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// 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.annotation.SuppressLint; |
+import android.content.Context; |
+import android.text.SpannableString; |
+import android.text.Spanned; |
+import android.text.style.ClickableSpan; |
+import android.util.AttributeSet; |
+import android.view.Gravity; |
+import android.view.MotionEvent; |
+import android.view.View; |
+import android.view.ViewGroup; |
+import android.widget.LinearLayout; |
+import android.widget.ListView; |
+import android.widget.RelativeLayout; |
+import android.widget.TextView; |
+ |
+import org.chromium.base.ApiCompatibilityUtils; |
+import org.chromium.ui.text.NoUnderlineClickableSpan; |
+import org.chromium.ui.text.SpanApplier; |
+ |
+/** |
+ * A preference representing one browsing data type in ClearBrowsingDataPreferencesTab. |
+ * This class allows clickable links inside the checkbox summary. |
+ */ |
+public class ClearBrowsingDataTabCheckBoxPreference extends ClearBrowsingDataCheckBoxPreference { |
+ private Runnable mLinkClickDelegate; |
+ private boolean mHasClickableSpans; |
+ |
+ /** |
+ * Constructor for inflating from XML. |
+ */ |
+ public ClearBrowsingDataTabCheckBoxPreference(Context context, AttributeSet attrs) { |
+ super(context, attrs); |
+ } |
+ |
+ /** |
+ * @param linkClickDelegate A Runnable that is executed when a link inside the summary is |
+ * clicked. |
+ */ |
+ public void setLinkClickDelegate(Runnable linkClickDelegate) { |
+ mLinkClickDelegate = linkClickDelegate; |
+ } |
+ |
+ @Override |
+ public View onCreateView(ViewGroup parent) { |
+ View view = super.onCreateView(parent); |
+ |
+ final TextView textView = (TextView) view.findViewById(android.R.id.summary); |
+ |
+ // Create custom onTouch listener to be able to respond to click events inside the summary. |
+ textView.setOnTouchListener(new View.OnTouchListener() { |
+ @Override |
+ @SuppressLint("ClickableViewAccessibility") |
+ public boolean onTouch(View v, MotionEvent event) { |
+ if (!mHasClickableSpans) { |
+ return false; |
+ } |
+ // Find out which character was touched. |
+ int offset = textView.getOffsetForPosition(event.getX(), event.getY()); |
+ // Check if this character contains a span. |
+ Spanned text = (Spanned) textView.getText(); |
+ ClickableSpan[] types = text.getSpans(offset, offset, ClickableSpan.class); |
+ |
+ if (types.length > 0) { |
+ if (event.getAction() == MotionEvent.ACTION_UP) { |
+ for (ClickableSpan type : types) { |
+ type.onClick(textView); |
+ } |
+ } |
+ return true; |
+ } else { |
+ return false; |
+ } |
+ } |
+ }); |
+ |
+ return view; |
+ } |
+ |
+ @Override |
+ protected void setupLayout(LinearLayout view) { |
+ float density = view.getResources().getDisplayMetrics().density; |
+ // Use a custom layout to improve the design with large summary texts. |
+ view.setGravity(Gravity.TOP); |
+ ListView.LayoutParams lp = (ListView.LayoutParams) view.getLayoutParams(); |
+ ApiCompatibilityUtils.setPaddingRelative(view, ApiCompatibilityUtils.getPaddingStart(view), |
+ (int) (6 * density), ApiCompatibilityUtils.getPaddingEnd(view), |
+ (int) (6 * density)); |
+ view.setLayoutParams(lp); |
+ |
+ RelativeLayout textLayout = |
+ (RelativeLayout) view.findViewById(android.R.id.title).getParent(); |
+ ApiCompatibilityUtils.setPaddingRelative(textLayout, |
+ ApiCompatibilityUtils.getPaddingStart(textLayout), 0, |
+ ApiCompatibilityUtils.getPaddingEnd(textLayout), 0); |
+ |
+ LinearLayout widgetFrame = (LinearLayout) view.findViewById(android.R.id.widget_frame); |
+ widgetFrame.setGravity(Gravity.TOP); |
+ } |
+ |
+ @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, |
+ new SpanApplier.SpanInfo("<link>", "</link>", new NoUnderlineClickableSpan() { |
+ @Override |
+ public void onClick(View widget) { |
+ if (mLinkClickDelegate != null) mLinkClickDelegate.run(); |
+ } |
+ })); |
+ |
+ mHasClickableSpans = true; |
+ super.setSummary(summaryWithLink); |
+ } |
+} |