ae94adc52206d811296f8c49547c7b9f86892c3a
[strongswan.git] / src / frontends / android / src / org / strongswan / android / ui / adapter / TrustedCertificateAdapter.java
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 package org.strongswan.android.ui.adapter;
19
20 import java.security.cert.X509Certificate;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Hashtable;
24 import java.util.Map.Entry;
25
26 import org.strongswan.android.R;
27
28 import android.content.Context;
29 import android.net.http.SslCertificate;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.BaseAdapter;
34 import android.widget.TextView;
35
36 public class TrustedCertificateAdapter extends BaseAdapter
37 {
38 private final ArrayList<CertEntry> mContent;
39 private final Context mContext;
40
41 public class CertEntry implements Comparable<CertEntry>
42 {
43 public X509Certificate mCert;
44 public String mAlias;
45 public String mDisplayName;
46
47 public CertEntry(String alias, X509Certificate cert)
48 {
49 mCert = cert;
50 mAlias = alias;
51 }
52
53 public String getDisplayText()
54 {
55 if (mDisplayName == null)
56 {
57 SslCertificate cert = new SslCertificate(mCert);
58 String o = cert.getIssuedTo().getOName();
59 String ou = cert.getIssuedTo().getUName();
60 String cn = cert.getIssuedTo().getCName();
61 if (!o.isEmpty())
62 {
63 mDisplayName = o;
64 if (!cn.isEmpty())
65 {
66 mDisplayName = mDisplayName + ", " + cn;
67 }
68 else if (!ou.isEmpty())
69 {
70 mDisplayName = mDisplayName + ", " + ou;
71 }
72 }
73 else if (!cn.isEmpty())
74 {
75 mDisplayName = cn;
76 }
77 else
78 {
79 mDisplayName = cert.getIssuedTo().getDName();
80 }
81 }
82 return mDisplayName;
83 }
84
85 @Override
86 public int compareTo(CertEntry another)
87 {
88 return getDisplayText().compareToIgnoreCase(another.getDisplayText());
89 }
90 }
91
92 public TrustedCertificateAdapter(Context context,
93 Hashtable<String, X509Certificate> content)
94 {
95 mContext = context;
96 mContent = new ArrayList<TrustedCertificateAdapter.CertEntry>();
97 for (Entry<String, X509Certificate> entry : content.entrySet())
98 {
99 mContent.add(new CertEntry(entry.getKey(), entry.getValue()));
100 }
101 Collections.sort(mContent);
102 }
103
104 @Override
105 public int getCount()
106 {
107 return mContent.size();
108 }
109
110 @Override
111 public Object getItem(int position)
112 {
113 return mContent.get(position);
114 }
115
116 /**
117 * Returns the position (index) of the entry with the given alias.
118 *
119 * @param alias alias of the item to find
120 * @return the position (index) in the list
121 */
122 public int getItemPosition(String alias)
123 {
124 for (int i = 0; i < mContent.size(); i++)
125 {
126 if (mContent.get(i).mAlias.equals(alias))
127 {
128 return i;
129 }
130 }
131 return -1;
132 }
133
134 @Override
135 public long getItemId(int position)
136 {
137 return position;
138 }
139
140 @Override
141 public View getView(int position, View convertView, ViewGroup parent)
142 {
143 LayoutInflater inflater = LayoutInflater.from(mContext);
144 final View certView = inflater.inflate(R.layout.trusted_certificates_item, null);
145 final TextView certText = (TextView)certView.findViewById(R.id.certificate_name);
146 certText.setText(mContent.get(position).getDisplayText());
147 return certView;
148 }
149 }