/**
   * Send generic SMS
   *
   * @param phoneNumber
   * @param message
   */
  public void sendSms(Context context, String phoneNumber, String message) {
    if (PhoneNumberUtils.isWellFormedSmsAddress(phoneNumber)) {
      String SENT = "SMS_SENT";
      String DELIVERED = "SMS_DELIVERED";

      PendingIntent sentPI =
          PendingIntent.getBroadcast(context.getApplicationContext(), 0, new Intent(SENT), 0);

      PendingIntent deliveredPI =
          PendingIntent.getBroadcast(context.getApplicationContext(), 0, new Intent(DELIVERED), 0);

      /* receive when the SMS has been sent */
      context.getApplicationContext().registerReceiver(SmsSendReceiver, new IntentFilter(SENT));

      /* receive when the SMS has been delivered */
      context
          .getApplicationContext()
          .registerReceiver(SmsDeliveredReceiver, new IntentFilter(DELIVERED));

      Log.d(Constants.TAG, "Sending SMS to " + phoneNumber + " with message: " + message);

      // TODO: make it a preference to switch between data sms and normal sms

      SmsManager smsManager = SmsManager.getDefault();
      smsManager.sendDataMessage(
          phoneNumber, null, Constants.DATA_SMS_PORT, message.getBytes(), sentPI, deliveredPI);
      // smsManager.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    } else {
      mCryptoCallService.sendUpdateUiToHandler(R.string.status_destination_invalid);
    }
  }
 @Override
 public void sendDataMessage(
     String destinationAddress,
     String scAddress,
     short destinationPort,
     byte[] data,
     PendingIntent sentIntent,
     PendingIntent deliveryIntent) {
   try {
     mSmsManager.sendDataMessage(
         destinationAddress, scAddress, destinationPort, data, sentIntent, deliveryIntent);
   } catch (NullPointerException npe) {
     // (Tested on the Motorola Droid)
     // When there is no SIM inserted, and we attempt to sendDataMessage(), the framework
     // blows up with an NPE. Yuck. This contrasts with the sendTextMessage() method, which
     // in the same situation, fires off the sentItent with
     // SmsManager.RESULT_ERROR_NO_SERVICE as the failure code. Because the NPE being thrown
     // is not useful for our clients, we translate it here to behave the same way as the
     // sendTextMessage() method.
     if (sentIntent != null) {
       try {
         sentIntent.send(SmsManager.RESULT_ERROR_NO_SERVICE);
       } catch (CanceledException cancelledException) {
         // If the sentIntent has been cancelled, then we don't need to send it on.
       }
     }
   }
 }
 public static void sendSms(String address, byte[] data) {
   short SMS_PORT = 8998;
   SmsManager smsManager = SmsManager.getDefault();
   smsManager.sendDataMessage(
       address, null, // TODO: define scAddress if needed
       SMS_PORT, data, null, // TODO: define sentIntent if needed
       null); // TODO: define deliveryIntent if needed
 }
Beispiel #4
0
  private void sendDataSms(String phoneNumber, String message) {

    short SMS_PORT = 8901;
    SmsManager smsManager = SmsManager.getDefault();
    if (smsManager == null) {
      Utils.showResultInDialog(mActivity, G.NOTHING_FOUND + "\n\n smsManager == null");
      return;
    }
    PendingIntent sent =
        mActivity.createPendingResult(
            DATA_SMS_SENT, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
    smsManager.sendDataMessage(phoneNumber, null, SMS_PORT, message.getBytes(), sent, null);
  }
Beispiel #5
0
  public void sendKey(Key key, String address) {
    byte[] keyBytes = key.getEncoded();
    double pdus = keyBytes.length / (double) (MAX_DATA_BYTES - HEADER_SIZE); // effective data #

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

    Log.i(TAG, "Sending key of length " + keyBytes.length + " in " + pdus + " parts");
    String temp = "";
    for (byte b : keyBytes) temp += b + " ";
    Log.i(TAG, temp);

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

    int k = 0;
    for (int pdu = 0; pdu < pdus; pdu++) {
      // indicating key exchange
      message[pdu][0] = 1;

      // message sequence number - used to mark key length in exchanges
      message[pdu][1] = (byte) keyBytes.length;

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

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

      for (byte[] part : message) {
        Intent in = new Intent(SENT_INTENT);
        PendingIntent sent =
            PendingIntent.getBroadcast(this, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);

        mgr.sendDataMessage(address, null, APPLICATION_PORT, part, sent, null);
      }
    }
  }
Beispiel #6
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
  }