aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CachingCryptoOperationFragment.java
blob: d0b6f502f3d9d03d1b7d4f8e806b7cf4de1d52da (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
package org.sufficientlysecure.keychain.ui.base;


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

import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;


public abstract class CachingCryptoOperationFragment <T extends Parcelable> extends CryptoOperationFragment {

    public static final String ARG_CACHED_ACTIONS = "cached_actions";

    private T mCachedActionsParcel;

    @Override
    protected void cryptoOperation(CryptoInputParcel cryptoInput) {
        cryptoOperation(cryptoInput, mCachedActionsParcel);
    }

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

        outState.putParcelable(ARG_CACHED_ACTIONS, mCachedActionsParcel);
    }

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

        if (savedInstanceState != null) {
            mCachedActionsParcel = savedInstanceState.getParcelable(ARG_CACHED_ACTIONS);
        }
    }

    @Override
    public boolean handlePendingMessage(Message message) {
        // see if it's an InputPendingResult, and if so don't care
        if (super.handlePendingMessage(message)) {
            return true;
        }

        // if it's a non-input-pending OKAY message, always clear the cached actions parcel
        if (message.arg1 == ServiceProgressHandler.MessageStatus.OKAY.ordinal()) {
            mCachedActionsParcel = null;
        }

        return false;
    }

    protected abstract void cryptoOperation(CryptoInputParcel cryptoInput, T cachedActionsParcel);

    protected void cacheActionsParcel(T cachedActionsParcel) {
        mCachedActionsParcel = cachedActionsParcel;
    }

    protected void onCryptoOperationCancelled() {
        mCachedActionsParcel = null;
    }

}