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


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;

import org.sufficientlysecure.keychain.operations.results.CertifyResult;
import org.sufficientlysecure.keychain.operations.results.InputPendingResult;
import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler.MessageStatus;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.service.input.NfcOperationsParcel;


public abstract class CryptoOperationFragment extends Fragment {

    public static final int REQUEST_CODE_PASSPHRASE = 0x00008001;
    public static final int REQUEST_CODE_NFC = 0x00008002;

    private void startPassphraseDialog(long subkeyId) {
        Intent intent = new Intent(getActivity(), PassphraseDialogActivity.class);
        intent.putExtra(PassphraseDialogActivity.EXTRA_SUBKEY_ID, subkeyId);
        startActivityForResult(intent, REQUEST_CODE_PASSPHRASE);
    }

    private void initiateNfcInput(NfcOperationsParcel nfcOps) {
        Intent intent = new Intent(getActivity(), NfcOperationActivity.class);
        intent.putExtra(NfcOperationActivity.EXTRA_PIN, "123456");
        intent.putExtra(NfcOperationActivity.EXTRA_NFC_OPS, nfcOps);
        startActivityForResult(intent, REQUEST_CODE_NFC);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CODE_PASSPHRASE: {
                if (resultCode == Activity.RESULT_OK && data != null) {
                    String passphrase = data.getStringExtra(PassphraseDialogActivity.MESSAGE_DATA_PASSPHRASE);
                    cryptoOperation(passphrase);
                }
                return;
            }

            case REQUEST_CODE_NFC: {
                if (resultCode == Activity.RESULT_OK && data != null) {
                    CryptoInputParcel cryptoInput =
                            data.getParcelableExtra(NfcOperationActivity.RESULT_DATA);
                    cryptoOperation(cryptoInput);
                    return;
                }
                break;
            }

            default: {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }
    }

    public boolean handlePendingMessage(Message message) {

        if (message.arg1 == MessageStatus.OKAY.ordinal()) {
            Bundle data = message.getData();

            InputPendingResult result = data.getParcelable(CertifyResult.EXTRA_RESULT);

            if (result != null && result.isPending()) {
                if (result.isPassphrasePending()) {
                    startPassphraseDialog(result.getPassphraseKeyId());
                    return true;
                } else if (result.isNfcPending()) {
                    NfcOperationsParcel requiredInput = result.getNfcOperationsParcel();
                    initiateNfcInput(requiredInput);
                    return true;
                } else {
                    throw new RuntimeException("Unhandled pending result!");
                }
            }
        }

        return false;
    }

    protected abstract void cryptoOperation(CryptoInputParcel cryptoInput);
    protected abstract void cryptoOperation(String passphrase);

}