HypoAlarm/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmAlertActivity.java

117 lines
4.1 KiB
Java

package za.org.treehouse.hypoalarm;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
// TODO See GlowPad.
// TODO sound audible alarm -- see AlarmKlaxon.java
// TODO Lower alarm volume if in phone call (and detect phone call!)
// TODO Snooze? set another alarm for the next half-hour (or grace_period / 2)?
public class AlarmAlertActivity extends Activity {
// TODO correct alert lifetime
private static final int ALERT_LIFE = 1000*10; //1000*60*2; // 2 minutes
private static final long[] vPattern = {500, 500};
private static Boolean userCancelled;
private static Vibrator vibrator;
private static Intent notifyIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
// Set to use the full screen
int fullScreen = WindowManager.LayoutParams.FLAG_FULLSCREEN;
// TODO if KitKat, mimic the main alarm
fullScreen = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| fullScreen
);
setContentView(R.layout.alarm_alert);
notifyIntent = new Intent(getApplicationContext(), AlarmNotify.class);
// Disable any current notifications
stopService(notifyIntent);
// Turn off the alert activity, and switch to a notification
new Handler().postDelayed(new Runnable() {
public void run() {
// Close the dialogue and switch to notification
// if the Activity has not been closed by the user
if (!userCancelled) {
startService(notifyIntent);
finish();
}
}
}, ALERT_LIFE);
}
@Override
public void onStart() {
super.onStart();
userCancelled = false;
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(vPattern, 0);
// TODO change button to slide...
// TODO change slide to circle slide? https://github.com/JesusM/HoloCircleSeekBar
Button cancelButton = (Button) findViewById(R.id.cancel_dialog_button);
cancelButton.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View view) {
cancelGraceAlarm();
}
});
}
/**
* Handle the user pressing the back/return button
* TODO Do we want this to cancel the alarm and grace period?
* TODO Or do we trigger the notification and finish() right away?
* TODO probably most intuitive to cancel the alarms...
*/
@Override
public void onBackPressed() {
cancelGraceAlarm();
/* OR:
// Ensure that the we don't trigger the notification a second time
userCancelled = true;
startService(notifyIntent);
finish();
*/
}
public void onStop() {
vibrator.cancel();
super.onStop();
}
private void cancelGraceAlarm() {
AlarmManager graceManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent graceIntent = new Intent(getApplicationContext(), GraceReceiver.class);
PendingIntent 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 now
userCancelled = true;
// Close the dialogue (stop vibration &c)
finish();
}
}