@@ -85,6 +85,7 @@ import org.wordpress.aztec.spans.UnknownClickableSpan
8585import org.wordpress.aztec.spans.UnknownHtmlSpan
8686import org.wordpress.aztec.toolbar.AztecToolbar
8787import org.wordpress.aztec.util.AztecLog
88+ import org.wordpress.aztec.util.InstanceStateUtils
8889import org.wordpress.aztec.util.SpanWrapper
8990import org.wordpress.aztec.util.coerceToHtmlText
9091import org.wordpress.aztec.watchers.BlockElementWatcher
@@ -108,12 +109,6 @@ import org.wordpress.aztec.watchers.event.text.BeforeTextChangedEventData
108109import org.wordpress.aztec.watchers.event.text.OnTextChangedEventData
109110import org.wordpress.aztec.watchers.event.text.TextWatcherEvent
110111import org.xml.sax.Attributes
111- import java.io.File
112- import java.io.FileInputStream
113- import java.io.FileOutputStream
114- import java.io.IOException
115- import java.io.ObjectInputStream
116- import java.io.ObjectOutputStream
117112import java.util.ArrayList
118113import java.util.Arrays
119114import java.util.LinkedList
@@ -521,17 +516,17 @@ class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknownHtmlT
521516 val savedState = state as SavedState
522517 super .onRestoreInstanceState(savedState.superState)
523518 val customState = savedState.state
524- val array = readAndPurgeTempInstance<ArrayList <String >>(HISTORY_LIST_KEY , ArrayList <String >(), savedState.state)
519+ val array = InstanceStateUtils . readAndPurgeTempInstance<ArrayList <String >>(HISTORY_LIST_KEY , ArrayList <String >(), savedState.state)
525520 val list = LinkedList <String >()
526521
527522 list + = array
528523
529524 history.historyList = list
530525 history.historyCursor = customState.getInt(HISTORY_CURSOR_KEY )
531- history.inputLast = readAndPurgeTempInstance<String >(INPUT_LAST_KEY , " " , savedState.state)
526+ history.inputLast = InstanceStateUtils . readAndPurgeTempInstance<String >(INPUT_LAST_KEY , " " , savedState.state)
532527 visibility = customState.getInt(VISIBILITY_KEY )
533528
534- val retainedHtml = readAndPurgeTempInstance<String >(RETAINED_HTML_KEY , " " , savedState.state)
529+ val retainedHtml = InstanceStateUtils . readAndPurgeTempInstance<String >(RETAINED_HTML_KEY , " " , savedState.state)
535530 fromHtml(retainedHtml)
536531
537532 val retainedSelectionStart = customState.getInt(SELECTION_START_KEY )
@@ -555,7 +550,7 @@ class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknownHtmlT
555550 if (retainedBlockHtmlIndex != - 1 ) {
556551 val unknownSpan = text.getSpans(retainedBlockHtmlIndex, retainedBlockHtmlIndex + 1 , UnknownHtmlSpan ::class .java).firstOrNull()
557552 if (unknownSpan != null ) {
558- val retainedBlockHtml = readAndPurgeTempInstance<String >(BLOCK_EDITOR_HTML_KEY , " " ,
553+ val retainedBlockHtml = InstanceStateUtils . readAndPurgeTempInstance<String >(BLOCK_EDITOR_HTML_KEY , " " ,
559554 savedState.state)
560555 showBlockEditorDialog(unknownSpan, retainedBlockHtml)
561556 }
@@ -567,15 +562,23 @@ class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknownHtmlT
567562 enableTextChangedListener()
568563 }
569564
565+ // Do not include the content of the editor when saving state to bundle.
566+ // EditText has it `true` by default, and then the content was saved in bundle making the app crashing
567+ // due to the TransactionTooLargeException Exception.
568+ // The content is saved in tmp files in `onSaveInstanceState`. See: https://github.com/wordpress-mobile/AztecEditor-Android/pull/641
569+ override fun getFreezesText (): Boolean {
570+ return false
571+ }
572+
570573 override fun onSaveInstanceState (): Parcelable {
571574 val superState = super .onSaveInstanceState()
572575 val savedState = SavedState (superState)
573576 val bundle = Bundle ()
574- writeTempInstance(HISTORY_LIST_KEY , ArrayList <String >(history.historyList), bundle)
577+ InstanceStateUtils . writeTempInstance(context, externalLogger, HISTORY_LIST_KEY , ArrayList <String >(history.historyList), bundle)
575578 bundle.putInt(HISTORY_CURSOR_KEY , history.historyCursor)
576- writeTempInstance(INPUT_LAST_KEY , history.inputLast, bundle)
579+ InstanceStateUtils . writeTempInstance(context, externalLogger, INPUT_LAST_KEY , history.inputLast, bundle)
577580 bundle.putInt(VISIBILITY_KEY , visibility)
578- writeTempInstance(RETAINED_HTML_KEY , toHtml(false ), bundle)
581+ InstanceStateUtils . writeTempInstance(context, externalLogger, RETAINED_HTML_KEY , toHtml(false ), bundle)
579582 bundle.putInt(SELECTION_START_KEY , selectionStart)
580583 bundle.putInt(SELECTION_END_KEY , selectionEnd)
581584
@@ -594,7 +597,7 @@ class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknownHtmlT
594597
595598 bundle.putBoolean(BLOCK_DIALOG_VISIBLE_KEY , true )
596599 bundle.putInt(BLOCK_EDITOR_START_INDEX_KEY , unknownBlockSpanStart)
597- writeTempInstance(BLOCK_EDITOR_HTML_KEY , source?.getPureHtml(false ), bundle)
600+ InstanceStateUtils . writeTempInstance(context, externalLogger, BLOCK_EDITOR_HTML_KEY , source?.getPureHtml(false ), bundle)
598601 }
599602
600603 bundle.putBoolean(IS_MEDIA_ADDED_KEY , isMediaAdded)
@@ -603,69 +606,6 @@ class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknownHtmlT
603606 return savedState
604607 }
605608
606- private fun cacheFilenameKey (varName : String ): String {
607- return " CACHEFILENAMEKEY_$varName "
608- }
609-
610- private fun logCacheWriteException (varName : String , e : Exception ) {
611- AppLog .w(AppLog .T .EDITOR , " Error trying to write cache for $varName . Exception: ${e.message} " )
612- externalLogger?.logException(e, " Error trying to write cache for $varName ." )
613- }
614-
615- private fun writeTempInstance (varName : String , obj : Any? , bundle : Bundle ) {
616- try {
617- with (File .createTempFile(varName, " .inst" , context.getCacheDir())) {
618- deleteOnExit() // just make sure if we miss deleting this cache file the VM will eventually do it
619-
620- FileOutputStream (this ).use { output ->
621- ObjectOutputStream (output).use { objectOutput ->
622- objectOutput.writeObject(obj)
623-
624- // keep the filename in the bundle to use it to read the object back
625- bundle.putString(cacheFilenameKey(varName), this .path)
626- }
627- }
628- }
629- } catch (e: IOException ) {
630- logCacheWriteException(varName, e)
631- } catch (e: SecurityException ) {
632- logCacheWriteException(varName, e)
633- } catch (e: NullPointerException ) {
634- logCacheWriteException(varName, e)
635- }
636- }
637-
638- private fun <T > readAndPurgeTempInstance (varName : String , defaultValue : T , bundle : Bundle ): T {
639- // the full path is kept in the bundle so, get it from there
640- val filename = bundle.getString(cacheFilenameKey(varName))
641-
642- if (TextUtils .isEmpty(filename)) {
643- return defaultValue
644- }
645-
646- val file = File (filename)
647-
648- if (! file.exists()) {
649- return defaultValue
650- }
651-
652- var obj: T = defaultValue
653-
654- with (file) {
655- FileInputStream (this ).use { input ->
656- ObjectInputStream (input).use { objectInput ->
657- val r: Any? = objectInput.readObject()
658-
659- @Suppress(" UNCHECKED_CAST" )
660- obj = (r ? : defaultValue) as T
661- }
662- }
663- delete() // eagerly delete the cache file. If any is missed the VM will delete it on reboot.
664- }
665-
666- return obj
667- }
668-
669609 internal class SavedState : BaseSavedState {
670610 var state: Bundle = Bundle ()
671611
0 commit comments