Changed TrustedCertificateAdapter for use with ListViews and TrustedCertificateEntry

This commit is contained in:
Tobias Brunner
2012-08-14 12:01:41 +02:00
parent af46e950b1
commit db8bea8311
2 changed files with 45 additions and 116 deletions
@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- <!--
Copyright (C) 2012 Tobias Brunner Copyright (C) 2012 Tobias Brunner
Copyright (C) 2012 Giuliano Grassi
Copyright (C) 2012 Ralf Sager
Hochschule fuer Technik Rapperswil Hochschule fuer Technik Rapperswil
This program is free software; you can redistribute it and/or modify it This program is free software; you can redistribute it and/or modify it
@@ -15,15 +13,24 @@
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details. for more details.
--> -->
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:orientation="vertical" > android:minHeight="?android:attr/listPreferredItemHeight"
android:background="?android:attr/selectableItemBackground"
android:padding="10dp" >
<TextView <TextView android:id="@+id/subject_primary"
android:id="@+id/certificate_name"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:textSize="18sp" /> android:textAppearance="?android:attr/textAppearanceMedium" />
</TableLayout> <TextView android:id="@+id/subject_secondary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/subject_primary"
android:layout_alignLeft="@id/subject_primary"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary" />
</RelativeLayout>
@@ -1,7 +1,5 @@
/* /*
* Copyright (C) 2012 Tobias Brunner * Copyright (C) 2012 Tobias Brunner
* Copyright (C) 2012 Giuliano Grassi
* Copyright (C) 2012 Ralf Sager
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
@@ -17,133 +15,57 @@
package org.strongswan.android.ui.adapter; package org.strongswan.android.ui.adapter;
import java.security.cert.X509Certificate; import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Map.Entry;
import org.strongswan.android.R; import org.strongswan.android.R;
import org.strongswan.android.data.TrustedCertificateEntry;
import android.content.Context; import android.content.Context;
import android.net.http.SslCertificate;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.BaseAdapter; import android.widget.ArrayAdapter;
import android.widget.TextView; import android.widget.TextView;
public class TrustedCertificateAdapter extends BaseAdapter public class TrustedCertificateAdapter extends ArrayAdapter<TrustedCertificateEntry>
{ {
private final ArrayList<CertEntry> mContent; public TrustedCertificateAdapter(Context context)
private final Context mContext;
public class CertEntry implements Comparable<CertEntry>
{ {
public X509Certificate mCert; super(context, R.layout.trusted_certificates_item);
public String mAlias;
public String mDisplayName;
public CertEntry(String alias, X509Certificate cert)
{
mCert = cert;
mAlias = alias;
}
public String getDisplayText()
{
if (mDisplayName == null)
{
SslCertificate cert = new SslCertificate(mCert);
String o = cert.getIssuedTo().getOName();
String ou = cert.getIssuedTo().getUName();
String cn = cert.getIssuedTo().getCName();
if (!o.isEmpty())
{
mDisplayName = o;
if (!cn.isEmpty())
{
mDisplayName = mDisplayName + ", " + cn;
}
else if (!ou.isEmpty())
{
mDisplayName = mDisplayName + ", " + ou;
}
}
else if (!cn.isEmpty())
{
mDisplayName = cn;
}
else
{
mDisplayName = cert.getIssuedTo().getDName();
}
}
return mDisplayName;
}
@Override
public int compareTo(CertEntry another)
{
return getDisplayText().compareToIgnoreCase(another.getDisplayText());
}
}
public TrustedCertificateAdapter(Context context,
Hashtable<String, X509Certificate> content)
{
mContext = context;
mContent = new ArrayList<TrustedCertificateAdapter.CertEntry>();
for (Entry<String, X509Certificate> entry : content.entrySet())
{
mContent.add(new CertEntry(entry.getKey(), entry.getValue()));
}
Collections.sort(mContent);
}
@Override
public int getCount()
{
return mContent.size();
}
@Override
public Object getItem(int position)
{
return mContent.get(position);
} }
/** /**
* Returns the position (index) of the entry with the given alias. * Set new data for this adapter.
* *
* @param alias alias of the item to find * @param data the new data (null to clear)
* @return the position (index) in the list
*/ */
public int getItemPosition(String alias) public void setData(List<TrustedCertificateEntry> data)
{ {
for (int i = 0; i < mContent.size(); i++) clear();
if (data != null)
{ {
if (mContent.get(i).mAlias.equals(alias)) addAll(data);
{
return i;
}
} }
return -1;
}
@Override
public long getItemId(int position)
{
return position;
} }
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) public View getView(int position, View convertView, ViewGroup parent)
{ {
LayoutInflater inflater = LayoutInflater.from(mContext); View view;
final View certView = inflater.inflate(R.layout.trusted_certificates_item, null); if (convertView != null)
final TextView certText = (TextView)certView.findViewById(R.id.certificate_name); {
certText.setText(mContent.get(position).getDisplayText()); view = convertView;
return certView; }
else
{
LayoutInflater inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.trusted_certificates_item, parent, false);
}
TrustedCertificateEntry item = getItem(position);
TextView text = (TextView)view.findViewById(R.id.subject_primary);
text.setText(item.getSubjectPrimary());
text = (TextView)view.findViewById(R.id.subject_secondary);
text.setText(item.getSubjectSecondary());
return view;
} }
} }