2 * Copyright (C) 2012-2015 Tobias Brunner
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 package org.strongswan.android.ui;
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.text.TextUtils;
22 import android.view.Menu;
23 import android.view.MenuInflater;
24 import android.view.MenuItem;
25 import android.view.View;
26 import android.widget.ListView;
27 import android.widget.SearchView;
28 import android.widget.SearchView.OnQueryTextListener;
30 import org.strongswan.android.R;
31 import org.strongswan.android.logic.TrustedCertificateManager;
32 import org.strongswan.android.logic.TrustedCertificateManager.TrustedCertificateSource;
33 import org.strongswan.android.security.TrustedCertificateEntry;
34 import org.strongswan.android.ui.adapter.TrustedCertificateAdapter;
36 import java.beans.PropertyChangeEvent;
37 import java.beans.PropertyChangeListener;
38 import java.security.cert.X509Certificate;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.Hashtable;
42 import java.util.List;
43 import java.util.Map.Entry;
45 import androidx.annotation.NonNull;
46 import androidx.annotation.Nullable;
47 import androidx.fragment.app.ListFragment;
48 import androidx.loader.app.LoaderManager;
49 import androidx.loader.app.LoaderManager.LoaderCallbacks;
50 import androidx.loader.content.AsyncTaskLoader;
51 import androidx.loader.content.Loader;
53 public class TrustedCertificateListFragment extends ListFragment implements LoaderCallbacks<List<TrustedCertificateEntry>>, OnQueryTextListener
55 public static final String EXTRA_CERTIFICATE_SOURCE = "certificate_source";
56 private OnTrustedCertificateSelectedListener mListener;
57 private TrustedCertificateAdapter mAdapter;
58 private TrustedCertificateSource mSource = TrustedCertificateSource.SYSTEM;
61 * The activity containing this fragment should implement this interface
63 public interface OnTrustedCertificateSelectedListener
65 public void onTrustedCertificateSelected(TrustedCertificateEntry selected);
69 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
71 super.onViewCreated(view, savedInstanceState);
72 setHasOptionsMenu(true);
74 setEmptyText(getString(R.string.no_certificates));
76 mAdapter = new TrustedCertificateAdapter(getActivity());
77 setListAdapter(mAdapter);
81 Bundle arguments = getArguments();
82 if (arguments != null)
84 mSource = (TrustedCertificateSource)arguments.getSerializable(EXTRA_CERTIFICATE_SOURCE);
87 LoaderManager.getInstance(this).initLoader(0, null, this);
91 public void onDestroy()
97 public void onAttach(Context context)
99 super.onAttach(context);
101 if (context instanceof OnTrustedCertificateSelectedListener)
103 mListener = (OnTrustedCertificateSelectedListener)context;
108 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
110 MenuItem item = menu.add(R.string.search);
111 item.setIcon(android.R.drawable.ic_menu_search);
112 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
114 SearchView sv = new SearchView(getActivity());
115 sv.setOnQueryTextListener(this);
116 item.setActionView(sv);
120 public boolean onQueryTextSubmit(String query)
121 { /* already handled when the text changes */
126 public boolean onQueryTextChange(String newText)
128 String search = TextUtils.isEmpty(newText) ? null : newText;
129 mAdapter.getFilter().filter(search);
134 public Loader<List<TrustedCertificateEntry>> onCreateLoader(int id, Bundle args)
135 { /* we don't need the id as we have only one loader */
136 return new CertificateListLoader(getActivity(), mSource);
140 public void onLoadFinished(Loader<List<TrustedCertificateEntry>> loader, List<TrustedCertificateEntry> data)
142 mAdapter.setData(data);
150 setListShownNoAnimation(true);
155 public void onLoaderReset(Loader<List<TrustedCertificateEntry>> loader)
157 mAdapter.setData(null);
161 public void onListItemClick(ListView l, View v, int position, long id)
163 if (mListener != null)
165 mListener.onTrustedCertificateSelected(mAdapter.getItem(position));
169 public static class CertificateListLoader extends AsyncTaskLoader<List<TrustedCertificateEntry>>
171 private List<TrustedCertificateEntry> mData;
172 private final TrustedCertificateSource mSource;
173 private TrustedCertificateManagerObserver mObserver;
175 public CertificateListLoader(Context context, TrustedCertificateSource source)
182 public List<TrustedCertificateEntry> loadInBackground()
184 TrustedCertificateManager certman = TrustedCertificateManager.getInstance().load();
185 Hashtable<String, X509Certificate> certificates = certman.getCACertificates(mSource);
186 List<TrustedCertificateEntry> selected;
188 selected = new ArrayList<TrustedCertificateEntry>();
189 for (Entry<String, X509Certificate> entry : certificates.entrySet())
191 selected.add(new TrustedCertificateEntry(entry.getKey(), entry.getValue()));
193 Collections.sort(selected);
198 protected void onStartLoading()
201 { /* if we have data ready, deliver it directly */
202 deliverResult(mData);
204 if (takeContentChanged() || mData == null)
211 public void deliverResult(List<TrustedCertificateEntry> data)
219 { /* if it is started we deliver the data directly,
220 * otherwise this is handled in onStartLoading */
221 if (mObserver == null)
223 mObserver = new TrustedCertificateManagerObserver();
224 TrustedCertificateManager.getInstance().addObserver(mObserver);
226 super.deliverResult(data);
231 protected void onReset()
233 if (mObserver != null)
235 TrustedCertificateManager.getInstance().deleteObserver(mObserver);
243 protected void onAbandon()
245 if (mObserver != null)
247 TrustedCertificateManager.getInstance().deleteObserver(mObserver);
252 private class TrustedCertificateManagerObserver implements PropertyChangeListener
254 private ForceLoadContentObserver mContentObserver = new ForceLoadContentObserver();
257 public void propertyChange(PropertyChangeEvent evt)
259 mContentObserver.onChange(false);