aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/QueueingCryptoOperationFragment.java
blob: 65e0ce94155208db123a54b3404a786bc1947e35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package org.sufficientlysecure.keychain.ui.base;


import android.os.Bundle;
import android.os.Parcelable;

import org.sufficientlysecure.keychain.operations.results.OperationResult;


/** CryptoOperationFragment which calls crypto operation results only while
 * attached to Activity.
 *
 * This subclass of CryptoOperationFragment substitutes the onCryptoOperation*
 * methods for onQueuedOperation* ones, which are ensured to be called while
 * the fragment is attached to an Activity, possibly delaying the call until
 * the Fragment is re-attached.
 *
 * TODO merge this functionality into CryptoOperationFragment?
 *
 * @see CryptoOperationFragment
 */
public abstract class QueueingCryptoOperationFragment<T extends Parcelable, S extends OperationResult>
        extends CryptoOperationFragment<T,S> {

    public static final String ARG_QUEUED_RESULT = "queued_result";
    private S mQueuedResult;

    public QueueingCryptoOperationFragment() {
        super();
    }

    public QueueingCryptoOperationFragment(Integer initialProgressMsg) {
        super(initialProgressMsg);
    }

    @Override
    public void onViewStateRestored(Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);

        if (mQueuedResult != null) {
            try {
                if (mQueuedResult.success()) {
                    onQueuedOperationSuccess(mQueuedResult);
                } else {
                    onQueuedOperationError(mQueuedResult);
                }
            } finally {
                mQueuedResult = null;
            }
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putParcelable(ARG_QUEUED_RESULT, mQueuedResult);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            mQueuedResult = savedInstanceState.getParcelable(ARG_QUEUED_RESULT);
        }
    }

    public abstract void onQueuedOperationSuccess(S result);

    public void onQueuedOperationError(S result) {
        hideKeyboard();
        result.createNotify(getActivity()).show();
    }

    @Override
    final public void onCryptoOperationSuccess(S result) {
        if (getActivity() == null) {
            mQueuedResult = result;
            return;
        }
        onQueuedOperationSuccess(result);
    }

    @Override
    final public void onCryptoOperationError(S result) {
        if (getActivity() == null) {
            mQueuedResult = result;
            return;
        }
        onQueuedOperationError(result);
    }
}