Added a ListFragment that lists trusted certificates (loaded via a custom Loader)
[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.ListFragment;
31 import android.app.LoaderManager.LoaderCallbacks;
32 import android.content.AsyncTaskLoader;
33 import android.content.Context;
34 import android.content.Loader;
35 import android.os.Bundle;
36
37 public class TrustedCertificateListFragment extends ListFragment implements LoaderCallbacks<List<TrustedCertificateEntry>>
38 {
39 private TrustedCertificateAdapter mAdapter;
40 private boolean mUser;
41
42 @Override
43 public void onActivityCreated(Bundle savedInstanceState)
44 {
45 super.onActivityCreated(savedInstanceState);
46
47 setEmptyText(getString(R.string.no_certificates));
48
49 mAdapter = new TrustedCertificateAdapter(getActivity());
50 setListAdapter(mAdapter);
51
52 setListShown(false);
53
54 /* non empty arguments mean we list user certificate */
55 mUser = getArguments() != null;
56
57 getLoaderManager().initLoader(0, null, this);
58 }
59
60 @Override
61 public void onDestroy()
62 {
63 super.onDestroy();
64 }
65
66 @Override
67 public Loader<List<TrustedCertificateEntry>> onCreateLoader(int id, Bundle args)
68 { /* we don't need the id as we have only one loader */
69 return new CertificateListLoader(getActivity(), mUser);
70 }
71
72 @Override
73 public void onLoadFinished(Loader<List<TrustedCertificateEntry>> loader, List<TrustedCertificateEntry> data)
74 {
75 mAdapter.setData(data);
76
77 if (isResumed())
78 {
79 setListShown(true);
80 }
81 else
82 {
83 setListShownNoAnimation(true);
84 }
85 }
86
87 @Override
88 public void onLoaderReset(Loader<List<TrustedCertificateEntry>> loader)
89 {
90 mAdapter.setData(null);
91 }
92
93 public static class CertificateListLoader extends AsyncTaskLoader<List<TrustedCertificateEntry>>
94 {
95 private List<TrustedCertificateEntry> mData;
96 private final boolean mUser;
97
98 public CertificateListLoader(Context context, boolean user)
99 {
100 super(context);
101 mUser = user;
102 }
103
104 @Override
105 public List<TrustedCertificateEntry> loadInBackground()
106 {
107 TrustedCertificateManager certman = TrustedCertificateManager.getInstance().load();
108 Hashtable<String,X509Certificate> certificates;
109 List<TrustedCertificateEntry> selected;
110
111 certificates = mUser ? certman.getUserCACertificates() : certman.getSystemCACertificates();
112 selected = new ArrayList<TrustedCertificateEntry>();
113 for (Entry<String, X509Certificate> entry : certificates.entrySet())
114 {
115 selected.add(new TrustedCertificateEntry(entry.getKey(), entry.getValue()));
116 }
117 Collections.sort(selected);
118 return selected;
119 }
120
121 @Override
122 protected void onStartLoading()
123 {
124 if (mData != null)
125 { /* if we have data ready, deliver it directly */
126 deliverResult(mData);
127 return;
128 }
129 forceLoad();
130 }
131
132 @Override
133 public void deliverResult(List<TrustedCertificateEntry> data)
134 {
135 if (isReset())
136 {
137 return;
138 }
139 mData = data;
140 if (isStarted())
141 { /* if it is started we deliver the data directly,
142 * otherwise this is handled in onStartLoading */
143 super.deliverResult(data);
144 }
145 }
146
147 @Override
148 protected void onReset()
149 {
150 mData = null;
151 }
152 }
153 }