Skip to content

Instantly share code, notes, and snippets.

@chrisking
Created May 3, 2012 16:57
Show Gist options
  • Select an option

  • Save chrisking/2587214 to your computer and use it in GitHub Desktop.

Select an option

Save chrisking/2587214 to your computer and use it in GitHub Desktop.
schedule
package com.pruvop.drugdb;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import com.pruvop.medtracker.AlertDialogActivity;
import com.pruvop.medtracker.receiver.AlarmReceiver;
import com.j256.ormlite.field.DatabaseField;
public class Schedule {
@DatabaseField(generatedId = true)
private int id;
public static String ID_FIELD_NAME = "_id";
private static String LOG_TAG = "Schedule";
@DatabaseField(columnName = "SCHEDTIME")
private long sched_time; //DATE_LONG
public Schedule() {
}
public Schedule(Calendar newTime) {
Calendar utc_calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
//TODO convert newTime to UTC
utc_calendar.setTimeInMillis(newTime.getTimeInMillis());
sched_time = utc_calendar.getTimeInMillis();
}
//TODO move this to Prescription.Schedule shouldn't know this type of detail
public void activate(Context context, long prescription_id) {
Intent drug_notify = new Intent(context, AlarmReceiver.class);
drug_notify.setAction(AlertDialogActivity.ACTION);
drug_notify.putExtra("PrescriptionId", prescription_id);
PendingIntent drug_notifier = PendingIntent.getBroadcast(context,
0, drug_notify, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) context.getSystemService(
Context.ALARM_SERVICE);
Log.d(LOG_TAG, "Created alarm event for: " + getSched_time().toString());
//alarms.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 5000, drug_notifier); //debug purposes
alarms.setRepeating(AlarmManager.RTC_WAKEUP, getSched_time().getTimeInMillis(),
AlarmManager.INTERVAL_DAY, drug_notifier);
}
public Calendar getSched_time() {
Calendar utc_calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
//TODO convert to UTC
utc_calendar.setTimeInMillis(sched_time);
return utc_calendar;
}
public void setSched_time(Calendar sched_time) {
Calendar utc_calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
//TODO convert sched_time to UTC
utc_calendar.setTimeInMillis(sched_time.getTimeInMillis());
this.sched_time = utc_calendar.getTimeInMillis();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment