Home | History | Annotate | Download | only in utility
      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 
     18 package com.android.emailcommon.utility;
     19 
     20 import android.app.Activity;
     21 import android.content.Intent;
     22 import android.os.Bundle;
     23 import android.security.KeyChain;
     24 import android.security.KeyChainAliasCallback;
     25 
     26 /**
     27  * A headless Activity which simply calls into the framework {@link KeyChain} service to select
     28  * a certificate to use for establishing secure connections in the Email app.
     29  */
     30 public class CertificateRequestor extends Activity implements KeyChainAliasCallback {
     31 
     32     public static final String ACTION_REQUEST_CERT = "com.android.emailcommon.REQUEST_CERT";
     33 
     34     public static final String EXTRA_HOST = "CertificateRequestor.host";
     35     public static final String EXTRA_PORT = "CertificateRequestor.port";
     36 
     37     public static final String RESULT_ALIAS = "CertificateRequestor.alias";
     38 
     39     @Override
     40     protected void onCreate(Bundle savedInstanceState) {
     41         super.onCreate(savedInstanceState);
     42 
     43         Intent i = getIntent();
     44         String host = i.getStringExtra(EXTRA_HOST);
     45         int port = i.getIntExtra(EXTRA_PORT, -1);
     46 
     47         KeyChain.choosePrivateKeyAlias(
     48                 this, this,
     49                 null /* keytypes */, null /* issuers */,
     50                 host, port,
     51                 null /* alias */);
     52     }
     53 
     54     /**
     55      * Callback for the certificate request. Does not happen on the UI thread.
     56      */
     57     @Override
     58     public void alias(String alias) {
     59         if (alias == null) {
     60             setResult(RESULT_CANCELED);
     61         } else {
     62             Intent data = new Intent();
     63             data.putExtra(RESULT_ALIAS, alias);
     64             setResult(RESULT_OK, data);
     65         }
     66         finish();
     67     }
     68 }
     69