Search in sources :

Example 6 with TextMessageHelper

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

the class AndroidRelay method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    // migrate old preferences
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (prefs.contains("rapidsms_hostname")) {
        Editor editor = prefs.edit();
        editor.putString("router_hostname", prefs.getString("rapidsms_hostname", null));
        editor.putString("router_password", prefs.getString("rapidsms_password", null));
        editor.putString("router_backend", prefs.getString("rapidsms_backend", null));
        editor.remove("rapidsms_hostname");
        editor.remove("rapidsms_password");
        editor.remove("rapidsms_backend");
        editor.commit();
    }
    m_helper = new TextMessageHelper(this);
    m_phoneState = new PhoneState();
    TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
    telephonyManager.listen(m_phoneState, PhoneStateListener.LISTEN_CALL_STATE | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
Also used : SharedPreferences(android.content.SharedPreferences) TelephonyManager(android.telephony.TelephonyManager) Editor(android.content.SharedPreferences.Editor) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Example 7 with TextMessageHelper

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

the class RelayService method onSMSSendError.

public void onSMSSendError(String token, String errorDetails) {
    TextMessage msg = null;
    TextMessageHelper helper = getHelper();
    msg = helper.withId(Long.parseLong(token));
    if (msg != null) {
        msg.status = TextMessage.ERRORED;
        msg.error = "SMS send error";
        helper.updateMessage(msg);
        Log.d(TAG, "=== SMS ERROR:" + token + " Details: " + errorDetails);
        MainActivity.updateMessage(msg);
    }
}
Also used : TextMessage(com.nyaruka.androidrelay.data.TextMessage) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Example 8 with TextMessageHelper

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

the class RelayService method sendMessageToServer.

/**
	 * Sends a message to our server.
	 * @param msg
	 */
public void sendMessageToServer(TextMessage msg) throws IOException {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    String receiveURL = prefs.getString("receive_url", null);
    boolean process_outgoing = prefs.getBoolean("process_outgoing", false);
    TextMessageHelper helper = getHelper();
    Log.d(TAG, "Receive URL: " + receiveURL);
    // no delivery url means we don't do anything
    if (receiveURL == null || receiveURL.length() == 0) {
        return;
    }
    String url = receiveURL + "&sender=" + URLEncoder.encode(msg.number) + "&message=" + URLEncoder.encode(msg.text);
    Log.d(TAG, "Sending: " + url);
    try {
        String content = fetchURL(url);
        if (content.trim().length() > 0) {
            JSONObject json = new JSONObject(content);
            // if we are supposed to process outgoing messages, then read any responses
            if (process_outgoing) {
                JSONArray responses = json.getJSONArray("responses");
                for (int i = 0; i < responses.length(); i++) {
                    JSONObject response = responses.getJSONObject(i);
                    String number = "+" + response.getString("contact");
                    String message = response.getString("text");
                    long serverId = response.getLong("id");
                    if ("O".equals(response.getString("direction")) && "Q".equals(response.getString("status"))) {
                        // if this message doesn't already exist
                        TextMessage existing = helper.withServerId(this.getApplicationContext(), serverId);
                        if (existing == null) {
                            Log.d(TAG, "Got reply: " + serverId + ": " + message);
                            TextMessage toSend = new TextMessage(number, message, serverId);
                            helper.createMessage(toSend);
                            sendMessage(toSend);
                        }
                    }
                }
            }
        }
        msg.status = TextMessage.HANDLED;
        msg.error = null;
        Log.d(TAG, "Msg '" + msg.text + "' handed to server.");
    } catch (HttpResponseException e) {
        Log.d(TAG, "HTTP ERROR Got Error: " + e.getMessage(), e);
        msg.error = e.getClass().getSimpleName() + ": " + e.getMessage();
        msg.status = TextMessage.ERRORED;
    } catch (IOException e) {
        msg.error = e.getClass().getSimpleName() + ": " + e.getMessage();
        msg.status = TextMessage.ERRORED;
        throw e;
    } catch (Throwable t) {
        Log.d(TAG, "THROWABLE Got Error: " + t.getMessage(), t);
        msg.error = t.getClass().getSimpleName() + ": " + t.getMessage();
        msg.status = TextMessage.ERRORED;
    } finally {
        helper.updateMessage(msg);
        MainActivity.updateMessage(msg);
    }
}
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)

Example 9 with TextMessageHelper

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

the class RelayService method sendAlert.

/**
	 * Sends an alert to our server with the passed in subject.  The body will contain
	 * configuration attributes and debugging information.
	 * 
	 * TODO: Would be nice to echo these out to SMS if the client is configured 
	 *       with an alert phone number.
	 *       
	 * @param subject The subject of the alert to send to the server
	 */
public static boolean sendAlert(Context context, String subject, String body) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String hostname = prefs.getString("router_hostname", null);
    Log.d(TAG, "__SENDING ALERT: " + subject);
    if (hostname != null && subject != null) {
        // send the alert off
        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter("http.connection-manager.timeout", new Integer(60000));
        client.getParams().setParameter("http.connection.timeout", new Integer(60000));
        client.getParams().setParameter("http.socket.timeout", new Integer(60000));
        StringBuilder conf = new StringBuilder();
        conf.append("SMS Relay Version: " + AndroidRelay.getVersionNumber(context));
        conf.append("\n");
        conf.append("\nHostname: " + prefs.getString("router_hostname", null));
        String backend = prefs.getString("router_backend", null);
        conf.append("\nBackend:" + backend);
        conf.append("\nPassword:" + prefs.getString("router_password", null));
        conf.append("\n");
        conf.append("\nProcess Incoming:" + prefs.getBoolean("process_incoming", false));
        conf.append("\nProcess Outgoing:" + prefs.getBoolean("process_outgoing", false));
        conf.append("\nInterval:" + prefs.getString("update_interval", "null"));
        TextMessageHelper helper = AndroidRelay.getHelper(context);
        int total = helper.getAllMessages().size();
        int unsynced = helper.withStatus(context, TextMessage.INCOMING, TextMessage.RECEIVED).size();
        conf.append("\n");
        conf.append("\nTotal Messages:" + total);
        conf.append("\nUnsynced:" + unsynced);
        List<TextMessage> erroredOut = helper.withStatus(context, TextMessage.OUTGOING, TextMessage.ERRORED);
        conf.append("\n\nErrored Out: " + erroredOut.size());
        for (TextMessage msg : erroredOut) {
            conf.append("\n" + msg.number + ": " + msg.text);
        }
        List<TextMessage> erroredIn = helper.withStatus(context, TextMessage.INCOMING, TextMessage.ERRORED);
        conf.append("\n\nErrored In: " + erroredIn.size());
        for (TextMessage msg : erroredIn) {
            conf.append("\n" + msg.number + ": " + msg.text);
        }
        conf.append("\n\nLog:\n\n");
        // prepend our configuration to our body
        body = conf.toString() + body;
        try {
            HttpPost post = new HttpPost("http://" + hostname + "/router/alert");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("password", "" + prefs.getString("router_password", null)));
            nameValuePairs.add(new BasicNameValuePair("subject", "[" + backend + "] " + subject));
            nameValuePairs.add(new BasicNameValuePair("body", body));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            Log.e(MainActivity.TAG, "Sending log to: " + post.getURI().toURL().toString());
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            client.execute(post, responseHandler);
            return true;
        } catch (Throwable t) {
            Log.e(MainActivity.TAG, "Sending of alert failed", t);
            return false;
        }
    }
    return false;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) TextMessage(com.nyaruka.androidrelay.data.TextMessage) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Example 10 with TextMessageHelper

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

the class RelayService method sendPendingMessagesToServer.

/**
	 * Sends all our pending outgoing messages to our server.
	 */
public void sendPendingMessagesToServer() throws IOException {
    List<TextMessage> msgs = null;
    // first send any that haven't yet been tried
    TextMessageHelper helper = getHelper();
    msgs = helper.withStatus(this.getApplicationContext(), TextMessage.INCOMING, TextMessage.RECEIVED);
    for (TextMessage msg : msgs) {
        sendMessageToServer(msg);
    }
    // then those that had an error when we tried to contact the server
    helper = getHelper();
    msgs = helper.withStatus(this.getApplicationContext(), TextMessage.INCOMING, TextMessage.ERRORED);
    for (TextMessage msg : msgs) {
        sendMessageToServer(msg);
    }
}
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