Commit afdb0295 authored by Boris Sazonov's avatar Boris Sazonov Committed by Commit Bot

[Unity][Android] Use TextMessagePreference in PrivacyPreferences

Some informational messages in preferences use text appearance that is
normally used for preference summaries (for example, the link to 'Sync
and other Google services' in PrivacyPreferences that is shown if
UNIFIED_CONSENT is enabled). This CL improves TextMessagePreference to
support messages in summaries and updates PrivacyPreferences to use
TextMessagePreference, thus fixing the alignment issue there. No longer
used TextMessageWithLinkAndIconPreference is removed.

Bug: 864806
Change-Id: Ia4375a767a34eadbdc82a7ebd8fa0e90a3e254a5
Reviewed-on: https://chromium-review.googlesource.com/1187152Reviewed-by: default avatarTheresa <twellington@chromium.org>
Commit-Queue: Boris Sazonov <bsazonov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#585675}
parent 55408a2c
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2016 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. -->
<!-- Layout used by the TextMessageWithLinkAndIconPreference. -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/PreferenceLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="24dp">
<ImageView
android:id="@android:id/icon"
tools:ignore="ContentDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" />
<org.chromium.ui.widget.TextViewWithClickableSpans
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@android:id/icon"
android:layout_alignTop="@android:id/icon"
android:layout_marginStart="16dp"
style="@style/PreferenceTitle" />
<org.chromium.ui.widget.TextViewWithClickableSpans
android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_toEndOf="@android:id/icon"
android:layout_marginStart="16dp"
style="@style/PreferenceSummary" />
</RelativeLayout>
\ No newline at end of file
......@@ -69,14 +69,6 @@
<attr name="titleText" format="string" />
</declare-styleable>
<declare-styleable name="TextMessageWithLinkAndIconPreference">
<!-- Used on a TextMessageWithLinkAndIconPreference that is followed
by another TextMessageWithLinkAndIconPreference to remove the
bottom spacing, so that related paragraphs of text are closer
to each other. -->
<attr name="noBottomSpacing" format="boolean" />
</declare-styleable>
<declare-styleable name="SpinnerPreference">
<!-- Used to change the SpinnerPreference to display the TextView and the Spinner on the
same line. Devices with a smallest width of less than 360dp will still use two separate
......
......@@ -3,7 +3,10 @@
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<org.chromium.chrome.browser.preferences.ChromeBaseCheckBoxPreference
android:key="navigation_error"
android:title="@string/navigation_error_title"
......@@ -52,7 +55,7 @@
android:title="@string/clear_browsing_data_title"
android:summary="@string/clear_browsing_data_summary"
android:fragment="org.chromium.chrome.browser.preferences.privacy.ClearBrowsingDataTabsFragment" />
<org.chromium.chrome.browser.preferences.TextMessageWithLinkAndIconPreference
<org.chromium.chrome.browser.preferences.TextMessagePreference
android:key="sync_and_services_link"
android:summary="@string/privacy_sync_and_services_link"/>
tools:summary="@string/privacy_sync_and_services_link"/>
</PreferenceScreen>
......@@ -5,6 +5,7 @@
package org.chromium.chrome.browser.preferences;
import android.content.Context;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.view.View;
......@@ -26,9 +27,18 @@ public class TextMessagePreference extends ChromeBasePreference {
protected void onBindView(View view) {
super.onBindView(view);
TextView textView = (TextView) view.findViewById(android.R.id.title);
textView.setSingleLine(false);
textView.setMaxLines(Integer.MAX_VALUE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
TextView titleView = (TextView) view.findViewById(android.R.id.title);
if (!TextUtils.isEmpty(getTitle())) {
titleView.setVisibility(View.VISIBLE);
titleView.setSingleLine(false);
titleView.setMaxLines(Integer.MAX_VALUE);
titleView.setMovementMethod(LinkMovementMethod.getInstance());
} else {
titleView.setVisibility(View.GONE);
}
TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
// No need to manually toggle visibility for summary - it is done in super.onBindView.
summaryView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
// Copyright 2016 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.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint.FontMetrics;
import android.support.v4.view.ViewCompat;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import org.chromium.chrome.R;
import org.chromium.ui.text.NoUnderlineClickableSpan;
import org.chromium.ui.text.SpanApplier;
/**
* A text message whose summary can contain a link. The link should be denoted by "<link></link>"
* tags and the action upon its clicking is defined by |setLinkClickDelegate()|.
*/
public class TextMessageWithLinkAndIconPreference extends TextMessagePreference {
private Runnable mLinkClickDelegate;
private boolean mNoBottomSpacing;
/**
* Constructor for inflating from XML.
*/
public TextMessageWithLinkAndIconPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setLayoutResource(R.layout.text_message_with_link_and_icon_preference);
TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.TextMessageWithLinkAndIconPreference);
mNoBottomSpacing = a.getBoolean(
R.styleable.TextMessageWithLinkAndIconPreference_noBottomSpacing, false);
a.recycle();
setSummary(getSummary()); // Apply spans to the summary loaded from XML.
}
/**
* @param delegate A delegate to handle link click.
*/
public void setLinkClickDelegate(Runnable delegate) {
mLinkClickDelegate = delegate;
}
@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((widget) -> {
if (mLinkClickDelegate != null) mLinkClickDelegate.run();
})));
super.setSummary(summaryWithLink);
}
@Override
public View onCreateView(ViewGroup parent) {
View view = super.onCreateView(parent);
if (mNoBottomSpacing) {
ViewCompat.setPaddingRelative(view, ViewCompat.getPaddingStart(view),
view.getPaddingTop(), ViewCompat.getPaddingEnd(view), 0);
}
((TextView) view.findViewById(android.R.id.summary)).setMovementMethod(
LinkMovementMethod.getInstance());
// The icon is aligned to the top of the text view, which can be higher than the
// ascender line of the text, and makes it look aligned improperly.
TextView textView = (TextView) view.findViewById(
getTitle() != null ? android.R.id.title : android.R.id.summary);
FontMetrics metrics = textView.getPaint().getFontMetrics();
ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
ViewCompat.setPaddingRelative(icon, 0, (int) Math.ceil(metrics.ascent - metrics.top), 0, 0);
return view;
}
}
......@@ -27,8 +27,9 @@ import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.chrome.browser.preferences.PreferenceUtils;
import org.chromium.chrome.browser.preferences.PreferencesLauncher;
import org.chromium.chrome.browser.preferences.SyncAndServicesPreferences;
import org.chromium.chrome.browser.preferences.TextMessageWithLinkAndIconPreference;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.ui.text.NoUnderlineClickableSpan;
import org.chromium.ui.text.SpanApplier;
/**
* Fragment to keep track of the all the privacy related preferences.
......@@ -81,13 +82,14 @@ public class PrivacyPreferences extends PreferenceFragment
preferenceScreen.removePreference(findPreference(PREF_CONTEXTUAL_SEARCH));
preferenceScreen.removePreference(findPreference(PREF_USAGE_AND_CRASH_REPORTING));
TextMessageWithLinkAndIconPreference syncAndServicesLink =
(TextMessageWithLinkAndIconPreference) findPreference(
PREF_SYNC_AND_SERVICES_LINK);
syncAndServicesLink.setLinkClickDelegate(() -> {
Preference syncAndServicesLink = findPreference(PREF_SYNC_AND_SERVICES_LINK);
NoUnderlineClickableSpan linkSpan = new NoUnderlineClickableSpan(view -> {
PreferencesLauncher.launchSettingsPage(
getActivity(), SyncAndServicesPreferences.class.getName());
});
syncAndServicesLink.setSummary(
SpanApplier.applySpans(getString(R.string.privacy_sync_and_services_link),
new SpanApplier.SpanInfo("<link>", "</link>", linkSpan)));
updateSummaries();
return;
......
......@@ -1156,7 +1156,6 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/preferences/SyncedAccountPreference.java",
"java/src/org/chromium/chrome/browser/preferences/TextAndButtonPreference.java",
"java/src/org/chromium/chrome/browser/preferences/TextMessagePreference.java",
"java/src/org/chromium/chrome/browser/preferences/TextMessageWithLinkAndIconPreference.java",
"java/src/org/chromium/chrome/browser/preferences/TextScalePreference.java",
"java/src/org/chromium/chrome/browser/preferences/autofill/AndroidPaymentAppPreference.java",
"java/src/org/chromium/chrome/browser/preferences/autofill/AndroidPaymentAppsFragment.java",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment