aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java
blob: c5404094a83a6d07b8b67bfd57384b56710d827d (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * 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.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tokenautocomplete.TokenCompleteTextView;

import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.ui.widget.EncryptKeyCompletionView;
import org.sufficientlysecure.keychain.ui.widget.KeySpinner;
import org.sufficientlysecure.keychain.util.Log;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class EncryptAsymmetricFragment extends Fragment implements EncryptActivityInterface.UpdateListener {
    ProviderHelper mProviderHelper;

    // view
    private KeySpinner mSign;
    private EncryptKeyCompletionView mEncryptKeyView;

    // model
    private EncryptActivityInterface mEncryptInterface;

    @Override
    public void onNotifyUpdate() {
        if (mSign != null) {
            mSign.setSelectedKeyId(mEncryptInterface.getSignatureKey());
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mEncryptInterface = (EncryptActivityInterface) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement EncryptActivityInterface");
        }
    }

    private void setSignatureKeyId(long signatureKeyId) {
        mEncryptInterface.setSignatureKey(signatureKeyId);
    }

    private void setEncryptionKeyIds(long[] encryptionKeyIds) {
        mEncryptInterface.setEncryptionKeys(encryptionKeyIds);
    }

    private void setEncryptionUserIds(String[] encryptionUserIds) {
        mEncryptInterface.setEncryptionUsers(encryptionUserIds);
    }

    /**
     * Inflate the layout for this fragment
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.encrypt_asymmetric_fragment, container, false);

        mSign = (KeySpinner) view.findViewById(R.id.sign);
        mSign.setOnKeyChangedListener(new KeySpinner.OnKeyChangedListener() {
            @Override
            public void onKeyChanged(long masterKeyId) {
                setSignatureKeyId(masterKeyId);
            }
        });
        mEncryptKeyView = (EncryptKeyCompletionView) view.findViewById(R.id.recipient_list);
        mEncryptKeyView.setThreshold(1); // Start working from first character

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mProviderHelper = new ProviderHelper(getActivity());

        // preselect keys given
        preselectKeys();

        mEncryptKeyView.setTokenListener(new TokenCompleteTextView.TokenListener() {
            @Override
            public void onTokenAdded(Object token) {
                if (token instanceof EncryptKeyCompletionView.EncryptionKey) {
                    updateEncryptionKeys();
                }
            }

            @Override
            public void onTokenRemoved(Object token) {
                if (token instanceof EncryptKeyCompletionView.EncryptionKey) {
                    updateEncryptionKeys();
                }
            }
        });
    }

    /**
     * If an Intent gives a signatureMasterKeyId and/or encryptionMasterKeyIds, preselect those!
     */
    private void preselectKeys() {
        // TODO all of this works under the assumption that the first suitable subkey is always used!
        // not sure if we need to distinguish between different subkeys here?
        long signatureKey = mEncryptInterface.getSignatureKey();
        if (signatureKey != Constants.key.none) {
            try {
                CachedPublicKeyRing keyring = mProviderHelper.getCachedPublicKeyRing(
                        KeyRings.buildUnifiedKeyRingUri(signatureKey));
                if (keyring.hasAnySecret()) {
                    setSignatureKeyId(keyring.getMasterKeyId());
                    mSign.setSelectedKeyId(mEncryptInterface.getSignatureKey());
                }
            } catch (PgpKeyNotFoundException e) {
                Log.e(Constants.TAG, "key not found!", e);
            }
        }

        long[] encryptionKeyIds = mEncryptInterface.getEncryptionKeys();
        if (encryptionKeyIds != null) {
            for (long preselectedId : encryptionKeyIds) {
                try {
                    CachedPublicKeyRing ring = mProviderHelper.getCachedPublicKeyRing(
                            KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(preselectedId));
                    mEncryptKeyView.addObject(mEncryptKeyView.new EncryptionKey(ring));
                } catch (PgpKeyNotFoundException e) {
                    Log.e(Constants.TAG, "key not found!", e);
                }
            }
            // This is to work-around a rendering bug in TokenCompleteTextView
            mEncryptKeyView.requestFocus();
            updateEncryptionKeys();
        }
    }

    private void updateEncryptionKeys() {
        List<Object> objects = mEncryptKeyView.getObjects();
        List<Long> keyIds = new ArrayList<>();
        List<String> userIds = new ArrayList<>();
        for (Object object : objects) {
            if (object instanceof EncryptKeyCompletionView.EncryptionKey) {
                keyIds.add(((EncryptKeyCompletionView.EncryptionKey) object).getKeyId());
                userIds.add(((EncryptKeyCompletionView.EncryptionKey) object).getUserId());
            }
        }
        long[] keyIdsArr = new long[keyIds.size()];
        Iterator<Long> iterator = keyIds.iterator();
        for (int i = 0; i < keyIds.size(); i++) {
            keyIdsArr[i] = iterator.next();
        }
        setEncryptionKeyIds(keyIdsArr);
        setEncryptionUserIds(userIds.toArray(new String[userIds.size()]));
    }
}