Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(652)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/WebsiteSettingsPopup.java

Issue 1077483002: Truncate long URL fragments in Android page info popup (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Formatting Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/android/javatests/src/org/chromium/chrome/browser/WebsiteSettingsPopupTest.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..0c1257187cb49d53db4815018b7e5c336b62d322 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,45 @@ 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);
juhonurm 2015/04/15 05:00:41 OmniboxUrlEmphasizer.getOriginEndIndex only works
tsergeant 2015/04/15 05:47:20 Done.
// 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('#');
+ if (fragmentStartIndex == -1) fragmentStartIndex = urlText.length();
- // 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;
+ int fragmentStartLine = getLineForIndex(fragmentStartIndex);
+ mFullLinesToDisplay = fragmentStartLine + 1;
if (updateMaxLines()) super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@@ -153,7 +172,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 +184,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 +313,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 +324,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).
*
« no previous file with comments | « no previous file | chrome/android/javatests/src/org/chromium/chrome/browser/WebsiteSettingsPopupTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698