init commit
Change-Id: Ib96d43f22db2bdf360d1d1e0693f6aa5e3aa50ab
This commit is contained in:
208
DroidFinder/src/com/TwentyCodes/android/DroidFinderFull/SMS.java
Normal file
208
DroidFinder/src/com/TwentyCodes/android/DroidFinderFull/SMS.java
Normal file
@@ -0,0 +1,208 @@
|
||||
/**
|
||||
* @author Twenty Codes
|
||||
* @author ricky barrette
|
||||
*/
|
||||
|
||||
package com.TwentyCodes.android.DroidFinderFull;
|
||||
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
import android.location.Location;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.telephony.gsm.SmsManager;
|
||||
import android.telephony.gsm.SmsMessage;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* this class will handle all receiving and sending of SMS messages
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public class SMS extends BroadcastReceiver{
|
||||
|
||||
protected static final String DELETE_CONFIRMATION = "delete_comfirmation";
|
||||
protected static final String RETURN_ADDRESS = "return_address";
|
||||
protected static final String NUMBERS = "numbers";
|
||||
protected static final String LAT = "lat";
|
||||
protected static final String LON = "lon";
|
||||
protected static final String ACCURACY = "accuracy";
|
||||
|
||||
private SharedPreferences numbers;
|
||||
private SharedPreferences settings;
|
||||
private String tag = "SmsReceiver";
|
||||
|
||||
/**
|
||||
* checks the md5 hash code of a string msg to the saved md5 hash code of a password
|
||||
* @param msg
|
||||
* @param key
|
||||
* @return true if the md5 hash code matches the saved md5 hash code saved in the passwords.xml
|
||||
* @author ricky barrette
|
||||
*/
|
||||
// private boolean checkmd5sum(String msg, String key){
|
||||
// String correctMd5 = passwords.getString(key,null);
|
||||
// if (correctMd5 != null) {
|
||||
// String md5hash = DroidFinder.getMd5Hash(msg);
|
||||
// if (md5hash.equals(correctMd5)) {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Log.i(tag,"onRecive()");
|
||||
|
||||
settings = context.getSharedPreferences(SettingsActivity.SETTINGS, 0);
|
||||
numbers = context.getSharedPreferences(NUMBERS, 0);
|
||||
|
||||
//get the SMS message
|
||||
Bundle bundle = intent.getExtras();
|
||||
|
||||
if (bundle != null)
|
||||
{
|
||||
SmsMessage[] msgs = null;
|
||||
StringBuilder smsMsg = new StringBuilder();
|
||||
StringBuilder phoneNumber = new StringBuilder();
|
||||
|
||||
//read the SMS message received
|
||||
Object[] pdus = (Object[]) bundle.get("pdus");
|
||||
msgs = new SmsMessage[pdus.length];
|
||||
for (int i=0; i<msgs.length; i++){
|
||||
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
|
||||
smsMsg.append(msgs[i].getMessageBody().toString());
|
||||
phoneNumber.append(msgs[i].getOriginatingAddress());
|
||||
}
|
||||
|
||||
String msg = smsMsg.toString();
|
||||
|
||||
/*
|
||||
* locate
|
||||
* if the SMS message matches a specified string (ignoring case)
|
||||
* then do something
|
||||
*/
|
||||
if (msg.equalsIgnoreCase(settings.getString(SettingsActivity.LOCATE_PASS, null)) &&
|
||||
settings.getBoolean(SettingsActivity.LOCATE_ENABLED, false)){
|
||||
|
||||
if (phoneNumber.toString().length() > 0 && storeNumber(phoneNumber.toString())){
|
||||
Log.i(tag, "saved number successfuly!");
|
||||
|
||||
Log.i(tag,"sending phone location to sender");
|
||||
|
||||
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
||||
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||
String number = numbers.getString(RETURN_ADDRESS, null);
|
||||
if (number != null){
|
||||
sendSMS(number,
|
||||
"DroidFinder: " +
|
||||
location.getLatitude() + ", " + location.getLongitude()
|
||||
+ "\n Accuracy (meters)= " + location.getAccuracy());
|
||||
// + "\n Speed (meters/second) = "
|
||||
// + location.getSpeed());
|
||||
}
|
||||
|
||||
} else {
|
||||
Log.w(tag, "did not save number successfuly");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* if the sms msg contains the char sequence "DroidFinder: " then save the 3 floats
|
||||
* for lat, lon, and accuracy.
|
||||
*/
|
||||
if (msg.startsWith("DroidFinder")){
|
||||
Log.i(tag,"recived a droidfinder message");
|
||||
Scanner scan = new Scanner(msg);
|
||||
int index = 0;
|
||||
Editor editor = settings.edit();
|
||||
|
||||
/*
|
||||
* loops though a human readable message than contains a lat, lon, and accuracy in a specific order.
|
||||
*
|
||||
* while the is a other token in the string, try to save it as lat, lon, or accuracy (based on what has been already saved)
|
||||
* if we cant save due to an InputMismatchException, then log it and skip over the token.
|
||||
*/
|
||||
do {
|
||||
try {
|
||||
switch (index){
|
||||
case 0:
|
||||
Log.i(tag,"tring to save token as lat");
|
||||
editor.putInt(LAT, (int) (scan.nextDouble() * 1e6));
|
||||
index++;
|
||||
Log.i(tag,"save success");
|
||||
break;
|
||||
case 1:
|
||||
Log.i(tag,"tring to save token as lon");
|
||||
editor.putInt(LON, (int) (scan.nextDouble() * 1e6));
|
||||
index++;
|
||||
Log.i(tag,"save success");
|
||||
break;
|
||||
case 2:
|
||||
Log.i(tag,"tring to save token as accuracy");
|
||||
editor.putFloat(ACCURACY, scan.nextFloat());
|
||||
index++;
|
||||
Log.i(tag,"save success");
|
||||
break;
|
||||
}
|
||||
} catch (InputMismatchException e) {
|
||||
Log.e(tag,"save failed");
|
||||
e.printStackTrace();
|
||||
scan.next();
|
||||
}
|
||||
} while(scan.hasNext());
|
||||
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
/*
|
||||
* lockPattern
|
||||
* if the SMS message matches a specified string (ignoring case)
|
||||
* then do something
|
||||
*/
|
||||
if (msg.equalsIgnoreCase(settings.getString(SettingsActivity.LOCK_PATTERN_PASS, null))&&
|
||||
settings.getBoolean(SettingsActivity.LOCK_PATTERN_ENABLED, false)){
|
||||
Log.i(tag,"locking phone and dimming screen to save battery");
|
||||
|
||||
SettingsManager sm = new SettingsManager(context);
|
||||
|
||||
//dim the screen to minimum
|
||||
sm.setScreenBrightness(0);
|
||||
//set screen timeout to 1 second
|
||||
sm.setScreenTimeOff(1);
|
||||
//lock the screen
|
||||
sm.setLockPatternEnabled(true);
|
||||
//disable wifi
|
||||
sm.setWifiEnabled(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* sends a SMS message
|
||||
* @param phoneNumber
|
||||
* @param message
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void sendSMS(String phoneNumber, String message){
|
||||
SmsManager sms = SmsManager.getDefault();
|
||||
sms.sendTextMessage(phoneNumber, null, message, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* stores the senders number to shared_prefs numbers.xml
|
||||
* @param number
|
||||
* @return true if save was successful
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private boolean storeNumber(String number){
|
||||
Editor editor = numbers.edit();
|
||||
editor.putString(RETURN_ADDRESS, number);
|
||||
return editor.commit();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user