1/*
2 * Copyright 2013 Google Inc.
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 com.android.demo.jobSchedulerApp.service;
18
19import android.app.job.JobInfo;
20import android.app.job.JobScheduler;
21import android.app.job.JobParameters;
22import android.app.job.JobService;
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.Intent;
26import android.os.AsyncTask;
27import android.os.Message;
28import android.os.Messenger;
29import android.os.RemoteException;
30import android.util.Log;
31import android.util.SparseArray;
32import android.widget.Toast;
33
34import com.android.demo.jobSchedulerApp.MainActivity;
35
36import java.util.HashMap;
37import java.util.LinkedList;
38import java.util.Random;
39
40
41/**
42 * Service to handle sync requests.
43 * <p>
44 * This service is invoked in response to Intents with action android.content.SyncAdapter, and
45 * returns a Binder connection to SyncAdapter.
46 * <p>
47 * For performance, only one sync adapter will be initialized within this application's context.
48 * <p>
49 * Note: The SyncService itself is not notified when a new sync occurs. It's role is to manage the
50 * lifecycle of our and provide a handle to said SyncAdapter to the OS on
51 * request.
52 */
53public class TestJobService extends JobService {
54    private static final String TAG = "SyncService";
55
56    @Override
57    public void onCreate() {
58        super.onCreate();
59        Log.i(TAG, "Service created");
60    }
61
62    @Override
63    public void onDestroy() {
64        super.onDestroy();
65        Log.i(TAG, "Service destroyed");
66    }
67
68    @Override
69    public int onStartCommand(Intent intent, int flags, int startId) {
70        Messenger callback = intent.getParcelableExtra("messenger");
71        Message m = Message.obtain();
72        m.what = MainActivity.MSG_SERVICE_OBJ;
73        m.obj = this;
74        try {
75            callback.send(m);
76        } catch (RemoteException e) {
77            Log.e(TAG, "Error passing service object back to activity.");
78        }
79        return START_NOT_STICKY;
80    }
81
82    @Override
83    public boolean onStartJob(JobParameters params) {
84        Log.i(TAG, "on start job: " + params.getJobId());
85        currentId++;
86        jobParamsMap.put(currentId, params);
87        final int currId = this.currentId;
88        if (mActivity != null) {
89            mActivity.onReceivedStartJob(params);
90        }
91
92        Toast.makeText(
93                this, "On start job: '" + params.getJobId() + "' deadline exceeded: " +
94                        params.isOverrideDeadlineExpired(),
95                Toast.LENGTH_LONG).show();
96
97        return true;
98    }
99
100
101    @Override
102    public boolean onStopJob(JobParameters params) {
103        Log.i(TAG, "on stop job: " + params.getJobId());
104        int ind = jobParamsMap.indexOfValue(params);
105        jobParamsMap.remove(ind);
106        mActivity.onReceivedStopJob();
107        return false; // no reschedule
108    }
109
110    static int currentId = 0;
111    MainActivity mActivity;
112    private final SparseArray<JobParameters> jobParamsMap = new SparseArray<JobParameters>();
113
114
115    public void setUiCallback(MainActivity activity) {
116        mActivity = activity;
117    }
118
119    /** Send job to the JobScheduler. */
120    public void scheduleJob(JobInfo job) {
121        Log.d(TAG, "Scheduling job " + job);
122        JobScheduler tm =
123                (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
124        tm.schedule(job);
125    }
126
127    public boolean callJobFinished() {
128        if (jobParamsMap.size() == 0) {
129            return false;
130        }
131        JobParameters params = jobParamsMap.valueAt(0);
132        if (params == null) {
133            return false;
134        } else {
135            jobFinished(params, false);
136            jobParamsMap.removeAt(0);
137            return true;
138        }
139    }
140
141}
142