android: Automatically reload certificates if manager is reset
authorTobias Brunner <tobias@strongswan.org>
Thu, 26 Nov 2015 15:33:08 +0000 (16:33 +0100)
committerTobias Brunner <tobias@strongswan.org>
Wed, 27 Apr 2016 12:24:26 +0000 (14:24 +0200)
No need to manually reset the fragments anymore.

src/frontends/android/app/src/main/java/org/strongswan/android/ui/TrustedCertificateListFragment.java
src/frontends/android/app/src/main/java/org/strongswan/android/ui/TrustedCertificatesActivity.java

index 12da596..9782a30 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012-2014 Tobias Brunner
+ * Copyright (C) 2012-2015 Tobias Brunner
  * Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
 
 package org.strongswan.android.ui;
 
-import android.app.Activity;
 import android.content.Context;
 import android.os.Bundle;
 import android.support.v4.app.ListFragment;
-import android.support.v4.app.LoaderManager;
+import android.support.v4.app.LoaderManager.LoaderCallbacks;
 import android.support.v4.content.AsyncTaskLoader;
 import android.support.v4.content.Loader;
 import android.text.TextUtils;
@@ -43,8 +42,10 @@ import java.util.Collections;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map.Entry;
+import java.util.Observable;
+import java.util.Observer;
 
-public class TrustedCertificateListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<TrustedCertificateEntry>>, OnQueryTextListener
+public class TrustedCertificateListFragment extends ListFragment implements LoaderCallbacks<List<TrustedCertificateEntry>>, OnQueryTextListener
 {
        public static final String EXTRA_CERTIFICATE_SOURCE = "certificate_source";
        private OnTrustedCertificateSelectedListener mListener;
@@ -88,13 +89,13 @@ public class TrustedCertificateListFragment extends ListFragment implements Load
        }
 
        @Override
-       public void onAttach(Activity activity)
+       public void onAttach(Context context)
        {
-               super.onAttach(activity);
+               super.onAttach(context);
 
-               if (activity instanceof OnTrustedCertificateSelectedListener)
+               if (context instanceof OnTrustedCertificateSelectedListener)
                {
-                       mListener = (OnTrustedCertificateSelectedListener)activity;
+                       mListener = (OnTrustedCertificateSelectedListener)context;
                }
        }
 
@@ -124,18 +125,6 @@ public class TrustedCertificateListFragment extends ListFragment implements Load
                return true;
        }
 
-       /**
-        * Reset the loader of this list fragment
-        */
-       public void reset()
-       {
-               if (isResumed())
-               {
-                       setListShown(false);
-               }
-               getLoaderManager().restartLoader(0, null, this);
-       }
-
        @Override
        public Loader<List<TrustedCertificateEntry>> onCreateLoader(int id, Bundle args)
        {       /* we don't need the id as we have only one loader */
@@ -176,6 +165,7 @@ public class TrustedCertificateListFragment extends ListFragment implements Load
        {
                private List<TrustedCertificateEntry> mData;
                private final TrustedCertificateSource mSource;
+               private TrustedCertificateManagerObserver mObserver;
 
                public CertificateListLoader(Context context, TrustedCertificateSource source)
                {
@@ -205,9 +195,11 @@ public class TrustedCertificateListFragment extends ListFragment implements Load
                        if (mData != null)
                        {       /* if we have data ready, deliver it directly */
                                deliverResult(mData);
-                               return;
                        }
-                       forceLoad();
+                       if (takeContentChanged() || mData == null)
+                       {
+                               forceLoad();
+                       }
                }
 
                @Override
@@ -221,6 +213,11 @@ public class TrustedCertificateListFragment extends ListFragment implements Load
                        if (isStarted())
                        {       /* if it is started we deliver the data directly,
                                 * otherwise this is handled in onStartLoading */
+                               if (mObserver == null)
+                               {
+                                       mObserver = new TrustedCertificateManagerObserver();
+                                       TrustedCertificateManager.getInstance().addObserver(mObserver);
+                               }
                                super.deliverResult(data);
                        }
                }
@@ -228,7 +225,34 @@ public class TrustedCertificateListFragment extends ListFragment implements Load
                @Override
                protected void onReset()
                {
+                       if (mObserver != null)
+                       {
+                               TrustedCertificateManager.getInstance().deleteObserver(mObserver);
+                               mObserver = null;
+                       }
                        mData = null;
+                       super.onReset();
+               }
+
+               @Override
+               protected void onAbandon()
+               {
+                       if (mObserver != null)
+                       {
+                               TrustedCertificateManager.getInstance().deleteObserver(mObserver);
+                               mObserver = null;
+                       }
+               }
+
+               private class TrustedCertificateManagerObserver implements Observer
+               {
+                       private ForceLoadContentObserver mContentObserver = new ForceLoadContentObserver();
+
+                       @Override
+                       public void update(Observable observable, Object data)
+                       {
+                               mContentObserver.onChange(false);
+                       }
                }
        }
 }
index bba61ba..c817438 100644 (file)
@@ -182,12 +182,6 @@ public class TrustedCertificatesActivity extends AppCompatActivity implements Tr
        private void reloadCertificates()
        {
                TrustedCertificateManager.getInstance().reset();
-               for (int i = 0; i < getSupportActionBar().getTabCount(); i++)
-               {
-                       ActionBar.Tab tab = getSupportActionBar().getTabAt(i);
-                       TrustedCertificatesTabListener listener = (TrustedCertificatesTabListener)tab.getTag();
-                       listener.reset();
-               }
        }
 
        public static class TrustedCertificatesTabListener implements ActionBar.TabListener
@@ -243,13 +237,5 @@ public class TrustedCertificatesActivity extends AppCompatActivity implements Tr
                {
                        /* nothing to be done */
                }
-
-               public void reset()
-               {
-                       if (mFragment != null)
-                       {
-                               ((TrustedCertificateListFragment)mFragment).reset();
-                       }
-               }
        }
 }