Enable search for certificate lists (via SearchView in ActionBar)
authorTobias Brunner <tobias@strongswan.org>
Tue, 14 Aug 2012 09:50:03 +0000 (11:50 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 14 Aug 2012 10:01:41 +0000 (12:01 +0200)
src/frontends/android/res/values-de/strings.xml
src/frontends/android/res/values/strings.xml
src/frontends/android/src/org/strongswan/android/data/TrustedCertificateEntry.java
src/frontends/android/src/org/strongswan/android/ui/TrustedCertificateListFragment.java

index 5207deb..13a34b2 100644 (file)
@@ -22,6 +22,7 @@
     <string name="main_activity_name">strongSwan</string>
     <string name="reload_trusted_certs">CA-Zertifikate neu laden</string>
     <string name="show_log">Log anzeigen</string>
+    <string name="search">Suchen</string>
 
     <!-- Log view -->
     <string name="log_title">Log</string>
index 8188db1..07dd8c2 100644 (file)
@@ -22,6 +22,7 @@
     <string name="main_activity_name">strongSwan</string>
     <string name="reload_trusted_certs">Reload CA certificates</string>
     <string name="show_log">View log</string>
+    <string name="search">Search</string>
 
     <!-- Log view -->
     <string name="log_title">Log</string>
index bedf06f..de7ea32 100644 (file)
@@ -107,6 +107,20 @@ public class TrustedCertificateEntry implements Comparable<TrustedCertificateEnt
        }
 
        @Override
+       public String toString()
+       {       /* combination of both subject lines, used for filtering lists */
+               if (mString == null)
+               {
+                       mString = mSubjectPrimary;
+                       if (!mSubjectSecondary.isEmpty())
+                       {
+                               mString += ", " + mSubjectSecondary;
+                       }
+               }
+               return mString;
+       }
+
+       @Override
        public int compareTo(TrustedCertificateEntry another)
        {
                int diff = mSubjectPrimary.compareToIgnoreCase(another.mSubjectPrimary);
index f5cc57a..4e8e0dd 100644 (file)
@@ -34,10 +34,16 @@ import android.content.AsyncTaskLoader;
 import android.content.Context;
 import android.content.Loader;
 import android.os.Bundle;
+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>>
+public class TrustedCertificateListFragment extends ListFragment implements LoaderCallbacks<List<TrustedCertificateEntry>>, OnQueryTextListener
 {
        private OnTrustedCertificateSelectedListener mListener;
        private TrustedCertificateAdapter mAdapter;
@@ -54,6 +60,7 @@ public class TrustedCertificateListFragment extends ListFragment implements Load
        public void onActivityCreated(Bundle savedInstanceState)
        {
                super.onActivityCreated(savedInstanceState);
+               setHasOptionsMenu(true);
 
                setEmptyText(getString(R.string.no_certificates));
 
@@ -86,6 +93,32 @@ public class TrustedCertificateListFragment extends ListFragment implements Load
        }
 
        @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);