From 5f2ddcfdc0bc7813e43461cb3941f25ea52280c4 Mon Sep 17 00:00:00 2001 From: Vavassor Date: Sun, 26 Mar 2017 17:21:18 -0400 Subject: [PATCH] Fixes composer not saving/restoring its text on configuration changes for some devices. --- .../keylesspalace/tusky/ComposeActivity.java | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index 4d6be505..7c731227 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -390,6 +390,7 @@ public class ComposeActivity extends BaseActivity { Intent intent = getIntent(); String[] mentionedUsernames = null; + inReplyToId = null; if (intent != null) { inReplyToId = intent.getStringExtra("in_reply_to_id"); String replyVisibility = intent.getStringExtra("reply_visibility"); @@ -410,7 +411,7 @@ public class ComposeActivity extends BaseActivity { textEditor = createEditText(null); // new String[] { "image/gif", "image/webp" } if (savedInstanceState != null) { - textEditor.onRestoreInstanceState(savedInstanceState.getParcelable("textEditorState")); + restoreTextEditorState(savedInstanceState.getParcelable("textEditorState")); } RelativeLayout editArea = (RelativeLayout) findViewById(R.id.compose_edit_area); /* Adding this at index zero because it implicitly gives it the lowest input priority. So, @@ -541,7 +542,7 @@ public class ComposeActivity extends BaseActivity { outState.putString("statusVisibility", statusVisibility); outState.putBoolean("statusMarkSensitive", statusMarkSensitive); outState.putBoolean("statusHideText", statusHideText); - outState.putParcelable("textEditorState", textEditor.onSaveInstanceState()); + outState.putParcelable("textEditorState", saveTextEditorState()); if (currentInputContentInfo != null) { outState.putParcelable("commitContentInputContentInfo", (Parcelable) currentInputContentInfo.unwrap()); @@ -552,6 +553,28 @@ public class ComposeActivity extends BaseActivity { super.onSaveInstanceState(outState); } + private Parcelable saveTextEditorState() { + Bundle bundle = new Bundle(); + bundle.putString("text", HtmlUtils.toHtml(textEditor.getText())); + bundle.putInt("selectionStart", textEditor.getSelectionStart()); + bundle.putInt("selectionEnd", textEditor.getSelectionEnd()); + return bundle; + } + + private void restoreTextEditorState(Parcelable state) { + Bundle bundle = (Bundle) state; + textEditor.setText(HtmlUtils.fromHtml(bundle.getString("text"))); + int start = bundle.getInt("selectionStart"); + int end = bundle.getInt("selectionEnd"); + if (start != -1) { + if (end != -1) { + textEditor.setSelection(start, end); + } else { + textEditor.setSelection(start); + } + } + } + @Override protected void onPause() { super.onPause();