@Override
  protected void onResume() {
    super.onResume();

    DbGroupsAdapter mDbHelper = new DbGroupsAdapter(ctx);
    mDbHelper.open();
    Cursor mCursor = mDbHelper.getGroups();
    ListAdapter adapter = new GroupsCursorAdapter(ctx, mCursor);
    setListAdapter(adapter);
    mDbHelper.close();
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ctx = this;

    setContentView(R.layout.groups);

    SharedPreferences prefs = ctx.getSharedPreferences("Cals", 0);

    DbGroupsAdapter mDbHelper = new DbGroupsAdapter(this);
    mDbHelper.open();

    Cursor mCursor = mDbHelper.getGroups();
    ListAdapter adapter = new GroupsCursorAdapter(this, mCursor);
    setListAdapter(adapter);
    mDbHelper.close();

    Button newgroup = (Button) findViewById(R.id.newgroup);
    newgroup.setOnClickListener(
        new OnClickListener() {
          public void onClick(View v) {

            DbGroupsAdapter mDbHelper = new DbGroupsAdapter(ctx);
            mDbHelper.open();
            mDbHelper.newGroup();

            Cursor mCursor = mDbHelper.getGroups();
            ListAdapter adapter = new GroupsCursorAdapter(ctx, mCursor);
            setListAdapter(adapter);
            mDbHelper.close();
          }
        });

    AlarmManager alarmManager = (AlarmManager) ctx.getSystemService("alarm");
    alarmManager.setInexactRepeating(
        AlarmManager.ELAPSED_REALTIME_WAKEUP,
        0,
        prefs.getInt("frequency", 3600000),
        PendingIntent.getBroadcast(ctx, 0, new Intent(ctx, CheckForEvents.class), 0));

    if (!prefs.getBoolean("advanced", false)) {
      Intent i = new Intent(ctx, GroupsSettings.class);
      i.putExtra("group", "1");
      startActivity(i);
      this.finish();
      // return;
    }
  }
  @Override
  public void onReceive(Context ctx, Intent intent) {
    Toast toast;
    SharedPreferences prefs = ctx.getSharedPreferences("Cals", 0);
    if (prefs.getBoolean("toasts", false)) {
      toast = Toast.makeText(ctx, "Checking Calendars", Toast.LENGTH_SHORT);
      toast.show();
    }

    AlarmManager alarmManager = (AlarmManager) ctx.getSystemService("alarm");
    Calendar cal = Calendar.getInstance();
    ContentResolver cr = ctx.getContentResolver();
    Uri uri = CalendarContract.Instances.CONTENT_URI;
    Builder builder = uri.buildUpon();

    Cursor CALcur;
    int after, before, freq, avail, allday, eventId;
    long dtend, dtstart;
    String title, location, description;
    String[] calendars;

    DbGroupsAdapter mDbHelper = new DbGroupsAdapter(ctx);
    mDbHelper.open();

    Cursor DBcur;
    if (prefs.getBoolean("advanced", false)) DBcur = mDbHelper.getGroupsData("DESC");
    else DBcur = mDbHelper.getGroupData("1");

    while (DBcur.moveToNext()) {
      freq = prefs.getInt("frequency", 3600000);
      after = DBcur.getInt(DBcur.getColumnIndex("after"));
      before = DBcur.getInt(DBcur.getColumnIndex("before"));
      allday = DBcur.getInt(DBcur.getColumnIndex("allday"));
      avail = DBcur.getInt(DBcur.getColumnIndex("busy"));
      title = DBcur.getString(DBcur.getColumnIndex("title"));
      location = DBcur.getString(DBcur.getColumnIndex("location"));
      description = DBcur.getString(DBcur.getColumnIndex("description"));
      calendars = DBcur.getString(DBcur.getColumnIndex("calendars")).split(",");

      String selection =
          "("
              + CalendarContract.Instances.END
              + " >  "
              + (cal.getTimeInMillis() - ((after * 60 * 1000)))
              + " ) ";
      selection +=
          " AND ("
              + CalendarContract.Instances.BEGIN
              + " <= "
              + (cal.getTimeInMillis() + ((before * 60 * 1000) + 2 * freq))
              + " ) ";

      if (allday == 0) selection += " AND ( NOT " + CalendarContract.Instances.ALL_DAY + " ) ";
      else if (allday == 2) selection += " AND ( " + CalendarContract.Instances.ALL_DAY + " ) ";

      if (avail == 0)
        selection +=
            " AND ("
                + CalendarContract.Instances.AVAILABILITY
                + " = "
                + CalendarContract.Instances.AVAILABILITY_BUSY
                + " ) ";
      else if (avail == 1)
        selection +=
            " AND ("
                + CalendarContract.Instances.AVAILABILITY
                + " = "
                + CalendarContract.Instances.AVAILABILITY_FREE
                + " ) ";

      if (title.length() > 0 && prefs.getBoolean("title", false))
        selection +=
            " AND ("
                + CalendarContract.Instances.TITLE
                + " LIKE '%"
                + title.replace("'", "\'")
                + "%' ) ";

      if (location.length() > 0 && prefs.getBoolean("location", false))
        selection +=
            " AND ("
                + CalendarContract.Instances.EVENT_LOCATION
                + " LIKE '%"
                + location.replace("'", "\'")
                + "%' ) ";

      if (description.length() > 0 && prefs.getBoolean("description", false))
        selection +=
            " AND ("
                + CalendarContract.Instances.DESCRIPTION
                + " LIKE '%"
                + description.replace("'", "\'")
                + "%' ) ";

      selection = "( " + selection + " )";

      builder = uri.buildUpon();
      ContentUris.appendId(builder, cal.getTimeInMillis() - ((after * 60 * 1000)));
      ContentUris.appendId(builder, cal.getTimeInMillis() + ((before * 60 * 1000) + 2 * freq));

      CALcur =
          cr.query(
              builder.build(),
              new String[] {
                CalendarContract.Instances._ID,
                CalendarContract.Instances.CALENDAR_ID,
                CalendarContract.Instances.TITLE,
                CalendarContract.Instances.END,
                CalendarContract.Instances.BEGIN
              },
              selection,
              null,
              null);
      while (CALcur.moveToNext()) {

        if (!Arrays.asList(calendars)
            .contains(
                CALcur.getString(CALcur.getColumnIndex(CalendarContract.Instances.CALENDAR_ID))))
          continue;

        if (prefs.getBoolean("toasts", false)) {
          toast =
              Toast.makeText(
                  ctx,
                  "Scheduleing Event: "
                      + CALcur.getString(CALcur.getColumnIndex(CalendarContract.Instances.TITLE)),
                  Toast.LENGTH_SHORT);
          toast.show();
        }

        eventId = CALcur.getInt(CALcur.getColumnIndex(CalendarContract.Instances._ID));
        dtend =
            CALcur.getLong(CALcur.getColumnIndex(CalendarContract.Instances.END))
                + (after * 60 * 1000);
        dtstart =
            CALcur.getLong(CALcur.getColumnIndex(CalendarContract.Instances.BEGIN))
                - (before * 60 * 1000);

        alarmManager.set(
            AlarmManager.RTC_WAKEUP,
            dtstart,
            PendingIntent.getBroadcast(ctx, eventId, new Intent(ctx, SetSilent.class), 0));
        alarmManager.set(
            AlarmManager.RTC_WAKEUP,
            dtend,
            PendingIntent.getBroadcast(ctx, -eventId, new Intent(ctx, SetSilent.class), 0));
      }
    }
    mDbHelper.close();
    ctx.sendBroadcast(new Intent(ctx, SetSilent.class));
  }