aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java
blob: 255290de38dbed63ed8caf3edb7206fd29dd08e8 (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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
 * Copyright (C) 2012-2014 Dominik Schürmann <dominik@dominikschuermann.de>
 * Copyright (C) 2011 Senecaso
 *
 * 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.sufficientlysecure.keychain.ui;

import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.OtherHelper;
import org.sufficientlysecure.keychain.helper.Preferences;
import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
import org.sufficientlysecure.keychain.service.KeychainIntentService;
import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler;
import org.sufficientlysecure.keychain.service.OperationResults.ImportKeyResult;
import org.sufficientlysecure.keychain.ui.adapter.PagerTabStripAdapter;
import org.sufficientlysecure.keychain.ui.widget.SlidingTabLayout;
import org.sufficientlysecure.keychain.util.FileImportCache;
import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.Notify;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

public class ImportKeysActivity extends ActionBarActivity {
    public static final String ACTION_IMPORT_KEY = Constants.INTENT_PREFIX + "IMPORT_KEY";
    public static final String ACTION_IMPORT_KEY_FROM_QR_CODE = Constants.INTENT_PREFIX
            + "IMPORT_KEY_FROM_QR_CODE";
    public static final String ACTION_IMPORT_KEY_FROM_KEYSERVER = Constants.INTENT_PREFIX
            + "IMPORT_KEY_FROM_KEYSERVER";
    public static final String ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT =
            Constants.INTENT_PREFIX + "IMPORT_KEY_FROM_KEY_SERVER_AND_RETURN_RESULT";
    public static final String ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN = Constants.INTENT_PREFIX
            + "IMPORT_KEY_FROM_KEY_SERVER_AND_RETURN";
    public static final String ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN = Constants.INTENT_PREFIX
            + "IMPORT_KEY_FROM_FILE_AND_RETURN";
    public static final String ACTION_IMPORT_KEY_FROM_KEYBASE = Constants.INTENT_PREFIX
            + "IMPORT_KEY_FROM_KEYBASE";

    // Actions for internal use only:
    public static final String ACTION_IMPORT_KEY_FROM_FILE = Constants.INTENT_PREFIX
            + "IMPORT_KEY_FROM_FILE";
    public static final String ACTION_IMPORT_KEY_FROM_NFC = Constants.INTENT_PREFIX
            + "IMPORT_KEY_FROM_NFC";

    public static final String EXTRA_RESULT = "result";

    // only used by ACTION_IMPORT_KEY
    public static final String EXTRA_KEY_BYTES = "key_bytes";

    // only used by ACTION_IMPORT_KEY_FROM_KEYSERVER
    public static final String EXTRA_QUERY = "query";
    public static final String EXTRA_KEY_ID = "key_id";
    public static final String EXTRA_FINGERPRINT = "fingerprint";

    // only used by ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN when used from OpenPgpService
    public static final String EXTRA_PENDING_INTENT_DATA = "data";
    private Intent mPendingIntentData;

    // view
    private ImportKeysListFragment mListFragment;
    private View mImportButton;
    private ViewPager mViewPager;
    private SlidingTabLayout mSlidingTabLayout;
    private PagerTabStripAdapter mTabsAdapter;

    public static final int VIEW_PAGER_HEIGHT = 64; // dp

    private static final int ALL_TABS = -1;
    private static final int TAB_KEYSERVER = 0;
    private static final int TAB_QR_CODE = 1;
    private static final int TAB_FILE = 2;
    private static final int TAB_KEYBASE = 3;

    private int mSwitchToTab = TAB_KEYSERVER;

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

        setContentView(R.layout.import_keys_activity);

        mViewPager = (ViewPager) findViewById(R.id.import_pager);
        mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.import_sliding_tab_layout);

        mImportButton = findViewById(R.id.import_import);
        mImportButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                importKeys();
            }
        });

        handleActions(savedInstanceState, getIntent());
    }

    protected void handleActions(Bundle savedInstanceState, Intent intent) {
        String action = intent.getAction();
        Bundle extras = intent.getExtras();
        Uri dataUri = intent.getData();
        String scheme = intent.getScheme();

        if (extras == null) {
            extras = new Bundle();
        }

        if (Intent.ACTION_VIEW.equals(action)) {
            // Android's Action when opening file associated to Keychain (see AndroidManifest.xml)
            // delegate action to ACTION_IMPORT_KEY
            action = ACTION_IMPORT_KEY;
        }

        if (scheme != null && scheme.toLowerCase(Locale.ENGLISH).equals(Constants.FINGERPRINT_SCHEME)) {
            /* Scanning a fingerprint directly with Barcode Scanner */
            // delegate action to ACTION_IMPORT_KEY_FROM_KEYSERVER
            String fingerprint = getFingerprintFromUri(dataUri);
            action = ACTION_IMPORT_KEY_FROM_KEYSERVER;
            extras.putString(EXTRA_FINGERPRINT, fingerprint);
        }

        Bundle serverBundle = null;
        int showTabOnly = ALL_TABS;
        if (ACTION_IMPORT_KEY.equals(action)) {
            /* Keychain's own Actions */

            // display file fragment
            mViewPager.setCurrentItem(TAB_FILE);

            if (dataUri != null) {
                // action: directly load data
                startListFragment(savedInstanceState, null, dataUri, null);
            } else if (extras.containsKey(EXTRA_KEY_BYTES)) {
                byte[] importData = extras.getByteArray(EXTRA_KEY_BYTES);

                // action: directly load data
                startListFragment(savedInstanceState, importData, null, null);
            }
        } else if (ACTION_IMPORT_KEY_FROM_KEYSERVER.equals(action)
                || ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN.equals(action)
                || ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT.equals(action)) {

            // only used for OpenPgpService
            if (extras.containsKey(EXTRA_PENDING_INTENT_DATA)) {
                mPendingIntentData = extras.getParcelable(EXTRA_PENDING_INTENT_DATA);
            }
            if (extras.containsKey(EXTRA_QUERY) || extras.containsKey(EXTRA_KEY_ID)) {
                /* simple search based on query or key id */

                String query = null;
                if (extras.containsKey(EXTRA_QUERY)) {
                    query = extras.getString(EXTRA_QUERY);
                } else if (extras.containsKey(EXTRA_KEY_ID)) {
                    long keyId = extras.getLong(EXTRA_KEY_ID, 0);
                    if (keyId != 0) {
                        query = PgpKeyHelper.convertKeyIdToHex(keyId);
                    }
                }

                if (query != null && query.length() > 0) {
                    // display keyserver fragment with query
                    serverBundle = new Bundle();
                    serverBundle.putString(ImportKeysServerFragment.ARG_QUERY, query);
                    mSwitchToTab = TAB_KEYSERVER;

                    // action: search immediately
                    startListFragment(savedInstanceState, null, null, query);
                } else {
                    Log.e(Constants.TAG, "Query is empty!");
                    return;
                }
            } else if (extras.containsKey(EXTRA_FINGERPRINT)) {
                /*
                 * search based on fingerprint, here we can enforce a check in the end
                 * if the right key has been downloaded
                 */

                String fingerprint = extras.getString(EXTRA_FINGERPRINT);
                if (isFingerprintValid(fingerprint)) {
                    String query = "0x" + fingerprint;

                    // display keyserver fragment with query
                    serverBundle = new Bundle();
                    serverBundle.putString(ImportKeysServerFragment.ARG_QUERY, query);
                    serverBundle.putBoolean(ImportKeysServerFragment.ARG_DISABLE_QUERY_EDIT, true);
                    // display server tab only
                    showTabOnly = TAB_KEYSERVER;
                    mSwitchToTab = TAB_KEYSERVER;

                    // action: search immediately
                    startListFragment(savedInstanceState, null, null, query);
                }
            } else {
                Log.e(Constants.TAG,
                        "IMPORT_KEY_FROM_KEYSERVER action needs to contain the 'query', 'key_id', or " +
                                "'fingerprint' extra!"
                );
                return;
            }
        } else if (ACTION_IMPORT_KEY_FROM_FILE.equals(action)) {
            // NOTE: this only displays the appropriate fragment, no actions are taken
            mSwitchToTab = TAB_FILE;

            // no immediate actions!
            startListFragment(savedInstanceState, null, null, null);
        } else if (ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN.equals(action)) {
            // NOTE: this only displays the appropriate fragment, no actions are taken
            mSwitchToTab = TAB_FILE;
            // display file tab only
            showTabOnly = TAB_FILE;

            // no immediate actions!
            startListFragment(savedInstanceState, null, null, null);
        } else if (ACTION_IMPORT_KEY_FROM_QR_CODE.equals(action)) {
            // also exposed in AndroidManifest

            // NOTE: this only displays the appropriate fragment, no actions are taken
            mSwitchToTab = TAB_QR_CODE;

            // no immediate actions!
            startListFragment(savedInstanceState, null, null, null);
        } else if (ACTION_IMPORT_KEY_FROM_NFC.equals(action)) {

            // NOTE: this only displays the appropriate fragment, no actions are taken
            mSwitchToTab = TAB_QR_CODE;

            // no immediate actions!
            startListFragment(savedInstanceState, null, null, null);
        } else if (ACTION_IMPORT_KEY_FROM_KEYBASE.equals(action)) {
            // NOTE: this only displays the appropriate fragment, no actions are taken
            mSwitchToTab = TAB_KEYBASE;

            // no immediate actions!
            startListFragment(savedInstanceState, null, null, null);
        } else {
            startListFragment(savedInstanceState, null, null, null);
        }

        initTabs(serverBundle, showTabOnly);
    }

    private void initTabs(Bundle serverBundle, int showTabOnly) {
        mTabsAdapter = new PagerTabStripAdapter(this);
        mViewPager.setAdapter(mTabsAdapter);
        mSlidingTabLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // resize view pager back to 64 if keyserver settings have been collapsed
                if (getViewPagerHeight() > VIEW_PAGER_HEIGHT) {
                    resizeViewPager(VIEW_PAGER_HEIGHT);
                }
            }

            @Override
            public void onPageSelected(int position) {
                // cancel loader and clear list
                mListFragment.destroyLoader();
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        switch (showTabOnly) {
            case ALL_TABS:
                // show all tabs
                mTabsAdapter.addTab(ImportKeysServerFragment.class,
                        serverBundle, getString(R.string.import_tab_keyserver));
                mTabsAdapter.addTab(ImportKeysQrCodeFragment.class,
                        null, getString(R.string.import_tab_qr_code));
                mTabsAdapter.addTab(ImportKeysFileFragment.class,
                        null, getString(R.string.import_tab_direct));
                mTabsAdapter.addTab(ImportKeysKeybaseFragment.class,
                        null, getString(R.string.import_tab_keybase));
                break;
            case TAB_KEYSERVER:
                mTabsAdapter.addTab(ImportKeysServerFragment.class,
                        serverBundle, getString(R.string.import_tab_keyserver));
                break;
            case TAB_QR_CODE:
                mTabsAdapter.addTab(ImportKeysQrCodeFragment.class,
                        null, getString(R.string.import_tab_qr_code));
                break;
            case TAB_FILE:
                mTabsAdapter.addTab(ImportKeysFileFragment.class,
                        null, getString(R.string.import_tab_direct));
                break;
            case TAB_KEYBASE:
                mTabsAdapter.addTab(ImportKeysKeybaseFragment.class,
                        null, getString(R.string.import_tab_keybase));
                break;
        }

        // update layout after operations
        mSlidingTabLayout.setViewPager(mViewPager);

        mViewPager.setCurrentItem(mSwitchToTab);
    }

    private void startListFragment(Bundle savedInstanceState, byte[] bytes, Uri dataUri, String serverQuery) {
        // However, if we're being restored from a previous state,
        // then we don't need to do anything and should return or else
        // we could end up with overlapping fragments.
        if (savedInstanceState != null) {
            return;
        }

        // Create an instance of the fragment
        mListFragment = ImportKeysListFragment.newInstance(bytes, dataUri, serverQuery);

        // Add the fragment to the 'fragment_container' FrameLayout
        // NOTE: We use commitAllowingStateLoss() to prevent weird crashes!
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.import_keys_list_container, mListFragment)
                .commitAllowingStateLoss();
        // do it immediately!
        getSupportFragmentManager().executePendingTransactions();
    }

    public void resizeViewPager(int dp) {
        ViewGroup.LayoutParams params = mViewPager.getLayoutParams();
        params.height = OtherHelper.dpToPx(this, dp);
        // update layout after operations
        mSlidingTabLayout.setViewPager(mViewPager);
    }

    public int getViewPagerHeight() {
        ViewGroup.LayoutParams params = mViewPager.getLayoutParams();
        return OtherHelper.pxToDp(this, params.height);
    }

    private String getFingerprintFromUri(Uri dataUri) {
        String fingerprint = dataUri.toString().split(":")[1].toLowerCase(Locale.ENGLISH);
        Log.d(Constants.TAG, "fingerprint: " + fingerprint);
        return fingerprint;
    }

    public void loadFromFingerprintUri(Uri dataUri) {
        String query = "0x" + getFingerprintFromUri(dataUri);

        // setCurrentItem does not work directly after onResume (from qr code scanner)
        // see http://stackoverflow.com/q/19316729
        // so, reset adapter completely!
        if (mViewPager.getAdapter() != null)
            mViewPager.setAdapter(null);
        mViewPager.setAdapter(mTabsAdapter);
        mViewPager.setCurrentItem(TAB_KEYSERVER);

        ImportKeysServerFragment f = (ImportKeysServerFragment)
                getActiveFragment(mViewPager, TAB_KEYSERVER);

        // ask favorite keyserver
        String keyserver = Preferences.getPreferences(ImportKeysActivity.this).getKeyServers()[0];

        // set fields of ImportKeysServerFragment
        f.setQueryAndKeyserver(query, keyserver);
        // search directly
        loadCallback(new ImportKeysListFragment.KeyserverLoaderState(query, keyserver));
    }

    // http://stackoverflow.com/a/9293207
    public Fragment getActiveFragment(ViewPager container, int position) {
        String name = makeFragmentName(container.getId(), position);
        return getSupportFragmentManager().findFragmentByTag(name);
    }

    // http://stackoverflow.com/a/9293207
    private static String makeFragmentName(int viewId, int index) {
        return "android:switcher:" + viewId + ":" + index;
    }

    private boolean isFingerprintValid(String fingerprint) {
        if (fingerprint == null || fingerprint.length() < 40) {
            Notify.showNotify(this, R.string.import_qr_code_too_short_fingerprint, Notify.Style.ERROR);
            return false;
        } else {
            return true;
        }
    }

    /**
     * Scroll ViewPager left and right
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean result = super.onTouchEvent(event);

        if (!result) {
            try {
                mViewPager.onTouchEvent(event);
            } catch (IllegalArgumentException e) {
                // workaround for Android bug?
                // http://stackoverflow.com/q/16459196
                Log.d(Constants.TAG, "Workaround: Catched IllegalArgumentException");
            }
        }

        return result;
    }

    public void loadCallback(ImportKeysListFragment.LoaderState loaderState) {
        mListFragment.loadNew(loaderState);
    }

    /**
     * Import keys with mImportData
     */
    public void importKeys() {
        // Message is received after importing is done in KeychainIntentService
        KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(
                this,
                getString(R.string.progress_importing),
                ProgressDialog.STYLE_HORIZONTAL) {
            public void handleMessage(Message message) {
                // handle messages by standard KeychainIntentServiceHandler first
                super.handleMessage(message);

                if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) {
                    // get returned data bundle
                    Bundle returnData = message.getData();
                    if (returnData == null) {
                        return;
                    }
                    final ImportKeyResult result =
                            returnData.getParcelable(KeychainIntentService.RESULT_IMPORT);
                    if (result == null) {
                        return;
                    }

                    if (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT.equals(getIntent().getAction())) {
                        Intent intent = new Intent();
                        intent.putExtra(ImportKeyResult.EXTRA_RESULT, result);
                        ImportKeysActivity.this.setResult(RESULT_OK, intent);
                        ImportKeysActivity.this.finish();
                        return;
                    }
                    if (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN.equals(getIntent().getAction())) {
                        ImportKeysActivity.this.setResult(RESULT_OK, mPendingIntentData);
                        ImportKeysActivity.this.finish();
                        return;
                    }
                    if (ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN.equals(getIntent().getAction())) {
                        ImportKeysActivity.this.setResult(RESULT_OK);
                        ImportKeysActivity.this.finish();
                        return;
                    }

                    result.createNotify(ImportKeysActivity.this).show();
                }
            }
        };

        ImportKeysListFragment.LoaderState ls = mListFragment.getLoaderState();
        if (ls instanceof ImportKeysListFragment.BytesLoaderState) {
            Log.d(Constants.TAG, "importKeys started");

            // Send all information needed to service to import key in other thread
            Intent intent = new Intent(this, KeychainIntentService.class);

            intent.setAction(KeychainIntentService.ACTION_IMPORT_KEYRING);

            // fill values for this action
            Bundle data = new Bundle();

            // get DATA from selected key entries
            ArrayList<ParcelableKeyRing> selectedEntries = mListFragment.getSelectedData();

            // instead of given the entries by Intent extra, cache them into a file
            // to prevent Java Binder problems on heavy imports
            // read FileImportCache for more info.
            try {
                FileImportCache<ParcelableKeyRing> cache = new FileImportCache<ParcelableKeyRing>(this);
                cache.writeCache(selectedEntries);

                intent.putExtra(KeychainIntentService.EXTRA_DATA, data);

                // Create a new Messenger for the communication back
                Messenger messenger = new Messenger(saveHandler);
                intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger);

                // show progress dialog
                saveHandler.showProgressDialog(this);

                // start service with intent
                startService(intent);
            } catch (IOException e) {
                Log.e(Constants.TAG, "Problem writing cache file", e);
                Notify.showNotify(this, "Problem writing cache file!", Notify.Style.ERROR);
            }
        } else if (ls instanceof ImportKeysListFragment.KeyserverLoaderState) {
            ImportKeysListFragment.KeyserverLoaderState sls = (ImportKeysListFragment.KeyserverLoaderState) ls;

            // Send all information needed to service to query keys in other thread
            Intent intent = new Intent(this, KeychainIntentService.class);

            intent.setAction(KeychainIntentService.ACTION_DOWNLOAD_AND_IMPORT_KEYS);

            // fill values for this action
            Bundle data = new Bundle();

            data.putString(KeychainIntentService.DOWNLOAD_KEY_SERVER, sls.keyserver);

            // get selected key entries
            ArrayList<ImportKeysListEntry> selectedEntries = mListFragment.getSelectedEntries();
            data.putParcelableArrayList(KeychainIntentService.DOWNLOAD_KEY_LIST, selectedEntries);

            intent.putExtra(KeychainIntentService.EXTRA_DATA, data);

            // Create a new Messenger for the communication back
            Messenger messenger = new Messenger(saveHandler);
            intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger);

            // show progress dialog
            saveHandler.showProgressDialog(this);

            // start service with intent
            startService(intent);
        } else if (ls instanceof ImportKeysListFragment.KeybaseLoaderState) {
            // Send all information needed to service to query keys in other thread
            Intent intent = new Intent(this, KeychainIntentService.class);

            intent.setAction(KeychainIntentService.ACTION_IMPORT_KEYBASE_KEYS);

            // fill values for this action
            Bundle data = new Bundle();

            // get selected key entries
            ArrayList<ImportKeysListEntry> selectedEntries = mListFragment.getSelectedEntries();
            data.putParcelableArrayList(KeychainIntentService.DOWNLOAD_KEY_LIST, selectedEntries);

            intent.putExtra(KeychainIntentService.EXTRA_DATA, data);

            // Create a new Messenger for the communication back
            Messenger messenger = new Messenger(saveHandler);
            intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger);

            // show progress dialog
            saveHandler.showProgressDialog(this);

            // start service with intent
            startService(intent);

        } else {
            Notify.showNotify(this, R.string.error_nothing_import, Notify.Style.ERROR);
        }
    }

    /**
     * NFC
     */
    @Override
    public void onResume() {
        super.onResume();

        // Check to see if the Activity started due to an Android Beam
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
                handleActionNdefDiscovered(getIntent());
            } else {
                Log.d(Constants.TAG, "NFC: No NDEF discovered!");
            }
        } else {
            Log.e(Constants.TAG, "Android Beam not supported by Android < 4.1");
        }
    }

    /**
     * NFC
     */
    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        setIntent(intent);
    }

    /**
     * NFC: Parses the NDEF Message from the intent and prints to the TextView
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    void handleActionNdefDiscovered(Intent intent) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        // only one message sent during the beam
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        // record 0 contains the MIME type, record 1 is the AAR, if present
        byte[] receivedKeyringBytes = msg.getRecords()[0].getPayload();

        Intent importIntent = new Intent(this, ImportKeysActivity.class);
        importIntent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY);
        importIntent.putExtra(ImportKeysActivity.EXTRA_KEY_BYTES, receivedKeyringBytes);

        handleActions(null, importIntent);
    }

}