@Override
 public void onInit(int status) {
   if (status == TextToSpeech.SUCCESS && mTts != null) {
     Log.i("TTS initialized!");
     mTts.setLanguage(mLocale);
     mTtsAvailable = true;
   } else {
     Log.e("Can't initialise TTS!");
     mTtsAvailable = false;
   }
 }
예제 #2
0
  /**
   * Invites the user to a room for the given contact name and number if the user (or someone else)
   * writes to this room, a SMS is send to the number
   *
   * @param number
   * @return true if successful, otherwise false
   * @throws XMPPException
   */
  public MultiUserChat inviteRoom(String number, String contact, int mode) throws Exception {
    MultiUserChat muc;
    if (!mRooms.containsKey(number)) {
      Log.i("No existing chat room with " + contact + ". Creating a new one...");
      muc = createRoom(number, contact, mode);
      mRooms.put(number, muc);

    } else {
      muc = mRooms.get(number);
      Log.i("Opening existing room for " + contact);
      if (muc != null) {
        Collection<Occupant> occupants = muc.getParticipants();

        // Logging participants
        for (Occupant occupant : occupants) {
          Log.d(occupant.getJid() + " already in the room");
        }

        // Invite notified addresses if needed
        for (String notifiedAddress : mSettings.getNotifiedAddresses().getAll()) {
          boolean found = false;
          for (Occupant occupant : occupants) {
            if (occupant.getJid().startsWith(notifiedAddress + "/")) {
              found = true;
              break;
            }
          }
          if (!found) {
            Log.d("Inviting notified address '" + notifiedAddress + "' in the room for " + contact);
            muc.invite(notifiedAddress, "SMS conversation with " + contact);
          }
        }
      }
    }
    return muc;
  }
예제 #3
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]);
        }
      }
    }
예제 #4
0
  /**
   * Creates a new MUC AND invites the user room name will be extended with an random number for
   * security purposes
   *
   * @param number
   * @param name - the name of the contact to chat via SMS with
   * @return
   * @throws XMPPException
   */
  private MultiUserChat createRoom(String number, String name, int mode) throws Exception {
    MultiUserChat multiUserChat;
    Integer randomInt;

    // With "@conference.jabber.org" messages are sent several times...
    // Jwchat seems to work fine and is the default
    final String roomJID;
    final String subjectInviteStr;

    do {
      randomInt = mRndGen.nextInt();
    } while (mRoomNumbers.contains(randomInt));

    String normalizedName = name.replaceAll(" ", "_").replaceAll("[\\W]|�", "");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
      normalizedName =
          Normalizer.normalize(normalizedName, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");
    }
    String cleanLogin = mSettings.getLogin().replaceAll("@", "_");
    String roomUID = normalizedName + "_" + ROOM_START_TAG + randomInt + "_" + cleanLogin;

    switch (mode) {
      case MODE_SMS:
        roomJID = roomUID + "_SMS_" + "@" + getMUCServer();
        subjectInviteStr = mCtx.getString(R.string.xmpp_muc_sms) + name;
        break;

      case MODE_SHELL:
        roomJID = roomUID + "_Shell_" + number + "@" + getMUCServer();
        subjectInviteStr = mCtx.getString(R.string.xmpp_muc_shell) + name + " " + number;
        name = "Shell " + number;
        break;

      default:
        roomJID = null;
        subjectInviteStr = null;
        break;
    }
    Log.i("Creating room " + roomJID + " " + getRoomInt(roomJID));

    // See issue 136
    try {
      multiUserChat = new MultiUserChat(mConnection, roomJID);
    } catch (Exception e) {
      Log.e("MUC creation failed: ", e);
      throw new Exception("MUC creation failed for " + roomJID + ": " + e.getLocalizedMessage(), e);
    }

    try {
      multiUserChat.createOrJoin(name);
    } catch (Exception e) {
      Log.e("MUC creation failed: ", e);
      throw new Exception("MUC creation failed for " + name + ": " + e.getLocalizedMessage(), e);
    }

    try {
      // Since this is a private room, make the room not public and set user as owner of the room.
      Form submitForm = multiUserChat.getConfigurationForm().createAnswerForm();
      submitForm.setAnswer("muc#roomconfig_publicroom", false);
      submitForm.setAnswer("muc#roomconfig_roomname", name);
      try {
        submitForm.setAnswer("muc#roomconfig_roomdesc", name);
      } catch (Exception ex) {
        Log.w("Unable to configure room description to " + name, ex);
      }

      try {
        submitForm.setAnswer("muc#roomconfig_whois", "anyone");
      } catch (Exception ex) {
        Log.w("Unable to configure setting whois");
      }

      try {
        List<String> owners = new ArrayList<String>();
        if (mConnection.getUser() != null) {
          owners.add(mConnection.getUser());
        } else {
          owners.add(mSettings.getLogin());
        }
        Collections.addAll(owners, mSettings.getNotifiedAddresses().getAll());
        submitForm.setAnswer("muc#roomconfig_roomowners", owners);
        submitForm.setAnswer("muc#roomconfig_membersonly", true);
      } catch (Exception ex) {
        // Password protected MUC fallback code begins here
        Log.w(
            "Unable to configure room owners on Server "
                + getMUCServer()
                + ". Falling back to room passwords",
            ex);
        // See http://xmpp.org/registrar/formtypes.html#http:--jabber.org-protocol-mucroomconfig
        try {
          if (submitForm.getField("muc#roomconfig_passwordprotectedroom") != null) {
            submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true);
          }
          submitForm.setAnswer("muc#roomconfig_roomsecret", mSettings.roomPassword);
        } catch (IllegalArgumentException iae) {
          // If a server doesn't provide even password protected MUC, the setAnswer
          // call will result in an IllegalArgumentException, which we wrap into an XMPPException
          // See also Issue 247 http://code.google.com/p/gtalksms/issues/detail?id=247
          throw iae;
        }
      }

      Log.d(submitForm.getDataFormToSend().toXML().toString());
      multiUserChat.sendConfigurationForm(submitForm);
      multiUserChat.changeSubject(subjectInviteStr);
    } catch (XMPPException e1) {
      Log.w("Unable to send conference room configuration form.", e1);
      send(mCtx.getString(R.string.chat_sms_muc_conf_error, e1.getMessage()));
      // then we also should not send an invite as the room will be locked
      throw e1;
    }

    for (String notifiedAddress : mSettings.getNotifiedAddresses().getAll()) {
      multiUserChat.invite(notifiedAddress, subjectInviteStr);
    }

    registerRoom(multiUserChat, number, name, randomInt, mode);
    return multiUserChat;
  }
  protected void execute(Command cmd) {
    Log.i("TTS: " + cmd.getOriginalCommand());

    if (isMatchingCmd(cmd, "tts")) {
      if (mTtsAvailable) {
        mTts.speak(cmd.getAllArg1(), TextToSpeech.QUEUE_ADD, null);
      } else {
        send(R.string.chat_tts_installation);
        Intent intent = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        sContext.startActivity(intent);
      }
    } else if (isMatchingCmd(cmd, "tts-engine-list")) {
      if (Build.VERSION.SDK_INT < VERSION_CODES.ICE_CREAM_SANDWICH) {
        send(R.string.android_version_incompatible, "ICE CREAM SANDWICH");
      } else {
        StringBuilder sb = new StringBuilder(getString(R.string.chat_tts_engines));
        for (EngineInfo engine : mTts.getEngines()) {
          sb.append(engine.label).append(" - ").append(engine.name).append("\n");
        }
        send(sb.substring(0, Math.max(0, sb.length() - 1)));
      }
    } else if (isMatchingCmd(cmd, "tts-lang-list")) {
      StringBuilder sb = new StringBuilder(getString(R.string.chat_tts_languages));
      for (Locale locale : Locale.getAvailableLocales()) {
        switch (mTts.isLanguageAvailable(locale)) {
          case TextToSpeech.LANG_AVAILABLE:
            sb.append(locale.getDisplayLanguage());
            if (locale.getDisplayCountry() != null && locale.getDisplayCountry().length() > 0) {
              sb.append(" (").append(locale.getDisplayCountry()).append(")");
            }
            sb.append(" - ").append(locale.getLanguage());

            if (locale.getDisplayCountry() != null && locale.getDisplayCountry().length() > 0) {
              sb.append(":").append(locale.getCountry());
            }
            sb.append("\n");
            break;
        }
      }
      send(sb.substring(0, Math.max(0, sb.length() - 1)));
    } else if (isMatchingCmd(cmd, "tts-engine")) {
      if (Build.VERSION.SDK_INT < VERSION_CODES.ICE_CREAM_SANDWICH) {
        send(R.string.android_version_incompatible, "ICE CREAM SANDWICH");
      } else {
        mTts = new TextToSpeech(sContext, this, cmd.getAllArg1());
        send(getString(R.string.chat_tts_engine) + mTts.getDefaultEngine());
      }
    } else if (isMatchingCmd(cmd, "tts-lang")) {
      String arg1 = cmd.getArg1();
      String arg2 = cmd.getArg2();
      if (!arg1.equals("") && !arg2.equals("")) {
        mLocale = new Locale(arg1, arg2);
        mTts.setLanguage(mLocale);
      } else if (!arg1.equals("")) {
        mLocale = new Locale(arg1);
        mTts.setLanguage(mLocale);
      }
      send(getString(R.string.chat_tts_language) + mTts.getLanguage().getDisplayName());
    }
  }