Debian Codegraph @

tests/JobSchedulerTestApp/src/com/android/demo/jobSchedulerApp/service/TestJobService.java

raw symbols SCIP
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
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 + " deadline?=" + params.isOverrideDeadlineExpired());
86 currentId++;
87 jobParamsMap.put(currentId, params);
88 final int currId = this.currentId;
89 if (mActivity != null) {
90 mActivity.onReceivedStartJob(params);
91 }
92
93 Toast.makeText(
94 this, "On start job: '" + params.getJobId() + "' deadline exceeded: " +
95 params.isOverrideDeadlineExpired(),
96 Toast.LENGTH_LONG).show();
97
98 return true;
99 }
100
101
102 @Override
103 public boolean onStopJob(JobParameters params) {
104 Log.i(TAG, "on stop job: " + params.getJobId());
105 int ind = jobParamsMap.indexOfValue(params);
106 jobParamsMap.remove(ind);
107 mActivity.onReceivedStopJob();
108 return false; // no reschedule
109 }
110
111 static int currentId = 0;
112 MainActivity mActivity;
113 private final SparseArray<JobParameters> jobParamsMap = new SparseArray<JobParameters>();
114
115
116 public void setUiCallback(MainActivity activity) {
117 mActivity = activity;
118 }
119
120 /** Send job to the JobScheduler. */
121 public void scheduleJob(JobInfo job) {
122 Log.d(TAG, "Scheduling job " + job);
123 JobScheduler tm =
124 (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
125 tm.schedule(job);
126 }
127
128 public boolean callJobFinished() {
129 if (jobParamsMap.size() == 0) {
130 return false;
131 }
132 JobParameters params = jobParamsMap.valueAt(0);
133 if (params == null) {
134 return false;
135 } else {
136 jobFinished(params, false);
137 jobParamsMap.removeAt(0);
138 return true;
139 }
140 }
141
142 }