diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index 046e1227..06de4d2c 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -1220,21 +1220,37 @@ public class Actions public static void sendTextMessage(Context context, String[] parametersArray) { - String phoneNumber, message; + String phoneNumber, message, messageType = "sms", filePath = null; phoneNumber = parametersArray[0]; message = parametersArray[1]; + if(parametersArray.length > 2) + { + messageType = parametersArray[2]; + + if(parametersArray.length > 3) + filePath = parametersArray[3]; + } + try { String textToSend = Miscellaneous.replaceVariablesInText(message, context); + if(messageType.equals("sms")) + sendSmsMessage(phoneNumber, textToSend); + else + sendMmsMessage(phoneNumber, textToSend, filePath); + } + catch (Exception e) + { + Miscellaneous.logEvent("e", Miscellaneous.getAnyContext().getString(R.string.sendTextMessage), "Error in sendTextMessage: " + Log.getStackTraceString(e), 3); + } + } - /* - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + phoneNumber)); - intent.putExtra("sms_body", message); - AutomationService.getInstance().startActivity(intent); - */ - + private static void sendSmsMessage(String phoneNumber, String textToSend) + { + try + { PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, Actions.class), 0); SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, textToSend, pi, null); @@ -1245,6 +1261,29 @@ public class Actions } } + 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); + } + catch (Exception e) + { + Miscellaneous.logEvent("e", Miscellaneous.getAnyContext().getString(R.string.sendTextMessage), "Error in sendTextMessage: " + Log.getStackTraceString(e), 3); + } + } + private static class WakeUpDeviceClass implements Runnable { private long awakeTime; diff --git a/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java b/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java index f9bc909a..e8351d8a 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java @@ -17,6 +17,7 @@ import android.widget.CompoundButton; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; +import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; @@ -29,9 +30,11 @@ public class ActivityManageActionSendTextMessage extends Activity Button bSaveSendTextMessage, bImportNumberFromContacts, bMmsAttachment; EditText etPhoneNumber, etSendTextMessage; RadioButton rbMessageTypeSms, rbMessageTypeMms; + TextView tvSendMmsFileAttachment; protected final static int requestCodeForContactsPermissions = 9876; protected final static int requestCodeGetContact = 3235; + protected final static int requestCodeGetMMSattachment = 3236; public static boolean edit = false; public static Action resultingAction = null; @@ -49,6 +52,7 @@ public class ActivityManageActionSendTextMessage extends Activity rbMessageTypeSms = (RadioButton)findViewById(R.id.rbMessageTypeSms); rbMessageTypeMms = (RadioButton) findViewById(R.id.rbMessageTypeMms); bMmsAttachment = (Button)findViewById(R.id.bMmsAttachment); + tvSendMmsFileAttachment = (TextView)findViewById(R.id.tvSendMmsFileAttachment); bSaveSendTextMessage.setOnClickListener(new OnClickListener() { @@ -94,6 +98,18 @@ public class ActivityManageActionSendTextMessage extends Activity rbMessageTypeSms.setOnCheckedChangeListener(checkedChangedListener); rbMessageTypeMms.setOnCheckedChangeListener(checkedChangedListener); + bMmsAttachment.setOnClickListener(new OnClickListener() + { + @Override + public void onClick(View view) + { + Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT); + chooseFileIntent.setType("*/*"); + chooseFileIntent = Intent.createChooser(chooseFileIntent, getResources().getString(R.string.chooseFile)); + startActivityForResult(chooseFileIntent, requestCodeGetMMSattachment); + } + }); + ActivityManageActionSendTextMessage.edit = getIntent().getBooleanExtra("edit", false); if(edit) { @@ -160,10 +176,10 @@ public class ActivityManageActionSendTextMessage extends Activity @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if(requestCode == requestCodeGetContact) - { - if(resultCode == Activity.RESULT_OK) - { + if(resultCode == Activity.RESULT_OK) + { + if(requestCode == requestCodeGetContact) + { String phoneNo = null; String name = null; @@ -181,6 +197,12 @@ public class ActivityManageActionSendTextMessage extends Activity etPhoneNumber.setText(phoneNo); } } + else if (requestCode == requestCodeGetMMSattachment) + { + Uri fileUri = data.getData(); + String filePath = fileUri.getPath(); + tvSendMmsFileAttachment.setText(filePath); + } } //super.onActivityResult(requestCode, resultCode, data); } diff --git a/app/src/main/res/layout/activity_manage_action_send_textmessage.xml b/app/src/main/res/layout/activity_manage_action_send_textmessage.xml index 8eab8431..633ac6d5 100644 --- a/app/src/main/res/layout/activity_manage_action_send_textmessage.xml +++ b/app/src/main/res/layout/activity_manage_action_send_textmessage.xml @@ -97,7 +97,7 @@ android:text="@string/attachment" />