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.arch.lifecycle.LifecycleService;
21 import android.content.Intent;
22 import android.support.annotation.MainThread;
23 import android.support.annotation.RestrictTo;
24 import android.util.Log;
25 
26 /**
27  * Service invoked by {@link android.app.AlarmManager} to run work tasks.
28  *
29  * @hide
30  */
31 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
32 public class SystemAlarmService extends LifecycleService
33         implements SystemAlarmDispatcher.CommandsCompletedListener {
34 
35     private static final String TAG = "SystemAlarmService";
36 
37     private SystemAlarmDispatcher mDispatcher;
38 
39     @Override
onCreate()40     public void onCreate() {
41         super.onCreate();
42         mDispatcher = new SystemAlarmDispatcher(this);
43         mDispatcher.setCompletedListener(this);
44     }
45 
46     @Override
onDestroy()47     public void onDestroy() {
48         super.onDestroy();
49         mDispatcher.onDestroy();
50     }
51 
52     @Override
onStartCommand(Intent intent, int flags, int startId)53     public int onStartCommand(Intent intent, int flags, int startId) {
54         super.onStartCommand(intent, flags, startId);
55         if (intent != null) {
56             mDispatcher.add(intent, startId);
57         }
58         return Service.START_STICKY;
59     }
60 
61     @MainThread
62     @Override
onAllCommandsCompleted()63     public void onAllCommandsCompleted() {
64         Log.d(TAG, "All commands completed in dispatcher");
65         stopSelf();
66     }
67 }
68