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
.text
.TextUtils
;
38 import android
.view
.Menu
;
39 import android
.view
.MenuInflater
;
40 import android
.view
.MenuItem
;
41 import android
.view
.View
;
42 import android
.widget
.ListView
;
43 import android
.widget
.SearchView
;
44 import android
.widget
.SearchView
.OnQueryTextListener
;
46 public class TrustedCertificateListFragment
extends ListFragment
implements LoaderCallbacks
<List
<TrustedCertificateEntry
>>, OnQueryTextListener
48 private OnTrustedCertificateSelectedListener mListener
;
49 private TrustedCertificateAdapter mAdapter
;
50 private boolean mUser
;
53 * The activity containing this fragment should implement this interface
55 public interface OnTrustedCertificateSelectedListener
{
56 public void onTrustedCertificateSelected(TrustedCertificateEntry selected
);
60 public void onActivityCreated(Bundle savedInstanceState
)
62 super.onActivityCreated(savedInstanceState
);
63 setHasOptionsMenu(true
);
65 setEmptyText(getString(R
.string
.no_certificates
));
67 mAdapter
= new TrustedCertificateAdapter(getActivity());
68 setListAdapter(mAdapter
);
72 /* non empty arguments mean we list user certificate */
73 mUser
= getArguments() != null
;
75 getLoaderManager().initLoader(0, null
, this);
79 public void onDestroy()
85 public void onAttach(Activity activity
)
87 super.onAttach(activity
);
89 if (activity
instanceof OnTrustedCertificateSelectedListener
)
91 mListener
= (OnTrustedCertificateSelectedListener
)activity
;
96 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
)
98 MenuItem item
= menu
.add(R
.string
.search
);
99 item
.setIcon(android
.R
.drawable
.ic_menu_search
);
100 item
.setShowAsAction(MenuItem
.SHOW_AS_ACTION_IF_ROOM
);
102 SearchView sv
= new SearchView(getActivity());
103 sv
.setOnQueryTextListener(this);
104 item
.setActionView(sv
);
108 public boolean onQueryTextSubmit(String query
)
109 { /* already handled when the text changes */
114 public boolean onQueryTextChange(String newText
)
116 String search
= TextUtils
.isEmpty(newText
) ? null
: newText
;
117 mAdapter
.getFilter().filter(search
);
122 public Loader
<List
<TrustedCertificateEntry
>> onCreateLoader(int id
, Bundle args
)
123 { /* we don't need the id as we have only one loader */
124 return new CertificateListLoader(getActivity(), mUser
);
128 public void onLoadFinished(Loader
<List
<TrustedCertificateEntry
>> loader
, List
<TrustedCertificateEntry
> data
)
130 mAdapter
.setData(data
);
138 setListShownNoAnimation(true
);
143 public void onLoaderReset(Loader
<List
<TrustedCertificateEntry
>> loader
)
145 mAdapter
.setData(null
);
149 public void onListItemClick(ListView l
, View v
, int position
, long id
)
151 if (mListener
!= null
)
153 mListener
.onTrustedCertificateSelected(mAdapter
.getItem(position
));
157 public static class CertificateListLoader
extends AsyncTaskLoader
<List
<TrustedCertificateEntry
>>
159 private List
<TrustedCertificateEntry
> mData
;
160 private final boolean mUser
;
162 public CertificateListLoader(Context context
, boolean user
)
169 public List
<TrustedCertificateEntry
> loadInBackground()
171 TrustedCertificateManager certman
= TrustedCertificateManager
.getInstance().load();
172 Hashtable
<String
,X509Certificate
> certificates
;
173 List
<TrustedCertificateEntry
> selected
;
175 certificates
= mUser ? certman
.getUserCACertificates() : certman
.getSystemCACertificates();
176 selected
= new ArrayList
<TrustedCertificateEntry
>();
177 for (Entry
<String
, X509Certificate
> entry
: certificates
.entrySet())
179 selected
.add(new TrustedCertificateEntry(entry
.getKey(), entry
.getValue()));
181 Collections
.sort(selected
);
186 protected void onStartLoading()
189 { /* if we have data ready, deliver it directly */
190 deliverResult(mData
);
197 public void deliverResult(List
<TrustedCertificateEntry
> data
)
205 { /* if it is started we deliver the data directly,
206 * otherwise this is handled in onStartLoading */
207 super.deliverResult(data
);
212 protected void onReset()