Example #1
0
    private void rejoinRooms() {
      String[][] mucDB = mMucHelper.getAllMUC();
      if (mucDB == null) {
        return;
      }

      for (String[] aMucDB : mucDB) {
        if (!mConnection.isAuthenticated()) {
          return;
        }

        Log.i(
            "Trying to reconnect to the room with parameters: Muc="
                + aMucDB[0]
                + ", Number="
                + aMucDB[1]
                + ", Mode="
                + aMucDB[2]);

        RoomInfo info = getRoomInfo(aMucDB[0]);
        // if info is not null, the room exists on the server, so lets check if we can reuse it
        if (info != null) {
          MultiUserChat muc = new MultiUserChat(mConnection, aMucDB[0]);
          int mode = Integer.parseInt(aMucDB[2]);
          // Hardcoded room name for shell
          String name =
              mode == MODE_SMS
                  ? ContactsManager.getContactName(mCtx, aMucDB[1])
                  : "Shell " + aMucDB[1];

          try {
            if (info.isPasswordProtected()) {
              muc.join(name, mSettings.roomPassword, mDiscussionHistory, JOIN_TIMEOUT);
            } else {
              muc.join(name, null, mDiscussionHistory, JOIN_TIMEOUT);

              // Openfire needs some time to collect the owners list
              try {
                Thread.sleep(REJOIN_ROOMS_SLEEP);
              } catch (InterruptedException e1) {
                /* Ignore */
              }
              // check here if we are still owner of these room, in case somebody has taken over
              // ownership
              // sadly getOwners() throws sometimes a 403 on my openfire server
              try {
                if (!affiliateCheck(muc.getOwners())) {
                  Log.i("rejoinRooms: leaving " + muc.getRoom() + " because affiliateCheck failed");
                  leaveRoom(muc);
                  continue;
                }

                // TODO this shouldn't happen any more
                // catch the 403 that sometimes shows up and fall back to some easier check if the
                // room
                // is still under our control
              } catch (XMPPException e) {
                Log.d("rejoinRooms: Exception, falling back", e);
                if (!(info.isMembersOnly() || info.isPasswordProtected())) {
                  Log.i(
                      "rejoinRooms: leaving "
                          + muc.getRoom()
                          + " because of membersOnly="
                          + info.isMembersOnly()
                          + " passwordProtected="
                          + info.isPasswordProtected());
                  leaveRoom(muc);
                  continue;
                }
              }
            }
            // looks like there is no one in the room
            if (info.getOccupantsCount() == 0) {
              Log.i("rejoinRooms: leaving " + muc.getRoom() + " because there is no one there");
              leaveRoom(muc);
              continue;
            }
          } catch (Exception e) {
            Log.i("rejoinRooms: leaving " + muc.getRoom() + " because of XMMPException", e);

            // TODO decide in which cases it would be the best to remove the room from the DB,
            // because of a persistent error
            // and in which cases the error will not be permanent
            if (mConnection.isAuthenticated()) {
              try {
                leaveRoom(muc);
              } catch (SmackException.NotConnectedException e1) {
                Log.i(
                    "rejoinRooms: error when leaving " + muc.getRoom() + " because of Exception",
                    e);
              }
              continue;
            } else {
              break;
            }
          }

          Log.i("Connected to the room '" + aMucDB[0]);

          // MUC has passed all tests and is fully usable
          registerRoom(muc, aMucDB[1], name, mode);
        } else {
          Log.i("The room '" + aMucDB[0] + "'is no more available");
          mMucHelper.deleteMUC(aMucDB[0]);
        }
      }
    }