Merge pull request #2206 from k9mail/fix-cancel-crypto

Fix consecutive retrying of crypto operations
This commit is contained in:
Philip 2017-02-09 23:28:39 +00:00 committed by GitHub
commit fc6d5188f7
2 changed files with 16 additions and 5 deletions

View file

@ -258,7 +258,7 @@ public class MessageLoaderHelper {
// process with crypto helper // process with crypto helper
private void startOrResumeCryptoOperation() { private void startOrResumeCryptoOperation() {
RetainFragment<MessageCryptoHelper> retainCryptoHelperFragment = getMessageCryptoHelperRetainFragment(); RetainFragment<MessageCryptoHelper> retainCryptoHelperFragment = getMessageCryptoHelperRetainFragment(true);
if (retainCryptoHelperFragment.hasData()) { if (retainCryptoHelperFragment.hasData()) {
messageCryptoHelper = retainCryptoHelperFragment.getData(); messageCryptoHelper = retainCryptoHelperFragment.getData();
} else { } else {
@ -270,7 +270,7 @@ public class MessageLoaderHelper {
} }
private void cancelAndClearCryptoOperation() { private void cancelAndClearCryptoOperation() {
RetainFragment<MessageCryptoHelper> retainCryptoHelperFragment = getMessageCryptoHelperRetainFragment(); RetainFragment<MessageCryptoHelper> retainCryptoHelperFragment = getMessageCryptoHelperRetainFragment(false);
if (retainCryptoHelperFragment != null) { if (retainCryptoHelperFragment != null) {
if (retainCryptoHelperFragment.hasData()) { if (retainCryptoHelperFragment.hasData()) {
messageCryptoHelper = retainCryptoHelperFragment.getData(); messageCryptoHelper = retainCryptoHelperFragment.getData();
@ -281,8 +281,12 @@ public class MessageLoaderHelper {
} }
} }
private RetainFragment<MessageCryptoHelper> getMessageCryptoHelperRetainFragment() { private RetainFragment<MessageCryptoHelper> getMessageCryptoHelperRetainFragment(boolean createIfNotExists) {
return RetainFragment.findOrCreate(fragmentManager, "crypto_helper_" + messageReference.hashCode()); if (createIfNotExists) {
return RetainFragment.findOrCreate(fragmentManager, "crypto_helper_" + messageReference.hashCode());
} else {
return RetainFragment.findOrNull(fragmentManager, "crypto_helper_" + messageReference.hashCode());
}
} }
private MessageCryptoCallback messageCryptoCallback = new MessageCryptoCallback() { private MessageCryptoCallback messageCryptoCallback = new MessageCryptoCallback() {

View file

@ -10,6 +10,7 @@ import android.os.Bundle;
public class RetainFragment<T> extends Fragment { public class RetainFragment<T> extends Fragment {
private T data; private T data;
private boolean cleared;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -30,11 +31,16 @@ public class RetainFragment<T> extends Fragment {
this.data = data; this.data = data;
} }
public static <T> RetainFragment<T> findOrNull(FragmentManager fm, String tag) {
// noinspection unchecked, we know this is the the right type
return (RetainFragment<T>) fm.findFragmentByTag(tag);
}
public static <T> RetainFragment<T> findOrCreate(FragmentManager fm, String tag) { public static <T> RetainFragment<T> findOrCreate(FragmentManager fm, String tag) {
// noinspection unchecked, we know this is the the right type // noinspection unchecked, we know this is the the right type
RetainFragment<T> retainFragment = (RetainFragment<T>) fm.findFragmentByTag(tag); RetainFragment<T> retainFragment = (RetainFragment<T>) fm.findFragmentByTag(tag);
if (retainFragment == null) { if (retainFragment == null || retainFragment.cleared) {
retainFragment = new RetainFragment<>(); retainFragment = new RetainFragment<>();
fm.beginTransaction() fm.beginTransaction()
.add(retainFragment, tag) .add(retainFragment, tag)
@ -46,6 +52,7 @@ public class RetainFragment<T> extends Fragment {
public void clearAndRemove(FragmentManager fm) { public void clearAndRemove(FragmentManager fm) {
data = null; data = null;
cleared = true;
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1 && fm.isDestroyed()) { if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1 && fm.isDestroyed()) {
return; return;