summaryrefslogtreecommitdiff
path: root/android/text/NonEditableTextGenerator.java
diff options
context:
space:
mode:
authorJustin Klaassen <justinklaassen@google.com>2017-10-10 15:20:13 -0400
committerJustin Klaassen <justinklaassen@google.com>2017-10-10 15:20:13 -0400
commit93b7ee4fce01df52a6607f0b1965cbafdfeaf1a6 (patch)
tree49f76f879a89c256a4f65b674086be50760bdffb /android/text/NonEditableTextGenerator.java
parentbc81c7ada5aab3806dd0b17498f5c9672c9b33c4 (diff)
downloadandroid-28-93b7ee4fce01df52a6607f0b1965cbafdfeaf1a6.tar.gz
Import Android SDK Platform P [4386628]
/google/data/ro/projects/android/fetch_artifact \ --bid 4386628 \ --target sdk_phone_armv7-win_sdk \ sdk-repo-linux-sources-4386628.zip AndroidVersion.ApiLevel has been modified to appear as 28 Change-Id: I9b8400ac92116cae4f033d173f7a5682b26ccba9
Diffstat (limited to 'android/text/NonEditableTextGenerator.java')
-rw-r--r--android/text/NonEditableTextGenerator.java138
1 files changed, 138 insertions, 0 deletions
diff --git a/android/text/NonEditableTextGenerator.java b/android/text/NonEditableTextGenerator.java
new file mode 100644
index 00000000..7c0cf0ee
--- /dev/null
+++ b/android/text/NonEditableTextGenerator.java
@@ -0,0 +1,138 @@
+package android.text;
+
+import static android.text.Spanned.SPAN_INCLUSIVE_INCLUSIVE;
+
+import android.text.style.BulletSpan;
+
+import java.util.Random;
+
+/**
+ *
+ */
+public class NonEditableTextGenerator {
+
+ enum TextType {
+ STRING,
+ SPANNED,
+ SPANNABLE_BUILDER
+ }
+
+ private boolean mCreateBoring;
+ private TextType mTextType;
+ private int mSequenceLength;
+ private final Random mRandom;
+
+ public NonEditableTextGenerator(Random random) {
+ mRandom = random;
+ }
+
+ public NonEditableTextGenerator setCreateBoring(boolean createBoring) {
+ mCreateBoring = createBoring;
+ return this;
+ }
+
+ public NonEditableTextGenerator setTextType(TextType textType) {
+ mTextType = textType;
+ return this;
+ }
+
+ public NonEditableTextGenerator setSequenceLength(int sequenceLength) {
+ mSequenceLength = sequenceLength;
+ return this;
+ }
+
+ /**
+ * Sample charSequence generated:
+ * NRjPzjvUadHmH ExoEoTqfx pCLw qtndsqfpk AqajVCbgjGZ igIeC dfnXRgA
+ */
+ public CharSequence build() {
+ final RandomCharSequenceGenerator sequenceGenerator = new RandomCharSequenceGenerator(
+ mRandom);
+ if (mSequenceLength > 0) {
+ sequenceGenerator.setSequenceLength(mSequenceLength);
+ }
+
+ final CharSequence charSequence = sequenceGenerator.buildLatinSequence();
+
+ switch (mTextType) {
+ case SPANNED:
+ case SPANNABLE_BUILDER:
+ return createSpannable(charSequence);
+ case STRING:
+ default:
+ return createString(charSequence);
+ }
+ }
+
+ private Spannable createSpannable(CharSequence charSequence) {
+ final Spannable spannable = (mTextType == TextType.SPANNABLE_BUILDER) ?
+ new SpannableStringBuilder(charSequence) : new SpannableString(charSequence);
+
+ if (!mCreateBoring) {
+ // add a paragraph style to make it non boring
+ spannable.setSpan(new BulletSpan(), 0, spannable.length(), SPAN_INCLUSIVE_INCLUSIVE);
+ }
+
+ spannable.setSpan(new Object(), 0, spannable.length(), SPAN_INCLUSIVE_INCLUSIVE);
+ spannable.setSpan(new Object(), 0, 1, SPAN_INCLUSIVE_INCLUSIVE);
+
+ return spannable;
+ }
+
+ private String createString(CharSequence charSequence) {
+ if (mCreateBoring) {
+ return charSequence.toString();
+ } else {
+ // BoringLayout checks to see if there is a surrogate pair and if so tells that
+ // the charSequence is not suitable for boring. Add an emoji to make it non boring.
+ // Emoji is added instead of RTL, since emoji stays in the same run and is a more
+ // common case.
+ return charSequence.toString() + "\uD83D\uDC68\uD83C\uDFFF";
+ }
+ }
+
+ public static class RandomCharSequenceGenerator {
+
+ private static final int DEFAULT_MIN_WORD_LENGTH = 3;
+ private static final int DEFAULT_MAX_WORD_LENGTH = 15;
+ private static final int DEFAULT_SEQUENCE_LENGTH = 256;
+
+ private int mMinWordLength = DEFAULT_MIN_WORD_LENGTH;
+ private int mMaxWordLength = DEFAULT_MAX_WORD_LENGTH;
+ private int mSequenceLength = DEFAULT_SEQUENCE_LENGTH;
+ private final Random mRandom;
+
+ public RandomCharSequenceGenerator(Random random) {
+ mRandom = random;
+ }
+
+ public RandomCharSequenceGenerator setSequenceLength(int sequenceLength) {
+ mSequenceLength = sequenceLength;
+ return this;
+ }
+
+ public CharSequence buildLatinSequence() {
+ final StringBuilder result = new StringBuilder();
+ while (result.length() < mSequenceLength) {
+ // add random word
+ result.append(buildLatinWord());
+ result.append(' ');
+ }
+ return result.substring(0, mSequenceLength);
+ }
+
+ public CharSequence buildLatinWord() {
+ final StringBuilder result = new StringBuilder();
+ // create a random length that is (mMinWordLength + random amount of chars) where
+ // total size is less than mMaxWordLength
+ final int length = mRandom.nextInt(mMaxWordLength - mMinWordLength) + mMinWordLength;
+ while (result.length() < length) {
+ // add random letter
+ int base = mRandom.nextInt(2) == 0 ? 'A' : 'a';
+ result.append(Character.toChars(mRandom.nextInt(26) + base));
+ }
+ return result.toString();
+ }
+ }
+
+}