From 6c386d5baf472757fd9c8cf4fc8d4dee347aea5d Mon Sep 17 00:00:00 2001 From: tim Date: Fri, 18 Apr 2014 17:26:03 +0200 Subject: [PATCH] Only display toast of text message if debugging is on; get user to approve location queries when starting the app, not when sending text messages; re-add wakelock to AlarmService, otherwise snoozes don't happen. --- HypoAlarm/HypoAlarm-HypoAlarm.iml | 40 +++++++------ .../org/treehouse/hypoalarm/AlarmService.java | 5 +- .../treehouse/hypoalarm/GraceReceiver.java | 7 ++- .../org/treehouse/hypoalarm/MainActivity.java | 58 +++++++++++++++---- 4 files changed, 76 insertions(+), 34 deletions(-) diff --git a/HypoAlarm/HypoAlarm-HypoAlarm.iml b/HypoAlarm/HypoAlarm-HypoAlarm.iml index 70b00c6..978d53d 100644 --- a/HypoAlarm/HypoAlarm-HypoAlarm.iml +++ b/HypoAlarm/HypoAlarm-HypoAlarm.iml @@ -8,10 +8,11 @@ - - + - - - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmService.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmService.java index 0a7061f..3c2f715 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmService.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmService.java @@ -33,11 +33,10 @@ public class AlarmService extends Service { @Override public void onCreate() { - // Ensure that CPU runs while the service is running, so we don't miss an alert or snooze + // Ensure that CPU runs while the service is running, so we don't miss an alert after snoozing PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AlarmService"); - // TODO: wake lock? - //wl.acquire(); + wl.acquire(); } @Override public void onDestroy() { diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/GraceReceiver.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/GraceReceiver.java index 69f4081..87a0808 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/GraceReceiver.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/GraceReceiver.java @@ -23,7 +23,7 @@ import com.google.android.gms.location.LocationClient; */ public class GraceReceiver extends BroadcastReceiver { - private static LocationClient locationClient; + private static LocationClient locationClient = null; private static String phoneNumber; private static String message; @@ -49,11 +49,11 @@ public class GraceReceiver extends BroadcastReceiver { String uri = " http://maps.google.com?q=" + location.getLatitude() + "," + location.getLongitude(); message += uri; sendText(context); - locationClient.disconnect(); } else { Log.e("GraceReceiver", "No location data available. Sending text message anyway."); sendText(context); } + locationClient.disconnect(); } @Override @@ -84,8 +84,9 @@ public class GraceReceiver extends BroadcastReceiver { } else { if (!MainActivity.HYPOALARM_DEBUG) { sms.sendTextMessage(phoneNumber, null, message, null, null); + } else { + Toast.makeText(context, message, Toast.LENGTH_LONG).show(); } - Toast.makeText(context, message, Toast.LENGTH_LONG).show(); Log.d("GraceReceiver", "Sending sms to " + phoneNumber + " with message: " + message); } } diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java index 1714251..7ef5a0d 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java @@ -12,6 +12,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.database.Cursor; +import android.location.Location; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; @@ -38,6 +39,11 @@ import android.widget.Spinner; import android.widget.TimePicker; 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; + import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -58,17 +64,18 @@ import java.util.regex.Pattern; // Alerts via Whatsapp and other protocols? public class MainActivity extends ActionBarActivity { - public static final int ALARM_REQUEST = 1; - public static final int GRACE_REQUEST = 2; - public static final int PRE_NOTIFY_REQUEST = 3; - public static final int CANCEL_GRACE_REQUEST = 4; - public static final int CANCEL_ALARM_REQUEST = 5; - public static final int PHONE_NUMBER_REQUEST = 6; - public static final int RINGTONE_REQUEST = 7; - public static final String TIMEPICKER_TAG = "alarmTimePicker"; - public static final String defaultTimeStr = "09:00"; - public static final int defaultGracePeriod = 60; - public static final Boolean defaultActive = true; + private static LocationClient locationClient = null; + public static final int ALARM_REQUEST = 1; + public static final int GRACE_REQUEST = 2; + public static final int PRE_NOTIFY_REQUEST = 3; + public static final int CANCEL_GRACE_REQUEST = 4; + public static final int CANCEL_ALARM_REQUEST = 5; + public static final int PHONE_NUMBER_REQUEST = 6; + public static final int RINGTONE_REQUEST = 7; + public static final String TIMEPICKER_TAG = "alarmTimePicker"; + public static final String defaultTimeStr = "09:00"; + public static final int defaultGracePeriod = 60; + public static final Boolean defaultActive = true; public static final Boolean HYPOALARM_DEBUG = false; @@ -290,6 +297,35 @@ public class MainActivity extends ActionBarActivity { PackageManager.DONT_KILL_APP); Log.d("MainActivity", "Setting boot receiver"); } + + // Get the location now, so that user has to grant permission for location now, not when + // the text message is sent. + if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) == ConnectionResult.SUCCESS) { + locationClient = new LocationClient(getActivity(), + new GooglePlayServicesClient.ConnectionCallbacks() { + @Override + public void onConnected(Bundle bundle) { + Location location = locationClient.getLastLocation(); + if (location == null) { + Log.e("MainActivity", "No location data available."); + } + locationClient.disconnect(); + } + @Override + public void onDisconnected() { + } + }, + new GooglePlayServicesClient.OnConnectionFailedListener() { + @Override + public void onConnectionFailed(ConnectionResult connectionResult) { + Log.e("MainActivity", "Failed connection to location manager " + connectionResult.toString()); + } + } + ); + locationClient.connect(); + } else { + Log.e("GraceReceiver", "Google Play Services is not available."); + } } @Override