| 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.SQLException; |
| 12 | import android.database.sqlite.SQLiteDatabase; |
| 13 | import android.view.View; |
| 14 | import android.view.ViewGroup; |
| 15 | |
| 16 | final class Utils { |
| 17 | private Utils(){ /* do nothing */ } |
| 18 | |
| 19 | public static void setEnabledRecursively(final View view, final boolean enabled){ |
| 20 | view.setEnabled(enabled); |
| 21 | if(view instanceof ViewGroup){ |
| 22 | final ViewGroup group=(ViewGroup) view; |
| 23 | for(int i=0;i<group.getChildCount();i++) |
| 24 | setEnabledRecursively(group.getChildAt(i), enabled); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | public static String formatDate(final int year, final int month, final int day){ |
| 29 | return String.format(Locale.ENGLISH, "%4d-%02d-%02d", year, month, day); |
| 30 | } |
| 31 | |
| 32 | public static void addAlarm(final Application application, final long time, final long eventId) throws SQLException{ |
| 33 | final SQLiteDatabase db = application.alarmOpenHelper.getWritableDatabase(); |
| 34 | final ContentValues values = new ContentValues(2); |
| 35 | values.put(AlarmOpenHelper.TIME, time); |
| 36 | values.put(AlarmOpenHelper.EVENT, eventId); |
| 37 | db.insertOrThrow(AlarmOpenHelper.TABLE, null, values); |
| 38 | setAlarm(application, time, eventId); |
| 39 | } |
| 40 | |
| 41 | public static void deleteAlarm(final Application application, final long time) throws SQLException{ |
| 42 | final SQLiteDatabase db = application.alarmOpenHelper.getWritableDatabase(); |
| 43 | db.delete(AlarmOpenHelper.TABLE, AlarmOpenHelper.TIME + " = " + time, new String[]{}); |
| 44 | unsetAlarm(application, time); |
| 45 | } |
| 46 | |
| 47 | public static void setAlarm(final Context context, final long time, final long eventId){ |
| 48 | final Intent intent = new Intent(context, AlarmReceiverActivity.class); |
| 49 | intent.setAction(Long.toString(time)); |
| 50 | intent.putExtra(AlarmReceiverActivity.EXTRA_EVENT, eventId); |
| 51 | final AlarmManager man = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 52 | man.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getActivity(context, 0, intent, 0)); |
| 53 | } |
| 54 | |
| 55 | public static void unsetAlarm(final Context context, final long time){ |
| 56 | final Intent intent = new Intent(context, AlarmReceiverActivity.class); |
| 57 | intent.setAction(Long.toString(time)); |
| 58 | final AlarmManager man = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 59 | man.cancel(PendingIntent.getActivity(context, 0, intent, 0)); |
| 60 | } |
| 61 | } |