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() : ""; } /** 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..b9c02154d926 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,22 @@ 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 + */ + // 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; + } + AppLog.w(T.UTILS, "sqlite > max text exceeded, storing truncated text"); + return text.substring(0, MAX_TEXT_LEN); + } }