Back to project page silent-meeting.
The source code is released under:
GNU General Public License
If you think the Android project silent-meeting listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package uk.co.bensproule.silentmeeting.receiver; /*from w ww .j a va2 s . c o m*/ import android.app.AlarmManager; import android.app.PendingIntent; import android.content.*; import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.provider.CalendarContract; import android.util.Log; import uk.co.bensproule.silentmeeting.constants.Actions; import uk.co.bensproule.silentmeeting.constants.Setting; import uk.co.bensproule.silentmeeting.dao.InstanceDao; import uk.co.bensproule.silentmeeting.domain.Attendee; import uk.co.bensproule.silentmeeting.domain.Calendar; import uk.co.bensproule.silentmeeting.domain.Event; import uk.co.bensproule.silentmeeting.domain.Instance; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import static java.lang.System.currentTimeMillis; public class CalendarEventsReceiver extends BroadcastReceiver { private Context context; private InstanceDao instanceDao; private AlarmManager alarmManager; @Override public void onReceive(Context context, Intent intent) { setContext(context); setInstanceDao(new InstanceDao(context)); setAlarmManager((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)); setEventsToSilent(); } private void setContext(Context context) { this.context = context; } public void setInstanceDao(InstanceDao instanceDao) { this.instanceDao = instanceDao; } private void setAlarmManager(AlarmManager alarmManager) { this.alarmManager = alarmManager; } private void setEventsToSilent() { Log.d("CalendarEvents", "Getting events from calendars"); // TODO: What about events that will start before it has been re-processed? deleteStoredInstances(); List<Calendar> calendars = getCalendars(); for (Calendar calendar : calendars) { List<Event> events = getEvents(calendar); for (Event event : events) { List<Attendee> attendees = getAttendees(event); if (attendees.size() == 0) { setMuteAlarms(event); continue; } for (Attendee attendee : attendees) { if (calendar.getAccountName().equals(attendee.getEmail()) && attendee.getStatus() == CalendarContract.Attendees.ATTENDEE_STATUS_ACCEPTED) { setMuteAlarms(event); } } } } } private void setMuteAlarms(Event event) { for (Instance instance : event.getInstances()) { setMuteAlarm(instance); } } private void deleteStoredInstances() { List<Instance> storedInstances = instanceDao.getAll(); for (Instance storedInstance : storedInstances) { alarmManager.cancel(getPendingIntent(storedInstance)); instanceDao.delete(storedInstance.getId()); } } private List<Calendar> getCalendars() { ContentResolver contentResolver = context.getContentResolver(); String[] projection = { CalendarContract.Calendars._ID, CalendarContract.Calendars.NAME, CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.ACCOUNT_NAME, CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.Calendars.OWNER_ACCOUNT}; Cursor cursor = contentResolver.query(CalendarContract.Calendars.CONTENT_URI, projection, null, null, null); assert cursor != null; List<Calendar> calendars = new ArrayList<>(); while (cursor.moveToNext()) { Calendar calendar = new Calendar(cursor.getInt(cursor.getColumnIndex(CalendarContract.Calendars._ID)), cursor.getString(cursor.getColumnIndex(CalendarContract.Calendars.NAME)), cursor.getInt(cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL)), cursor.getString(cursor.getColumnIndex(CalendarContract.Calendars.ACCOUNT_NAME)), cursor.getString(cursor.getColumnIndex(CalendarContract.Calendars.ACCOUNT_TYPE)), cursor.getString(cursor.getColumnIndex(CalendarContract.Calendars.OWNER_ACCOUNT))); if (!calendar.getOwnerAccount().matches(".*@group\\.v\\.calendar\\.google\\.com")) { calendars.add(calendar); } } cursor.close(); return calendars; } private List<Event> getEvents(Calendar calendar) { ContentResolver contentResolver = context.getContentResolver(); String[] projection = { CalendarContract.Events._ID, CalendarContract.Events.CALENDAR_ID, CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.AVAILABILITY, CalendarContract.Events.ORGANIZER }; String selection = "((" + CalendarContract.Events.DTSTART + " < ?) AND (" + CalendarContract.Events.CALENDAR_ID + " = ?))"; String[] selectionArgs = new String[]{"" + currentTimeMillis() + (24 * 60 * 60 * 1000), "" + calendar.getId()}; Cursor cursor = contentResolver.query(CalendarContract.Events.CONTENT_URI, projection, selection, selectionArgs, null); assert cursor != null; List<Event> events = new ArrayList<>(); while (cursor.moveToNext()) { Event event = new Event(cursor.getInt(cursor.getColumnIndex(CalendarContract.Events._ID)), cursor.getInt(cursor.getColumnIndex(CalendarContract.Events.CALENDAR_ID)), cursor.getString(cursor.getColumnIndex(CalendarContract.Events.TITLE)), cursor.getLong(cursor.getColumnIndex(CalendarContract.Events.DTSTART)), cursor.getLong(cursor.getColumnIndex(CalendarContract.Events.DTEND)), cursor.getInt(cursor.getColumnIndex(CalendarContract.Events.AVAILABILITY)), cursor.getString(cursor.getColumnIndex(CalendarContract.Events.ORGANIZER))); event.setInstances(getInstances(contentResolver, event)); if (event.getInstances().size() > 0) { events.add(event); } } cursor.close(); return events; } private List<Instance> getInstances(ContentResolver contentResolver, Event event) { String[] projection = { CalendarContract.Instances._ID, CalendarContract.Instances.EVENT_ID, CalendarContract.Instances.TITLE, CalendarContract.Instances.BEGIN, CalendarContract.Instances.END, CalendarContract.Instances.AVAILABILITY, CalendarContract.Instances.ORGANIZER }; String selection = "(" + CalendarContract.Instances.AVAILABILITY + " = ?) AND (" + CalendarContract.Instances.EVENT_ID + " = ?)"; String[] selectionArgs = new String[]{"" + CalendarContract.Instances.AVAILABILITY_BUSY, "" + event.getId()}; Uri.Builder contentUri = CalendarContract.Instances.CONTENT_URI.buildUpon(); ContentUris.appendId(contentUri, currentTimeMillis()); ContentUris.appendId(contentUri, currentTimeMillis() + (24 * 60 * 60 * 1000)); Cursor cursor = contentResolver.query(contentUri.build(), projection, selection, selectionArgs, null); List<Instance> instances = new ArrayList<>(); while (cursor.moveToNext()) { Instance instance = new Instance(cursor.getInt(cursor.getColumnIndex(CalendarContract.Instances._ID)), cursor.getInt(cursor.getColumnIndex(CalendarContract.Instances.EVENT_ID)), cursor.getString(cursor.getColumnIndex(CalendarContract.Instances.TITLE)), cursor.getLong(cursor.getColumnIndex(CalendarContract.Instances.BEGIN)), cursor.getLong(cursor.getColumnIndex(CalendarContract.Instances.END)), cursor.getInt(cursor.getColumnIndex(CalendarContract.Instances.AVAILABILITY)), cursor.getString(cursor.getColumnIndex(CalendarContract.Instances.ORGANIZER)), false); instances.add(instance); } cursor.close(); return instances; } private List<Attendee> getAttendees(Event event) { ContentResolver contentResolver = context.getContentResolver(); String[] projection = { CalendarContract.Attendees.ATTENDEE_STATUS, CalendarContract.Attendees.ATTENDEE_EMAIL}; String selection = "((" + CalendarContract.Attendees.EVENT_ID + " = ?) AND (" + CalendarContract.Attendees.CALENDAR_ID + " = ?))"; String[] selectionArgs = new String[]{ String.valueOf(event.getId()), String.valueOf(event.getCalendarID()) }; Cursor cursor = contentResolver.query(CalendarContract.Attendees.CONTENT_URI, projection, selection, selectionArgs, null); assert cursor != null; List<Attendee> attendees = new ArrayList<>(); while (cursor.moveToNext()) { Attendee attendee = new Attendee(cursor.getInt(cursor.getColumnIndex(CalendarContract.Attendees.ATTENDEE_STATUS)), cursor.getString(cursor.getColumnIndex(CalendarContract.Attendees.ATTENDEE_EMAIL))); attendees.add(attendee); } cursor.close(); return attendees; } private void setMuteAlarm(Instance instance) { if (instance.isRunning()) { return; } PendingIntent pendingIntent = getPendingIntent(instance); long activeBeforeEvents = Setting.getActiveBeforeEvents(context); java.util.Calendar calendar = java.util.Calendar.getInstance(); calendar.setTimeInMillis(instance.getStartDate() - activeBeforeEvents); Log.d("CalendarInstances", "Instance " + instance.toString() + " set for " + new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(calendar.getTime())); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); } else { alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); } instanceDao.save(instance); } private PendingIntent getPendingIntent(Instance instance) { Intent muteIntent = new Intent(context, MutePhoneBroadcastReceiver.class); muteIntent.setAction(Actions.MUTE); muteIntent.putExtra(CalendarContract.Instances.DTEND, instance.getEndDate()); muteIntent.putExtra(CalendarContract.Instances._ID, instance.getId()); return PendingIntent.getBroadcast(context, instance.getId(), muteIntent, PendingIntent.FLAG_CANCEL_CURRENT); } }