1/*
2 * Copyright 2017 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
17package androidx.work.impl.background.firebase;
18
19import android.support.annotation.NonNull;
20import android.support.annotation.RestrictTo;
21import android.text.TextUtils;
22import android.util.Log;
23
24import androidx.work.impl.ExecutionListener;
25import androidx.work.impl.WorkManagerImpl;
26
27import com.firebase.jobdispatcher.JobParameters;
28import com.firebase.jobdispatcher.JobService;
29
30import java.util.HashMap;
31import java.util.Map;
32
33/**
34 * Service invoked by {@link com.firebase.jobdispatcher.FirebaseJobDispatcher} to run work tasks.
35 *
36 * @hide
37 */
38@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
39public class FirebaseJobService extends JobService implements ExecutionListener {
40    private static final String TAG = "FirebaseJobService";
41    private WorkManagerImpl mWorkManagerImpl;
42    private final Map<String, JobParameters> mJobParameters = new HashMap<>();
43
44    @Override
45    public void onCreate() {
46        super.onCreate();
47        mWorkManagerImpl = WorkManagerImpl.getInstance();
48        mWorkManagerImpl.getProcessor().addExecutionListener(this);
49    }
50
51    @Override
52    public void onDestroy() {
53        super.onDestroy();
54        mWorkManagerImpl.getProcessor().removeExecutionListener(this);
55    }
56
57    @Override
58    public boolean onStartJob(JobParameters params) {
59        String workSpecId = params.getTag();
60        if (TextUtils.isEmpty(workSpecId)) {
61            Log.e(TAG, "WorkSpec id not found!");
62            return false;
63        }
64
65        Log.d(TAG, String.format("onStartJob for %s", workSpecId));
66        synchronized (mJobParameters) {
67            mJobParameters.put(workSpecId, params);
68        }
69        mWorkManagerImpl.startWork(workSpecId);
70        return true;
71    }
72
73    @Override
74    public boolean onStopJob(JobParameters params) {
75        String workSpecId = params.getTag();
76        if (TextUtils.isEmpty(workSpecId)) {
77            Log.e(TAG, "WorkSpec id not found!");
78            return false;
79        }
80
81        Log.d(TAG, String.format("onStopJob for %s", workSpecId));
82
83        synchronized (mJobParameters) {
84            mJobParameters.remove(workSpecId);
85        }
86        mWorkManagerImpl.stopWork(workSpecId);
87        return !mWorkManagerImpl.getProcessor().isCancelled(workSpecId);
88    }
89
90    @Override
91    public void onExecuted(
92            @NonNull String workSpecId,
93            boolean isSuccessful,
94            boolean needsReschedule) {
95        Log.d(TAG, String.format("%s executed on FirebaseJobDispatcher", workSpecId));
96        JobParameters parameters;
97        synchronized (mJobParameters) {
98            parameters = mJobParameters.get(workSpecId);
99        }
100        if (parameters != null) {
101            jobFinished(parameters, needsReschedule);
102        }
103    }
104}
105