/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.arch.background.workmanager.systemjob;

import android.annotation.TargetApi;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.arch.background.workmanager.ExecutionListener;
import android.arch.background.workmanager.WorkDatabase;
import android.arch.background.workmanager.WorkManager;
import android.arch.background.workmanager.WorkerWrapper;
import android.arch.background.workmanager.background.BackgroundProcessor;
import android.content.Context;
import android.support.annotation.RestrictTo;
import android.text.TextUtils;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;

/**
 * Service invoked by {@link android.app.job.JobScheduler} to run work tasks.
 *
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@TargetApi(23)
public class SystemJobService extends JobService implements ExecutionListener {
    private static final String TAG = "SystemJobService";
    private BackgroundProcessor mProcessor;
    private Map<String, JobParameters> mJobParameters = new HashMap<>();

    @Override
    public void onCreate() {
        super.onCreate();
        Context context = getApplicationContext();
        WorkManager workManager = WorkManager.getInstance(context);
        WorkDatabase database = workManager.getWorkDatabase();
        mProcessor = new BackgroundProcessor(context, database, workManager.getScheduler(), this);
    }

    @Override
    public boolean onStartJob(JobParameters params) {
        String workSpecId = params.getExtras().getString(SystemJobInfoConverter.EXTRA_WORK_SPEC_ID);
        if (TextUtils.isEmpty(workSpecId)) {
            Log.e(TAG, "WorkSpec id not found!");
            return false;
        }

        boolean isPeriodic = params.getExtras().getBoolean(SystemJobInfoConverter.EXTRA_IS_PERIODIC,
                false);
        if (isPeriodic && params.isOverrideDeadlineExpired()) {
            Log.d(TAG,
                    "Override deadling expired for id " + workSpecId
                            + "; asking system to retry");
            jobFinished(params, true);
            return false;
        }

        Log.d(TAG, workSpecId + " started on JobScheduler");
        mJobParameters.put(workSpecId, params);

        // Delay has already occurred via JobScheduler.
        mProcessor.process(workSpecId, 0L);
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        String workSpecId = params.getExtras().getString(SystemJobInfoConverter.EXTRA_WORK_SPEC_ID);
        if (TextUtils.isEmpty(workSpecId)) {
            Log.e(TAG, "WorkSpec id not found!");
            return false;
        }
        boolean cancelled = mProcessor.cancel(workSpecId, true);
        Log.d(TAG, workSpecId + "; cancel = " + cancelled);
        return cancelled;
    }

    @Override
    public void onExecuted(String workSpecId, @WorkerWrapper.ExecutionResult int result) {
        Log.d(TAG, workSpecId + " executed on JobScheduler");
        JobParameters parameters = mJobParameters.get(workSpecId);
        boolean needsReschedule = result == WorkerWrapper.RESULT_INTERRUPTED;
        jobFinished(parameters, needsReschedule);
    }
}

@@ -1120,16 +1120,53 @@
 ontext;%0A
+import android.os.PersistableBundle;%0A
 import a
@@ -2139,32 +2139,87 @@
 eters params) %7B%0A
+        PersistableBundle extras = params.getExtras();%0A
         String w
@@ -2230,34 +2230,22 @@
 pecId = 
-params.getE
+e
 xtras
-()
 .getStri
@@ -2452,34 +2452,22 @@
 iodic = 
-params.getE
+e
 xtras
-()
 .getBool
@@ -2511,32 +2511,16 @@
 ERIODIC,
-%0A               
  false);
@@ -2606,36 +2606,16 @@
 g.d(TAG,
-%0A                   
  %22Overri
@@ -2624,17 +2624,17 @@
  deadlin
-g
+e
  expired
@@ -2659,64 +2659,29 @@
 ecId
-%0A                            + %22; asking system to retry
+ + %22. Retry requested
 %22);%0A

Fix typo in SystemJobService debug log statement