Ejemplo n.º 1
0
  private void confirmMessagePart(String number, int pos) {
    Log.i(TAG, number + " " + pos);

    MessageConfirmation confirmation = partialConfs.get(number).get(pos);

    if (confirmation.getMessageParts() > 0)
      confirmation.setMessageParts(confirmation.getMessageParts() - 1);

    if (confirmation.getMessageParts() == 0) {
      String time = DateUtils.buildDate();

      if (!confirmTimes.containsKey(number))
        confirmTimes.put(number, new TreeMap<Integer, String>());

      confirmTimes.get(number).put(pos, time);

      dbUtils.confirmMessageSent(time, confirmation.getMessageId());

      Log.i(TAG, "confirmMessagePart Processing status " + processingStatus);
      partialConfs.get(number).remove(pos);
      processingStatus--;
      Log.i(TAG, "confirmMessagePart Processing status " + processingStatus);

      if (ConversationActivity.isActive() && ConversationActivity.currentNumber().equals(number)) {
        Intent in = new Intent(this, ConversationActivity.class);
        in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        in.putExtra(EncrypText.THREAD_POSITION, pos);
        in.putExtra(EncrypText.TIME, time);

        startActivity(in);
      } else if (!ConversationActivity.isActive()
          && ConversationActivity.currentNumber().equals(number))
        ConversationActivity.markNewConfs();
    }
  }
Ejemplo n.º 2
0
  public synchronized void sendMessage(ConversationEntry item, int place, Key key) {
    String address = item.getNumber();

    if (!currentConv.equals(address)) {
      sequenceNo = 0;
      currentConv = address;
    }

    byte[] encryptedMessage;

    try {
      encryptedMessage =
          cryptor.encryptMessage(item.getMessage().getBytes(), (SecretKey) key, sequenceNo);
    } catch (InvalidKeyException
        | BadPaddingException
        | IllegalBlockSizeException
        | InvalidAlgorithmParameterException e) {
      Log.e(TAG, "While encrypting message", e);
      Toast.makeText(this, "Encryption error while sending message", Toast.LENGTH_SHORT).show();
      return;
    }

    double packets = encryptedMessage.length / (double) (MAX_DATA_BYTES - HEADER_SIZE);

    if (packets % 1 != 0) // making sure to round up to next whole packet
    packets = (int) packets + 1;

    byte[][] message = new byte[(int) packets][MAX_DATA_BYTES];

    int k = 0;
    for (int pdu = 0; pdu < packets; pdu++) {
      // 2 for AES
      message[pdu][0] = 2;

      // message sequence number - session based - can only send 127 messages per session
      if (sequenceNo > 127) return;
      else {
        message[pdu][1] = (byte) sequenceNo;
      }

      message[pdu][2] = (byte) packets;
      message[pdu][3] = (byte) pdu;

      for (int j = 4; j < MAX_DATA_BYTES && k < encryptedMessage.length; j++) {
        message[pdu][j] = encryptedMessage[k];
        k++;
      }
    }

    long messageId = dbUtils.storeMessage(item);
    // long pos = manager.writeSMS(address, item, -1, this);

    setupMessageConfirmation(address, place, messageId, message.length);

    for (byte[] pdu : message) {
      Intent in = new Intent(SENT_INTENT);
      in.putExtra(EncrypText.THREAD_POSITION, place);
      in.putExtra(EncrypText.ADDRESS, address);
      PendingIntent sent =
          PendingIntent.getBroadcast(this, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);

      mgr.sendDataMessage(address, null, APPLICATION_PORT, pdu, sent, null);
    }

    sequenceNo++; // next sequence number
  }