1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-25 15:52:06 +01:00

Add RosterLoadedListener

This commit is contained in:
Florian Schmaus 2015-02-17 15:24:33 +01:00
parent b910d026cd
commit 98cfad6cc6
2 changed files with 87 additions and 0 deletions

View file

@ -134,6 +134,11 @@ public class Roster extends Manager {
private final Set<RosterListener> rosterListeners = new LinkedHashSet<>();
private final Map<String, Map<String, Presence>> presenceMap = new ConcurrentHashMap<String, Map<String, Presence>>();
/**
* Listeners called when the Roster was loaded.
*/
private final Set<RosterLoadedListener> rosterLoadedListeners = new LinkedHashSet<>();
/**
* Mutually exclude roster listener invocation and changing the {@link entries} map. Also used
* to synchronize access to either the roster listeners or the entries map.
@ -387,6 +392,34 @@ public class Roster extends Manager {
}
}
/**
* Add a roster loaded listener.
*
* @param rosterLoadedListener the listener to add.
* @return true if the listener was not already added.
* @see RosterLoadedListener
* @since 4.1
*/
public boolean addRosterLoadedListener(RosterLoadedListener rosterLoadedListener) {
synchronized (rosterLoadedListener) {
return rosterLoadedListeners.add(rosterLoadedListener);
}
}
/**
* Remove a roster loaded listener.
*
* @param rosterLoadedListener the listener to remove.
* @return true if the listener was active and got removed.
* @see RosterLoadedListener
* @since 4.1
*/
public boolean removeRosterLoadedListener(RosterLoadedListener rosterLoadedListener) {
synchronized (rosterLoadedListener) {
return rosterLoadedListeners.remove(rosterLoadedListener);
}
}
/**
* Creates a new group.<p>
* <p/>
@ -1309,6 +1342,22 @@ public class Roster extends Manager {
}
// Fire event for roster listeners.
fireRosterChangedEvent(addedEntries, updatedEntries, deletedEntries);
// Call the roster loaded listeners after the roster events have been fired. This is
// imporant because the user may call getEntriesAndAddListener() in onRosterLoaded(),
// and if the order would be the other way around, the roster listener added by
// getEntriesAndAddListener() would be invoked with information that was already
// available at the time getEntriesAndAddListenr() was called.
try {
synchronized (rosterLoadedListeners) {
for (RosterLoadedListener rosterLoadedListener : rosterLoadedListeners) {
rosterLoadedListener.onRosterLoaded(Roster.this);
}
}
}
catch (Exception e) {
LOGGER.log(Level.WARNING, "RosterLoadedListener threw exception", e);
}
}
}

View file

@ -0,0 +1,38 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.roster;
/**
* Roster loaded listeners are invoked once the {@link Roster} was successfully loaded.
* <p>
* A common approach is to call
* {@link Roster#getEntriesAndAddListener(RosterListener, RosterEntries)} within
* {@link #onRosterLoaded(Roster)}, to initialize or update your UI components with the current
* roster state.
* </p>
*/
public interface RosterLoadedListener {
/**
* Called when the Roster was loaded successfully.
*
* @param roster the Roster that was loaded successfully.
*/
public void onRosterLoaded(Roster roster);
}