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.content.BroadcastReceiver;
21 import android.content.ContentProvider;
22 import android.content.Context;
23 import android.content.IContentProvider;
24 import android.content.Intent;
25 import android.provider.CalendarContract;
26 import android.util.Log;
27 import android.util.Slog;
28 
29 public class CalendarProviderBroadcastReceiver extends BroadcastReceiver {
30     private static final String TAG = CalendarProvider2.TAG;
31 
32     @Override
onReceive(Context context, Intent intent)33     public void onReceive(Context context, Intent intent) {
34         String action = intent.getAction();
35         if (!CalendarAlarmManager.ACTION_CHECK_NEXT_ALARM.equals(action)
36                 && !CalendarContract.ACTION_EVENT_REMINDER.equals(action)) {
37             Log.e(TAG, "Received invalid intent: " + intent);
38             setResultCode(Activity.RESULT_CANCELED);
39             return;
40         }
41         if (Log.isLoggable(TAG, Log.DEBUG)) {
42             Log.d(TAG, "Received intent: " + intent);
43         }
44         final IContentProvider iprovider =
45                 context.getContentResolver().acquireProvider(CalendarContract.AUTHORITY);
46         final ContentProvider cprovider = ContentProvider.coerceToLocalContentProvider(iprovider);
47 
48         if (!(cprovider instanceof CalendarProvider2)) {
49             Slog.wtf(TAG, "CalendarProvider2 not found in CalendarProviderBroadcastReceiver.");
50             return;
51         }
52 
53         final CalendarProvider2 provider = (CalendarProvider2) cprovider;
54 
55         final PendingResult result = goAsync();
56 
57         new Thread(() -> {
58             // Schedule the next alarm. Please be noted that for ACTION_EVENT_REMINDER broadcast,
59             // we never remove scheduled alarms.
60             final boolean removeAlarms = intent
61                     .getBooleanExtra(CalendarAlarmManager.KEY_REMOVE_ALARMS, false);
62             provider.getOrCreateCalendarAlarmManager().runScheduleNextAlarm(removeAlarms, provider);
63 
64             if (Log.isLoggable(TAG, Log.DEBUG)) {
65                 Log.d(TAG, "Next alarm set.");
66             }
67 
68             result.setResultCode(Activity.RESULT_OK);
69             result.finish();
70         }).start();
71 
72     }
73 }
74