113b0241248716b126a538457828a964e86109d58Matthew Williams/* 213b0241248716b126a538457828a964e86109d58Matthew Williams * Copyright 2013 Google Inc. 313b0241248716b126a538457828a964e86109d58Matthew Williams * 413b0241248716b126a538457828a964e86109d58Matthew Williams * Licensed under the Apache License, Version 2.0 (the "License"); 513b0241248716b126a538457828a964e86109d58Matthew Williams * you may not use this file except in compliance with the License. 613b0241248716b126a538457828a964e86109d58Matthew Williams * You may obtain a copy of the License at 713b0241248716b126a538457828a964e86109d58Matthew Williams * 813b0241248716b126a538457828a964e86109d58Matthew Williams * http://www.apache.org/licenses/LICENSE-2.0 913b0241248716b126a538457828a964e86109d58Matthew Williams * 1013b0241248716b126a538457828a964e86109d58Matthew Williams * Unless required by applicable law or agreed to in writing, software 1113b0241248716b126a538457828a964e86109d58Matthew Williams * distributed under the License is distributed on an "AS IS" BASIS, 1213b0241248716b126a538457828a964e86109d58Matthew Williams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1313b0241248716b126a538457828a964e86109d58Matthew Williams * See the License for the specific language governing permissions and 1413b0241248716b126a538457828a964e86109d58Matthew Williams * limitations under the License. 1513b0241248716b126a538457828a964e86109d58Matthew Williams */ 1613b0241248716b126a538457828a964e86109d58Matthew Williams 1713b0241248716b126a538457828a964e86109d58Matthew Williamspackage com.android.demo.jobSchedulerApp.service; 1813b0241248716b126a538457828a964e86109d58Matthew Williams 197060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tateimport android.app.job.JobInfo; 207060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tateimport android.app.job.JobScheduler; 217060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tateimport android.app.job.JobParameters; 227060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tateimport android.app.job.JobService; 23ee410da42b6b8352213f03f7725fd041f703b035Matthew Williamsimport android.content.ComponentName; 2413b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.content.Context; 2513b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.content.Intent; 26ee410da42b6b8352213f03f7725fd041f703b035Matthew Williamsimport android.os.AsyncTask; 2713b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.os.Message; 2813b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.os.Messenger; 2913b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.os.RemoteException; 3013b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.util.Log; 31ee410da42b6b8352213f03f7725fd041f703b035Matthew Williamsimport android.util.SparseArray; 3203a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williamsimport android.widget.Toast; 3313b0241248716b126a538457828a964e86109d58Matthew Williams 3413b0241248716b126a538457828a964e86109d58Matthew Williamsimport com.android.demo.jobSchedulerApp.MainActivity; 3513b0241248716b126a538457828a964e86109d58Matthew Williams 36ee410da42b6b8352213f03f7725fd041f703b035Matthew Williamsimport java.util.HashMap; 3713b0241248716b126a538457828a964e86109d58Matthew Williamsimport java.util.LinkedList; 38ee410da42b6b8352213f03f7725fd041f703b035Matthew Williamsimport java.util.Random; 3913b0241248716b126a538457828a964e86109d58Matthew Williams 4013b0241248716b126a538457828a964e86109d58Matthew Williams 4113b0241248716b126a538457828a964e86109d58Matthew Williams/** 4213b0241248716b126a538457828a964e86109d58Matthew Williams * Service to handle sync requests. 4313b0241248716b126a538457828a964e86109d58Matthew Williams * <p> 4413b0241248716b126a538457828a964e86109d58Matthew Williams * This service is invoked in response to Intents with action android.content.SyncAdapter, and 4513b0241248716b126a538457828a964e86109d58Matthew Williams * returns a Binder connection to SyncAdapter. 4613b0241248716b126a538457828a964e86109d58Matthew Williams * <p> 4713b0241248716b126a538457828a964e86109d58Matthew Williams * For performance, only one sync adapter will be initialized within this application's context. 4813b0241248716b126a538457828a964e86109d58Matthew Williams * <p> 4913b0241248716b126a538457828a964e86109d58Matthew Williams * Note: The SyncService itself is not notified when a new sync occurs. It's role is to manage the 5013b0241248716b126a538457828a964e86109d58Matthew Williams * lifecycle of our and provide a handle to said SyncAdapter to the OS on 5113b0241248716b126a538457828a964e86109d58Matthew Williams * request. 5213b0241248716b126a538457828a964e86109d58Matthew Williams */ 537060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tatepublic class TestJobService extends JobService { 5413b0241248716b126a538457828a964e86109d58Matthew Williams private static final String TAG = "SyncService"; 5513b0241248716b126a538457828a964e86109d58Matthew Williams 5613b0241248716b126a538457828a964e86109d58Matthew Williams @Override 5713b0241248716b126a538457828a964e86109d58Matthew Williams public void onCreate() { 5813b0241248716b126a538457828a964e86109d58Matthew Williams super.onCreate(); 5913b0241248716b126a538457828a964e86109d58Matthew Williams Log.i(TAG, "Service created"); 6013b0241248716b126a538457828a964e86109d58Matthew Williams } 6113b0241248716b126a538457828a964e86109d58Matthew Williams 6213b0241248716b126a538457828a964e86109d58Matthew Williams @Override 6313b0241248716b126a538457828a964e86109d58Matthew Williams public void onDestroy() { 6413b0241248716b126a538457828a964e86109d58Matthew Williams super.onDestroy(); 6513b0241248716b126a538457828a964e86109d58Matthew Williams Log.i(TAG, "Service destroyed"); 6613b0241248716b126a538457828a964e86109d58Matthew Williams } 6713b0241248716b126a538457828a964e86109d58Matthew Williams 6813b0241248716b126a538457828a964e86109d58Matthew Williams @Override 6913b0241248716b126a538457828a964e86109d58Matthew Williams public int onStartCommand(Intent intent, int flags, int startId) { 7013b0241248716b126a538457828a964e86109d58Matthew Williams Messenger callback = intent.getParcelableExtra("messenger"); 7113b0241248716b126a538457828a964e86109d58Matthew Williams Message m = Message.obtain(); 7213b0241248716b126a538457828a964e86109d58Matthew Williams m.what = MainActivity.MSG_SERVICE_OBJ; 7313b0241248716b126a538457828a964e86109d58Matthew Williams m.obj = this; 7413b0241248716b126a538457828a964e86109d58Matthew Williams try { 7513b0241248716b126a538457828a964e86109d58Matthew Williams callback.send(m); 7613b0241248716b126a538457828a964e86109d58Matthew Williams } catch (RemoteException e) { 7713b0241248716b126a538457828a964e86109d58Matthew Williams Log.e(TAG, "Error passing service object back to activity."); 7813b0241248716b126a538457828a964e86109d58Matthew Williams } 7913b0241248716b126a538457828a964e86109d58Matthew Williams return START_NOT_STICKY; 8013b0241248716b126a538457828a964e86109d58Matthew Williams } 8113b0241248716b126a538457828a964e86109d58Matthew Williams 8213b0241248716b126a538457828a964e86109d58Matthew Williams @Override 837060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate public boolean onStartJob(JobParameters params) { 8406f0852ead5524c09987fc9da5129390a2dba251Christopher Tate Log.i(TAG, "on start job: " + params.getJobId() 8506f0852ead5524c09987fc9da5129390a2dba251Christopher Tate + " deadline?=" + params.isOverrideDeadlineExpired()); 86ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams currentId++; 87ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams jobParamsMap.put(currentId, params); 88ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams final int currId = this.currentId; 8913b0241248716b126a538457828a964e86109d58Matthew Williams if (mActivity != null) { 907060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate mActivity.onReceivedStartJob(params); 9113b0241248716b126a538457828a964e86109d58Matthew Williams } 92ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams 9303a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams Toast.makeText( 9403a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams this, "On start job: '" + params.getJobId() + "' deadline exceeded: " + 9503a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams params.isOverrideDeadlineExpired(), 9603a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams Toast.LENGTH_LONG).show(); 9703a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams 9813b0241248716b126a538457828a964e86109d58Matthew Williams return true; 9913b0241248716b126a538457828a964e86109d58Matthew Williams } 10013b0241248716b126a538457828a964e86109d58Matthew Williams 101ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams 10213b0241248716b126a538457828a964e86109d58Matthew Williams @Override 1037060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate public boolean onStopJob(JobParameters params) { 1047060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate Log.i(TAG, "on stop job: " + params.getJobId()); 105ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams int ind = jobParamsMap.indexOfValue(params); 106ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams jobParamsMap.remove(ind); 107ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams mActivity.onReceivedStopJob(); 10803a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams return false; // no reschedule 10913b0241248716b126a538457828a964e86109d58Matthew Williams } 11013b0241248716b126a538457828a964e86109d58Matthew Williams 111ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams static int currentId = 0; 11213b0241248716b126a538457828a964e86109d58Matthew Williams MainActivity mActivity; 113ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams private final SparseArray<JobParameters> jobParamsMap = new SparseArray<JobParameters>(); 114ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams 11513b0241248716b126a538457828a964e86109d58Matthew Williams 11613b0241248716b126a538457828a964e86109d58Matthew Williams public void setUiCallback(MainActivity activity) { 11713b0241248716b126a538457828a964e86109d58Matthew Williams mActivity = activity; 11813b0241248716b126a538457828a964e86109d58Matthew Williams } 11913b0241248716b126a538457828a964e86109d58Matthew Williams 12013b0241248716b126a538457828a964e86109d58Matthew Williams /** Send job to the JobScheduler. */ 121ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams public void scheduleJob(JobInfo job) { 122ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams Log.d(TAG, "Scheduling job " + job); 1237060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate JobScheduler tm = 1247060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); 125ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams tm.schedule(job); 12613b0241248716b126a538457828a964e86109d58Matthew Williams } 12713b0241248716b126a538457828a964e86109d58Matthew Williams 1287060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate public boolean callJobFinished() { 129ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams if (jobParamsMap.size() == 0) { 130ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams return false; 131ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams } 132ee410da42b6b8352213f03f7725fd041f703b035Matthew Williams JobParameters params = jobParamsMap.valueAt(0); 13313b0241248716b126a538457828a964e86109d58Matthew Williams if (params == null) { 13413b0241248716b126a538457828a964e86109d58Matthew Williams return false; 13513b0241248716b126a538457828a964e86109d58Matthew Williams } else { 1367060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate jobFinished(params, false); 13703a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams jobParamsMap.removeAt(0); 13813b0241248716b126a538457828a964e86109d58Matthew Williams return true; 13913b0241248716b126a538457828a964e86109d58Matthew Williams } 14013b0241248716b126a538457828a964e86109d58Matthew Williams } 14113b0241248716b126a538457828a964e86109d58Matthew Williams 14213b0241248716b126a538457828a964e86109d58Matthew Williams} 143