Android Open Source - watcher Sms Receiver






From Project

Back to project page watcher.

License

The source code is released under:

CC0 1.0 Universal Statement of Purpose The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent...

If you think the Android project watcher 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.appspot.staffordbears.watcher;
/*  w  ww.  j ava2s .  c  om*/
import java.io.File;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.app.PendingIntent;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.SmsMessage;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {

    private static final String TAG = "watcher.Sms";
    private static final String SECRET = "1jwd5";

      
    private int count(File dir) {
  int total=0;
  
  File listFile[] = dir.listFiles();
  if (listFile != null) {
      for (int i=0; i<listFile.length; i++) {
    if (listFile[i].isDirectory()) {
        count(listFile[i]);
    } else {
        total++;
    }
      }
  }
  return total;
    }

    
    private void msg_back (Context context, String number, String message) {
  SmsManager sms = SmsManager.getDefault();
  String sent = "android.telephony.SmsManager.STATUS_ON_ICC_SENT";
  PendingIntent piSent = PendingIntent.getBroadcast(context, 0,new Intent(sent), 0);  
  sms.sendTextMessage(number, null, message, piSent, null);
    }

    @Override
    public void onReceive(Context context, Intent intent) {

  Hermit mApp = (Hermit) context.getApplicationContext();

  // TODO Auto-generated method stub
  Bundle pudsBundle = intent.getExtras();
  Object[] pdus = (Object[]) pudsBundle.get("pdus");
  SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);

  String body = messages.getMessageBody();
  String source = messages.getOriginatingAddress();

  Log.i(TAG, "BODY: " + body);
  Log.i(TAG, "NUM: " + source);
  
  String[] tok = messages.getMessageBody().split(" ");
    
  if (tok[0] != null) {
      String code = tok[0];
      if (code.compareTo(SECRET) == 0) {
    if (tok[1] != null) {
        String cmd = tok[1];
        if (cmd.compareTo("start") == 0) {
      Log.i(TAG, "starting");
      Intent myIntent = new Intent(context, CommLink.class);
      myIntent.putExtra("extraData", "somedata");
      context.startService(myIntent);
      msg_back(context, source, "service started");
        } else if (cmd.compareTo("stop") == 0) {
      Log.i(TAG, "stopping");
      Intent myIntent = new Intent(context, CommLink.class);
      myIntent.putExtra("extraData", "somedata");
      context.stopService(myIntent);
      msg_back(context, source, "service stopped");
        } else if (cmd.compareTo("report") == 0) {
      Log.i(TAG, "generating report");
      String rep = new String();
      if (mApp.mService != null) {
          rep += "|service-UP" + "\n";
      } else {
          rep += "|service-DOWN" + "\n";
      }
      File sdDir = Environment.getExternalStorageDirectory();
      File pdir = new File(sdDir, "Watcher" + File.separator + "photos");
      int num = count(pdir);
      rep += "|pcount-" + num + "\n";
      msg_back(context, source, rep);
        } else {
      Log.w(TAG, "unknown command");
      msg_back(context, source, "unknown command");
        }
    }
      } else {
    Log.w(TAG, "break in attempt!");
      }
  }  
    }

}




Java Source Code List

com.appspot.staffordbears.watcher.CommLink.java
com.appspot.staffordbears.watcher.Hermit.java
com.appspot.staffordbears.watcher.SerialProtocol.java
com.appspot.staffordbears.watcher.SmsReceiver.java
com.appspot.staffordbears.watcher.Snap.java
com.appspot.staffordbears.watcher.Watch.java