Correct timer problem (remove redundant timer.cancel()s).

This commit is contained in:
Timothy Allen 2014-03-25 22:14:23 +02:00
parent b893ab6c72
commit d43bb2b1d5
8 changed files with 63 additions and 61 deletions

10
.idea/libraries/appcompat_v7_19_0_1.xml generated Normal file
View File

@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="appcompat-v7-19.0.1">
<CLASSES>
<root url="file://$PROJECT_DIR$/HypoAlarm/build/exploded-aar/com.android.support/appcompat-v7/19.0.1/res" />
<root url="jar://$PROJECT_DIR$/HypoAlarm/build/exploded-aar/com.android.support/appcompat-v7/19.0.1/classes.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

11
.idea/libraries/support_v4_19_0_1.xml generated Normal file
View File

@ -0,0 +1,11 @@
<component name="libraryTable">
<library name="support-v4-19.0.1">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/sdk/extras/android/m2repository/com/android/support/support-v4/19.0.1/support-v4-19.0.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$APPLICATION_HOME_DIR$/sdk/extras/android/m2repository/com/android/support/support-v4/19.0.1/support-v4-19.0.1-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -6,7 +6,6 @@ import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
@ -24,13 +23,14 @@ import java.util.TimerTask;
// TODO set another alarm for the next half-hour (or grace_period / 2)?
public class AlarmAlertActivity extends Activity {
// TODO correct life
// TODO correct alert lifetime
private static final int ALERT_LIFE = 1000*10;//1000*60*2; // 2 minutes
private static final Timer timer = new Timer();
private static PowerManager.WakeLock fullWl;
private static final Timer timer = new Timer("AlarmAlertActivity");
private static final long[] vPattern = {500, 500};
private static AlarmManager graceManager;
private static PendingIntent gracePendingIntent;
private static Vibrator vibrator;
private static Boolean userCancelled;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -38,7 +38,7 @@ public class AlarmAlertActivity extends Activity {
setContentView(R.layout.alarm_alert);
// Disable any current notifications
this.stopService(new Intent(getApplicationContext(), AlarmNotify.class));
stopService(new Intent(getApplicationContext(), AlarmNotify.class));
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
@ -48,10 +48,13 @@ public class AlarmAlertActivity extends Activity {
// Turn off the alert activity, and switch to a notification
timer.schedule(new TimerTask() {
public void run() {
// Switch to notification
startService(new Intent(getApplicationContext(), AlarmNotify.class));
// Close the dialogue
finish();
Log.d("AlarmAlertActivity", "Stopped AlarmAlertActivity");
Log.d("AlarmAlertActivity", "Started notification");
// Switch to notification if the Activity has not been closed by the user
if (!userCancelled) {
startService(new Intent(getApplicationContext(), AlarmNotify.class));
}
}
}, ALERT_LIFE);
}
@ -59,15 +62,10 @@ public class AlarmAlertActivity extends Activity {
@Override
public void onStart() {
super.onStart();
userCancelled = false;
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
fullWl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "AlarmAlertActivity");
fullWl.acquire();
long[] pattern = {0, 1000, 1000};
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern, 0);
vibrator.vibrate(vPattern, 0);
// TODO change button to slide...
// TODO change slide to circle slide? https://github.com/JesusM/HoloCircleSeekBar
@ -80,15 +78,16 @@ public class AlarmAlertActivity extends Activity {
gracePendingIntent = PendingIntent.getBroadcast(getApplicationContext(), MainActivity.GRACE_REQUEST, graceIntent, 0);
graceManager.cancel(gracePendingIntent);
Log.d("AlarmAlertActivity", "Cancelled grace alarm.");
// Ensure we don't load a notification later on
userCancelled = true;
// Close the dialogue (stop vibration &c)
finish();
}
});
}
public void onStop() {
timer.cancel();
vibrator.cancel();
fullWl.release();
super.onStop();
}

View File

@ -17,8 +17,8 @@ import java.util.Timer;
import java.util.TimerTask;
public class AlarmNotify extends Service {
public final int notifyID = 1;
private Timer timer = new Timer();
private static final Timer timer = new Timer("AlarmNotify");
public static final int notifyID = 1;
@Override
public IBinder onBind(Intent intent) {
@ -27,12 +27,10 @@ public class AlarmNotify extends Service {
@Override
public void onDestroy() {
// Remember to remove the notification in the notification bar
// Remove the notification in the notification bar
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(notifyID);
if (timer != null) {
timer.cancel();
}
timer.cancel();
}
@Override
@ -40,8 +38,8 @@ public class AlarmNotify extends Service {
final int UPDATE_INTERVAL = 10*1000; // Timer is updated six times a minute
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
//final String phoneNumber = sharedPref.getString(getString(R.string.PhoneNumberPref), null);
final int gracePeriod = sharedPref.getInt(getString(R.string.GracePeriodPref), 60);
final String phoneNumber = sharedPref.getString(getString(R.string.PhoneNumberPref), null);
// convert gracePeriod to milliseconds and calculate when it'll fire
final long endTime = System.currentTimeMillis() + (gracePeriod * 60 * 1000);
@ -67,9 +65,14 @@ public class AlarmNotify extends Service {
// Allow the user to cancel by clicking a "Cancel" button
notification.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.notificationCancellation), cancellerPendingIntent);
// Allow the user to cancel by selecting the ContentText or ContentTitle
// TODO load alert activity (without sound or vibration) on select? This would allow the user to test competence
notification.setContentIntent(cancellerPendingIntent);
// TODO load alert activity (without sound or vibration) on select? This would allow the user to test competence
Intent alertActivityIntent = new Intent(this, AlarmAlertActivity.class);
alertActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
//notification.setContentIntent(alertActivityIntent);
nm.cancel(notifyID);
nm.notify(notifyID, notification.build());

View File

@ -6,20 +6,16 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.util.Log;
import java.util.Calendar;
// TODO sound audible alarm -- see AlarmKlaxon.java
// TODO Lower alarm volume if in phone call (and detect phone call!)
// TODO set another alarm for the next half-hour (or grace_period / 2)?
public class AlarmReceiver extends BroadcastReceiver {
SharedPreferences sharedPref;
private AlarmManager alarmManager, graceManager;
private PendingIntent alarmPendingIntent, gracePendingIntent;
private static SharedPreferences sharedPref;
private static AlarmManager alarmManager, graceManager;
private static PendingIntent alarmPendingIntent, gracePendingIntent;
@Override
public void onReceive(Context context, Intent intent) {
@ -27,17 +23,8 @@ public class AlarmReceiver extends BroadcastReceiver {
Boolean alarmActive = sharedPref.getBoolean(context.getString(R.string.AlarmActivePref), true);
if (alarmActive) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock partialWl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AlarmReceiver");
partialWl.acquire();
// Disable any current notifications
//context.stopService(new Intent(context, AlarmNotify.class));
// Set a grace period alarm to send SMS
int gracePeriod = sharedPref.getInt(context.getString(R.string.GracePeriodPref), 60);
// TODO remove this
gracePeriod = 1;
Calendar graceCal = Calendar.getInstance();
graceCal.set(Calendar.SECOND, 0);
@ -66,8 +53,6 @@ public class AlarmReceiver extends BroadcastReceiver {
// TODO use set() for older APIs
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent);
Log.d("AlarmReceiver", "Resetting alarm for " + MainActivity.debugDate(cal));
partialWl.release();
}
}
}

View File

@ -12,9 +12,9 @@ import android.util.Log;
import java.util.Calendar;
public class BootReceiver extends BroadcastReceiver {
SharedPreferences sharedPref;
private AlarmManager alarmManager;
private PendingIntent alarmIntent;
private static SharedPreferences sharedPref;
private static AlarmManager alarmManager;
private static PendingIntent alarmIntent;
@Override
public void onReceive(Context context, Intent intent) {

View File

@ -10,7 +10,7 @@ import android.telephony.SmsManager;
import android.util.Log;
public class GraceReceiver extends BroadcastReceiver {
SharedPreferences sharedPref;
private static SharedPreferences sharedPref;
@Override
public void onReceive(Context context, Intent intent) {
@ -18,10 +18,6 @@ public class GraceReceiver extends BroadcastReceiver {
Boolean alarmActive = sharedPref.getBoolean(context.getString(R.string.AlarmActivePref), true);
if (alarmActive) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock partialWl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AlarmReceiver");
partialWl.acquire();
String phoneNumber = sharedPref.getString(context.getString(R.string.PhoneNumberPref), null);
String message = sharedPref.getString(context.getString(R.string.MessagePref), null);
@ -29,8 +25,6 @@ public class GraceReceiver extends BroadcastReceiver {
// TODO uncomment this:
//sms.sendTextMessage(phoneNumber, null, message, null, null);
Log.d("GraceReceiver", "Sending sms to " + phoneNumber);
partialWl.release();
}
}
}

View File

@ -59,14 +59,14 @@ import java.util.regex.Pattern;
// TODO: glowpad dismissal (or circular seekbar)
public class MainActivity extends ActionBarActivity {
static int ALARM_REQUEST = 1;
static int GRACE_REQUEST = 2;
static int CANCEL_GRACE_REQUEST = 3;
static Switch alarmActiveSwitch;
static Button alarmTimeButton;
static Spinner gracePeriodSpinner;
static EditText phoneNumberButton;
static EditText messageButton;
public static int ALARM_REQUEST = 1;
public static int GRACE_REQUEST = 2;
public static int CANCEL_GRACE_REQUEST = 3;
private static Switch alarmActiveSwitch;
private static Button alarmTimeButton;
private static Spinner gracePeriodSpinner;
private static EditText phoneNumberButton;
private static EditText messageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {