From 2c3667aad296f22e36a7a1c4a31560d100f96641 Mon Sep 17 00:00:00 2001 From: Suhail Alkowaileet Date: Tue, 23 Oct 2018 09:08:11 +0300 Subject: [PATCH] - Get notified when a new event has come. --- AndroidManifest.xml | 3 ++ .../android/calendar/AllInOneActivity.java | 3 ++ .../alerts/ProviderChangedReceiverAsJob.java | 54 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 src/com/android/calendar/alerts/ProviderChangedReceiverAsJob.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index b120a8ac3..a301c2e29 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -253,6 +253,9 @@ + + diff --git a/src/com/android/calendar/AllInOneActivity.java b/src/com/android/calendar/AllInOneActivity.java index 775fd5b44..1ab426a7d 100644 --- a/src/com/android/calendar/AllInOneActivity.java +++ b/src/com/android/calendar/AllInOneActivity.java @@ -80,6 +80,7 @@ import com.android.calendar.CalendarController.EventType; import com.android.calendar.CalendarController.ViewType; import com.android.calendar.agenda.AgendaFragment; +import com.android.calendar.alerts.ProviderChangedReceiverAsJob; import com.android.calendar.month.MonthByWeekFragment; import com.android.calendar.selectcalendars.SelectVisibleCalendarsFragment; import com.android.datetimepicker.date.DatePickerDialog; @@ -273,6 +274,8 @@ protected void onCreate(Bundle icicle) { }, null, null /* selection args */, null /* sort order */); } + ProviderChangedReceiverAsJob.schedule(this); + // This needs to be created before setContentView mController = CalendarController.getInstance(this); diff --git a/src/com/android/calendar/alerts/ProviderChangedReceiverAsJob.java b/src/com/android/calendar/alerts/ProviderChangedReceiverAsJob.java new file mode 100644 index 000000000..dd438af5b --- /dev/null +++ b/src/com/android/calendar/alerts/ProviderChangedReceiverAsJob.java @@ -0,0 +1,54 @@ +package com.android.calendar.alerts; + +import android.annotation.TargetApi; +import android.app.job.JobInfo; +import android.app.job.JobParameters; +import android.app.job.JobScheduler; +import android.app.job.JobService; +import android.appwidget.AppWidgetManager; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.Build; +import android.os.Handler; +import android.provider.CalendarContract; +import android.widget.Toast; + +import com.android.calendar.widget.CalendarAppWidgetProvider; +import com.android.calendar.widget.CalendarAppWidgetService; + +@TargetApi(Build.VERSION_CODES.N) +public class ProviderChangedReceiverAsJob extends JobService { + private static final int mJobId = 1005; //Job Id + final Handler mHandler = new Handler(); //Just to display Toasts + + public static void schedule(Context oContext) { + ComponentName oComponentName = new ComponentName(oContext, ProviderChangedReceiverAsJob.class); + JobInfo.Builder oJobInfoBuilder = new JobInfo.Builder(mJobId, oComponentName); + final Uri CALENDAR_URI = Uri.parse("content://" + CalendarContract.AUTHORITY + "/"); + oJobInfoBuilder.addTriggerContentUri(new JobInfo.TriggerContentUri(CalendarContract.CONTENT_URI, JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS)); + oJobInfoBuilder.addTriggerContentUri(new JobInfo.TriggerContentUri(CALENDAR_URI, 0)); + JobScheduler jobScheduler = (JobScheduler) oContext.getSystemService(Context.JOB_SCHEDULER_SERVICE); + jobScheduler.schedule(oJobInfoBuilder.build()); + } + + + @Override + public boolean onStartJob(JobParameters params) { + //Toast.makeText(getApplicationContext(), "Update The widget!", Toast.LENGTH_SHORT).show(); + + //TODO: Here should be the update of the widgets. + + + schedule(this); //Reschedule to receive future changes + return (false); + } + + + @Override + synchronized public boolean onStopJob(JobParameters params) { + return (false); + } + +} \ No newline at end of file