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