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 
17 package com.android.demo.jobSchedulerApp.service;
18 
19 import android.app.job.JobInfo;
20 import android.app.job.JobScheduler;
21 import android.app.job.JobParameters;
22 import android.app.job.JobService;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.AsyncTask;
27 import android.os.Message;
28 import android.os.Messenger;
29 import android.os.RemoteException;
30 import android.util.Log;
31 import android.util.SparseArray;
32 import android.widget.Toast;
33 
34 import com.android.demo.jobSchedulerApp.MainActivity;
35 
36 import java.util.HashMap;
37 import java.util.LinkedList;
38 import 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  */
53 public class TestJobService extends JobService {
54     private static final String TAG = "SyncService";
55 
56     @Override
onCreate()57     public void onCreate() {
58         super.onCreate();
59         Log.i(TAG, "Service created");
60     }
61 
62     @Override
onDestroy()63     public void onDestroy() {
64         super.onDestroy();
65         Log.i(TAG, "Service destroyed");
66     }
67 
68     @Override
onStartCommand(Intent intent, int flags, int startId)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
onStartJob(JobParameters params)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
onStopJob(JobParameters params)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 
setUiCallback(MainActivity activity)115     public void setUiCallback(MainActivity activity) {
116         mActivity = activity;
117     }
118 
119     /** Send job to the JobScheduler. */
scheduleJob(JobInfo job)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 
callJobFinished()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