1 /* 2 * Copyright 2018 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 androidx.work.impl.background.systemalarm; 18 19 import android.app.Service; 20 import android.content.Intent; 21 22 import androidx.annotation.MainThread; 23 import androidx.annotation.RestrictTo; 24 import androidx.lifecycle.LifecycleService; 25 import androidx.work.Logger; 26 import androidx.work.impl.utils.WakeLocks; 27 28 /** 29 * Service invoked by {@link android.app.AlarmManager} to run work tasks. 30 * 31 */ 32 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 33 public class SystemAlarmService extends LifecycleService 34 implements SystemAlarmDispatcher.CommandsCompletedListener { 35 36 private static final String TAG = Logger.tagWithPrefix("SystemAlarmService"); 37 38 private SystemAlarmDispatcher mDispatcher; 39 private boolean mIsShutdown; 40 41 @Override onCreate()42 public void onCreate() { 43 super.onCreate(); 44 initializeDispatcher(); 45 mIsShutdown = false; 46 } 47 48 @Override onDestroy()49 public void onDestroy() { 50 super.onDestroy(); 51 mIsShutdown = true; 52 mDispatcher.onDestroy(); 53 } 54 55 @Override onStartCommand(Intent intent, int flags, int startId)56 public int onStartCommand(Intent intent, int flags, int startId) { 57 super.onStartCommand(intent, flags, startId); 58 if (mIsShutdown) { 59 Logger.get().info(TAG, 60 "Re-initializing SystemAlarmDispatcher after a request to shut-down."); 61 62 // Destroy the old dispatcher to complete it's lifecycle. 63 mDispatcher.onDestroy(); 64 // Create a new dispatcher to setup a new lifecycle. 65 initializeDispatcher(); 66 // Set mIsShutdown to false, to correctly accept new commands. 67 mIsShutdown = false; 68 } 69 70 if (intent != null) { 71 mDispatcher.add(intent, startId); 72 } 73 74 // If the service were to crash, we want all unacknowledged Intents to get redelivered. 75 return Service.START_REDELIVER_INTENT; 76 } 77 78 @MainThread 79 @Override onAllCommandsCompleted()80 public void onAllCommandsCompleted() { 81 mIsShutdown = true; 82 Logger.get().debug(TAG, "All commands completed in dispatcher"); 83 // Check to see if we hold any more wake locks. 84 WakeLocks.checkWakeLocks(); 85 // No need to pass in startId; stopSelf() translates to stopSelf(-1) which is a hard stop 86 // of all startCommands. This is the behavior we want. 87 stopSelf(); 88 } 89 90 @MainThread initializeDispatcher()91 private void initializeDispatcher() { 92 mDispatcher = new SystemAlarmDispatcher(this); 93 mDispatcher.setCompletedListener(this); 94 } 95 } 96