]> iEval git - unical.git/commitdiff
Add (untested) alarm backend
authorMarius Gavrilescu <marius@ieval.ro>
Tue, 13 Aug 2013 08:41:51 +0000 (11:41 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Tue, 13 Aug 2013 08:41:51 +0000 (11:41 +0300)
AndroidManifest.xml
src/ro/ieval/unical/AlarmOpenHelper.java [new file with mode: 0644]
src/ro/ieval/unical/AlarmReceiverActivity.java
src/ro/ieval/unical/Application.java [new file with mode: 0644]
src/ro/ieval/unical/Utils.java

index d7b414b73fe5d6db4ea8285fa3863f59c6442c84..0e0fe9493a561a5feba6bc79b7a775c1d85e3d8c 100644 (file)
@@ -11,6 +11,7 @@
     <uses-permission android:name="android.permission.WRITE_CALENDAR"/>
 
     <application
+               android:name="ro.ieval.unical.Application"
         android:allowBackup="true"
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
@@ -28,4 +29,4 @@
         <activity android:name=".AlarmReceiverActivity"></activity>
     </application>
 
-</manifest>
\ No newline at end of file
+</manifest>
diff --git a/src/ro/ieval/unical/AlarmOpenHelper.java b/src/ro/ieval/unical/AlarmOpenHelper.java
new file mode 100644 (file)
index 0000000..d219bed
--- /dev/null
@@ -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
+       }
+}
index d96a5af9ef00de588248ef88dbe84c378d1b59a4..f01417a83acfc61bd11f991b23bcff0d7d49f12b 100644 (file)
@@ -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 (file)
index 0000000..265de0d
--- /dev/null
@@ -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());
+       }
+}
index df57cda73262ff9f5ac6d13aeb1492cf786b9b0f..e8423478c84b112f02aa1195ce560f3633f5283c 100644 (file)
@@ -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));
        }
 }
This page took 0.031724 seconds and 4 git commands to generate.