Exemple #1
0
  private void registerRosterListener() {
    // flush roster on connecting.
    mRoster = mXMPPConnection.getRoster();
    mRoster.setSubscriptionMode(Roster.SubscriptionMode.manual);

    if (mRosterListener != null) mRoster.removeRosterListener(mRosterListener);

    mRosterListener =
        new RosterListener() {
          private boolean first_roster = true;

          public void entriesAdded(Collection<String> entries) {
            debugLog("entriesAdded(" + entries + ")");

            ContentValues[] cvs = new ContentValues[entries.size()];
            int i = 0;
            for (String entry : entries) {
              RosterEntry rosterEntry = mRoster.getEntry(entry);
              cvs[i++] = getContentValuesForRosterEntry(rosterEntry);
            }
            mContentResolver.bulkInsert(RosterProvider.CONTENT_URI, cvs);
            // when getting the roster in the beginning, remove remains of old one
            if (first_roster) {
              removeOldRosterEntries();
              first_roster = false;
              mServiceCallBack.rosterChanged();
            }
            debugLog("entriesAdded() done");
          }

          public void entriesDeleted(Collection<String> entries) {
            debugLog("entriesDeleted(" + entries + ")");

            for (String entry : entries) {
              deleteRosterEntryFromDB(entry);
            }
            mServiceCallBack.rosterChanged();
          }

          public void entriesUpdated(Collection<String> entries) {
            debugLog("entriesUpdated(" + entries + ")");

            for (String entry : entries) {
              RosterEntry rosterEntry = mRoster.getEntry(entry);
              updateRosterEntryInDB(rosterEntry);
            }
            mServiceCallBack.rosterChanged();
          }

          public void presenceChanged(Presence presence) {
            debugLog("presenceChanged(" + presence.getFrom() + "): " + presence);

            String jabberID = getBareJID(presence.getFrom());
            RosterEntry rosterEntry = mRoster.getEntry(jabberID);
            updateRosterEntryInDB(rosterEntry);
            mServiceCallBack.rosterChanged();
          }
        };
    mRoster.addRosterListener(mRosterListener);
  }
Exemple #2
0
  public void unRegisterCallback() {
    debugLog("unRegisterCallback()");
    // remove callbacks _before_ tossing old connection
    try {
      mXMPPConnection.getRoster().removeRosterListener(mRosterListener);
      mXMPPConnection.removePacketListener(mPacketListener);
      mXMPPConnection.removePacketListener(mPresenceListener);

      mXMPPConnection.removePacketListener(mPongListener);
      unregisterPongListener();
    } catch (Exception e) {
      // ignore it!
      e.printStackTrace();
    }
    requestConnectionState(ConnectionState.OFFLINE);
    setStatusOffline();
    mService.unregisterReceiver(mPingAlarmReceiver);
    mService.unregisterReceiver(mPongTimeoutAlarmReceiver);
    this.mServiceCallBack = null;
  }