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

98 lines
3.6 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.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import java.util.Timer;
import java.util.TimerTask;
// TODO See GlowPad.
// 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 AlarmAlertActivity extends Activity {
// TODO correct alert lifetime
private static final int ALERT_LIFE = 1000*10;//1000*60*2; // 2 minutes
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) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_FULLSCREEN
);
setContentView(R.layout.alarm_alert);
// Disable any current notifications
stopService(new Intent(getApplicationContext(), AlarmNotify.class));
// Turn off the alert activity, and switch to a notification
timer.schedule(new TimerTask() {
public void run() {
// Close the dialogue
finish();
// Switch to notification if the Activity has not been closed by the user
if (!userCancelled) {
startService(new Intent(getApplicationContext(), AlarmNotify.class));
Log.d("AlarmAlertActivity", "Started notification");
}
}
}, 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) {
graceManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent graceIntent = new Intent(getApplicationContext(), GraceReceiver.class);
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() {
vibrator.cancel();
super.onStop();
}
}