| 1 | package ro.ieval.unical; |
| 2 | |
| 3 | import java.util.GregorianCalendar; |
| 4 | import java.util.Locale; |
| 5 | |
| 6 | import android.app.AlarmManager; |
| 7 | import android.app.PendingIntent; |
| 8 | import android.content.ContentValues; |
| 9 | import android.content.Context; |
| 10 | import android.content.Intent; |
| 11 | import android.database.Cursor; |
| 12 | import android.database.SQLException; |
| 13 | import android.database.sqlite.SQLiteDatabase; |
| 14 | import android.view.View; |
| 15 | import android.view.ViewGroup; |
| 16 | |
| 17 | final class Utils { |
| 18 | private Utils(){ /* do nothing */ } |
| 19 | |
| 20 | public static void setEnabledRecursively(final View view, final boolean enabled){ |
| 21 | view.setEnabled(enabled); |
| 22 | if(view instanceof ViewGroup){ |
| 23 | final ViewGroup group=(ViewGroup) view; |
| 24 | for(int i=0;i<group.getChildCount();i++) |
| 25 | setEnabledRecursively(group.getChildAt(i), enabled); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | public static String formatDate(final int year, final int month, final int day){ |
| 30 | return String.format(Locale.ENGLISH, "%4d-%02d-%02d", year, month, day); |
| 31 | } |
| 32 | |
| 33 | public static void addAlarm(final Application application, final long time, final long eventId) throws SQLException{ |
| 34 | final SQLiteDatabase db = application.alarmOpenHelper.getWritableDatabase(); |
| 35 | final ContentValues values = new ContentValues(2); |
| 36 | values.put(AlarmOpenHelper.TIME, time); |
| 37 | values.put(AlarmOpenHelper.EVENT, eventId); |
| 38 | db.insertOrThrow(AlarmOpenHelper.TABLE, null, values); |
| 39 | setAlarm(application, time, eventId); |
| 40 | } |
| 41 | |
| 42 | public static void deleteAlarm(final Application application, final long time) throws SQLException{ |
| 43 | final SQLiteDatabase db = application.alarmOpenHelper.getWritableDatabase(); |
| 44 | db.delete(AlarmOpenHelper.TABLE, AlarmOpenHelper.TIME + " = " + time, new String[0]); |
| 45 | unsetAlarm(application, time); |
| 46 | } |
| 47 | |
| 48 | public static long[] getAlarmsByEvent(final Application application, final long eventId) throws SQLException{ |
| 49 | final SQLiteDatabase db = application.alarmOpenHelper.getWritableDatabase(); |
| 50 | final Cursor cursor = db.query(AlarmOpenHelper.TABLE, new String[]{AlarmOpenHelper.TIME}, AlarmOpenHelper.EVENT + " = " + eventId, new String[0], null, null, null); |
| 51 | if(cursor.moveToFirst()){ |
| 52 | final long[] ret = new long[cursor.getCount()]; |
| 53 | for(int i=0;i<ret.length;i++){ |
| 54 | ret[i]=cursor.getLong(i); |
| 55 | cursor.moveToNext(); |
| 56 | } |
| 57 | return ret; |
| 58 | } |
| 59 | return new long[0]; |
| 60 | } |
| 61 | |
| 62 | public static void setAlarm(final Context context, final long time, final long eventId){ |
| 63 | final Intent intent = new Intent(context, AlarmReceiverActivity.class); |
| 64 | intent.setAction(Long.toString(time)); |
| 65 | intent.putExtra(AlarmReceiverActivity.EXTRA_EVENT, eventId); |
| 66 | final AlarmManager man = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 67 | man.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getActivity(context, 0, intent, 0)); |
| 68 | } |
| 69 | |
| 70 | public static void unsetAlarm(final Context context, final long time){ |
| 71 | final Intent intent = new Intent(context, AlarmReceiverActivity.class); |
| 72 | intent.setAction(Long.toString(time)); |
| 73 | final AlarmManager man = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 74 | man.cancel(PendingIntent.getActivity(context, 0, intent, 0)); |
| 75 | } |
| 76 | } |