diff options
| author | 2024-05-08 23:06:46 +0000 | |
|---|---|---|
| committer | 2024-05-09 15:38:28 +0000 | |
| commit | 9da2f664d3ea8e7d4788411b15d23fa60fbd1631 (patch) | |
| tree | 852cd15dd3370c3dd5ec268bd7b731e331d949e2 | |
| parent | 6f8c3e613af8eeef2e3123558298748eb22d939e (diff) | |
[Text Consistency] Improve consecutive newlines cleaning
NewlineNormalizer replaces consecutive newlines with a single newline in the input text.
This also catches whitespace characters.
Fixes: 337021118
Test: NewlineNormalizerTest
Flag: ACONFIG android.app.Flags.clean_up_spans_and_new_lines STAGING
Change-Id: Id0d0ec324ba40ce7f1acf9dd9b712f46b8ed1431
3 files changed, 112 insertions, 1 deletions
diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java index 05a2aece789f..60ae65b0546c 100644 --- a/core/java/android/app/Notification.java +++ b/core/java/android/app/Notification.java @@ -115,6 +115,7 @@ import com.android.internal.annotations.VisibleForTesting; import com.android.internal.graphics.ColorUtils; import com.android.internal.util.ArrayUtils; import com.android.internal.util.ContrastColorUtil; +import com.android.internal.util.NewlineNormalizer; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -3187,7 +3188,7 @@ public class Notification implements Parcelable return charSequence; } - return charSequence.toString().replaceAll("[\r\n]+", "\n"); + return NewlineNormalizer.normalizeNewlines(charSequence.toString()); } private static CharSequence removeTextSizeSpans(CharSequence charSequence) { diff --git a/core/java/com/android/internal/util/NewlineNormalizer.java b/core/java/com/android/internal/util/NewlineNormalizer.java new file mode 100644 index 000000000000..0104d1f56f83 --- /dev/null +++ b/core/java/com/android/internal/util/NewlineNormalizer.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.internal.util; + + +import java.util.regex.Pattern; + +/** + * Utility class that replaces consecutive empty lines with single new line. + * @hide + */ +public class NewlineNormalizer { + + private static final Pattern MULTIPLE_NEWLINES = Pattern.compile("\\v(\\s*\\v)?"); + + // Private constructor to prevent instantiation + private NewlineNormalizer() {} + + /** + * Replaces consecutive newlines with a single newline in the input text. + */ + public static String normalizeNewlines(String text) { + return MULTIPLE_NEWLINES.matcher(text).replaceAll("\n"); + } +} diff --git a/core/tests/utiltests/src/com/android/internal/util/NewlineNormalizerTest.java b/core/tests/utiltests/src/com/android/internal/util/NewlineNormalizerTest.java new file mode 100644 index 000000000000..bcdac610a49d --- /dev/null +++ b/core/tests/utiltests/src/com/android/internal/util/NewlineNormalizerTest.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.internal.util; + +import static junit.framework.Assert.assertEquals; + + +import android.platform.test.annotations.DisabledOnRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Test for {@link NewlineNormalizer} + * @hide + */ +@DisabledOnRavenwood(blockedBy = NewlineNormalizer.class) +@RunWith(AndroidJUnit4.class) +public class NewlineNormalizerTest { + + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); + + @Test + public void testEmptyInput() { + assertEquals("", NewlineNormalizer.normalizeNewlines("")); + } + + @Test + public void testSingleNewline() { + assertEquals("\n", NewlineNormalizer.normalizeNewlines("\n")); + } + + @Test + public void testMultipleConsecutiveNewlines() { + assertEquals("\n", NewlineNormalizer.normalizeNewlines("\n\n\n\n\n")); + } + + @Test + public void testNewlinesWithSpacesAndTabs() { + String input = "Line 1\n \n \t \n\tLine 2"; + // Adjusted expected output to include the tab character + String expected = "Line 1\n\tLine 2"; + assertEquals(expected, NewlineNormalizer.normalizeNewlines(input)); + } + + @Test + public void testMixedNewlineCharacters() { + String input = "Line 1\r\nLine 2\u000BLine 3\fLine 4\u2028Line 5\u2029Line 6"; + String expected = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6"; + assertEquals(expected, NewlineNormalizer.normalizeNewlines(input)); + } +} |