Search code examples
javaandroidobserver-pattern

Java Observer Problem


I am having some trouble with implementing my own observer in Java on the Android platform.

I have created a class call NetworkPathJni that uses an Observer interface class called NetworkPathJniObserver to notify other objects of changes.

Here is the code for NetworkPathJni.java


public class NetworkPathJni {

NetworkPathJniObserver networkPathJniObserver;

  public NetworkPathJni(NetworkPathJniObserver aObserver){

    networkPathJniObserver = aObserver;
    Log.d("Phone", "NetworkPathJni Created" );

  }

  public void NetworkPathStateChanged(int aAvailabilityState){
      Log.d("Phone", "NetworkPathStateChanged new state = " + aAvailabilityState );
      TAvailabilityState availabilityState = intToAvailability(aAvailabilityState);
      Log.d("Phone", "Is SipNetworkPath alive?" +  networkPathJniObserver.isAlive());
      networkPathJniObserver.NetworkPathStateChanged(availabilityState);
      Log.d("Phone", "NetworkPathStateChanged end" );
      Log.d("Phone", "Is SipNetworkPath alive? (2)" +  networkPathJniObserver.isAlive());

  }

And here is the code for the observer


public interface NetworkPathJniObserver {

void NetworkPathStateChanged(TAvailabilityState aAvailabilityState);

boolean isAlive();
}

The observer is implemented as follows in a class called SipNetworkPath


public class SipNetworkPath implements NetworkPathInterface, NetworkPathJniObserver{

NetworkPathObserverInterface observer;
NetworkPathJni networkPathJni;

public SipNetworkPath(NetworkPathObserverInterface aObserver){
    domainType = aDomainType;
    observer = aObserver;
    networkPathJni = new NetworkPathJni(this);
    Log.d("Phone", "SipNetworkPath created" );
}

//NetworkPathJniObserver

@Override
public void NetworkPathStateChanged(TAvailabilityState availabilityState) {
    Log.d("SipNetworkPath", "SipNetworkPath - Do networkpathstate changed!");
}

@Override
public boolean isAlive() {
    return true;

}

And SipNetworkPath is instanciated as follows


public class WifiNetworkPath extends SipNetworkPath{

public WifiNetworkPath(NetworkPathObserverInterface aObserver) {
    super(aObserver);
}

The logging shows that both NetworkPathJni and SipNetworkPath get created and that NetworkPathStateChanged(int aAvailabilityState) is called.

Within that method all the logging comes back but the method does not get called in the observer and I get false when I ask "Is SipNetworkPath alive?" in the logging.

Is the observer class losing reference or something or is there a mistake in my way of doing this?


Solution

  • In method NetworkPathStateChanged you're doing nothing with the availability state. Usually you would store it internally, like this:

    private TAvailabilityState availablilityState = null; // or a more suiteable initial value
    @Override
    public void NetworkPathStateChanged(TAvailabilityState availabilityState) {
        this.availabilityState = availabilityState;
        Log.d("SipNetworkPath", "SipNetworkPath - Do networkpathstate changed!");
    }
    

    Then, in method isAlive you would respond based on the state:

    @Override
    public boolean isAlive() {
        return availabilityState == TAvailabilityState.ALIVE; // <-- this is just a guess, I don't know this class or enum.  
    }
    

    In this case (referring to your comment) another implementation of NetworkPathJniObserver must exist somewhere or you still have an old class file on the classpath. Just because: if an isAlive() method returns false while the one in the source code can't, then there must be some other code in your application.

    I'd add some debug code to the method to clearly log which instances are actually called. (My guess would be, that the last compiled version(s) hasn't been deployed...)