aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java
blob: 9567fc9c0c9007fd7197b5be82539d450b260fa6 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.sufficientlysecure.keychain.util;

import android.support.v4.app.FragmentActivity;

import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.ExportResult;
import org.sufficientlysecure.keychain.service.ExportKeyringParcel;
import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper;

import java.io.File;

public class ExportHelper
        implements CryptoOperationHelper.Callback <ExportKeyringParcel, ExportResult> {
    protected File mExportFile;

    FragmentActivity mActivity;

    private CryptoOperationHelper<ExportKeyringParcel, ExportResult> mExportOpHelper;
    private boolean mExportSecret;
    private long[] mMasterKeyIds;

    public ExportHelper(FragmentActivity activity) {
        super();
        this.mActivity = activity;
    }

    /**
     * Show dialog where to export keys
     */
    public void showExportKeysDialog(final long[] masterKeyIds, final File exportFile,
                                     final boolean showSecretCheckbox) {
        mExportFile = exportFile;

        String title = null;
        if (masterKeyIds == null) {
            // export all keys
            title = mActivity.getString(R.string.title_export_keys);
        } else {
            // export only key specified at data uri
            title = mActivity.getString(R.string.title_export_key);
        }

        String message = mActivity.getString(R.string.specify_file_to_export_to);
        String checkMsg = showSecretCheckbox ?
                mActivity.getString(R.string.also_export_secret_keys) : null;

        FileHelper.saveFile(new FileHelper.FileDialogCallback() {
            @Override
            public void onFileSelected(File file, boolean checked) {
                mExportFile = file;
                exportKeys(masterKeyIds, checked);
            }
        }, mActivity.getSupportFragmentManager() ,title, message, exportFile, checkMsg);
    }

    // TODO: If ExportHelper requires pending data (see CryptoOPerationHelper), activities using
    // TODO: this class should be able to call mExportOpHelper.handleActivity

    /**
     * Export keys
     */
    public void exportKeys(long[] masterKeyIds, boolean exportSecret) {
        Log.d(Constants.TAG, "exportKeys started");
        mExportSecret = exportSecret;
        mMasterKeyIds = masterKeyIds; // if masterKeyIds is null it means export all

        mExportOpHelper = new CryptoOperationHelper(mActivity, this, R.string.progress_exporting);
        mExportOpHelper.cryptoOperation();
    }

    @Override
    public ExportKeyringParcel createOperationInput() {
        return new ExportKeyringParcel(mMasterKeyIds, mExportSecret, mExportFile.getAbsolutePath());
    }

    @Override
    public void onCryptoOperationSuccess(ExportResult result) {
        result.createNotify(mActivity).show();
    }

    @Override
    public void onCryptoOperationCancelled() {

    }

    @Override
    public void onCryptoOperationError(ExportResult result) {
        result.createNotify(mActivity).show();
    }

    @Override
    public boolean onCryptoSetProgress(String msg, int progress, int max) {
        return false;
    }
}