use of com.nyaruka.androidrelay.data.TextMessage in project android-sms-relay by nyaruka.
the class RelayService method onSMSSent.
public void onSMSSent(String token) {
TextMessage msg = null;
TextMessageHelper helper = getHelper();
msg = helper.withId(Long.parseLong(token));
if (msg != null) {
msg.status = TextMessage.SENT;
msg.error = "";
helper.updateMessage(msg);
Log.d(TAG, "=== SMS SENT: " + token);
MainActivity.updateMessage(msg);
kickService();
}
}
use of com.nyaruka.androidrelay.data.TextMessage in project android-sms-relay by nyaruka.
the class RelayService method onSMSSendError.
public void onSMSSendError(String token, String errorDetails) {
TextMessage msg = null;
TextMessageHelper helper = getHelper();
msg = helper.withId(Long.parseLong(token));
if (msg != null) {
msg.status = TextMessage.ERRORED;
msg.error = "SMS send error";
helper.updateMessage(msg);
Log.d(TAG, "=== SMS ERROR:" + token + " Details: " + errorDetails);
MainActivity.updateMessage(msg);
}
}
use of com.nyaruka.androidrelay.data.TextMessage in project android-sms-relay by nyaruka.
the class RelayService method sendMessageToServer.
/**
* Sends a message to our server.
* @param msg
*/
public void sendMessageToServer(TextMessage msg) throws IOException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String receiveURL = prefs.getString("receive_url", null);
boolean process_outgoing = prefs.getBoolean("process_outgoing", false);
TextMessageHelper helper = getHelper();
Log.d(TAG, "Receive URL: " + receiveURL);
// no delivery url means we don't do anything
if (receiveURL == null || receiveURL.length() == 0) {
return;
}
String url = receiveURL + "&sender=" + URLEncoder.encode(msg.number) + "&message=" + URLEncoder.encode(msg.text);
Log.d(TAG, "Sending: " + url);
try {
String content = fetchURL(url);
if (content.trim().length() > 0) {
JSONObject json = new JSONObject(content);
// if we are supposed to process outgoing messages, then read any responses
if (process_outgoing) {
JSONArray responses = json.getJSONArray("responses");
for (int i = 0; i < responses.length(); i++) {
JSONObject response = responses.getJSONObject(i);
String number = "+" + response.getString("contact");
String message = response.getString("text");
long serverId = response.getLong("id");
if ("O".equals(response.getString("direction")) && "Q".equals(response.getString("status"))) {
// if this message doesn't already exist
TextMessage existing = helper.withServerId(this.getApplicationContext(), serverId);
if (existing == null) {
Log.d(TAG, "Got reply: " + serverId + ": " + message);
TextMessage toSend = new TextMessage(number, message, serverId);
helper.createMessage(toSend);
sendMessage(toSend);
}
}
}
}
}
msg.status = TextMessage.HANDLED;
msg.error = null;
Log.d(TAG, "Msg '" + msg.text + "' handed to server.");
} catch (HttpResponseException e) {
Log.d(TAG, "HTTP ERROR Got Error: " + e.getMessage(), e);
msg.error = e.getClass().getSimpleName() + ": " + e.getMessage();
msg.status = TextMessage.ERRORED;
} catch (IOException e) {
msg.error = e.getClass().getSimpleName() + ": " + e.getMessage();
msg.status = TextMessage.ERRORED;
throw e;
} catch (Throwable t) {
Log.d(TAG, "THROWABLE Got Error: " + t.getMessage(), t);
msg.error = t.getClass().getSimpleName() + ": " + t.getMessage();
msg.status = TextMessage.ERRORED;
} finally {
helper.updateMessage(msg);
MainActivity.updateMessage(msg);
}
}
use of com.nyaruka.androidrelay.data.TextMessage in project android-sms-relay by nyaruka.
the class RelayService method sendAlert.
/**
* Sends an alert to our server with the passed in subject. The body will contain
* configuration attributes and debugging information.
*
* TODO: Would be nice to echo these out to SMS if the client is configured
* with an alert phone number.
*
* @param subject The subject of the alert to send to the server
*/
public static boolean sendAlert(Context context, String subject, String body) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String hostname = prefs.getString("router_hostname", null);
Log.d(TAG, "__SENDING ALERT: " + subject);
if (hostname != null && subject != null) {
// send the alert off
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("http.connection-manager.timeout", new Integer(60000));
client.getParams().setParameter("http.connection.timeout", new Integer(60000));
client.getParams().setParameter("http.socket.timeout", new Integer(60000));
StringBuilder conf = new StringBuilder();
conf.append("SMS Relay Version: " + AndroidRelay.getVersionNumber(context));
conf.append("\n");
conf.append("\nHostname: " + prefs.getString("router_hostname", null));
String backend = prefs.getString("router_backend", null);
conf.append("\nBackend:" + backend);
conf.append("\nPassword:" + prefs.getString("router_password", null));
conf.append("\n");
conf.append("\nProcess Incoming:" + prefs.getBoolean("process_incoming", false));
conf.append("\nProcess Outgoing:" + prefs.getBoolean("process_outgoing", false));
conf.append("\nInterval:" + prefs.getString("update_interval", "null"));
TextMessageHelper helper = AndroidRelay.getHelper(context);
int total = helper.getAllMessages().size();
int unsynced = helper.withStatus(context, TextMessage.INCOMING, TextMessage.RECEIVED).size();
conf.append("\n");
conf.append("\nTotal Messages:" + total);
conf.append("\nUnsynced:" + unsynced);
List<TextMessage> erroredOut = helper.withStatus(context, TextMessage.OUTGOING, TextMessage.ERRORED);
conf.append("\n\nErrored Out: " + erroredOut.size());
for (TextMessage msg : erroredOut) {
conf.append("\n" + msg.number + ": " + msg.text);
}
List<TextMessage> erroredIn = helper.withStatus(context, TextMessage.INCOMING, TextMessage.ERRORED);
conf.append("\n\nErrored In: " + erroredIn.size());
for (TextMessage msg : erroredIn) {
conf.append("\n" + msg.number + ": " + msg.text);
}
conf.append("\n\nLog:\n\n");
// prepend our configuration to our body
body = conf.toString() + body;
try {
HttpPost post = new HttpPost("http://" + hostname + "/router/alert");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("password", "" + prefs.getString("router_password", null)));
nameValuePairs.add(new BasicNameValuePair("subject", "[" + backend + "] " + subject));
nameValuePairs.add(new BasicNameValuePair("body", body));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.e(MainActivity.TAG, "Sending log to: " + post.getURI().toURL().toString());
ResponseHandler<String> responseHandler = new BasicResponseHandler();
client.execute(post, responseHandler);
return true;
} catch (Throwable t) {
Log.e(MainActivity.TAG, "Sending of alert failed", t);
return false;
}
}
return false;
}
use of com.nyaruka.androidrelay.data.TextMessage in project android-sms-relay by nyaruka.
the class RelayService method sendPendingMessagesToServer.
/**
* Sends all our pending outgoing messages to our server.
*/
public void sendPendingMessagesToServer() throws IOException {
List<TextMessage> msgs = null;
// first send any that haven't yet been tried
TextMessageHelper helper = getHelper();
msgs = helper.withStatus(this.getApplicationContext(), TextMessage.INCOMING, TextMessage.RECEIVED);
for (TextMessage msg : msgs) {
sendMessageToServer(msg);
}
// then those that had an error when we tried to contact the server
helper = getHelper();
msgs = helper.withStatus(this.getApplicationContext(), TextMessage.INCOMING, TextMessage.ERRORED);
for (TextMessage msg : msgs) {
sendMessageToServer(msg);
}
}
Aggregations