68 lines
2.9 KiB
Java
68 lines
2.9 KiB
Java
package za.org.treehouse.hypoalarm;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.location.Location;
|
|
import android.location.LocationListener;
|
|
import android.location.LocationManager;
|
|
import android.os.Bundle;
|
|
import android.os.PowerManager;
|
|
import android.preference.PreferenceManager;
|
|
import android.telephony.SmsManager;
|
|
import android.util.Log;
|
|
import android.widget.Toast;
|
|
|
|
public class GraceReceiver extends BroadcastReceiver {
|
|
private static SharedPreferences sharedPref;
|
|
private static String uri;
|
|
|
|
@Override
|
|
public void onReceive(Context context, Intent intent) {
|
|
sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
|
|
Boolean alarmActive = sharedPref.getBoolean(context.getString(R.string.AlarmActivePref), true);
|
|
|
|
if (alarmActive) {
|
|
String phoneNumber = sharedPref.getString(context.getString(R.string.PhoneNumberPref), null);
|
|
String message = sharedPref.getString(context.getString(R.string.MessagePref), null);
|
|
|
|
// TODO get location of phone and send in the message
|
|
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
|
LocationListener locationListener = new LocationListener() {
|
|
public void onLocationChanged(Location location) {
|
|
// Called when a new location is found by the network location provider.
|
|
if (location != null) {
|
|
double latitude = location.getLatitude();
|
|
double longitude = location.getLongitude();
|
|
|
|
uri = " http://maps.google.com?q=" + location.getLatitude() + "," + location.getLongitude();
|
|
}
|
|
}
|
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
|
}
|
|
public void onProviderEnabled(String provider) {
|
|
}
|
|
public void onProviderDisabled(String provider) {
|
|
}
|
|
};
|
|
|
|
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
|
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
|
|
} else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
|
|
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
|
|
}
|
|
|
|
message += uri;
|
|
|
|
SmsManager sms = SmsManager.getDefault();
|
|
// TODO uncomment this:
|
|
//sms.sendTextMessage(phoneNumber, null, message, null, null);
|
|
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
|
|
Log.d("GraceReceiver", "Sending sms to " + phoneNumber);
|
|
}
|
|
}
|
|
|
|
|
|
}
|