1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.providers.calendar;
18 
19 import android.app.Activity;
20 import android.app.job.JobInfo;
21 import android.app.job.JobScheduler;
22 import android.app.job.JobWorkItem;
23 import android.content.BroadcastReceiver;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.provider.CalendarContract;
28 import android.util.Log;
29 import android.util.Slog;
30 
31 public class CalendarProviderBroadcastReceiver extends BroadcastReceiver {
32     private static final String TAG = CalendarProvider2.TAG;
33 
34     @Override
onReceive(Context context, Intent intent)35     public void onReceive(Context context, Intent intent) {
36         String action = intent.getAction();
37         if (action == null ||
38                 (!CalendarAlarmManager.ACTION_CHECK_NEXT_ALARM.equals(action)
39                     && !CalendarContract.ACTION_EVENT_REMINDER.equals(action))) {
40             Log.e(TAG, "Received invalid intent: " + intent);
41             setResultCode(Activity.RESULT_CANCELED);
42             return;
43         }
44         if (Log.isLoggable(TAG, Log.DEBUG)) {
45             Log.d(TAG, "Received intent: " + intent);
46         }
47 
48         JobWorkItem jwi = new JobWorkItem(intent);
49         JobInfo.Builder alarmJobBuilder = new JobInfo.Builder(CalendarProviderJobService.JOB_ID,
50                 new ComponentName(context, CalendarProviderJobService.class))
51                 .setExpedited(true);
52         JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
53         if (jobScheduler.enqueue(alarmJobBuilder.build(), jwi) == JobScheduler.RESULT_SUCCESS) {
54             setResultCode(Activity.RESULT_OK);
55         } else {
56             Slog.wtf(TAG, "Failed to schedule expedited job");
57             // Unable to schedule an expedited job. Fall back to a regular job.
58             alarmJobBuilder.setExpedited(false);
59             if (jobScheduler.enqueue(alarmJobBuilder.build(), jwi) == JobScheduler.RESULT_SUCCESS) {
60                 setResultCode(Activity.RESULT_OK);
61             } else {
62                 Slog.wtf(TAG, "Failed to schedule regular job");
63             }
64         }
65     }
66 }
67