2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
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>.
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
16 package org
.strongswan
.android
.ui
;
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
;
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
;
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
;
40 public class TrustedCertificateListFragment
extends ListFragment
implements LoaderCallbacks
<List
<TrustedCertificateEntry
>>
42 private OnTrustedCertificateSelectedListener mListener
;
43 private TrustedCertificateAdapter mAdapter
;
44 private boolean mUser
;
47 * The activity containing this fragment should implement this interface
49 public interface OnTrustedCertificateSelectedListener
{
50 public void onTrustedCertificateSelected(TrustedCertificateEntry selected
);
54 public void onActivityCreated(Bundle savedInstanceState
)
56 super.onActivityCreated(savedInstanceState
);
58 setEmptyText(getString(R
.string
.no_certificates
));
60 mAdapter
= new TrustedCertificateAdapter(getActivity());
61 setListAdapter(mAdapter
);
65 /* non empty arguments mean we list user certificate */
66 mUser
= getArguments() != null
;
68 getLoaderManager().initLoader(0, null
, this);
72 public void onDestroy()
78 public void onAttach(Activity activity
)
80 super.onAttach(activity
);
82 if (activity
instanceof OnTrustedCertificateSelectedListener
)
84 mListener
= (OnTrustedCertificateSelectedListener
)activity
;
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
);
95 public void onLoadFinished(Loader
<List
<TrustedCertificateEntry
>> loader
, List
<TrustedCertificateEntry
> data
)
97 mAdapter
.setData(data
);
105 setListShownNoAnimation(true
);
110 public void onLoaderReset(Loader
<List
<TrustedCertificateEntry
>> loader
)
112 mAdapter
.setData(null
);
116 public void onListItemClick(ListView l
, View v
, int position
, long id
)
118 if (mListener
!= null
)
120 mListener
.onTrustedCertificateSelected(mAdapter
.getItem(position
));
124 public static class CertificateListLoader
extends AsyncTaskLoader
<List
<TrustedCertificateEntry
>>
126 private List
<TrustedCertificateEntry
> mData
;
127 private final boolean mUser
;
129 public CertificateListLoader(Context context
, boolean user
)
136 public List
<TrustedCertificateEntry
> loadInBackground()
138 TrustedCertificateManager certman
= TrustedCertificateManager
.getInstance().load();
139 Hashtable
<String
,X509Certificate
> certificates
;
140 List
<TrustedCertificateEntry
> selected
;
142 certificates
= mUser ? certman
.getUserCACertificates() : certman
.getSystemCACertificates();
143 selected
= new ArrayList
<TrustedCertificateEntry
>();
144 for (Entry
<String
, X509Certificate
> entry
: certificates
.entrySet())
146 selected
.add(new TrustedCertificateEntry(entry
.getKey(), entry
.getValue()));
148 Collections
.sort(selected
);
153 protected void onStartLoading()
156 { /* if we have data ready, deliver it directly */
157 deliverResult(mData
);
164 public void deliverResult(List
<TrustedCertificateEntry
> data
)
172 { /* if it is started we deliver the data directly,
173 * otherwise this is handled in onStartLoading */
174 super.deliverResult(data
);
179 protected void onReset()