Android Open Source - sms-smap-gateway Gateway Service






From Project

Back to project page sms-smap-gateway.

License

The source code is released under:

GNU General Public License

If you think the Android project sms-smap-gateway listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.android.smap.sms;
/* ww  w  .j av  a2s. com*/
import java.io.IOException;
import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

import com.android.smap.api.models.Gojo;
import com.android.smap.commonsware.wakefull.WakefulIntentService;
import com.android.smap.controllers.ControllerError;
import com.android.smap.controllers.ControllerErrorListener;
import com.android.smap.controllers.ControllerFactory;
import com.android.smap.controllers.ControllerListener;
import com.android.smap.models.TextMessage;
import com.android.smap.samuel.Samuel;

public class GatewayService extends Service implements
    CellularModem.SmsModemListener,
    ControllerListener,
    ControllerErrorListener {

  public static final String  TAG    = GatewayService.class.getCanonicalName();

  public static GatewayService  s_this  = null;

  public CellularModem        modem;

  public static GatewayService get() {
    return s_this;
  }

  public static boolean  doReset  = false;

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onCreate() {
    s_this = this;
    modem = new CellularModem(getApplicationContext(), this);
    Log.d(TAG, "RelayService Created.");
    promoteErroredMessages();
    kickService();
  }

  /***
   * 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() {

    // for all messages that never got replied to (and so never had their
    // pending status revoked. ) ?
    // call sendPendingMessagesToServer?
    // call only at startup
    // TODO

  }

  /***
   * 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() {

    // TODO
  }

  /**
   * 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
    // get all the msgs with fresh status
    for (TextMessage msg : msgs) {
      sendMessageToServer(msg);
    }

    // then those that had an error when we tried to contact the server
    // get all the error sattus msgs
    for (TextMessage msg : msgs) {
      sendMessageToServer(msg);
    }
  }

  public void markDeliveries() {

    // change status to delivered
  }

  /**
   * 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);

    // message

    // send volley request
  }

  public void markMessageDelivered(TextMessage msg) {

    // update status of message, pending becomes sent. etc
  }

  // triggers our background service to go do things
  public void kickService() {
    WakefulIntentService.sendWakefulWork(this, MonitorService.class);
  }

  public void sendMessage(TextMessage msg) {

    Log.d(TAG, "=== SMS OUT: " + msg.number + ": " + msg.text);
    try {
      modem.sendSms(msg.number, msg.text, "" + msg.id);
    }
    catch (Exception e) {
      msg.status = TextMessage.ERRORED;
    }

  }

  // SMS recieved by gateway
  public void onNewSMS(String number, String message) {

    // commands could be called, return feedback straight away
    // if(message == reset){
    // modem.sendSMS(num, "resetting");
    // kickService(reset);

    // sendSMS or KickService basically...

    Gojo gojo = Samuel.parse(message);
    ControllerFactory.fetch(gojo, this).start();

    // kickService();
  }

  public void onSMSSendError(String token, String errorDetails) {
    TextMessage msg = null;

    // change status, add to backlog
  }

  public void onSMSSent(String token) {

    // change status to sent,
    // kickService()
  }

  @Override
  public void onControllerError(ControllerError error) {
    // TODO error with request handeling.

  }

  @Override
  public void onControllerResult() {
    // TODO succesfull outbound request

  }

}




Java Source Code List

com.android.smap.AppConfig.java
com.android.smap.GatewayApp.java
com.android.smap.PhoneStateWrapper.java
com.android.smap.activities.BaseActivity.java
com.android.smap.activities.FragmentWrapperActivity.java
com.android.smap.activities.MainActivity.java
com.android.smap.adapters.MenuDrawerAdapter.java
com.android.smap.api.ApiConstants.java
com.android.smap.api.models.Gojo.java
com.android.smap.api.models.Node.java
com.android.smap.api.models.SmapException.java
com.android.smap.api.models.Status.java
com.android.smap.api.models.validator.ModelValidator.java
com.android.smap.api.models.validator.Required.java
com.android.smap.api.requests.ApiRequest.java
com.android.smap.api.requests.GojoRequest.java
com.android.smap.api.requests.StatusRequest.java
com.android.smap.commonsware.wakefull.AlarmReceiver.java
com.android.smap.commonsware.wakefull.WakefulIntentService.java
com.android.smap.controllers.ControllerErrorListener.java
com.android.smap.controllers.ControllerError.java
com.android.smap.controllers.ControllerFactory.java
com.android.smap.controllers.ControllerListener.java
com.android.smap.controllers.Controller.java
com.android.smap.controllers.EmailController.java
com.android.smap.controllers.GojoController.java
com.android.smap.controllers.UIPagingController.java
com.android.smap.controllers.UIRequestController.java
com.android.smap.fragments.BaseFragment.java
com.android.smap.fragments.HomeFragment.java
com.android.smap.models.MenuDrawerItem.java
com.android.smap.models.TextMessage.java
com.android.smap.samuel.Samuel.java
com.android.smap.sms.CellularModem.java
com.android.smap.sms.GatewayService.java
com.android.smap.sms.MonitorService.java
com.android.smap.sms.ScheduleMonitoringListener.java
com.android.smap.sms.ServiceRebooter.java
com.android.smap.ui.ArrayVelocAdapter.java
com.android.smap.ui.OnErrorTouchedListener.java
com.android.smap.ui.Operation.java
com.android.smap.ui.VelocAdapter.java
com.android.smap.ui.ViewBinder.java
com.android.smap.ui.ViewQuery.java
com.android.smap.ui.views.AnimatedNetworkImageView.java
com.android.smap.ui.views.ErrorView.java
com.android.smap.ui.views.FontableTextView.java
com.android.smap.utils.BitmapLruCache.java
com.android.smap.utils.MWAPNUtils.java
com.android.smap.utils.MWAnimUtil.java
com.android.smap.utils.MWAssetsUtils.java
com.android.smap.utils.MWConnectionUtils.java
com.android.smap.utils.MWEmailUtils.java
com.android.smap.utils.MWUiUtils.java
com.android.smap.utils.PreferenceWrapper.java
com.android.smap.utils.Refresher.java
com.android.smap.utils.UriBuilder.java