1 /* 2 * Copyright (C) 2021 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.job.JobParameters; 20 import android.app.job.JobService; 21 import android.app.job.JobWorkItem; 22 import android.content.ContentProvider; 23 import android.content.IContentProvider; 24 import android.provider.CalendarContract; 25 import android.util.Log; 26 import android.util.Slog; 27 28 public class CalendarProviderJobService extends JobService { 29 private static final String TAG = CalendarProvider2.TAG; 30 static final int JOB_ID = CalendarProviderJobService.class.hashCode(); 31 32 private volatile boolean canRun; 33 34 @Override onStartJob(JobParameters params)35 public boolean onStartJob(JobParameters params) { 36 if (!params.isExpeditedJob()) { 37 Slog.w(TAG, "job not running as expedited"); 38 } 39 40 final IContentProvider iprovider = 41 getContentResolver().acquireProvider(CalendarContract.AUTHORITY); 42 final ContentProvider cprovider = ContentProvider.coerceToLocalContentProvider(iprovider); 43 44 if (!(cprovider instanceof CalendarProvider2)) { 45 Slog.wtf(TAG, "CalendarProvider2 not found in CalendarProviderJobService."); 46 return false; 47 } 48 49 canRun = true; 50 final CalendarProvider2 provider = (CalendarProvider2) cprovider; 51 52 new Thread(() -> { 53 for (JobWorkItem jwi = params.dequeueWork(); jwi != null; jwi = params.dequeueWork()) { 54 // Schedule the next alarm. Please be noted that for ACTION_EVENT_REMINDER 55 // broadcast, we never remove scheduled alarms. 56 final boolean removeAlarms = jwi.getIntent() 57 .getBooleanExtra(CalendarAlarmManager.KEY_REMOVE_ALARMS, false); 58 provider.getOrCreateCalendarAlarmManager() 59 .runScheduleNextAlarm(removeAlarms, provider); 60 61 if (Log.isLoggable(TAG, Log.DEBUG)) { 62 Log.d(TAG, "Next alarm set."); 63 } 64 if (!canRun) { 65 break; 66 } 67 params.completeWork(jwi); 68 } 69 }).start(); 70 return true; 71 } 72 73 @Override onStopJob(JobParameters params)74 public boolean onStopJob(JobParameters params) { 75 // The work should be very quick, so it'll be surprising if JS has to stop us. 76 Slog.wtf(TAG, "CalendarProviderJobService was stopped due to " 77 + JobParameters.getInternalReasonCodeDescription(params.getInternalStopReasonCode()) 78 + "(" + params.getStopReason() + ")"); 79 canRun = false; 80 return true; 81 } 82 } 83