1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package com.android.emailcommon.utility;
19
20import android.app.Activity;
21import android.content.Intent;
22import android.net.Uri;
23import android.os.Bundle;
24import android.security.KeyChain;
25import android.security.KeyChainAliasCallback;
26
27/**
28 * A headless Activity which simply calls into the framework {@link KeyChain} service to select
29 * a certificate to use for establishing secure connections in the Email app.
30 */
31public class CertificateRequestor extends Activity implements KeyChainAliasCallback {
32    public static final String EXTRA_HOST = "CertificateRequestor.host";
33    public static final String EXTRA_PORT = "CertificateRequestor.port";
34
35    public static final String RESULT_ALIAS = "CertificateRequestor.alias";
36
37    public static final Uri CERTIFICATE_REQUEST_URI =
38            Uri.parse("eas://com.android.emailcommon/certrequest");
39
40    @Override
41    protected void onCreate(Bundle savedInstanceState) {
42        super.onCreate(savedInstanceState);
43
44        Intent i = getIntent();
45        String host = i.getStringExtra(EXTRA_HOST);
46        int port = i.getIntExtra(EXTRA_PORT, -1);
47
48        if (savedInstanceState == null) {
49            KeyChain.choosePrivateKeyAlias(
50                    this, this,
51                    null /* keytypes */, null /* issuers */,
52                    host, port,
53                    null /* alias */);
54        }
55    }
56
57    /**
58     * Callback for the certificate request. Does not happen on the UI thread.
59     */
60    @Override
61    public void alias(String alias) {
62        if (alias == null) {
63            setResult(RESULT_CANCELED);
64        } else {
65            Intent data = new Intent();
66            data.putExtra(RESULT_ALIAS, alias);
67            setResult(RESULT_OK, data);
68        }
69        finish();
70    }
71}
72