aboutsummaryrefslogtreecommitdiffstats
path: root/org_apg/src/org/apg/ui/widget/SectionView.java
blob: 2206991248b9612d3dfcb4b0cec68a5ef4af7df3 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apg.ui.widget;

import org.apg.Apg;
import org.apg.Id;
import org.apg.ui.widget.Editor.EditorListener;
import org.apg.util.Choice;
import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.PGPSecretKey;
import org.apg.R;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Vector;

public class SectionView extends LinearLayout implements OnClickListener, EditorListener, Runnable {
    private LayoutInflater mInflater;
    private View mAdd;
    private ViewGroup mEditors;
    private TextView mTitle;
    private int mType = 0;

    private Choice mNewKeyAlgorithmChoice;
    private int mNewKeySize;

    volatile private PGPSecretKey mNewKey;
    private ProgressDialog mProgressDialog;
    private Thread mRunningThread = null;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Bundle data = msg.getData();
            if (data != null) {
                boolean closeProgressDialog = data.getBoolean("closeProgressDialog");
                if (closeProgressDialog) {
                    if (mProgressDialog != null) {
                        mProgressDialog.dismiss();
                        mProgressDialog = null;
                    }
                }

                String error = data.getString(Apg.EXTRA_ERROR);
                if (error != null) {
                    Toast.makeText(getContext(),
                                   getContext().getString(R.string.errorMessage, error),
                                   Toast.LENGTH_SHORT).show();
                }

                boolean gotNewKey = data.getBoolean("gotNewKey");
                if (gotNewKey) {
                    KeyEditor view =
                        (KeyEditor) mInflater.inflate(R.layout.edit_key_key_item,
                                                      mEditors, false);
                    view.setEditorListener(SectionView.this);
                    boolean isMasterKey = (mEditors.getChildCount() == 0);
                    view.setValue(mNewKey, isMasterKey);
                    mEditors.addView(view);
                    SectionView.this.updateEditorsVisible();
                }
            }
        }
    };

    public SectionView(Context context) {
        super(context);
    }

    public SectionView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ViewGroup getEditors() {
        return mEditors;
    }

    public void setType(int type) {
        mType = type;
        switch (type) {
            case Id.type.user_id: {
                mTitle.setText(R.string.section_userIds);
                break;
            }

            case Id.type.key: {
                mTitle.setText(R.string.section_keys);
                break;
            }

            default: {
                break;
            }
        }
    }

    /** {@inheritDoc} */
    @Override
    protected void onFinishInflate() {
        mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        setDrawingCacheEnabled(true);
        setAlwaysDrawnWithCacheEnabled(true);

        mAdd = findViewById(R.id.header);
        mAdd.setOnClickListener(this);

        mEditors = (ViewGroup) findViewById(R.id.editors);
        mTitle = (TextView) findViewById(R.id.title);

        updateEditorsVisible();
        super.onFinishInflate();
    }

    /** {@inheritDoc} */
    public void onDeleted(Editor editor) {
        this.updateEditorsVisible();
    }

    protected void updateEditorsVisible() {
        final boolean hasChildren = mEditors.getChildCount() > 0;
        mEditors.setVisibility(hasChildren ? View.VISIBLE : View.GONE);
    }

    /** {@inheritDoc} */
    public void onClick(View v) {
        switch (mType) {
            case Id.type.user_id: {
                UserIdEditor view =
                        (UserIdEditor) mInflater.inflate(R.layout.edit_key_user_id_item,
                                                         mEditors, false);
                view.setEditorListener(this);
                if (mEditors.getChildCount() == 0) {
                    view.setIsMainUserId(true);
                }
                mEditors.addView(view);
                break;
            }

            case Id.type.key: {
                AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());

                View view = mInflater.inflate(R.layout.create_key, null);
                dialog.setView(view);
                dialog.setTitle(R.string.title_createKey);
                dialog.setMessage(R.string.keyCreationElGamalInfo);

                boolean wouldBeMasterKey = (mEditors.getChildCount() == 0);

                final Spinner algorithm = (Spinner) view.findViewById(R.id.algorithm);
                Vector<Choice> choices = new Vector<Choice>();
                choices.add(new Choice(Id.choice.algorithm.dsa,
                                       getResources().getString(R.string.dsa)));
                if (!wouldBeMasterKey) {
                    choices.add(new Choice(Id.choice.algorithm.elgamal,
                                           getResources().getString(R.string.elgamal)));
                }

                choices.add(new Choice(Id.choice.algorithm.rsa,
                                       getResources().getString(R.string.rsa)));

                ArrayAdapter<Choice> adapter =
                        new ArrayAdapter<Choice>(getContext(),
                                                 android.R.layout.simple_spinner_item,
                                                 choices);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                algorithm.setAdapter(adapter);
                // make RSA the default
                for (int i = 0; i < choices.size(); ++i) {
                    if (choices.get(i).getId() == Id.choice.algorithm.rsa) {
                        algorithm.setSelection(i);
                        break;
                    }
                }

                final EditText keySize = (EditText) view.findViewById(R.id.size);

                dialog.setPositiveButton(android.R.string.ok,
                                         new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface di, int id) {
                        di.dismiss();
                        try {
                            mNewKeySize = Integer.parseInt("" + keySize.getText());
                        } catch (NumberFormatException e) {
                            mNewKeySize = 0;
                        }

                        mNewKeyAlgorithmChoice = (Choice) algorithm.getSelectedItem();
                        createKey();
                    }
                });

                dialog.setCancelable(true);
                dialog.setNegativeButton(android.R.string.cancel,
                                         new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface di, int id) {
                        di.dismiss();
                    }
                });

                dialog.create().show();
                break;
            }

            default: {
                break;
            }
        }
        this.updateEditorsVisible();
    }

    public void setUserIds(Vector<String> list) {
        if (mType != Id.type.user_id) {
            return;
        }

        mEditors.removeAllViews();
        for (String userId : list) {
            UserIdEditor view =
                (UserIdEditor) mInflater.inflate(R.layout.edit_key_user_id_item, mEditors, false);
            view.setEditorListener(this);
            view.setValue(userId);
            if (mEditors.getChildCount() == 0) {
                view.setIsMainUserId(true);
            }
            mEditors.addView(view);
        }

        this.updateEditorsVisible();
    }

    public void setKeys(Vector<PGPSecretKey> list) {
        if (mType != Id.type.key) {
            return;
        }

        mEditors.removeAllViews();
        for (PGPSecretKey key : list) {
            KeyEditor view =
                (KeyEditor) mInflater.inflate(R.layout.edit_key_key_item, mEditors, false);
            view.setEditorListener(this);
            boolean isMasterKey = (mEditors.getChildCount() == 0);
            view.setValue(key, isMasterKey);
            mEditors.addView(view);
        }

        this.updateEditorsVisible();
    }

    private void createKey() {
        mProgressDialog = new ProgressDialog(getContext());
        mProgressDialog.setMessage(getContext().getString(R.string.progress_generating));
        mProgressDialog.setCancelable(false);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.show();
        mRunningThread = new Thread(this);
        mRunningThread.start();
    }

    public void run() {
        String error = null;
        try {
            PGPSecretKey masterKey = null;
            String passPhrase;
            if (mEditors.getChildCount() > 0) {
                masterKey = ((KeyEditor) mEditors.getChildAt(0)).getValue();
                passPhrase = Apg.getCachedPassPhrase(masterKey.getKeyID());
            } else {
                passPhrase = "";
            }
            mNewKey = Apg.createKey(getContext(),
                                    mNewKeyAlgorithmChoice.getId(),
                                    mNewKeySize, passPhrase,
                                    masterKey);
        } catch (NoSuchProviderException e) {
            error = "" + e;
        } catch (NoSuchAlgorithmException e) {
            error = "" + e;
        } catch (PGPException e) {
            error = "" + e;
        } catch (InvalidParameterException e) {
            error = "" + e;
        } catch (InvalidAlgorithmParameterException e) {
            error = "" + e;
        } catch (Apg.GeneralException e) {
            error = "" + e;
        }

        Message message = new Message();
        Bundle data = new Bundle();
        data.putBoolean("closeProgressDialog", true);
        if (error != null) {
            data.putString(Apg.EXTRA_ERROR, error);
        } else {
            data.putBoolean("gotNewKey", true);
        }
        message.setData(data);
        mHandler.sendMessage(message);
    }
}