Search in sources :

Example 1 with TextMessageHelper

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

the class RelayService method onNewSMS.

public void onNewSMS(String number, String message) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    boolean process_messages = prefs.getBoolean("process_incoming", false);
    TextMessageHelper helper = getHelper();
    String keyword = prefs.getString("relay_password", "nyaruka").toLowerCase();
    // if we aren't supposed to process messages, ignore this message
    if (!process_messages) {
        return;
    }
    // if someone sends 'keyword log' then send a log to the server
    if (message.equalsIgnoreCase(keyword + " log")) {
        RelayService.doSendLog = true;
        int unsynced = helper.withStatus(getApplicationContext(), TextMessage.INCOMING, TextMessage.RECEIVED).size();
        int erroredOut = helper.withStatus(getApplicationContext(), TextMessage.OUTGOING, TextMessage.ERRORED).size();
        int erroredIn = helper.withStatus(getApplicationContext(), TextMessage.INCOMING, TextMessage.ERRORED).size();
        modem.sendSms(number, "Log being sent. " + unsynced + " unsynced.  " + erroredOut + " out errors.  " + erroredIn + " in errors.", "-1");
        return;
    }
    if (message.equalsIgnoreCase(keyword + " reset")) {
        // change the label to true
        doReset = true;
        Log.d(TAG, "The reset process is set to " + Boolean.toString(doReset));
        // start the check service for reset changes to take effect
        kickService();
        modem.sendSms(number, "Reset process has been started. ", "-1");
        return;
    }
    if (message.equalsIgnoreCase(keyword + " clear")) {
        Log.d(TAG, "Clearing all messages");
        AndroidRelay.clearMessages(RelayService.this);
        Toast.makeText(RelayService.this, "Messages cleared", Toast.LENGTH_LONG).show();
        modem.sendSms(number, "All messages have been removed.", "-1");
        return;
    }
    {
        SharedPreferences.Editor editor = prefs.edit();
        // process message with 'keyword data and change the network connection to specified mode
        if (message.equalsIgnoreCase(keyword + " data")) {
            Log.d(TAG, "Switching to data connection");
            editor.putString("pref_net", "1");
            editor.commit();
            modem.sendSms(number, "Your preferred network is now set to 'MOBILE DATA'", "-1");
            return;
        }
        // process message with 'keyword wifi and change the network connection to specified mode
        if (message.equalsIgnoreCase(keyword + " wifi")) {
            Log.d(TAG, "Switching to wifi connection");
            editor.putString("pref_net", "0");
            editor.commit();
            modem.sendSms(number, "Your preferred network is now set to 'WIFI'", "-1");
            return;
        }
    }
    TextMessage msg = null;
    msg = new TextMessage();
    msg.number = number;
    msg.text = message;
    msg.created = new Date();
    msg.direction = TextMessage.INCOMING;
    msg.status = TextMessage.RECEIVED;
    helper.createMessage(msg);
    Log.d(TAG, "=== SMS IN:" + msg.number + ": " + msg.text);
    MainActivity.updateMessage(msg);
    kickService();
}
Also used : SharedPreferences(android.content.SharedPreferences) TextMessage(com.nyaruka.androidrelay.data.TextMessage) Date(java.util.Date) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Example 2 with TextMessageHelper

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

the class RelayService method requeueErroredIncoming.

/***
	 * Goes through all our activations, retriggering syncs for all those that need to be sent.
	 */
public void requeueErroredIncoming() {
    TextMessageHelper helper = getHelper();
    List<TextMessage> msgs = helper.withStatus(this.getApplicationContext(), TextMessage.INCOMING, TextMessage.ERRORED);
    int count = 0;
    for (TextMessage msg : msgs) {
        msg.status = TextMessage.RECEIVED;
        helper.updateMessage(msg);
        MainActivity.updateMessage(msg);
        count++;
        if (count >= 5) {
            Log.d(TAG, "Reprocessed five messages, skipping rest.");
            break;
        }
    }
}
Also used : TextMessage(com.nyaruka.androidrelay.data.TextMessage) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Example 3 with TextMessageHelper

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

the class RelayService method resendErroredSMS.

/***
	 * Goes through all the messages which have errors and resends them.  Note that we only try to send
	 * five at a time, so it could take a bit to clear out the backlog.
	 */
public void resendErroredSMS() {
    TextMessageHelper helper = getHelper();
    List<TextMessage> msgs = helper.erroredOutgoing(getApplicationContext());
    int count = 0;
    for (TextMessage msg : msgs) {
        try {
            Log.d(TAG, "Sending [" + msg.id + "] - " + msg.number + " - " + msg.text);
            modem.sendSms(msg.number, msg.text, "" + msg.id);
            msg.status = TextMessage.QUEUED;
        } catch (Throwable t) {
            msg.status = TextMessage.ERRORED;
            Log.d(TAG, "Errored [" + msg.id + "] - " + msg.number + " - " + msg.text, t);
        }
        helper.updateMessage(msg);
        MainActivity.updateMessage(msg);
        count++;
        if (count >= 5) {
            Log.d(TAG, "Resent five messages, skipping rest.");
            break;
        }
    }
}
Also used : TextMessage(com.nyaruka.androidrelay.data.TextMessage) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Example 4 with TextMessageHelper

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

the class RelayService method markMessageDelivered.

public void markMessageDelivered(TextMessage msg) throws IOException {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    String deliveryURL = prefs.getString("delivery_url", null);
    TextMessageHelper helper = getHelper();
    Log.d(TAG, "Delivery URL: " + deliveryURL);
    // no delivery url means we don't do anything
    if (deliveryURL == null || deliveryURL.length() == 0) {
        return;
    }
    if (msg.serverId <= 0) {
        msg.status = TextMessage.DONE;
        msg.error = "Ignored due to 0 id";
    } else {
        String url = deliveryURL + "&message_id=" + msg.serverId;
        Log.d(TAG, "Sending: " + url);
        try {
            fetchURL(url);
            msg.status = TextMessage.DONE;
            msg.error = null;
            Log.d(TAG, "Msg " + msg.serverId + " marked as delivered.");
        } catch (IOException e) {
            msg.status = TextMessage.SENT;
            msg.error = e.getClass().getSimpleName() + ": " + e.getMessage();
            throw e;
        } catch (Throwable t) {
            Log.d(TAG, "Got Error: " + t.getMessage(), t);
            msg.status = TextMessage.SENT;
            msg.error = t.getClass().getSimpleName() + ": " + t.getMessage();
        } finally {
            helper.updateMessage(msg);
            MainActivity.updateMessage(msg);
        }
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) IOException(java.io.IOException) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Example 5 with TextMessageHelper

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