Browse Source

Plug SMS receiver on API

master
Gabriel Augendre 3 months ago
parent
commit
153fe5a1a5
  1. 1
      .idea/gradle.xml
  2. 2
      app/src/main/AndroidManifest.xml
  3. 33
      app/src/main/java/info/augendre/mmsforward/Message.java
  4. 96
      app/src/main/java/info/augendre/mmsforward/MessageSender.java
  5. 1
      app/src/main/res/values/strings.xml
  6. 15
      app/src/main/res/xml/root_preferences.xml

1
.idea/gradle.xml

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>

2
app/src/main/AndroidManifest.xml

@ -8,6 +8,8 @@
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<uses-permission android:name="android.permission.RECEIVE_WAP_PUSH" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"

33
app/src/main/java/info/augendre/mmsforward/Message.java

@ -8,6 +8,20 @@ import android.util.Log;
import androidx.preference.PreferenceManager;
import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Message {
private final String TAG = "Message";
@ -20,15 +34,22 @@ public class Message {
@NotNull
@Override
public String toString() {
return "phone: " + this.message.getOriginatingAddress() + ", content: " + this.message.getMessageBody();
return "phone: " + this.getPhoneNumber() + ", content: " + this.getContent();
}
protected String getPhoneNumber() {
return this.message.getOriginatingAddress();
}
protected String getContent() {
return this.message.getMessageBody();
}
public void send(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String rpiIp = preferences.getString("rpi_ip", "");
if (rpiIp.length() == 0) {
return;
}
Log.d(TAG, "Sending message to " + rpiIp + ": " + this.toString());
String serverIp = preferences.getString("rpi_ip", "");
String serverPort = preferences.getString("rpi_port", "");
Log.d(TAG, "Sending message to " + serverIp + ":" + serverPort + " -> " + this.toString());
new MessageSender(this).execute(serverIp, serverPort);
}
}

96
app/src/main/java/info/augendre/mmsforward/MessageSender.java

@ -0,0 +1,96 @@
package info.augendre.mmsforward;
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
class MessageSender extends AsyncTask<String, Void, Integer> {
private final String TAG = "MessageSender";
private final Message message;
private Exception exception;
public MessageSender(Message message) {
this.message = message;
}
protected Integer doInBackground(String ...params) {
String serverIp = params[0];
String serverPort = params[1];
URL url;
try {
url = new URL("http://" + serverIp + ":" + serverPort + "/api/message/");
} catch (MalformedURLException e) {
Log.e(TAG, "URL is malformed: " + e.toString());
this.exception = e;
return null;
}
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setDoOutput(true);
JSONObject jsonParam = new JSONObject();
jsonParam.put("phone_number", this.message.getPhoneNumber());
jsonParam.put("content", this.message.getContent());
byte[] bytes = jsonParam.toString().getBytes("UTF-8");
urlConnection.setFixedLengthStreamingMode(bytes.length);
DataOutputStream os = new DataOutputStream(urlConnection.getOutputStream());
os.write(bytes);
os.flush();
os.close();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONObject jsonResponse = new JSONObject(response.toString());
String status = jsonResponse.getString("status");
if (!status.equals("ok")) {
String msg = "Response status not OK";
Log.e(TAG, msg);
throw new JSONException(msg);
}
int messageId = jsonResponse.getJSONObject("data").getInt("id");
return messageId;
} catch (IOException | JSONException e) {
Log.e(TAG, "error: " + e.toString());
this.exception = e;
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
protected void onPostExecute(Integer messageId) {
if (this.exception != null) {
Log.e(TAG, "Couldn't complete due to exception");
return;
}
Log.i(TAG, "Found message id " + messageId);
}
}

1
app/src/main/res/values/strings.xml

@ -5,4 +5,5 @@
<string name="general_header">General</string>
<string name="rpi_ip_title">Raspberry Pi IP</string>
<string name="rpi_port_title">Raspberry Pi port</string>
</resources>

15
app/src/main/res/xml/root_preferences.xml

@ -4,9 +4,18 @@
<PreferenceCategory app:title="@string/general_header">
<EditTextPreference
android:defaultValue="192.168.1.1"
app:key="rpi_ip"
app:title="@string/rpi_ip_title"
android:defaultValue="192.168.0.24"
android:key="rpi_ip"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/rpi_ip_title"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="8000"
android:key="rpi_port"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/rpi_port_title"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>

Loading…
Cancel
Save