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.LoaderCallbacks;
22 import android.support.v4.content.AsyncTaskLoader;
23 import android.support.v4.content.Loader;
24 import android.text.TextUtils;
25 import android.view.Menu;
26 import android.view.MenuInflater;
27 import android.view.MenuItem;
28 import android.view.View;
29 import android.widget.ListView;
30 import android.widget.SearchView;
31 import android.widget.SearchView.OnQueryTextListener;
33 import org.strongswan.android.R;
34 import org.strongswan.android.logic.TrustedCertificateManager;
35 import org.strongswan.android.logic.TrustedCertificateManager.TrustedCertificateSource;
36 import org.strongswan.android.security.TrustedCertificateEntry;
37 import org.strongswan.android.ui.adapter.TrustedCertificateAdapter;
39 import java.security.cert.X509Certificate;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.Hashtable;
43 import java.util.List;
44 import java.util.Map.Entry;
45 import java.util.Observable;
46 import java.util.Observer;
48 public class TrustedCertificateListFragment extends ListFragment implements LoaderCallbacks<List<TrustedCertificateEntry>>, OnQueryTextListener
50 public static final String EXTRA_CERTIFICATE_SOURCE = "certificate_source";
51 private OnTrustedCertificateSelectedListener mListener;
52 private TrustedCertificateAdapter mAdapter;
53 private TrustedCertificateSource mSource = TrustedCertificateSource.SYSTEM;
56 * The activity containing this fragment should implement this interface
58 public interface OnTrustedCertificateSelectedListener
60 public void onTrustedCertificateSelected(TrustedCertificateEntry selected);
64 public void onActivityCreated(Bundle savedInstanceState)
66 super.onActivityCreated(savedInstanceState);
67 setHasOptionsMenu(true);
69 setEmptyText(getString(R.string.no_certificates));
71 mAdapter = new TrustedCertificateAdapter(getActivity());
72 setListAdapter(mAdapter);
76 Bundle arguments = getArguments();
77 if (arguments != null)
79 mSource = (TrustedCertificateSource)arguments.getSerializable(EXTRA_CERTIFICATE_SOURCE);
82 getLoaderManager().initLoader(0, null, this);
86 public void onDestroy()
92 public void onAttach(Context context)
94 super.onAttach(context);
96 if (context instanceof OnTrustedCertificateSelectedListener)
98 mListener = (OnTrustedCertificateSelectedListener)context;
103 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
105 MenuItem item = menu.add(R.string.search);
106 item.setIcon(android.R.drawable.ic_menu_search);
107 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
109 SearchView sv = new SearchView(getActivity());
110 sv.setOnQueryTextListener(this);
111 item.setActionView(sv);
115 public boolean onQueryTextSubmit(String query)
116 { /* already handled when the text changes */
121 public boolean onQueryTextChange(String newText)
123 String search = TextUtils.isEmpty(newText) ? null : newText;
124 mAdapter.getFilter().filter(search);
129 public Loader<List<TrustedCertificateEntry>> onCreateLoader(int id, Bundle args)
130 { /* we don't need the id as we have only one loader */
131 return new CertificateListLoader(getActivity(), mSource);
135 public void onLoadFinished(Loader<List<TrustedCertificateEntry>> loader, List<TrustedCertificateEntry> data)
137 mAdapter.setData(data);
145 setListShownNoAnimation(true);
150 public void onLoaderReset(Loader<List<TrustedCertificateEntry>> loader)
152 mAdapter.setData(null);
156 public void onListItemClick(ListView l, View v, int position, long id)
158 if (mListener != null)
160 mListener.onTrustedCertificateSelected(mAdapter.getItem(position));
164 public static class CertificateListLoader extends AsyncTaskLoader<List<TrustedCertificateEntry>>
166 private List<TrustedCertificateEntry> mData;
167 private final TrustedCertificateSource mSource;
168 private TrustedCertificateManagerObserver mObserver;
170 public CertificateListLoader(Context context, TrustedCertificateSource source)
177 public List<TrustedCertificateEntry> loadInBackground()
179 TrustedCertificateManager certman = TrustedCertificateManager.getInstance().load();
180 Hashtable<String, X509Certificate> certificates = certman.getCACertificates(mSource);
181 List<TrustedCertificateEntry> selected;
183 selected = new ArrayList<TrustedCertificateEntry>();
184 for (Entry<String, X509Certificate> entry : certificates.entrySet())
186 selected.add(new TrustedCertificateEntry(entry.getKey(), entry.getValue()));
188 Collections.sort(selected);
193 protected void onStartLoading()
196 { /* if we have data ready, deliver it directly */
197 deliverResult(mData);
199 if (takeContentChanged() || mData == null)
206 public void deliverResult(List<TrustedCertificateEntry> data)
214 { /* if it is started we deliver the data directly,
215 * otherwise this is handled in onStartLoading */
216 if (mObserver == null)
218 mObserver = new TrustedCertificateManagerObserver();
219 TrustedCertificateManager.getInstance().addObserver(mObserver);
221 super.deliverResult(data);
226 protected void onReset()
228 if (mObserver != null)
230 TrustedCertificateManager.getInstance().deleteObserver(mObserver);
238 protected void onAbandon()
240 if (mObserver != null)
242 TrustedCertificateManager.getInstance().deleteObserver(mObserver);
247 private class TrustedCertificateManagerObserver implements Observer
249 private ForceLoadContentObserver mContentObserver = new ForceLoadContentObserver();
252 public void update(Observable observable, Object data)
254 mContentObserver.onChange(false);