From 7768c624f99e168654a91961e8dc28c8de04a0bd Mon Sep 17 00:00:00 2001
From: str4d <str4d@mail.i2p>
Date: Thu, 5 Sep 2013 06:07:34 +0000
Subject: [PATCH] New I2PFragmentBase so Fragments can access RouterContext,
 migration changes

All page logic is in the Fragments. Activities don't need the convenience
methods.
---
 .../router/activity/I2PActivityBase.java      | 71 ++-------------
 .../android/router/activity/MainActivity.java | 42 ++++++++-
 .../router/fragment/I2PFragmentBase.java      | 91 +++++++++++++++++++
 .../android/router/fragment/MainFragment.java | 51 +++++++----
 .../android/router/fragment/NewsFragment.java |  4 +-
 .../router/fragment/PeersFragment.java        |  1 -
 .../router/fragment/VersionDialog.java        | 31 +++----
 .../android/router/fragment/WebFragment.java  |  1 -
 8 files changed, 183 insertions(+), 109 deletions(-)
 create mode 100644 src/net/i2p/android/router/fragment/I2PFragmentBase.java

diff --git a/src/net/i2p/android/router/activity/I2PActivityBase.java b/src/net/i2p/android/router/activity/I2PActivityBase.java
index 2a173867c..84c7e68fa 100644
--- a/src/net/i2p/android/router/activity/I2PActivityBase.java
+++ b/src/net/i2p/android/router/activity/I2PActivityBase.java
@@ -25,18 +25,13 @@ import android.widget.ListView;
 import net.i2p.android.i2ptunnel.activity.TunnelListActivity;
 import net.i2p.android.router.R;
 import net.i2p.android.router.binder.RouterBinder;
+import net.i2p.android.router.fragment.I2PFragmentBase;
 import net.i2p.android.router.service.RouterService;
 import net.i2p.android.router.util.Util;
-import net.i2p.router.CommSystemFacade;
-import net.i2p.router.NetworkDatabaseFacade;
-import net.i2p.router.Router;
 import net.i2p.router.RouterContext;
-import net.i2p.router.TunnelManagerFacade;
-import net.i2p.router.peermanager.ProfileOrganizer;
-import net.i2p.router.transport.FIFOBandwidthLimiter;
-import net.i2p.stat.StatManager;
 
-public abstract class I2PActivityBase extends ActionBarActivity {
+public abstract class I2PActivityBase extends ActionBarActivity implements
+        I2PFragmentBase.RouterContextProvider {
     /**
      * Navigation drawer variables
      */
@@ -62,7 +57,6 @@ public abstract class I2PActivityBase extends ActionBarActivity {
     protected static final String PREF_AUTO_START = "autoStart";
     /** true leads to a poor install experience, very slow to paint the screen */
     protected static final boolean DEFAULT_AUTO_START = false;
-    protected static final String PREF_INSTALLED_VERSION = "app.version";
 
     /**
      * Override this in subclasses that can use two panes, such as a
@@ -180,19 +174,19 @@ public abstract class I2PActivityBase extends ActionBarActivity {
     }
 
     /** @param def default */
-    protected String getPref(String pref, String def) {
+    public String getPref(String pref, String def) {
         return _sharedPrefs.getString(pref, def);
     }
 
     /** @return success */
-    protected boolean setPref(String pref, boolean val) {
+    public boolean setPref(String pref, boolean val) {
         SharedPreferences.Editor edit = _sharedPrefs.edit();
         edit.putBoolean(pref, val);
         return edit.commit();
     }
 
     /** @return success */
-    protected boolean setPref(String pref, String val) {
+    public boolean setPref(String pref, String val) {
         SharedPreferences.Editor edit = _sharedPrefs.edit();
         edit.putString(pref, val);
         return edit.commit();
@@ -427,61 +421,12 @@ public abstract class I2PActivityBase extends ActionBarActivity {
     /** callback from ServiceConnection, override as necessary */
     protected void onRouterUnbind() {}
 
-    ////// Router stuff
+    // I2PFragmentBase.RouterContextProvider
 
-    protected RouterContext getRouterContext() {
+    public RouterContext getRouterContext() {
         RouterService svc = _routerService;
         if (svc == null || !_isBound)
             return null;
         return svc.getRouterContext();
     }
-
-    protected Router getRouter() {
-        RouterContext ctx = getRouterContext();
-        if (ctx == null)
-            return null;
-        return ctx.router();
-    }
-
-    protected NetworkDatabaseFacade getNetDb() {
-        RouterContext ctx = getRouterContext();
-        if (ctx == null)
-            return null;
-        return ctx.netDb();
-    }
-
-    protected ProfileOrganizer getProfileOrganizer() {
-        RouterContext ctx = getRouterContext();
-        if (ctx == null)
-            return null;
-        return ctx.profileOrganizer();
-    }
-
-    protected TunnelManagerFacade getTunnelManager() {
-        RouterContext ctx = getRouterContext();
-        if (ctx == null)
-            return null;
-        return ctx.tunnelManager();
-    }
-
-    protected CommSystemFacade getCommSystem() {
-        RouterContext ctx = getRouterContext();
-        if (ctx == null)
-            return null;
-        return ctx.commSystem();
-    }
-
-    protected FIFOBandwidthLimiter getBandwidthLimiter() {
-        RouterContext ctx = getRouterContext();
-        if (ctx == null)
-            return null;
-        return ctx.bandwidthLimiter();
-    }
-
-    protected StatManager getStatManager() {
-        RouterContext ctx = getRouterContext();
-        if (ctx == null)
-            return null;
-        return ctx.statManager();
-    }
 }
diff --git a/src/net/i2p/android/router/activity/MainActivity.java b/src/net/i2p/android/router/activity/MainActivity.java
index 0f9baeb2c..e00dfa761 100644
--- a/src/net/i2p/android/router/activity/MainActivity.java
+++ b/src/net/i2p/android/router/activity/MainActivity.java
@@ -1,13 +1,17 @@
 package net.i2p.android.router.activity;
 
+import java.io.File;
+
 import android.os.Bundle;
 import net.i2p.android.router.R;
 import net.i2p.android.router.fragment.MainFragment;
 import net.i2p.android.router.fragment.VersionDialog;
+import net.i2p.android.router.service.RouterService;
 import net.i2p.android.router.util.Util;
 
-public class MainActivity extends I2PActivityBase
-                          implements VersionDialog.VersionDialogListener {
+public class MainActivity extends I2PActivityBase implements
+        MainFragment.RouterControlListener,
+        VersionDialog.VersionDialogListener {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -30,6 +34,40 @@ public class MainActivity extends I2PActivityBase
         super.onPostCreate(savedInstanceState);
     }
 
+    // MainFragment.RouterControlListener
+
+    public boolean shouldShowStart() {
+        RouterService svc = _routerService;
+        return ((svc == null) || (!_isBound) || svc.canManualStart())
+                && Util.isConnected(this);
+    }
+
+    public boolean shouldShowStop() {
+        RouterService svc = _routerService;
+        return svc != null && _isBound && svc.canManualStop();
+    }
+
+    public void onStartRouterClicked() {
+        RouterService svc = _routerService;
+        if(svc != null && _isBound) {
+            setPref(PREF_AUTO_START, true);
+            svc.manualStart();
+        } else {
+            (new File(_myDir, "wrapper.log")).delete();
+            startRouter();
+        }
+    }
+
+    public boolean onStopRouterClicked() {
+        RouterService svc = _routerService;
+        if(svc != null && _isBound) {
+            setPref(PREF_AUTO_START, false);
+            svc.manualQuit();
+            return true;
+        }
+        return false;
+    }
+
     // VersionDialog.VersionDialogListener
 
     public void onFirstRun() {
diff --git a/src/net/i2p/android/router/fragment/I2PFragmentBase.java b/src/net/i2p/android/router/fragment/I2PFragmentBase.java
new file mode 100644
index 000000000..c54b1719a
--- /dev/null
+++ b/src/net/i2p/android/router/fragment/I2PFragmentBase.java
@@ -0,0 +1,91 @@
+package net.i2p.android.router.fragment;
+
+import net.i2p.router.CommSystemFacade;
+import net.i2p.router.NetworkDatabaseFacade;
+import net.i2p.router.Router;
+import net.i2p.router.RouterContext;
+import net.i2p.router.TunnelManagerFacade;
+import net.i2p.router.peermanager.ProfileOrganizer;
+import net.i2p.router.transport.FIFOBandwidthLimiter;
+import net.i2p.stat.StatManager;
+import android.app.Activity;
+import android.support.v4.app.Fragment;
+
+public class I2PFragmentBase extends Fragment {
+    RouterContextProvider mCallback;
+
+    protected static final String PREF_INSTALLED_VERSION = "app.version";
+
+    // Container Activity must implement this interface
+    public interface RouterContextProvider {
+        public RouterContext getRouterContext();
+    }
+
+    @Override
+    public void onAttach(Activity activity) {
+        super.onAttach(activity);
+
+        // This makes sure that the container activity has implemented
+        // the callback interface. If not, it throws an exception
+        try {
+            mCallback = (RouterContextProvider) activity;
+        } catch (ClassCastException e) {
+            throw new ClassCastException(activity.toString()
+                    + " must implement RouterContextProvider");
+        }
+
+    }
+
+    protected RouterContext getRouterContext() {
+        return mCallback.getRouterContext();
+    }
+
+    protected Router getRouter() {
+        RouterContext ctx = getRouterContext();
+        if (ctx == null)
+            return null;
+        return ctx.router();
+    }
+
+    protected NetworkDatabaseFacade getNetDb() {
+        RouterContext ctx = getRouterContext();
+        if (ctx == null)
+            return null;
+        return ctx.netDb();
+    }
+
+    protected ProfileOrganizer getProfileOrganizer() {
+        RouterContext ctx = getRouterContext();
+        if (ctx == null)
+            return null;
+        return ctx.profileOrganizer();
+    }
+
+    protected TunnelManagerFacade getTunnelManager() {
+        RouterContext ctx = getRouterContext();
+        if (ctx == null)
+            return null;
+        return ctx.tunnelManager();
+    }
+
+    protected CommSystemFacade getCommSystem() {
+        RouterContext ctx = getRouterContext();
+        if (ctx == null)
+            return null;
+        return ctx.commSystem();
+    }
+
+    protected FIFOBandwidthLimiter getBandwidthLimiter() {
+        RouterContext ctx = getRouterContext();
+        if (ctx == null)
+            return null;
+        return ctx.bandwidthLimiter();
+    }
+
+    protected StatManager getStatManager() {
+        RouterContext ctx = getRouterContext();
+        if (ctx == null)
+            return null;
+        return ctx.statManager();
+    }
+}
diff --git a/src/net/i2p/android/router/fragment/MainFragment.java b/src/net/i2p/android/router/fragment/MainFragment.java
index 36310861a..41a550dd6 100644
--- a/src/net/i2p/android/router/fragment/MainFragment.java
+++ b/src/net/i2p/android/router/fragment/MainFragment.java
@@ -1,5 +1,6 @@
 package net.i2p.android.router.fragment;
 
+import android.app.Activity;
 import android.os.Bundle;
 import android.os.Handler;
 import android.view.LayoutInflater;
@@ -7,10 +8,9 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.TextView;
-import java.io.File;
 import java.text.DecimalFormat;
 import net.i2p.android.router.R;
-import net.i2p.android.router.service.RouterService;
+import net.i2p.android.router.activity.I2PActivityBase;
 import net.i2p.android.router.util.Util;
 import net.i2p.data.DataHelper;
 import net.i2p.router.RouterContext;
@@ -26,6 +26,30 @@ public class MainFragment extends I2PFragmentBase {
     private boolean _startPressed = false;
     protected static final String PROP_NEW_INSTALL = "i2p.newInstall";
     protected static final String PROP_NEW_VERSION = "i2p.newVersion";
+    RouterControlListener mCallback;
+
+    // Container Activity must implement this interface
+    public interface RouterControlListener {
+        public boolean shouldShowStart();
+        public boolean shouldShowStop();
+        public void onStartRouterClicked();
+        public boolean onStopRouterClicked();
+    }
+
+    @Override
+    public void onAttach(Activity activity) {
+        super.onAttach(activity);
+
+        // This makes sure that the container activity has implemented
+        // the callback interface. If not, it throws an exception
+        try {
+            mCallback = (RouterControlListener) activity;
+        } catch (ClassCastException e) {
+            throw new ClassCastException(activity.toString()
+                    + " must implement RouterControlListener");
+        }
+
+    }
 
     /**
      * Called when the fragment is first created.
@@ -34,7 +58,6 @@ public class MainFragment extends I2PFragmentBase {
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         // Init stuff here so settings work.
-        _myDir = getActivity().getFilesDir().getAbsolutePath();
         if(savedInstanceState != null) {
             String saved = savedInstanceState.getString("status");
             if(saved != null) {
@@ -142,14 +165,7 @@ public class MainFragment extends I2PFragmentBase {
 
             public void onClick(View view) {
                 _startPressed = true;
-                RouterService svc = _routerService;
-                if(svc != null && _isBound) {
-                    setPref(PREF_AUTO_START, true);
-                    svc.manualStart();
-                } else {
-                    (new File(_myDir, "wrapper.log")).delete();
-                    startRouter();
-                }
+                mCallback.onStartRouterClicked();
                 updateOneShot();
             }
         });
@@ -158,10 +174,7 @@ public class MainFragment extends I2PFragmentBase {
         b.setOnClickListener(new View.OnClickListener() {
 
             public void onClick(View view) {
-                RouterService svc = _routerService;
-                if(svc != null && _isBound) {
-                    setPref(PREF_AUTO_START, false);
-                    svc.manualQuit();
+                if(mCallback.onStopRouterClicked()) {
                     updateOneShot();
                 }
             }
@@ -232,13 +245,11 @@ public class MainFragment extends I2PFragmentBase {
     }
 
     private void updateVisibility() {
-        RouterService svc = _routerService;
-        boolean showStart = ((svc == null) || (!_isBound) || svc.canManualStart())
-                && Util.isConnected(getActivity());
+        boolean showStart = mCallback.shouldShowStart();
         Button start = (Button) getActivity().findViewById(R.id.router_start_button);
         start.setVisibility(showStart ? View.VISIBLE : View.INVISIBLE);
 
-        boolean showStop = svc != null && _isBound && svc.canManualStop();
+        boolean showStop = mCallback.shouldShowStop();
         // Old stop but leave in memory. Always hide for now.
         // Button stop = (Button) findViewById(R.id.router_stop_button);
         // stop.setVisibility( /* showStop ? View.VISIBLE : */ View.INVISIBLE);
@@ -394,7 +405,7 @@ public class MainFragment extends I2PFragmentBase {
 
     private void checkDialog() {
         VersionDialog dialog = new VersionDialog();
-        String oldVersion = getPref(PREF_INSTALLED_VERSION, "??");
+        String oldVersion = ((I2PActivityBase) getActivity()).getPref(PREF_INSTALLED_VERSION, "??");
         if(oldVersion.equals("??")) {
             Bundle args = new Bundle();
             args.putInt(VersionDialog.DIALOG_TYPE, VersionDialog.DIALOG_NEW_INSTALL);
diff --git a/src/net/i2p/android/router/fragment/NewsFragment.java b/src/net/i2p/android/router/fragment/NewsFragment.java
index a1a00d40d..9b988d03c 100644
--- a/src/net/i2p/android/router/fragment/NewsFragment.java
+++ b/src/net/i2p/android/router/fragment/NewsFragment.java
@@ -57,7 +57,8 @@ public class NewsFragment extends I2PFragmentBase {
         }
 
         // only update the webview if we need to
-        File newsFile = new File(_myDir, "docs/news.xml");
+        // XXX Gets dir directly instead of the one stored in the Activity (for now)
+        File newsFile = new File(getActivity().getFilesDir().getAbsolutePath(), "docs/news.xml");
         boolean newsExists = newsFile.exists();
         if (_lastChanged > 0 && ((!newsExists) || newsFile.lastModified() < _lastChanged))
             return;
@@ -96,7 +97,6 @@ public class NewsFragment extends I2PFragmentBase {
         }
     }
 
-    @Override
     public boolean onBackPressed() {
         WebView wv = (WebView) getActivity().findViewById(R.id.news_webview);
         _wvClient.cancelAll();
diff --git a/src/net/i2p/android/router/fragment/PeersFragment.java b/src/net/i2p/android/router/fragment/PeersFragment.java
index b8c5b9d11..010b5d648 100644
--- a/src/net/i2p/android/router/fragment/PeersFragment.java
+++ b/src/net/i2p/android/router/fragment/PeersFragment.java
@@ -78,7 +78,6 @@ public class PeersFragment extends I2PFragmentBase {
         }
     }
 
-    @Override
     public boolean onBackPressed() {
         WebView wv = (WebView) getActivity().findViewById(R.id.peers_webview);
         _wvClient.cancelAll();
diff --git a/src/net/i2p/android/router/fragment/VersionDialog.java b/src/net/i2p/android/router/fragment/VersionDialog.java
index 610457b13..857eed20d 100644
--- a/src/net/i2p/android/router/fragment/VersionDialog.java
+++ b/src/net/i2p/android/router/fragment/VersionDialog.java
@@ -1,6 +1,7 @@
 package net.i2p.android.router.fragment;
 
 import net.i2p.android.router.R;
+import net.i2p.android.router.activity.I2PActivityBase;
 import net.i2p.android.router.activity.LicenseActivity;
 import net.i2p.android.router.util.Util;
 import android.app.Activity;
@@ -50,20 +51,16 @@ public class VersionDialog extends DialogFragment {
                  .setPositiveButton("OK", new DialogInterface.OnClickListener() {
 
                     public void onClick(DialogInterface dialog, int id) {
-                        I2PFragmentBase fb = (I2PFragmentBase) getActivity()
-                                .getSupportFragmentManager()
-                                .findFragmentById(R.id.main_fragment);
-                        fb.setPref(MainFragment.PREF_INSTALLED_VERSION, currentVersion);
+                        I2PActivityBase ab = (I2PActivityBase) getActivity();
+                        ab.setPref(MainFragment.PREF_INSTALLED_VERSION, currentVersion);
                         dialog.cancel();
                         mListener.onFirstRun();
                     }
                 }).setNeutralButton(R.string.label_release_notes, new DialogInterface.OnClickListener() {
 
                     public void onClick(DialogInterface dialog, int id) {
-                        I2PFragmentBase fb = (I2PFragmentBase) getActivity()
-                                .getSupportFragmentManager()
-                                .findFragmentById(R.id.main_fragment);
-                        fb.setPref(MainFragment.PREF_INSTALLED_VERSION, currentVersion);
+                        I2PActivityBase ab = (I2PActivityBase) getActivity();
+                        ab.setPref(MainFragment.PREF_INSTALLED_VERSION, currentVersion);
                         dialog.cancel();
                         TextResourceDialog f = new TextResourceDialog();
                         Bundle args = new Bundle();
@@ -78,10 +75,8 @@ public class VersionDialog extends DialogFragment {
                 }).setNegativeButton(R.string.label_licenses, new DialogInterface.OnClickListener() {
 
                     public void onClick(DialogInterface dialog, int id) {
-                        I2PFragmentBase fb = (I2PFragmentBase) getActivity()
-                                .getSupportFragmentManager()
-                                .findFragmentById(R.id.main_fragment);
-                        fb.setPref(MainFragment.PREF_INSTALLED_VERSION, currentVersion);
+                        I2PActivityBase ab = (I2PActivityBase) getActivity();
+                        ab.setPref(MainFragment.PREF_INSTALLED_VERSION, currentVersion);
                         dialog.cancel();
                         Intent intent = new Intent(getActivity(), LicenseActivity.class);
                         startActivity(intent);
@@ -96,10 +91,8 @@ public class VersionDialog extends DialogFragment {
                  .setPositiveButton("OK", new DialogInterface.OnClickListener() {
 
                     public void onClick(DialogInterface dialog, int id) {
-                        I2PFragmentBase fb = (I2PFragmentBase) getActivity()
-                                .getSupportFragmentManager()
-                                .findFragmentById(R.id.main_fragment);
-                        fb.setPref(MainFragment.PREF_INSTALLED_VERSION, currentVersion);
+                        I2PActivityBase ab = (I2PActivityBase) getActivity();
+                        ab.setPref(MainFragment.PREF_INSTALLED_VERSION, currentVersion);
                         try {
                             dialog.dismiss();
                         } catch(Exception e) {
@@ -108,10 +101,8 @@ public class VersionDialog extends DialogFragment {
                 }).setNegativeButton(R.string.label_release_notes, new DialogInterface.OnClickListener() {
 
                     public void onClick(DialogInterface dialog, int id) {
-                        I2PFragmentBase fb = (I2PFragmentBase) getActivity()
-                                .getSupportFragmentManager()
-                                .findFragmentById(R.id.main_fragment);
-                        fb.setPref(MainFragment.PREF_INSTALLED_VERSION, currentVersion);
+                        I2PActivityBase ab = (I2PActivityBase) getActivity();
+                        ab.setPref(MainFragment.PREF_INSTALLED_VERSION, currentVersion);
                         try {
                             dialog.dismiss();
                         } catch(Exception e) {
diff --git a/src/net/i2p/android/router/fragment/WebFragment.java b/src/net/i2p/android/router/fragment/WebFragment.java
index e05f4d488..cb31713f6 100644
--- a/src/net/i2p/android/router/fragment/WebFragment.java
+++ b/src/net/i2p/android/router/fragment/WebFragment.java
@@ -79,7 +79,6 @@ public class WebFragment extends I2PFragmentBase {
         }
     }
 
-    @Override
     public boolean onBackPressed() {
         WebView wv = (WebView) getActivity().findViewById(R.id.browser_webview);
         _wvClient.cancelAll();
-- 
GitLab