List fragment for trusted certificates can notify listeners about clicks
authorTobias Brunner <tobias@strongswan.org>
Tue, 14 Aug 2012 08:43:03 +0000 (10:43 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 14 Aug 2012 10:01:41 +0000 (12:01 +0200)
src/frontends/android/src/org/strongswan/android/ui/TrustedCertificateListFragment.java

index a64a927..f5cc57a 100644 (file)
@@ -27,18 +27,29 @@ 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;
+import android.view.View;
+import android.widget.ListView;
 
 public class TrustedCertificateListFragment extends ListFragment implements LoaderCallbacks<List<TrustedCertificateEntry>>
 {
+       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)
        {
@@ -64,6 +75,17 @@ 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 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 +112,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;