Merge changes from topic "am-f5dc500e-4d32-4849-891e-c618cef666de" into oc-dev

* changes:
  [automerger] Make safe label more safe am: 2263da9539 am: 05086b1008 am: 77f449068a am: 46f4563313 am: 6be1d6713f
  [automerger] Make safe label more safe am: 2263da9539 am: 05086b1008 am: 77f449068a am: 46f4563313
  [automerger] Make safe label more safe am: 2263da9539 am: 05086b1008 am: 77f449068a
  [automerger] Make safe label more safe am: 2263da9539 am: 05086b1008
  [automerger] Make safe label more safe am: 2263da9539
  Make safe label more safe
This commit is contained in:
Atanas Kirilov
2018-06-05 19:37:52 +00:00
committed by Android (Google) Code Review

View File

@@ -42,6 +42,9 @@ import java.util.Comparator;
*/ */
public class PackageItemInfo { public class PackageItemInfo {
private static final float MAX_LABEL_SIZE_PX = 500f; private static final float MAX_LABEL_SIZE_PX = 500f;
/** The maximum length of a safe label, in characters */
private static final int MAX_SAFE_LABEL_LENGTH = 50000;
/** /**
* Public name of this item. From the "android:name" attribute. * Public name of this item. From the "android:name" attribute.
*/ */
@@ -169,7 +172,8 @@ public class PackageItemInfo {
// If the label contains new line characters it may push the UI // If the label contains new line characters it may push the UI
// down to hide a part of it. Labels shouldn't have new line // down to hide a part of it. Labels shouldn't have new line
// characters, so just truncate at the first time one is seen. // characters, so just truncate at the first time one is seen.
final int labelLength = labelStr.length(); final int labelLength = Math.min(labelStr.length(), MAX_SAFE_LABEL_LENGTH);
final StringBuffer sb = new StringBuffer(labelLength);
int offset = 0; int offset = 0;
while (offset < labelLength) { while (offset < labelLength) {
final int codePoint = labelStr.codePointAt(offset); final int codePoint = labelStr.codePointAt(offset);
@@ -181,14 +185,19 @@ public class PackageItemInfo {
break; break;
} }
// replace all non-break space to " " in order to be trimmed // replace all non-break space to " " in order to be trimmed
final int charCount = Character.charCount(codePoint);
if (type == Character.SPACE_SEPARATOR) { if (type == Character.SPACE_SEPARATOR) {
labelStr = labelStr.substring(0, offset) + " " + labelStr.substring(offset + sb.append(' ');
Character.charCount(codePoint)); } else {
sb.append(labelStr.charAt(offset));
if (charCount == 2) {
sb.append(labelStr.charAt(offset + 1));
} }
offset += Character.charCount(codePoint); }
offset += charCount;
} }
labelStr = labelStr.trim(); labelStr = sb.toString().trim();
if (labelStr.isEmpty()) { if (labelStr.isEmpty()) {
return packageName; return packageName;
} }