f5cc57ab5a962d8bfb3317fe94917fa4fec43350
[strongswan.git] / src / frontends / android / src / org / strongswan / android / ui / TrustedCertificateListFragment.java
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 package org.strongswan.android.ui;
17
18 import java.security.cert.X509Certificate;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Hashtable;
22 import java.util.List;
23 import java.util.Map.Entry;
24
25 import org.strongswan.android.R;
26 import org.strongswan.android.data.TrustedCertificateEntry;
27 import org.strongswan.android.logic.TrustedCertificateManager;
28 import org.strongswan.android.ui.adapter.TrustedCertificateAdapter;
29
30 import android.app.Activity;
31 import android.app.ListFragment;
32 import android.app.LoaderManager.LoaderCallbacks;
33 import android.content.AsyncTaskLoader;
34 import android.content.Context;
35 import android.content.Loader;
36 import android.os.Bundle;
37 import android.view.View;
38 import android.widget.ListView;
39
40 public class TrustedCertificateListFragment extends ListFragment implements LoaderCallbacks<List<TrustedCertificateEntry>>
41 {
42 private OnTrustedCertificateSelectedListener mListener;
43 private TrustedCertificateAdapter mAdapter;
44 private boolean mUser;
45
46 /**
47 * The activity containing this fragment should implement this interface
48 */
49 public interface OnTrustedCertificateSelectedListener {
50 public void onTrustedCertificateSelected(TrustedCertificateEntry selected);
51 }
52
53 @Override
54 public void onActivityCreated(Bundle savedInstanceState)
55 {
56 super.onActivityCreated(savedInstanceState);
57
58 setEmptyText(getString(R.string.no_certificates));
59
60 mAdapter = new TrustedCertificateAdapter(getActivity());
61 setListAdapter(mAdapter);
62
63 setListShown(false);
64
65 /* non empty arguments mean we list user certificate */
66 mUser = getArguments() != null;
67
68 getLoaderManager().initLoader(0, null, this);
69 }
70
71 @Override
72 public void onDestroy()
73 {
74 super.onDestroy();
75 }
76
77 @Override
78 public void onAttach(Activity activity)
79 {
80 super.onAttach(activity);
81
82 if (activity instanceof OnTrustedCertificateSelectedListener)
83 {
84 mListener = (OnTrustedCertificateSelectedListener)activity;
85 }
86 }
87
88 @Override
89 public Loader<List<TrustedCertificateEntry>> onCreateLoader(int id, Bundle args)
90 { /* we don't need the id as we have only one loader */
91 return new CertificateListLoader(getActivity(), mUser);
92 }
93
94 @Override
95 public void onLoadFinished(Loader<List<TrustedCertificateEntry>> loader, List<TrustedCertificateEntry> data)
96 {
97 mAdapter.setData(data);
98
99 if (isResumed())
100 {
101 setListShown(true);
102 }
103 else
104 {
105 setListShownNoAnimation(true);
106 }
107 }
108
109 @Override
110 public void onLoaderReset(Loader<List<TrustedCertificateEntry>> loader)
111 {
112 mAdapter.setData(null);
113 }
114
115 @Override
116 public void onListItemClick(ListView l, View v, int position, long id)
117 {
118 if (mListener != null)
119 {
120 mListener.onTrustedCertificateSelected(mAdapter.getItem(position));
121 }
122 }
123
124 public static class CertificateListLoader extends AsyncTaskLoader<List<TrustedCertificateEntry>>
125 {
126 private List<TrustedCertificateEntry> mData;
127 private final boolean mUser;
128
129 public CertificateListLoader(Context context, boolean user)
130 {
131 super(context);
132 mUser = user;
133 }
134
135 @Override
136 public List<TrustedCertificateEntry> loadInBackground()
137 {
138 TrustedCertificateManager certman = TrustedCertificateManager.getInstance().load();
139 Hashtable<String,X509Certificate> certificates;
140 List<TrustedCertificateEntry> selected;
141
142 certificates = mUser ? certman.getUserCACertificates() : certman.getSystemCACertificates();
143 selected = new ArrayList<TrustedCertificateEntry>();
144 for (Entry<String, X509Certificate> entry : certificates.entrySet())
145 {
146 selected.add(new TrustedCertificateEntry(entry.getKey(), entry.getValue()));
147 }
148 Collections.sort(selected);
149 return selected;
150 }
151
152 @Override
153 protected void onStartLoading()
154 {
155 if (mData != null)
156 { /* if we have data ready, deliver it directly */
157 deliverResult(mData);
158 return;
159 }
160 forceLoad();
161 }
162
163 @Override
164 public void deliverResult(List<TrustedCertificateEntry> data)
165 {
166 if (isReset())
167 {
168 return;
169 }
170 mData = data;
171 if (isStarted())
172 { /* if it is started we deliver the data directly,
173 * otherwise this is handled in onStartLoading */
174 super.deliverResult(data);
175 }
176 }
177
178 @Override
179 protected void onReset()
180 {
181 mData = null;
182 }
183 }
184 }