aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/src/com/android/utils/SdkUtils.java71
-rw-r--r--common/tests/src/com/android/utils/SdkUtilsTest.java49
2 files changed, 119 insertions, 1 deletions
diff --git a/common/src/com/android/utils/SdkUtils.java b/common/src/com/android/utils/SdkUtils.java
index 18d3ccd..160f95d 100644
--- a/common/src/com/android/utils/SdkUtils.java
+++ b/common/src/com/android/utils/SdkUtils.java
@@ -17,7 +17,9 @@
package com.android.utils;
import com.android.annotations.NonNull;
+import com.android.annotations.Nullable;
+/** Miscellaneous utilities used by the Android SDK tools */
public class SdkUtils {
/**
* Returns true if the given string ends with the given suffix, using a
@@ -111,7 +113,12 @@ public class SdkUtils {
return sb.toString();
}
- /** Returns true if the given string has an upper case character. */
+ /**
+ * Returns true if the given string has an upper case character.
+ *
+ * @param s the string to check
+ * @return true if it contains uppercase characters
+ */
public static boolean hasUpperCaseCharacter(String s) {
for (int i = 0; i < s.length(); i++) {
if (Character.isUpperCase(s.charAt(i))) {
@@ -144,4 +151,66 @@ public class SdkUtils {
return sLineSeparator;
}
+
+ /**
+ * Wraps the given text at the given line width, with an optional hanging
+ * indent.
+ *
+ * @param text the text to be wrapped
+ * @param lineWidth the number of characters to wrap the text to
+ * @param hangingIndent the hanging indent (to be used for the second and
+ * subsequent lines in each paragraph, or null if not known
+ * @return the string, wrapped
+ */
+ @NonNull
+ public static String wrap(
+ @NonNull String text,
+ int lineWidth,
+ @Nullable String hangingIndent) {
+ if (hangingIndent == null) {
+ hangingIndent = "";
+ }
+ int explanationLength = text.length();
+ StringBuilder sb = new StringBuilder(explanationLength * 2);
+ int index = 0;
+
+ while (index < explanationLength) {
+ int lineEnd = text.indexOf('\n', index);
+ int next;
+
+ if (lineEnd != -1 && (lineEnd - index) < lineWidth) {
+ next = lineEnd + 1;
+ } else {
+ // Line is longer than available width; grab as much as we can
+ lineEnd = Math.min(index + lineWidth, explanationLength);
+ if (lineEnd - index < lineWidth) {
+ next = explanationLength;
+ } else {
+ // then back up to the last space
+ int lastSpace = text.lastIndexOf(' ', lineEnd);
+ if (lastSpace > index) {
+ lineEnd = lastSpace;
+ next = lastSpace + 1;
+ } else {
+ // No space anywhere on the line: it contains something wider than
+ // can fit (like a long URL) so just hard break it
+ next = lineEnd + 1;
+ }
+ }
+ }
+
+ if (sb.length() > 0) {
+ sb.append(hangingIndent);
+ } else {
+ lineWidth -= hangingIndent.length();
+ }
+
+ sb.append(text.substring(index, lineEnd));
+ sb.append('\n');
+ index = next;
+ }
+
+ return sb.toString();
+ }
+
}
diff --git a/common/tests/src/com/android/utils/SdkUtilsTest.java b/common/tests/src/com/android/utils/SdkUtilsTest.java
index 29a4d51..030e1b7 100644
--- a/common/tests/src/com/android/utils/SdkUtilsTest.java
+++ b/common/tests/src/com/android/utils/SdkUtilsTest.java
@@ -18,6 +18,7 @@ package com.android.utils;
import junit.framework.TestCase;
+@SuppressWarnings("javadoc")
public class SdkUtilsTest extends TestCase {
public void testEndsWithIgnoreCase() {
assertTrue(SdkUtils.endsWithIgnoreCase("foo", "foo"));
@@ -81,4 +82,52 @@ public class SdkUtilsTest extends TestCase {
assertEquals("foobar", SdkUtils.stripWhitespace("foo bar"));
assertEquals("foobar", SdkUtils.stripWhitespace(" foo bar \n\t"));
}
+
+ public void testWrap() {
+ String s =
+ "Hardcoding text attributes directly in layout files is bad for several reasons:\n" +
+ "\n" +
+ "* When creating configuration variations (for example for landscape or portrait)" +
+ "you have to repeat the actual text (and keep it up to date when making changes)\n" +
+ "\n" +
+ "* The application cannot be translated to other languages by just adding new " +
+ "translations for existing string resources.";
+ String wrapped = SdkUtils.wrap(s, 70, "");
+ assertEquals(
+ "Hardcoding text attributes directly in layout files is bad for several\n" +
+ "reasons:\n" +
+ "\n" +
+ "* When creating configuration variations (for example for landscape or\n" +
+ "portrait)you have to repeat the actual text (and keep it up to date\n" +
+ "when making changes)\n" +
+ "\n" +
+ "* The application cannot be translated to other languages by just\n" +
+ "adding new translations for existing string resources.\n",
+ wrapped);
+ }
+
+ public void testWrapPrefix() {
+ String s =
+ "Hardcoding text attributes directly in layout files is bad for several reasons:\n" +
+ "\n" +
+ "* When creating configuration variations (for example for landscape or portrait)" +
+ "you have to repeat the actual text (and keep it up to date when making changes)\n" +
+ "\n" +
+ "* The application cannot be translated to other languages by just adding new " +
+ "translations for existing string resources.";
+ String wrapped = SdkUtils.wrap(s, 70, " ");
+ assertEquals(
+ "Hardcoding text attributes directly in layout files is bad for several\n" +
+ " reasons:\n" +
+ " \n" +
+ " * When creating configuration variations (for example for\n" +
+ " landscape or portrait)you have to repeat the actual text (and keep\n" +
+ " it up to date when making changes)\n" +
+ " \n" +
+ " * The application cannot be translated to other languages by just\n" +
+ " adding new translations for existing string resources.\n",
+ wrapped);
+ }
+
+
}