Skip to content

EnvironmentChanges

Thomas Schwotzer edited this page Aug 16, 2021 · 5 revisions

We already know the ASAPMessageReceivedListener.

There are other listeners:

ASAPEnvironmentChangesListener

The ASAPEnvironmentChangesListener interface has got a single method.

void onlinePeersChanged(Set<CharSequence> peerList);

Applications interested in those changes have to implement and register a listener.

DiscoverPeerAndStartEGChat implements this usage in an example.

private class YourApplication implements ASAPEnvironmentChangesListener /*, .. */ {
private Set<CharSequence> knowPeers = new HashSet<>();

@Override
public void onlinePeersChanged(Set<CharSequence> peerList) {
    // peer list has changed - maybe there is a new peer around
    for(CharSequence maybeNewPeerName : peerList) {
        CharSequence newPeerName = maybeNewPeerName;
        for (CharSequence peerName : this.knowPeers) {
            if(maybeNewPeerName.toString().equalsIgnoreCase(peerName.toString())) {
                newPeerName = null; // not new
                break; // found in my known peers list, try next in peerList
             }
         }
         if(newPeerName != null) {
             // found one - enough for this example
             this.doSomethingWith(newPeerName); // example
             break;
          }
    }
} // method
} // class

This class keeps a list of peers as member (knowPeers ). The listener method is called whenever a change of connections occurs. The algorithm looks for a peer that is not known yet. A method is performed for the first not yet known peer (doSomething()). A message is sent in this example.

Let the complete example run.

A listener is registered with a ASAPPeer.

private class YourApplication implements ASAPEnvironmentChangesListener /*, .. */ {
...
    ASAPPeer peer = ..;
    // object itself implements this listener interface.
    peer.addASAPEnvironmentChangesListener(this);

The object implements the interface itself in this case.

ASAPChannelContentChangedListener

Your application can subscribe with an object that implements ASAPChannelContentChangedListener.

Your listener is informed about newly arrived message by a call of this method:

void asapChannelContentChanged(CharSequence format, CharSequence uri, int era) 

What is the difference to ASAPMessageReceivedListener? There are no message is either parameter but very specific description where to find new messages in the ASAPStorage. This listener comes in handy for application how make use of ASAPStorage. A messenger like application has no need for its own persistent storage. It can directly use ASAPChannels. It just needs to be informed about a change in a channel to update a user interface. In most cases, format and uri are fully sufficient information.