New callback system to make getRouterContext() reliable

This commit is contained in:
str4d
2013-11-16 21:56:10 +00:00
parent 9f535a3260
commit 9832779a50
2 changed files with 31 additions and 1 deletions

View File

@ -406,7 +406,13 @@ public abstract class I2PActivityBase extends ActionBarActivity implements
}
/** callback from ServiceConnection, override as necessary */
protected void onRouterBind(RouterService svc) {}
protected void onRouterBind(RouterService svc) {
Fragment f = getSupportFragmentManager().findFragmentById(R.id.main_fragment);
if (f instanceof I2PFragmentBase)
((I2PFragmentBase) f).onRouterBind();
else if (f instanceof I2PFragmentBase.RouterContextUser)
((I2PFragmentBase.RouterContextUser) f).onRouterBind();
}
/** callback from ServiceConnection, override as necessary */
protected void onRouterUnbind() {}

View File

@ -9,13 +9,20 @@ import net.i2p.router.peermanager.ProfileOrganizer;
import net.i2p.router.transport.FIFOBandwidthLimiter;
import net.i2p.stat.StatManager;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
public class I2PFragmentBase extends Fragment {
private boolean mOnActivityCreated;
private boolean mOnRouterBind;
RouterContextProvider mCallback;
protected static final String PREF_INSTALLED_VERSION = "app.version";
public interface RouterContextUser {
public void onRouterBind();
}
// Container Activity must implement this interface
public interface RouterContextProvider {
public RouterContext getRouterContext();
@ -36,6 +43,23 @@ public class I2PFragmentBase extends Fragment {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mOnActivityCreated = true;
if (mOnRouterBind)
onRouterConnectionReady();
}
public void onRouterBind() {
mOnRouterBind = true;
if (mOnActivityCreated)
onRouterConnectionReady();
}
/** callback from I2PFragmentBase, override as necessary */
public void onRouterConnectionReady() {}
protected RouterContext getRouterContext() {
return mCallback.getRouterContext();
}