Search in sources :

Example 11 with TextMessage

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

Example 12 with TextMessage

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

Example 13 with TextMessage

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);
    }
}
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

TextMessage (com.nyaruka.androidrelay.data.TextMessage)13 TextMessageHelper (com.nyaruka.androidrelay.data.TextMessageHelper)11 SharedPreferences (android.content.SharedPreferences)5 IOException (java.io.IOException)2 HttpResponseException (org.apache.http.client.HttpResponseException)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 Message (android.os.Message)1 ListView (android.widget.ListView)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