Reputation: 1286
I am trying to install CA certificates from a class in android. This is what I am doing.
//File1.java - This is a non-activity
ArrayList<String> CACertsList = new ArrayList<String>();
CACertsList.add(encodedCACert1);
CACertsList.add(encodedCACert2);
Context context = _serviceInstance.getServiceContext();
Intent intent = new Intent(context, CACertInstallActivity.class);
intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
intent.putExtra( "CERTDATA", CACertsList );
context.startActivity(intent);
And the actual activity
public class CACertInstallActivity extends Activity
{
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
Bundle bundle = getIntent().getExtras();
ArrayList<String> CACerts = bundle.getStringArrayList("CERTDATA");
for( int i = 0; i < CACerts.size(); i++ )
{
try
{
// Convert base64encoded Cert data to byte array and converting it into X509 Certificate format
byte[] certData = Base64.decode( CACerts.get(i) );
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
InputStream in = new ByteArrayInputStream(certData);
X509Certificate cert = (X509Certificate)certFactory.generateCertificate(in);
// Start the install intent for the cert
Intent intent = KeyChain.createInstallIntent();
intent.addFlags( Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP );
intent.putExtra( KeyChain.EXTRA_CERTIFICATE, cert.getEncoded() );
intent.putExtra( KeyChain.EXTRA_NAME, cert.getIssuerDN().getName() );
this.startActivityForResult(intent,2);
}
catch (CertificateException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data )
{
if( resultCode == RESULT_OK )
{
// Success
}
else
{
// Failed
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Manifest declaration
<activity android:configChanges="orientation|screenSize|keyboard|keyboardHidden" android:name=".activity.CACertInstallActivity"/>
I have two problems. One is when I am installing multiple certs, only one of the cert installation prompt is coming up. Another is the onActivityResult method is never called. Can anyone figure out the error that I am doing?
Also, the createInstallIntent does return value
* <p>When used with {@link Activity#startActivityForResult},
* {@link Activity#RESULT_OK} will be returned if a credential was
* successfully installed, otherwise {@link
* Activity#RESULT_CANCELED} will be returned.
Upvotes: 1
Views: 446
Reputation: 4669
It's maybe late, but the startActivityForResult(intent, 2)
is not called from the Context, but from an Activity.
So from a class which extends wether AppCompatActivity or Activity, you just need to call :
startActivityForResult(intent, 2);
Now, about the error you are getting, you maybe solved this, but you have to wait into the onActivityResult
before starting another Intent to install the following certificate.
Also remove the flags from your intent.
From my side I basically made a queue ( a list) which takes all certificate, and then process this list, each time you go on the onActivityResult
, you just launch the next certificate intent installation.
The only problem I have now, is that the onActivityResult
function is not called if someone click on "Cancel" button. Which shouldn't be the case normally.
Upvotes: 1
Reputation: 18242
For the onActivityResult not being called, try changing context.startActivity(intent);
for context.startActivityForResult(intent);
Upvotes: 2