aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DeleteKeyDialogActivity.java
blob: 4782591332d9b15ec3f5a709dffb67734e8aabdc (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * Copyright (C) 2013-2015 Dominik Schürmann <dominik@dominikschuermann.de>
 * Copyright (C) 2015 Vincent Breitmoser <v.breitmoser@mugenguild.com>
 * Copyright (C) 2015 Adithya Abraham Philip <adithyaphilip@gmail.com>
 *
 * 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.support.v7.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;

import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.DeleteResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.operations.results.RevokeResult;
import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.service.DeleteKeyringParcel;
import org.sufficientlysecure.keychain.service.RevokeKeyringParcel;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper;
import org.sufficientlysecure.keychain.ui.dialog.CustomAlertDialogBuilder;
import org.sufficientlysecure.keychain.ui.util.ThemeChanger;
import org.sufficientlysecure.keychain.util.Log;

import java.util.Date;
import java.util.HashMap;

public class DeleteKeyDialogActivity extends FragmentActivity {
    public static final String EXTRA_DELETE_MASTER_KEY_IDS = "extra_delete_master_key_ids";
    public static final String EXTRA_HAS_SECRET = "extra_has_secret";
    public static final String EXTRA_KEYSERVER = "extra_keyserver";

    private CryptoOperationHelper<DeleteKeyringParcel, DeleteResult> mDeleteOpHelper;
    private CryptoOperationHelper<RevokeKeyringParcel, RevokeResult> mRevokeOpHelper;

    private long[] mMasterKeyIds;
    private boolean mHasSecret;

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

        mDeleteOpHelper = new CryptoOperationHelper<>(1, DeleteKeyDialogActivity.this,
                getDeletionCallback(), R.string.progress_deleting);

        mRevokeOpHelper = new CryptoOperationHelper<>(2, this,
                getRevocationCallback(), R.string.progress_revoking_uploading);

        mMasterKeyIds = getIntent().getLongArrayExtra(EXTRA_DELETE_MASTER_KEY_IDS);
        mHasSecret = getIntent().getBooleanExtra(EXTRA_HAS_SECRET, false);

        if (mMasterKeyIds.length > 1 && mHasSecret) {
            // secret keys can only be deleted individually
            OperationResult.OperationLog log = new OperationResult.OperationLog();
            log.add(OperationResult.LogType.MSG_DEL_ERROR_MULTI_SECRET, 0);
            returnResult(new DeleteResult(OperationResult.RESULT_ERROR, log, 0,
                    mMasterKeyIds.length));
        }

        if (mMasterKeyIds.length == 1 && mHasSecret) {
            // if mMasterKeyIds.length == 0 we let the DeleteOperation respond
            try {
                HashMap<String, Object> data = new ProviderHelper(this).getUnifiedData(
                        mMasterKeyIds[0], new String[]{
                                KeychainContract.KeyRings.USER_ID,
                                KeychainContract.KeyRings.IS_REVOKED
                        }, new int[]{
                                ProviderHelper.FIELD_TYPE_STRING,
                                ProviderHelper.FIELD_TYPE_INTEGER
                        }
                );

                String name;
                KeyRing.UserId mainUserId = KeyRing.splitUserId(
                        (String) data.get(KeychainContract.KeyRings.USER_ID));
                if (mainUserId.name != null) {
                    name = mainUserId.name;
                } else {
                    name = getString(R.string.user_id_no_name);
                }

                if ((long) data.get(KeychainContract.KeyRings.IS_REVOKED) > 0) {
                    showNormalDeleteDialog();
                } else {
                    showRevokeDeleteDialog(name);
                }
            } catch (ProviderHelper.NotFoundException e) {
                Log.e(Constants.TAG,
                        "Secret key to delete not found at DeleteKeyDialogActivity for "
                                + mMasterKeyIds[0], e);
                finish();
            }
        } else {
            showNormalDeleteDialog();
        }
    }

    private void showNormalDeleteDialog() {

        DeleteKeyDialogFragment deleteKeyDialogFragment
                = DeleteKeyDialogFragment.newInstance(mMasterKeyIds, mHasSecret);

        deleteKeyDialogFragment.show(getSupportFragmentManager(), "deleteKeyDialog");

    }

    private void showRevokeDeleteDialog(String keyname) {

        RevokeDeleteDialogFragment fragment = RevokeDeleteDialogFragment.newInstance(keyname);
        fragment.show(getSupportFragmentManager(), "deleteRevokeDialog");
    }

    private void startRevocationOperation() {
        mRevokeOpHelper.cryptoOperation(new CryptoInputParcel(new Date(), false));
    }

    private void startDeletionOperation() {
        mDeleteOpHelper.cryptoOperation();
    }

    private CryptoOperationHelper.Callback<RevokeKeyringParcel, RevokeResult> getRevocationCallback() {

        return new CryptoOperationHelper.Callback<RevokeKeyringParcel, RevokeResult>() {
            @Override
            public RevokeKeyringParcel createOperationInput() {
                return new RevokeKeyringParcel(mMasterKeyIds[0], true,
                        getIntent().getStringExtra(EXTRA_KEYSERVER));
            }

            @Override
            public void onCryptoOperationSuccess(RevokeResult result) {
                returnResult(result);
            }

            @Override
            public void onCryptoOperationCancelled() {
                setResult(RESULT_CANCELED);
                finish();
            }

            @Override
            public void onCryptoOperationError(RevokeResult result) {
                returnResult(result);
            }

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

    private CryptoOperationHelper.Callback<DeleteKeyringParcel, DeleteResult> getDeletionCallback() {

        return new CryptoOperationHelper.Callback<DeleteKeyringParcel, DeleteResult>() {
            @Override
            public DeleteKeyringParcel createOperationInput() {
                return new DeleteKeyringParcel(mMasterKeyIds, mHasSecret);
            }

            @Override
            public void onCryptoOperationSuccess(DeleteResult result) {
                returnResult(result);
            }

            @Override
            public void onCryptoOperationCancelled() {
                setResult(RESULT_CANCELED);
                finish();
            }

            @Override
            public void onCryptoOperationError(DeleteResult result) {
                returnResult(result);
            }

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

    private void returnResult(OperationResult result) {
        Intent intent = new Intent();
        intent.putExtra(OperationResult.EXTRA_RESULT, result);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        mDeleteOpHelper.handleActivityResult(requestCode, resultCode, data);
        mRevokeOpHelper.handleActivityResult(requestCode, resultCode, data);
    }

    public static class DeleteKeyDialogFragment extends DialogFragment {

        private static final String ARG_DELETE_MASTER_KEY_IDS = "delete_master_key_ids";
        private static final String ARG_HAS_SECRET = "has_secret";

        private TextView mMainMessage;
        private View mInflateView;

        /**
         * Creates new instance of this delete file dialog fragment
         */
        public static DeleteKeyDialogFragment newInstance(long[] masterKeyIds, boolean hasSecret) {
            DeleteKeyDialogFragment frag = new DeleteKeyDialogFragment();
            Bundle args = new Bundle();

            args.putLongArray(ARG_DELETE_MASTER_KEY_IDS, masterKeyIds);
            args.putBoolean(ARG_HAS_SECRET, hasSecret);

            frag.setArguments(args);

            return frag;
        }

        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final FragmentActivity activity = getActivity();

            final long[] masterKeyIds = getArguments().getLongArray(ARG_DELETE_MASTER_KEY_IDS);
            final boolean hasSecret = getArguments().getBoolean(ARG_HAS_SECRET);

            ContextThemeWrapper theme = ThemeChanger.getDialogThemeWrapper(activity);

            CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(theme);

            // Setup custom View to display in AlertDialog
            LayoutInflater inflater = LayoutInflater.from(theme);
            mInflateView = inflater.inflate(R.layout.view_key_delete_fragment, null);
            builder.setView(mInflateView);

            mMainMessage = (TextView) mInflateView.findViewById(R.id.mainMessage);

            // If only a single key has been selected
            if (masterKeyIds.length == 1) {
                long masterKeyId = masterKeyIds[0];

                try {
                    HashMap<String, Object> data = new ProviderHelper(activity).getUnifiedData(
                            masterKeyId, new String[]{
                                    KeychainContract.KeyRings.USER_ID,
                                    KeychainContract.KeyRings.HAS_ANY_SECRET
                            }, new int[]{
                                    ProviderHelper.FIELD_TYPE_STRING,
                                    ProviderHelper.FIELD_TYPE_INTEGER
                            }
                    );
                    String name;
                    KeyRing.UserId mainUserId = KeyRing.splitUserId((String) data.get(KeychainContract.KeyRings.USER_ID));
                    if (mainUserId.name != null) {
                        name = mainUserId.name;
                    } else {
                        name = getString(R.string.user_id_no_name);
                    }

                    if (hasSecret) {
                        // show title only for secret key deletions,
                        // see http://www.google.com/design/spec/components/dialogs.html#dialogs-behavior
                        builder.setTitle(getString(R.string.title_delete_secret_key, name));
                        mMainMessage.setText(getString(R.string.secret_key_deletion_confirmation, name));
                    } else {
                        mMainMessage.setText(getString(R.string.public_key_deletetion_confirmation, name));
                    }
                } catch (ProviderHelper.NotFoundException e) {
                    dismiss();
                    return null;
                }
            } else {
                mMainMessage.setText(R.string.key_deletion_confirmation_multi);
            }

            builder.setPositiveButton(R.string.btn_delete, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    ((DeleteKeyDialogActivity) getActivity()).startDeletionOperation();
                }
            });

            builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            return builder.show();
        }

        @Override
        public void onCancel(DialogInterface dialog) {
            super.onCancel(dialog);
            getActivity().setResult(RESULT_CANCELED);
            getActivity().finish();
        }
    }

    public static class RevokeDeleteDialogFragment extends DialogFragment {

        public static final String ARG_KEY_NAME = "arg_key_name";

        public static RevokeDeleteDialogFragment newInstance(String keyName) {
            Bundle args = new Bundle();
            args.putString(ARG_KEY_NAME, keyName);
            RevokeDeleteDialogFragment frag = new RevokeDeleteDialogFragment();
            frag.setArguments(args);
            return frag;
        }

        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Activity activity = getActivity();

            final String CHOICE_REVOKE = getString(R.string.del_rev_dialog_choice_rev_upload);
            final String CHOICE_DELETE = getString(R.string.del_rev_dialog_choice_delete);

            ContextThemeWrapper theme = ThemeChanger.getDialogThemeWrapper(activity);

            CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(theme);
            builder.setTitle(getString(R.string.del_rev_dialog_title,
                    getArguments().get(ARG_KEY_NAME)));

            LayoutInflater inflater = LayoutInflater.from(theme);
            View view = inflater.inflate(R.layout.del_rev_dialog, null);
            builder.setView(view);

            final Spinner spinner = (Spinner) view.findViewById(R.id.spinner);

            builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            builder.setPositiveButton(R.string.del_rev_dialog_btn_revoke,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            String choice = spinner.getSelectedItem().toString();
                            if (choice.equals(CHOICE_REVOKE)) {
                                ((DeleteKeyDialogActivity) activity)
                                        .startRevocationOperation();
                            } else if (choice.equals(CHOICE_DELETE)) {
                                ((DeleteKeyDialogActivity) activity)
                                        .showNormalDeleteDialog();
                            } else {
                                throw new AssertionError(
                                        "Unsupported delete type in RevokeDeleteDialogFragment");
                            }
                        }
                    });

            final AlertDialog alertDialog = builder.show();

            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

                    String choice = parent.getItemAtPosition(pos).toString();

                    if (choice.equals(CHOICE_REVOKE)) {
                        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
                                .setText(R.string.del_rev_dialog_btn_revoke);
                    } else if (choice.equals(CHOICE_DELETE)) {
                        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
                                .setText(R.string.del_rev_dialog_btn_delete);
                    } else {
                        throw new AssertionError(
                                "Unsupported delete type in RevokeDeleteDialogFragment");
                    }
                }

                public void onNothingSelected(AdapterView<?> parent) {
                }
            });

            return alertDialog;
        }

        @Override
        public void onCancel(DialogInterface dialog) {
            super.onCancel(dialog);
            getActivity().setResult(RESULT_CANCELED);
            getActivity().finish();
        }
    }

}