From bf3cd76cd580687b9874f74a147877d9dec14ee9 Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Thu, 18 Jun 2015 11:34:10 +0200 Subject: [PATCH 1/3] fix #2855: delete big comments and truncate new inserted comments --- .../org/wordpress/android/util/SqlUtils.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/SqlUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/SqlUtils.java index 8d1b4b4379c9..f8d20186bb77 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/SqlUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/SqlUtils.java @@ -7,6 +7,8 @@ import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteStatement; +import org.wordpress.android.util.AppLog.T; + import java.util.ArrayList; import java.util.List; @@ -118,4 +120,21 @@ public static boolean dropAllTables(SQLiteDatabase db) throws SQLiteException { db.endTransaction(); } } + + /* + * Android's CursorWindow has a max size of 2MB per row which can be exceeded + * with a very large text column, causing an IllegalStateException when the + * row is read - prevent this by limiting the amount of text that's stored in + * the text column. + * https://github.com/android/platform_frameworks_base/blob/b77bc869241644a662f7e615b0b00ecb5aee373d/core/res/res/values/config.xml#L1268 + * https://github.com/android/platform_frameworks_base/blob/3bdbf644d61f46b531838558fabbd5b990fc4913/core/java/android/database/CursorWindow.java#L103 + */ + private static final int MAX_TEXT_LEN = 1024 * 1024; + public static String maxSQLiteText(final String text) { + if (text.length() <= MAX_TEXT_LEN) { + return text; + } + AppLog.w(T.UTILS, "sqlite > max text exceeded, storing truncated text"); + return text.substring(0, MAX_TEXT_LEN); + } } From 3f98bb1b478f09db06a89a5c33bc092001aa7b17 Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Thu, 18 Jun 2015 12:30:28 +0200 Subject: [PATCH 2/3] limit text to 524288 characters which is always < 2Mb with UTF-8 --- .../src/main/java/org/wordpress/android/util/SqlUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/SqlUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/SqlUtils.java index f8d20186bb77..b9c02154d926 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/SqlUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/SqlUtils.java @@ -129,7 +129,8 @@ public static boolean dropAllTables(SQLiteDatabase db) throws SQLiteException { * https://github.com/android/platform_frameworks_base/blob/b77bc869241644a662f7e615b0b00ecb5aee373d/core/res/res/values/config.xml#L1268 * https://github.com/android/platform_frameworks_base/blob/3bdbf644d61f46b531838558fabbd5b990fc4913/core/java/android/database/CursorWindow.java#L103 */ - private static final int MAX_TEXT_LEN = 1024 * 1024; + // Max 512K characters (a UTF-8 char is 4 bytes max, so a 512K characters string is always < 2Mb) + private static final int MAX_TEXT_LEN = 1024 * 1024 / 2; public static String maxSQLiteText(final String text) { if (text.length() <= MAX_TEXT_LEN) { return text; From 0105ceec8077d52dda74b745ecf4ecd21b9ff9bb Mon Sep 17 00:00:00 2001 From: Tony Rankin Date: Thu, 18 Jun 2015 14:18:18 -0700 Subject: [PATCH 3/3] Updating EditTextUtils.getText method instead of creating new one. --- .../java/org/wordpress/android/util/EditTextUtils.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/EditTextUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/EditTextUtils.java index 64ee67e566a9..66a0c77fdf8f 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/EditTextUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/EditTextUtils.java @@ -4,6 +4,7 @@ import android.text.TextUtils; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; +import android.widget.TextView; /** * EditText utils @@ -14,13 +15,10 @@ private EditTextUtils() { } /** - * returns text string from passed EditText + * returns non-null text string from passed TextView */ - public static String getText(EditText edit) { - if (edit.getText() == null) { - return ""; - } - return edit.getText().toString(); + public static String getText(TextView textView) { + return (textView != null) ? textView.getText().toString() : ""; } /**