New I2PFragmentBase so Fragments can access RouterContext, migration changes

All page logic is in the Fragments. Activities don't need the convenience
methods.
This commit is contained in:
str4d
2013-09-05 06:07:34 +00:00
parent 72ed6bd170
commit 7768c624f9
8 changed files with 183 additions and 109 deletions

View File

@ -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();
}
}

View File

@ -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() {

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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();

View File

@ -78,7 +78,6 @@ public class PeersFragment extends I2PFragmentBase {
}
}
@Override
public boolean onBackPressed() {
WebView wv = (WebView) getActivity().findViewById(R.id.peers_webview);
_wvClient.cancelAll();

View File

@ -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) {

View File

@ -79,7 +79,6 @@ public class WebFragment extends I2PFragmentBase {
}
}
@Override
public boolean onBackPressed() {
WebView wv = (WebView) getActivity().findViewById(R.id.browser_webview);
_wvClient.cancelAll();