From 48da91cb4005460740d4602fe8e0e125847b1d88 Mon Sep 17 00:00:00 2001 From: jens Date: Sun, 13 Nov 2022 17:49:02 +0100 Subject: [PATCH] send mms with attachment --- .../java/com/jens/automation2/Actions.java | 23 ++++++-------- .../ActivityManageActionSendTextMessage.java | 31 ++++++++++++++++--- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index 06de4d2c..b65cacff 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -10,6 +10,7 @@ import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothProfile; import android.content.ActivityNotFoundException; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.media.AudioManager; @@ -28,6 +29,7 @@ import android.telecom.TelecomManager; import android.telephony.SmsManager; import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; +import android.text.TextUtils; import android.util.Log; import android.view.KeyEvent; import android.widget.Toast; @@ -51,6 +53,7 @@ import org.apache.http.impl.client.DefaultHttpClient; import java.io.BufferedReader; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -1220,7 +1223,7 @@ public class Actions public static void sendTextMessage(Context context, String[] parametersArray) { - String phoneNumber, message, messageType = "sms", filePath = null; + String phoneNumber, message, messageType = ActivityManageActionSendTextMessage.messageTypeSms, filePath = null; phoneNumber = parametersArray[0]; message = parametersArray[1]; @@ -1261,26 +1264,18 @@ public class Actions } } + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private static void sendMmsMessage(String phoneNumber, String textToSend, String fileToBeAttached) { try { - PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, Actions.class), 0); - SmsManager sms = SmsManager.getDefault(); - sms.sendMultimediaMessage(phoneNumber, null, textToSend, pi, null); - - if(!StringUtils.isEmpty(fileToBeAttached)) - { - Uri uri = Uri.parse("file://" + fileToBeAttached); - i.putExtra(Intent.EXTRA_STREAM, "file:/" + uri); - i.setType("image/png"); - } - - startActivity(i); + Miscellaneous.logEvent("i", "sendMmsMessage()", "Sending MMS message...", 2); + Uri contentUri = Uri.fromFile(new File(fileToBeAttached)); + SmsManager.getDefault().sendMultimediaMessage(context, contentUri, phoneNumber, null, null); } catch (Exception e) { - Miscellaneous.logEvent("e", Miscellaneous.getAnyContext().getString(R.string.sendTextMessage), "Error in sendTextMessage: " + Log.getStackTraceString(e), 3); + Miscellaneous.logEvent("e", "sendMmsMessage()", "Error in sendMmsMessage(): " + Log.getStackTraceString(e), 3); } } diff --git a/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java b/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java index e8351d8a..aab66a65 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java @@ -24,6 +24,8 @@ import androidx.annotation.NonNull; import com.jens.automation2.Action.Action_Enum; +import org.apache.commons.lang3.StringUtils; + public class ActivityManageActionSendTextMessage extends Activity { @@ -35,6 +37,9 @@ public class ActivityManageActionSendTextMessage extends Activity protected final static int requestCodeForContactsPermissions = 9876; protected final static int requestCodeGetContact = 3235; protected final static int requestCodeGetMMSattachment = 3236; + + public static final String messageTypeSms = "sms"; + public static final String messageTypeMms = "mms"; public static boolean edit = false; public static Action resultingAction = null; @@ -61,13 +66,29 @@ public class ActivityManageActionSendTextMessage extends Activity { if(etSendTextMessage.getText().toString().length() > 0 && etPhoneNumber.getText().toString().length() > 0) { - if(resultingAction == null) + if(rbMessageTypeMms.isChecked() && StringUtils.isEmpty(tvSendMmsFileAttachment.getText().toString())) + Toast.makeText(getBaseContext(), getResources().getString(R.string.chooseFile), Toast.LENGTH_LONG).show(); + else { - resultingAction = new Action(); - resultingAction.setAction(Action_Enum.sendTextMessage); - resultingAction.setParameter2(etPhoneNumber.getText().toString() + Actions.smsSeparator + etSendTextMessage.getText().toString()); + if (resultingAction == null) + { + resultingAction = new Action(); + resultingAction.setAction(Action_Enum.sendTextMessage); + String messageType = null; + String path = ""; + + if(rbMessageTypeSms.isChecked()) + messageType = messageTypeSms; + else + { + messageType = messageTypeMms; + path = Actions.smsSeparator + tvSendMmsFileAttachment.getText().toString(); + } + + resultingAction.setParameter2(etPhoneNumber.getText().toString() + Actions.smsSeparator + etSendTextMessage.getText().toString() + Actions.smsSeparator + messageType + path); + } + backToRuleManager(); } - backToRuleManager(); } else Toast.makeText(getBaseContext(), getResources().getString(R.string.enterPhoneNumberAndText), Toast.LENGTH_LONG).show();