use of com.nyaruka.androidrelay.data.TextMessage in project android-sms-relay by nyaruka.
the class RelayService method promoteErroredMessages.
/***
* This should be run only when our service starts, and takes care of resending any messages
* that were queued but which we never got a reply for. This could result in double sends
* but that's better than leaving a message on the floor.
*/
public void promoteErroredMessages() {
TextMessageHelper helper = getHelper();
List<TextMessage> msgs = helper.withStatus(this.getApplicationContext(), TextMessage.OUTGOING, TextMessage.QUEUED);
for (TextMessage msg : msgs) {
msg.status = TextMessage.ERRORED;
helper.updateMessage(msg);
MainActivity.updateMessage(msg);
}
}
use of com.nyaruka.androidrelay.data.TextMessage in project android-sms-relay by nyaruka.
the class RelayService method markDeliveriesOnServer.
public void markDeliveriesOnServer() throws IOException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String deliveryURL = prefs.getString("delivery_url", null);
if (deliveryURL != null && deliveryURL.length() > 0) {
TextMessageHelper helper = getHelper();
List<TextMessage> msgs = helper.withStatus(this.getApplicationContext(), TextMessage.OUTGOING, TextMessage.SENT);
for (TextMessage msg : msgs) {
markMessageDelivered(msg);
}
}
}
use of com.nyaruka.androidrelay.data.TextMessage in project android-sms-relay by nyaruka.
the class RelayService method checkOutbox.
/**
* Sends a message to our server.
* @param msg
*/
public void checkOutbox() throws IOException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String updateInterval = prefs.getString("update_interval", "30000");
long interval = Long.parseLong(updateInterval);
String outboxURL = prefs.getString("outbox_url", null);
TextMessageHelper helper = getHelper();
// no delivery url means we don't do anything
if (outboxURL == null || outboxURL.length() == 0) {
return;
}
// if our update interval is set to 0, then that means we shouldn't be checking, so skip
if (interval == 0) {
return;
}
Log.d(TAG, "Outbox URL: " + outboxURL);
try {
String content = fetchURL(outboxURL);
if (content.trim().length() > 0) {
JSONObject json = new JSONObject(content);
JSONArray responses = json.getJSONArray("outbox");
for (int i = 0; i < responses.length(); i++) {
JSONObject response = responses.getJSONObject(i);
if ("O".equals(response.getString("direction")) && "Q".equals(response.getString("status"))) {
String number = "+" + response.getString("contact");
String message = response.getString("text");
long serverId = response.getLong("id");
// if this message doesn't already exist
TextMessage existing = helper.withServerId(this.getApplicationContext(), serverId);
if (existing == null) {
Log.d(TAG, "New outgoing msg: " + serverId + ": " + message);
TextMessage toSend = new TextMessage(number, message, serverId);
helper.createMessage(toSend);
sendMessage(toSend);
} else {
if (existing.status == TextMessage.DONE) {
existing.status = TextMessage.SENT;
helper.updateMessage(existing);
}
Log.d(TAG, "Ignoring message: " + serverId + " already queued.");
}
}
}
}
Log.d(TAG, "Outbox fetched from server");
} catch (HttpResponseException e) {
Log.d(TAG, "Got Error: " + e.getMessage(), e);
} catch (IOException e) {
throw e;
} catch (Throwable t) {
Log.d(TAG, "Got Error: " + t.getMessage(), t);
}
}
Aggregations