Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/WebsiteSettingsPopup.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/WebsiteSettingsPopup.java b/chrome/android/java/src/org/chromium/chrome/browser/WebsiteSettingsPopup.java |
| index 4badd78676b613464423954e84d04fadf11927d4..8a9fcc9ecb0c338eb110acb2a3747e6ec0aa3965 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/WebsiteSettingsPopup.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/WebsiteSettingsPopup.java |
| @@ -9,7 +9,6 @@ import android.content.Context; |
| import android.content.DialogInterface; |
| import android.graphics.Color; |
| import android.graphics.drawable.ColorDrawable; |
| -import android.net.Uri; |
| import android.text.Layout; |
| import android.text.Spannable; |
| import android.text.SpannableStringBuilder; |
| @@ -75,12 +74,20 @@ public class WebsiteSettingsPopup implements OnClickListener, OnItemSelectedList |
| } |
| } |
| + /** |
| + * A TextView which truncates and displays a URL such that the origin is always visible. |
| + * The URL can be expanded by clicking on the it. |
| + */ |
| public static class ElidedUrlTextView extends TextView { |
| // The number of lines to display when the URL is truncated. This number |
| // should still allow the origin to be displayed. NULL before |
| // setUrlAfterLayout() is called. |
| private Integer mTruncatedUrlLinesToDisplay; |
| + // The number of lines to display when the URL is expanded. This should be enough to display |
| + // at most two lines of the fragment if there is one in the URL. |
| + private Integer mFullLinesToDisplay; |
| + |
| // If true, the text view will show the truncated text. If false, it |
| // will show the full, expanded text. |
| private boolean mIsShowingTruncatedText = true; |
| @@ -102,33 +109,48 @@ public class WebsiteSettingsPopup implements OnClickListener, OnItemSelectedList |
| mCurrentMaxLines = maxlines; |
| } |
| + /** |
| + * Find the number of lines of text which must be shown in order to display the character at |
| + * a given index. |
| + */ |
| + private int getLineForIndex(int index) { |
| + Layout layout = getLayout(); |
| + int endLine = 0; |
| + while (endLine < layout.getLineCount() && layout.getLineEnd(endLine) < index) { |
| + endLine++; |
| + } |
| + // Since endLine is an index, add 1 to get the number of lines. |
| + return endLine + 1; |
| + } |
| + |
| @Override |
| protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| setMaxLines(Integer.MAX_VALUE); |
| super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| assert mProfile != null : "setProfile() must be called before layout."; |
| + String urlText = getText().toString(); |
| // Lay out the URL in a StaticLayout that is the same size as our final |
| // container. |
| - Layout layout = getLayout(); |
| - int originEndIndex = |
| - OmniboxUrlEmphasizer.getOriginEndIndex(getText().toString(), mProfile); |
| + int originEndIndex = OmniboxUrlEmphasizer.getOriginEndIndex(urlText, mProfile); |
| // Find the range of lines containing the origin. |
| - int originEndLineIndex = 0; |
| - while (originEndLineIndex < layout.getLineCount() |
| - && layout.getLineEnd(originEndLineIndex) < originEndIndex) { |
| - originEndLineIndex++; |
| - } |
| + int originEndLine = getLineForIndex(originEndIndex); |
| // Display an extra line so we don't accidentally hide the origin with |
| // ellipses |
| - int lastLineIndexToDisplay = originEndLineIndex + 1; |
| + mTruncatedUrlLinesToDisplay = originEndLine + 1; |
| + |
| + // Find the line where the fragment starts. Since # is a reserved character, it is safe |
| + // to just search for the first # to appear in the url. |
| + int fragmentStartIndex = urlText.indexOf('#'); |
| + // Do not truncate a fragment in URLs which have no fragment or no origin (according to |
| + // OmniboxUrlEmphasizer). |
| + if (fragmentStartIndex == -1 || fragmentStartIndex < originEndIndex) { |
| + fragmentStartIndex = urlText.length(); |
|
juhonurm
2015/04/15 06:07:57
Now the original issue is ignored in non-HTTP(S) U
tsergeant
2015/04/15 07:13:54
Done.
|
| + } |
| - // Since lastLineToDisplay is an index, add 1 to get the maximum number |
| - // of lines. This will always be at least 2 lines (when the origin is |
| - // fully contained on line 0). |
| - mTruncatedUrlLinesToDisplay = lastLineIndexToDisplay + 1; |
| + mFullLinesToDisplay = getLineForIndex(fragmentStartIndex) + 1; |
| if (updateMaxLines()) super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| } |
| @@ -153,7 +175,7 @@ public class WebsiteSettingsPopup implements OnClickListener, OnItemSelectedList |
| } |
| private boolean updateMaxLines() { |
| - int maxLines = Integer.MAX_VALUE; |
| + int maxLines = mFullLinesToDisplay; |
| if (mIsShowingTruncatedText) maxLines = mTruncatedUrlLinesToDisplay; |
| if (maxLines != mCurrentMaxLines) { |
| setMaxLines(maxLines); |
| @@ -165,10 +187,6 @@ public class WebsiteSettingsPopup implements OnClickListener, OnItemSelectedList |
| private static final int MAX_TABLET_DIALOG_WIDTH_DP = 400; |
| - private static final char FIRST_UNICODE_WHITESPACE = '\u2000'; |
| - private static final char FINAL_UNICODE_WHITESPACE = '\u200F'; |
| - private static final char UNICODE_NBSP = '\u00A0'; |
| - |
| private final Context mContext; |
| private final Profile mProfile; |
| private final WebContents mWebContents; |
| @@ -298,8 +316,7 @@ public class WebsiteSettingsPopup implements OnClickListener, OnItemSelectedList |
| mSecurityLevel = ToolbarModel.getSecurityLevelForWebContents(mWebContents); |
| mDeprecatedSHA1Present = ToolbarModel.isDeprecatedSHA1Present(mWebContents); |
| - String displayUrl = prepareUrlForDisplay(mFullUrl); |
| - SpannableStringBuilder urlBuilder = new SpannableStringBuilder(displayUrl); |
| + SpannableStringBuilder urlBuilder = new SpannableStringBuilder(mFullUrl); |
| OmniboxUrlEmphasizer.emphasizeUrl(urlBuilder, mContext.getResources(), mProfile, |
| mSecurityLevel, mIsInternalPage, true); |
| mUrlTitle.setText(urlBuilder); |
| @@ -310,26 +327,6 @@ public class WebsiteSettingsPopup implements OnClickListener, OnItemSelectedList |
| } |
| /** |
| - * Percent-encodes suspicious Unicode whitespace characters in a URL so that it can be safely |
| - * displayed. |
| - */ |
| - public static String prepareUrlForDisplay(String urlStr) { |
| - StringBuilder urlBuilder = new StringBuilder(); |
| - for (int i = 0; i < urlStr.length(); i++) { |
| - char c = urlStr.charAt(i); |
| - if ((c >= FIRST_UNICODE_WHITESPACE |
| - && c <= FINAL_UNICODE_WHITESPACE) |
| - || c == ' ' |
| - || c == UNICODE_NBSP) { |
| - urlBuilder.append(Uri.encode(Character.toString(c))); |
| - } else { |
| - urlBuilder.append(c); |
| - } |
| - } |
| - return urlBuilder.toString(); |
| - } |
| - |
| - /** |
| * Sets the visibility of the lower area of the dialog (containing the permissions and 'Site |
| * Settings' button). |
| * |