Enable search for certificate lists (via SearchView in ActionBar)
[strongswan.git] / src / frontends / android / src / org / strongswan / android / ui / TrustedCertificateListFragment.java
index a64a927..4e8e0dd 100644 (file)
@@ -27,22 +27,40 @@ import org.strongswan.android.data.TrustedCertificateEntry;
 import org.strongswan.android.logic.TrustedCertificateManager;
 import org.strongswan.android.ui.adapter.TrustedCertificateAdapter;
 
+import android.app.Activity;
 import android.app.ListFragment;
 import android.app.LoaderManager.LoaderCallbacks;
 import android.content.AsyncTaskLoader;
 import android.content.Context;
 import android.content.Loader;
 import android.os.Bundle;
-
-public class TrustedCertificateListFragment extends ListFragment implements LoaderCallbacks<List<TrustedCertificateEntry>>
+import android.text.TextUtils;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
+import android.view.View;
+import android.widget.ListView;
+import android.widget.SearchView;
+import android.widget.SearchView.OnQueryTextListener;
+
+public class TrustedCertificateListFragment extends ListFragment implements LoaderCallbacks<List<TrustedCertificateEntry>>, OnQueryTextListener
 {
+       private OnTrustedCertificateSelectedListener mListener;
        private TrustedCertificateAdapter mAdapter;
        private boolean mUser;
 
+       /**
+        * The activity containing this fragment should implement this interface
+        */
+       public interface OnTrustedCertificateSelectedListener {
+               public void onTrustedCertificateSelected(TrustedCertificateEntry selected);
+       }
+
        @Override
        public void onActivityCreated(Bundle savedInstanceState)
        {
                super.onActivityCreated(savedInstanceState);
+               setHasOptionsMenu(true);
 
                setEmptyText(getString(R.string.no_certificates));
 
@@ -64,6 +82,43 @@ public class TrustedCertificateListFragment extends ListFragment implements Load
        }
 
        @Override
+       public void onAttach(Activity activity)
+       {
+               super.onAttach(activity);
+
+               if (activity instanceof OnTrustedCertificateSelectedListener)
+               {
+                       mListener = (OnTrustedCertificateSelectedListener)activity;
+               }
+       }
+
+       @Override
+       public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
+       {
+               MenuItem item = menu.add(R.string.search);
+               item.setIcon(android.R.drawable.ic_menu_search);
+               item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
+
+               SearchView sv = new SearchView(getActivity());
+               sv.setOnQueryTextListener(this);
+               item.setActionView(sv);
+       }
+
+       @Override
+       public boolean onQueryTextSubmit(String query)
+       {       /* already handled when the text changes */
+               return true;
+       }
+
+       @Override
+       public boolean onQueryTextChange(String newText)
+       {
+               String search = TextUtils.isEmpty(newText) ? null : newText;
+               mAdapter.getFilter().filter(search);
+               return true;
+       }
+
+       @Override
        public Loader<List<TrustedCertificateEntry>> onCreateLoader(int id, Bundle args)
        {       /* we don't need the id as we have only one loader */
                return new CertificateListLoader(getActivity(), mUser);
@@ -90,6 +145,15 @@ public class TrustedCertificateListFragment extends ListFragment implements Load
                mAdapter.setData(null);
        }
 
+       @Override
+       public void onListItemClick(ListView l, View v, int position, long id)
+       {
+               if (mListener != null)
+               {
+                       mListener.onTrustedCertificateSelected(mAdapter.getItem(position));
+               }
+       }
+
        public static class CertificateListLoader extends AsyncTaskLoader<List<TrustedCertificateEntry>>
        {
                private List<TrustedCertificateEntry> mData;