11package com .transistorsoft .tsbackgroundfetch ;
22
3+ import android .annotation .SuppressLint ;
34import android .app .AlarmManager ;
45import android .app .PendingIntent ;
56import android .app .job .JobInfo ;
@@ -125,18 +126,19 @@ static void schedule(Context context, BackgroundFetchConfig config) {
125126
126127 long interval = (config .isFetchTask ()) ? (TimeUnit .MINUTES .toMillis (config .getMinimumFetchInterval ())) : config .getDelay ();
127128
128- if (android . os . Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP && !config .getForceAlarmManager ()) {
129+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP && !config .getForceAlarmManager ()) {
129130 // API 21+ uses new JobScheduler API
130131
131132 JobScheduler jobScheduler = (JobScheduler ) context .getSystemService (Context .JOB_SCHEDULER_SERVICE );
133+ @ SuppressLint ("WrongConstant" )
132134 JobInfo .Builder builder = new JobInfo .Builder (config .getJobId (), new ComponentName (context , FetchJobService .class ))
133135 .setRequiredNetworkType (config .getRequiredNetworkType ())
134136 .setRequiresDeviceIdle (config .getRequiresDeviceIdle ())
135137 .setRequiresCharging (config .getRequiresCharging ())
136138 .setPersisted (config .getStartOnBoot () && !config .getStopOnTerminate ());
137139
138140 if (config .getPeriodic ()) {
139- if (android . os . Build .VERSION .SDK_INT >= 24 ) {
141+ if (Build .VERSION .SDK_INT >= Build . VERSION_CODES . N ) {
140142 builder .setPeriodic (interval , interval );
141143 } else {
142144 builder .setPeriodic (interval );
@@ -150,7 +152,7 @@ static void schedule(Context context, BackgroundFetchConfig config) {
150152
151153 builder .setExtras (extras );
152154
153- if (android . os . Build .VERSION .SDK_INT >= 26 ) {
155+ if (Build .VERSION .SDK_INT >= Build . VERSION_CODES . O ) {
154156 builder .setRequiresStorageNotLow (config .getRequiresStorageNotLow ());
155157 builder .setRequiresBatteryNotLow (config .getRequiresBatteryNotLow ());
156158 }
@@ -166,9 +168,17 @@ static void schedule(Context context, BackgroundFetchConfig config) {
166168 if (config .getPeriodic ()) {
167169 alarmManager .setRepeating (AlarmManager .RTC_WAKEUP , delay , interval , pi );
168170 } else {
169- if (android .os .Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
170- alarmManager .setExactAndAllowWhileIdle (AlarmManager .RTC_WAKEUP , delay , pi );
171- } else if (android .os .Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
171+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
172+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
173+ if (alarmManager .canScheduleExactAlarms ()) {
174+ alarmManager .setExactAndAllowWhileIdle (AlarmManager .RTC_WAKEUP , delay , pi );
175+ } else {
176+ alarmManager .setAndAllowWhileIdle (AlarmManager .RTC_WAKEUP , delay , pi );
177+ }
178+ } else {
179+ alarmManager .setExactAndAllowWhileIdle (AlarmManager .RTC_WAKEUP , delay , pi );
180+ }
181+ } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
172182 alarmManager .setExact (AlarmManager .RTC_WAKEUP , delay , pi );
173183 } else {
174184 alarmManager .set (AlarmManager .RTC_WAKEUP , delay , pi );
@@ -193,7 +203,11 @@ void onTimeout(Context context) {
193203 BackgroundFetchConfig config = adapter .getConfig (mTaskId );
194204 if (config != null ) {
195205 if (config .getJobService () != null ) {
196- fireHeadlessEvent (context , config );
206+ try {
207+ fireHeadlessEvent (context , config );
208+ } catch (BGTask .Error error ) {
209+ Log .e (BackgroundFetch .TAG , "Headless task error: " + error .getMessage ());
210+ }
197211 } else {
198212 adapter .finish (mTaskId );
199213 }
0 commit comments