From 57ca40519eadfe1757e8f21875c1fdf7fe380542 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Tue, 13 Aug 2013 11:41:51 +0300 Subject: [PATCH] Add (untested) alarm backend --- AndroidManifest.xml | 3 +- src/ro/ieval/unical/AlarmOpenHelper.java | 30 +++++++++++++++ .../ieval/unical/AlarmReceiverActivity.java | 3 +- src/ro/ieval/unical/Application.java | 18 +++++++++ src/ro/ieval/unical/Utils.java | 37 ++++++++++++++++--- 5 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 src/ro/ieval/unical/AlarmOpenHelper.java create mode 100644 src/ro/ieval/unical/Application.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index d7b414b..0e0fe94 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -11,6 +11,7 @@ - \ No newline at end of file + diff --git a/src/ro/ieval/unical/AlarmOpenHelper.java b/src/ro/ieval/unical/AlarmOpenHelper.java new file mode 100644 index 0000000..d219bed --- /dev/null +++ b/src/ro/ieval/unical/AlarmOpenHelper.java @@ -0,0 +1,30 @@ +package ro.ieval.unical; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +final class AlarmOpenHelper extends SQLiteOpenHelper{ + public static final String TIME = "time"; + public static final String EVENT = "event"; + public static final String TABLE = "alarms"; + + private static final int VERSION = 1; + private static final String DB = "alarms"; + + AlarmOpenHelper (final Context context) { + super(context, DB, null, VERSION); + } + + @Override + public void onCreate(final SQLiteDatabase db){ + db.execSQL("CREATE TABLE " + TABLE + " (" + + TIME + " INTEGER PRIMARY KEY, " + + EVENT + " INTEGER NOT NULL);"); + } + + @Override + public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion){ + // do nothing + } +} diff --git a/src/ro/ieval/unical/AlarmReceiverActivity.java b/src/ro/ieval/unical/AlarmReceiverActivity.java index d96a5af..f01417a 100644 --- a/src/ro/ieval/unical/AlarmReceiverActivity.java +++ b/src/ro/ieval/unical/AlarmReceiverActivity.java @@ -17,8 +17,9 @@ import android.view.WindowManager; import android.widget.Button; public class AlarmReceiverActivity extends Activity { + public static final String EXTRA_EVENT = "event"; private MediaPlayer mMediaPlayer; - + @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/src/ro/ieval/unical/Application.java b/src/ro/ieval/unical/Application.java new file mode 100644 index 0000000..265de0d --- /dev/null +++ b/src/ro/ieval/unical/Application.java @@ -0,0 +1,18 @@ +package ro.ieval.unical; + +import android.database.Cursor; + +public final class Application extends android.app.Application { + public AlarmOpenHelper alarmOpenHelper; + + @Override + public void onCreate(){ + alarmOpenHelper = new AlarmOpenHelper(this); + final Cursor cur = alarmOpenHelper.getReadableDatabase().query(AlarmOpenHelper.TABLE, null, null, null, null, null, null); + cur.moveToFirst(); + do { + Utils.setAlarm(this, cur.getLong(0), cur.getLong(1)); + cur.moveToNext(); + } while(!cur.isAfterLast()); + } +} diff --git a/src/ro/ieval/unical/Utils.java b/src/ro/ieval/unical/Utils.java index df57cda..e842347 100644 --- a/src/ro/ieval/unical/Utils.java +++ b/src/ro/ieval/unical/Utils.java @@ -5,8 +5,11 @@ import java.util.Locale; import android.app.AlarmManager; import android.app.PendingIntent; +import android.content.ContentValues; import android.content.Context; import android.content.Intent; +import android.database.SQLException; +import android.database.sqlite.SQLiteDatabase; import android.view.View; import android.view.ViewGroup; @@ -25,10 +28,34 @@ final class Utils { public static String formatDate(final int year, final int month, final int day){ return String.format(Locale.ENGLISH, "%4d-%02d-%02d", year, month, day); } - - public static void setAlarm(final GregorianCalendar cal, final Context context) { - final PendingIntent pi = PendingIntent.getActivity(context, 1234, new Intent(context, AlarmReceiverActivity.class), PendingIntent.FLAG_CANCEL_CURRENT); - final AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); - am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi); + + public static void addAlarm(final Application application, final long time, final long eventId) throws SQLException{ + final SQLiteDatabase db = application.alarmOpenHelper.getWritableDatabase(); + final ContentValues values = new ContentValues(2); + values.put(AlarmOpenHelper.TIME, time); + values.put(AlarmOpenHelper.EVENT, eventId); + db.insertOrThrow(AlarmOpenHelper.TABLE, null, values); + setAlarm(application, time, eventId); + } + + public static void deleteAlarm(final Application application, final long time) throws SQLException{ + final SQLiteDatabase db = application.alarmOpenHelper.getWritableDatabase(); + db.delete(AlarmOpenHelper.TABLE, AlarmOpenHelper.TIME + " = " + time, new String[]{}); + unsetAlarm(application, time); + } + + public static void setAlarm(final Context context, final long time, final long eventId){ + final Intent intent = new Intent(context, AlarmReceiverActivity.class); + intent.setAction(Long.toString(time)); + intent.putExtra(AlarmReceiverActivity.EXTRA_EVENT, eventId); + final AlarmManager man = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + man.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getActivity(context, 0, intent, 0)); + } + + public static void unsetAlarm(final Context context, final long time){ + final Intent intent = new Intent(context, AlarmReceiverActivity.class); + intent.setAction(Long.toString(time)); + final AlarmManager man = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + man.cancel(PendingIntent.getActivity(context, 0, intent, 0)); } } -- 2.39.2