Search in sources :

Example 11 with TextMessageHelper

use of com.nyaruka.androidrelay.data.TextMessageHelper 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);
    }
}
Also used : TextMessage(com.nyaruka.androidrelay.data.TextMessage) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Example 12 with TextMessageHelper

use of com.nyaruka.androidrelay.data.TextMessageHelper in project android-sms-relay by nyaruka.

the class RelayService method trimMessages.

/**
	 * Trims all but the latest 100 messages in our table.
	 */
public void trimMessages() {
    TextMessageHelper helper = getHelper();
    helper.trimMessages();
}
Also used : TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Example 13 with TextMessageHelper

use of com.nyaruka.androidrelay.data.TextMessageHelper 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);
        }
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) TextMessage(com.nyaruka.androidrelay.data.TextMessage) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Example 14 with TextMessageHelper

use of com.nyaruka.androidrelay.data.TextMessageHelper 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);
    }
}
Also used : JSONObject(org.json.JSONObject) SharedPreferences(android.content.SharedPreferences) JSONArray(org.json.JSONArray) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) TextMessage(com.nyaruka.androidrelay.data.TextMessage) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Aggregations

TextMessageHelper (com.nyaruka.androidrelay.data.TextMessageHelper)14 TextMessage (com.nyaruka.androidrelay.data.TextMessage)11 SharedPreferences (android.content.SharedPreferences)7 IOException (java.io.IOException)3 HttpResponseException (org.apache.http.client.HttpResponseException)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 Editor (android.content.SharedPreferences.Editor)1 TelephonyManager (android.telephony.TelephonyManager)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 NameValuePair (org.apache.http.NameValuePair)1 HttpClient (org.apache.http.client.HttpClient)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1 HttpPost (org.apache.http.client.methods.HttpPost)1 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)1 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1