From 75dd5dcc4545b7a81447c26128b6ce2db392e2ba Mon Sep 17 00:00:00 2001 From: Daniele Ricci Date: Wed, 9 Jul 2014 00:01:16 +0200 Subject: [PATCH] Fix mark as read being called when resuming from lock screen (fixes #19) Signed-off-by: Daniele Ricci --- src/org/kontalk/ui/ComposeMessage.java | 25 +++++++++++++++++ .../kontalk/ui/ComposeMessageFragment.java | 28 ++++++++++++++++--- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/org/kontalk/ui/ComposeMessage.java b/src/org/kontalk/ui/ComposeMessage.java index ebc275497..aa0017723 100644 --- a/src/org/kontalk/ui/ComposeMessage.java +++ b/src/org/kontalk/ui/ComposeMessage.java @@ -84,6 +84,11 @@ public class ComposeMessage extends ActionBarActivity { private TextView mTitleView; private TextView mSubtitleView; + /** + * True if the window has lost focus the last time + * {@link #onWindowFocusChanged} was called. */ + private boolean mLostFocus; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -409,6 +414,26 @@ protected void onSaveInstanceState(Bundle out) { out.putParcelable(Uri.class.getName(), Threads.getUri(mFragment.getUserId())); } + @Override + public void onWindowFocusChanged(boolean hasFocus) { + super.onWindowFocusChanged(hasFocus); + + if (hasFocus) { + if (mLostFocus) { + mFragment.onFocus(); + mLostFocus = false; + } + } + } + + public void fragmentLostFocus() { + mLostFocus = true; + } + + public boolean hasLostFocus() { + return mLostFocus; + } + public Intent getSendIntent() { return sendIntent; } diff --git a/src/org/kontalk/ui/ComposeMessageFragment.java b/src/org/kontalk/ui/ComposeMessageFragment.java index 59f8280e7..7db486a67 100644 --- a/src/org/kontalk/ui/ComposeMessageFragment.java +++ b/src/org/kontalk/ui/ComposeMessageFragment.java @@ -2069,9 +2069,15 @@ public void onResume() { return; } + ComposeMessage activity = getParentActivity(); + if (activity == null || !activity.hasLostFocus() || activity.hasWindowFocus()) { + onFocus(); + } + } + + public void onFocus() { // resume content watcher - if (mListAdapter != null) - mListAdapter.setOnContentChangedListener(mContentChangedListener); + resumeContentListener(); // we are updating the status now setActivityStatusUpdating(); @@ -2098,8 +2104,12 @@ public void onPause() { super.onPause(); // pause content watcher - if (mListAdapter != null) - mListAdapter.setOnContentChangedListener(null); + pauseContentListener(); + + // notify parent of pausing + ComposeMessage parent = getParentActivity(); + if (parent != null) + parent.fragmentLostFocus(); CharSequence text = mTextEntry.getText(); int len = text.length(); @@ -2188,6 +2198,16 @@ public void onDestroy() { } } + private void pauseContentListener() { + if (mListAdapter != null) + mListAdapter.setOnContentChangedListener(null); + } + + private void resumeContentListener() { + if (mListAdapter != null) + mListAdapter.setOnContentChangedListener(mContentChangedListener); + } + public final boolean isFinishing() { return (getActivity() == null || (getActivity() != null && getActivity() .isFinishing())) || isRemoving();