2 * Copyright (C) 2012-2015 Tobias Brunner
3 * HSR 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 android.content.Context;
19 import android.os.Bundle;
20 import android.support.v4.app.ListFragment;
21 import android.support.v4.app.LoaderManager;
22 import android.support.v4.app.LoaderManager.LoaderCallbacks;
23 import android.support.v4.content.AsyncTaskLoader;
24 import android.support.v4.content.Loader;
25 import android.text.TextUtils;
26 import android.view.Menu;
27 import android.view.MenuInflater;
28 import android.view.MenuItem;
29 import android.view.View;
30 import android.widget.ListView;
31 import android.widget.SearchView;
32 import android.widget.SearchView.OnQueryTextListener;
34 import org.strongswan.android.R;
35 import org.strongswan.android.logic.TrustedCertificateManager;
36 import org.strongswan.android.logic.TrustedCertificateManager.TrustedCertificateSource;
37 import org.strongswan.android.security.TrustedCertificateEntry;
38 import org.strongswan.android.ui.adapter.TrustedCertificateAdapter;
40 import java.security.cert.X509Certificate;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.Hashtable;
44 import java.util.List;
45 import java.util.Map.Entry;
46 import java.util.Observable;
47 import java.util.Observer;
49 public class TrustedCertificateListFragment extends ListFragment implements LoaderCallbacks<List<TrustedCertificateEntry>>, OnQueryTextListener
51 public static final String EXTRA_CERTIFICATE_SOURCE = "certificate_source";
52 private OnTrustedCertificateSelectedListener mListener;
53 private TrustedCertificateAdapter mAdapter;
54 private TrustedCertificateSource mSource = TrustedCertificateSource.SYSTEM;
57 * The activity containing this fragment should implement this interface
59 public interface OnTrustedCertificateSelectedListener
61 public void onTrustedCertificateSelected(TrustedCertificateEntry selected);
65 public void onActivityCreated(Bundle savedInstanceState)
67 super.onActivityCreated(savedInstanceState);
68 setHasOptionsMenu(true);
70 setEmptyText(getString(R.string.no_certificates));
72 mAdapter = new TrustedCertificateAdapter(getActivity());
73 setListAdapter(mAdapter);
77 Bundle arguments = getArguments();
78 if (arguments != null)
80 mSource = (TrustedCertificateSource)arguments.getSerializable(EXTRA_CERTIFICATE_SOURCE);
83 LoaderManager.getInstance(this).initLoader(0, null, this);
87 public void onDestroy()
93 public void onAttach(Context context)
95 super.onAttach(context);
97 if (context instanceof OnTrustedCertificateSelectedListener)
99 mListener = (OnTrustedCertificateSelectedListener)context;
104 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
106 MenuItem item = menu.add(R.string.search);
107 item.setIcon(android.R.drawable.ic_menu_search);
108 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
110 SearchView sv = new SearchView(getActivity());
111 sv.setOnQueryTextListener(this);
112 item.setActionView(sv);
116 public boolean onQueryTextSubmit(String query)
117 { /* already handled when the text changes */
122 public boolean onQueryTextChange(String newText)
124 String search = TextUtils.isEmpty(newText) ? null : newText;
125 mAdapter.getFilter().filter(search);
130 public Loader<List<TrustedCertificateEntry>> onCreateLoader(int id, Bundle args)
131 { /* we don't need the id as we have only one loader */
132 return new CertificateListLoader(getActivity(), mSource);
136 public void onLoadFinished(Loader<List<TrustedCertificateEntry>> loader, List<TrustedCertificateEntry> data)
138 mAdapter.setData(data);
146 setListShownNoAnimation(true);
151 public void onLoaderReset(Loader<List<TrustedCertificateEntry>> loader)
153 mAdapter.setData(null);
157 public void onListItemClick(ListView l, View v, int position, long id)
159 if (mListener != null)
161 mListener.onTrustedCertificateSelected(mAdapter.getItem(position));
165 public static class CertificateListLoader extends AsyncTaskLoader<List<TrustedCertificateEntry>>
167 private List<TrustedCertificateEntry> mData;
168 private final TrustedCertificateSource mSource;
169 private TrustedCertificateManagerObserver mObserver;
171 public CertificateListLoader(Context context, TrustedCertificateSource source)
178 public List<TrustedCertificateEntry> loadInBackground()
180 TrustedCertificateManager certman = TrustedCertificateManager.getInstance().load();
181 Hashtable<String, X509Certificate> certificates = certman.getCACertificates(mSource);
182 List<TrustedCertificateEntry> selected;
184 selected = new ArrayList<TrustedCertificateEntry>();
185 for (Entry<String, X509Certificate> entry : certificates.entrySet())
187 selected.add(new TrustedCertificateEntry(entry.getKey(), entry.getValue()));
189 Collections.sort(selected);
194 protected void onStartLoading()
197 { /* if we have data ready, deliver it directly */
198 deliverResult(mData);
200 if (takeContentChanged() || mData == null)
207 public void deliverResult(List<TrustedCertificateEntry> data)
215 { /* if it is started we deliver the data directly,
216 * otherwise this is handled in onStartLoading */
217 if (mObserver == null)
219 mObserver = new TrustedCertificateManagerObserver();
220 TrustedCertificateManager.getInstance().addObserver(mObserver);
222 super.deliverResult(data);
227 protected void onReset()
229 if (mObserver != null)
231 TrustedCertificateManager.getInstance().deleteObserver(mObserver);
239 protected void onAbandon()
241 if (mObserver != null)
243 TrustedCertificateManager.getInstance().deleteObserver(mObserver);
248 private class TrustedCertificateManagerObserver implements Observer
250 private ForceLoadContentObserver mContentObserver = new ForceLoadContentObserver();
253 public void update(Observable observable, Object data)
255 mContentObserver.onChange(false);