HypoAlarm/HypoAlarm/src/main/java/za/org/treehouse/HypoAlarm/GraceReceiver.java

94 lines
4.3 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.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
/*
* TODO: translate geographic coordinates into an address?
* import android.location.Address;
* import android.location.Geocoder;
*/
public class GraceReceiver extends BroadcastReceiver {
private static LocationClient locationClient = null;
private static String phoneNumber;
private static String message;
@Override
public void onReceive(final Context context, Intent intent) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
Boolean alarmActive = sharedPref.getBoolean(context.getString(R.string.AlarmActivePref), MainActivity.defaultActive);
phoneNumber = sharedPref.getString(context.getString(R.string.PhoneNumberPref), null);
message = sharedPref.getString(context.getString(R.string.MessagePref), context.getString(R.string.defaultMessage));
// Stop any lingering alarm service
context.stopService(new Intent(context, AlarmService.class));
if (alarmActive) {
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {
locationClient = new LocationClient(context,
new GooglePlayServicesClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Location location = locationClient.getLastLocation();
if (location != null) {
// uri must begin with a space
String uri = " http://maps.google.com/maps/?q=loc:" + location.getLatitude() + "," + location.getLongitude();
message += uri;
sendText(context);
} else {
Log.e("GraceReceiver", "No location data available. Sending text message anyway.");
sendText(context);
}
locationClient.disconnect();
}
@Override
public void onDisconnected() {
}
},
new GooglePlayServicesClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e("GraceReceiver", "Failed connection to location manager " + connectionResult.toString() + ". Sending text message anyway.");
sendText(context);
}
}
);
locationClient.connect();
} else {
Log.e("GraceReceiver", "Google Play Services is not available. Sending text message anyway.");
sendText(context);
}
}
}
private void sendText(Context context) {
SmsManager sms = SmsManager.getDefault();
if (phoneNumber == null || phoneNumber.isEmpty()) {
message = "You have not specified a phone number. No text message will be sent.";
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
Log.e("GraceReceiver", "ERROR: " + message);
} else {
if (!MainActivity.HYPOALARM_DEBUG) {
sms.sendTextMessage(phoneNumber, null, message, null, null);
} else {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
Log.d("GraceReceiver", "Sending sms to " + phoneNumber + " with message: " + message);
}
}
}