- //Using a ListView allows to show large text without the UI freeze that happens
- //when calling TextView#setText() with a large CharSequence
- ((TextLineAdapter) mTextPreviewList.getAdapter()).add(values[0].toString());
+ final char[] newTextAsCharArray = values[0].toString().toCharArray();
+ for (char c : newTextAsCharArray) {
+ accumulatedText.add(c);
+ }
+ addLine();
+ }
+
+ private synchronized void addLine() {
+ StringBuilder textForThisLine = new StringBuilder();
+ do {
+ Character polled = accumulatedText.poll();
+ textForThisLine.append(polled);
+ }
+ while (!isTooLarge(textForThisLine.toString()) && !accumulatedText.isEmpty());
+ String line = textForThisLine.toString();
+ ((TextLineAdapter) mTextPreviewList.getAdapter()).add(line.contentEquals("null") ? "" : line);
+ }
+
+ private boolean isTooLarge(String text) {
+ paint.getTextBounds(text, 0, text.length(), bounds);
+ int lineWidth = (int) Math.ceil(bounds.width());
+ return lineWidth / TEXTVIEW_WIDTH > 1;