Added a ListFragment that lists trusted certificates (loaded via a custom Loader)

This commit is contained in:
Tobias Brunner 2012-08-14 09:36:56 +02:00
parent db8bea8311
commit caf85c872f
3 changed files with 159 additions and 0 deletions

View File

@ -57,6 +57,9 @@
<string name="alert_text_nocertfound_title">Kein CA-Zertifikat ausgewählt</string>
<string name="alert_text_nocertfound">Bitte wählen Sie eines aus oder aktivieren Sie <i>Automatisch wählen</i></string>
<!-- Trusted certificate selection -->
<string name="no_certificates">Keine Zertifikate</string>
<!-- VPN state fragment -->
<string name="state_label">Status:</string>
<string name="profile_label">Profil:</string>

View File

@ -57,6 +57,9 @@
<string name="alert_text_nocertfound_title">No CA certificate selected</string>
<string name="alert_text_nocertfound">Please select one or activate <i>Select automatically</i></string>
<!-- Trusted certificate selection -->
<string name="no_certificates">No certificates</string>
<!-- VPN state fragment -->
<string name="state_label">Status:</string>
<string name="profile_label">Profile:</string>

View File

@ -0,0 +1,153 @@
/*
* Copyright (C) 2012 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* 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 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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.
*/
package org.strongswan.android.ui;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Map.Entry;
import org.strongswan.android.R;
import org.strongswan.android.data.TrustedCertificateEntry;
import org.strongswan.android.logic.TrustedCertificateManager;
import org.strongswan.android.ui.adapter.TrustedCertificateAdapter;
import android.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.AsyncTaskLoader;
import android.content.Context;
import android.content.Loader;
import android.os.Bundle;
public class TrustedCertificateListFragment extends ListFragment implements LoaderCallbacks<List<TrustedCertificateEntry>>
{
private TrustedCertificateAdapter mAdapter;
private boolean mUser;
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
setEmptyText(getString(R.string.no_certificates));
mAdapter = new TrustedCertificateAdapter(getActivity());
setListAdapter(mAdapter);
setListShown(false);
/* non empty arguments mean we list user certificate */
mUser = getArguments() != null;
getLoaderManager().initLoader(0, null, this);
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Override
public Loader<List<TrustedCertificateEntry>> onCreateLoader(int id, Bundle args)
{ /* we don't need the id as we have only one loader */
return new CertificateListLoader(getActivity(), mUser);
}
@Override
public void onLoadFinished(Loader<List<TrustedCertificateEntry>> loader, List<TrustedCertificateEntry> data)
{
mAdapter.setData(data);
if (isResumed())
{
setListShown(true);
}
else
{
setListShownNoAnimation(true);
}
}
@Override
public void onLoaderReset(Loader<List<TrustedCertificateEntry>> loader)
{
mAdapter.setData(null);
}
public static class CertificateListLoader extends AsyncTaskLoader<List<TrustedCertificateEntry>>
{
private List<TrustedCertificateEntry> mData;
private final boolean mUser;
public CertificateListLoader(Context context, boolean user)
{
super(context);
mUser = user;
}
@Override
public List<TrustedCertificateEntry> loadInBackground()
{
TrustedCertificateManager certman = TrustedCertificateManager.getInstance().load();
Hashtable<String,X509Certificate> certificates;
List<TrustedCertificateEntry> selected;
certificates = mUser ? certman.getUserCACertificates() : certman.getSystemCACertificates();
selected = new ArrayList<TrustedCertificateEntry>();
for (Entry<String, X509Certificate> entry : certificates.entrySet())
{
selected.add(new TrustedCertificateEntry(entry.getKey(), entry.getValue()));
}
Collections.sort(selected);
return selected;
}
@Override
protected void onStartLoading()
{
if (mData != null)
{ /* if we have data ready, deliver it directly */
deliverResult(mData);
return;
}
forceLoad();
}
@Override
public void deliverResult(List<TrustedCertificateEntry> data)
{
if (isReset())
{
return;
}
mData = data;
if (isStarted())
{ /* if it is started we deliver the data directly,
* otherwise this is handled in onStartLoading */
super.deliverResult(data);
}
}
@Override
protected void onReset()
{
mData = null;
}
}
}