package com.imofan.android.pns;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.OfflineMessageManager;

import java.util.Iterator;

/**
 *
 */
public class XmppOfflineMessages {
    private final static String LOG_TAG = "Mofang_PNS_XmppOfflineMessages";

    public static void handleOfflineMessages(XMPPConnection connection) throws XMPPException {
        PNLog.i(LOG_TAG, "Begin retrival of offline messages from server");
        OfflineMessageManager offlineMessageManager = new OfflineMessageManager(connection);

        if (!offlineMessageManager.supportsFlexibleRetrieval())
            return;

        Iterator<Message> i = offlineMessageManager.getMessages();
        while (i.hasNext()) {
            PNService.processMessage(i.next());
        }
        offlineMessageManager.deleteMessages();
        PNLog.i(LOG_TAG, "End of retrival of offline messages from server");
    }
}
