@@ -53,10 +53,13 @@ public class JobWorkService extends JobService {
5353 /**
5454 * This is a task to dequeue and process work in the background.
5555 */
56- final class CommandProcessor extends AsyncTask <Void , Void , Void > {
56+ static final class CommandProcessor extends AsyncTask <Void , Void , Void > {
5757 private final JobParameters mParams ;
58- CommandProcessor (JobParameters params ) {
59- mParams = params ;
58+ private final Logger logger ;
59+ private final Context context ;
60+
61+ CommandProcessor (JobParameters params , Context context , Logger logger ) {
62+ mParams = params ; this .logger = logger ; this .context = context ;
6063 }
6164 @ Override
6265 protected Void doInBackground (Void ... params ) {
@@ -89,7 +92,7 @@ protected Void doInBackground(Void... params) {
8992 callOnHandleIntent (intentService , work .getIntent ());
9093 completeWork (mParams , work );
9194 } else {
92- Handler mainHandler = new Handler (getApplicationContext ().getMainLooper ());
95+ Handler mainHandler = new Handler (context . getApplicationContext ().getMainLooper ());
9396 final Service mainService = (Service )service ;
9497 final JobWorkItem workItem = work ;
9598 final Intent manServiceIntent = work .getIntent ();
@@ -121,6 +124,49 @@ public void run() {
121124 }
122125 return null ;
123126 }
127+ private void setContext (Service service ) {
128+ callMethod (ContextWrapper .class , service , "attachBaseContext" , new Class [] { Context .class }, context .getApplicationContext ());
129+ }
130+
131+ private void callOnStartCommand (Service service , Intent intent ) {
132+ callMethod (Service .class , service , "onStartCommand" , new Class [] { Intent .class , int .class , int .class }, intent , 0 , 1 );
133+ }
134+
135+ private void callOnHandleIntent (IntentService intentService , Intent intent ) {
136+ callMethod (IntentService .class , intentService , "onHandleIntent" , new Class [] { Intent .class }, intent );
137+ }
138+
139+ private void callMethod (Class clazz , Object object , String methodName , Class [] parameterTypes , Object ... parameters ) {
140+ try {
141+ Method method = clazz .getDeclaredMethod (methodName , parameterTypes );
142+ method .setAccessible (true );
143+ method .invoke (object , parameters );
144+
145+ } catch (NoSuchMethodException e ) {
146+ logger .error ("Error calling method " + methodName , e );
147+ } catch (InvocationTargetException e ) {
148+ logger .error ("Error calling method " + methodName , e );
149+ } catch (IllegalAccessException e ) {
150+ logger .error ("Error calling method " + methodName , e );
151+ }
152+
153+ }
154+
155+ private void completeWork (JobParameters jobParameters , JobWorkItem jobWorkItem ) {
156+ Intent intent = jobWorkItem .getIntent ();
157+ if (intent != null && intent .hasExtra (INTENT_EXTRA_JWS_PERIODIC )) {
158+ logger .info ("Periodic work item completed " );
159+ jobParameters .completeWork (jobWorkItem );
160+ //reschedule(jobWorkItem);
161+ }
162+ else {
163+ logger .info ("work item completed" );
164+ jobParameters .completeWork (jobWorkItem );
165+
166+ }
167+
168+ }
169+
124170 }
125171 @ Override
126172 public void onCreate () {
@@ -134,7 +180,7 @@ public void onDestroy() {
134180 @ Override
135181 public boolean onStartJob (JobParameters params ) {
136182 // Start task to pull work out of the queue and process it.
137- mCurProcessor = new CommandProcessor (params );
183+ mCurProcessor = new CommandProcessor (params , getApplicationContext (), this . logger );
138184 mCurProcessor .executeOnExecutor (AsyncTask .THREAD_POOL_EXECUTOR );
139185 // Allow the job to continue running while we process work.
140186 return true ;
@@ -150,49 +196,6 @@ public boolean onStopJob(JobParameters params) {
150196 return true ;
151197 }
152198
153- private void setContext (Service service ) {
154- callMethod (ContextWrapper .class , service , "attachBaseContext" , new Class [] { Context .class }, getApplicationContext ());
155- }
156-
157- private void callOnStartCommand (Service service , Intent intent ) {
158- callMethod (Service .class , service , "onStartCommand" , new Class [] { Intent .class , int .class , int .class }, intent , 0 , 1 );
159- }
160-
161- private void callOnHandleIntent (IntentService intentService , Intent intent ) {
162- callMethod (IntentService .class , intentService , "onHandleIntent" , new Class [] { Intent .class }, intent );
163- }
164-
165- private void callMethod (Class clazz , Object object , String methodName , Class [] parameterTypes , Object ... parameters ) {
166- try {
167- Method method = clazz .getDeclaredMethod (methodName , parameterTypes );
168- method .setAccessible (true );
169- method .invoke (object , parameters );
170-
171- } catch (NoSuchMethodException e ) {
172- logger .error ("Error calling method " + methodName , e );
173- } catch (InvocationTargetException e ) {
174- logger .error ("Error calling method " + methodName , e );
175- } catch (IllegalAccessException e ) {
176- logger .error ("Error calling method " + methodName , e );
177- }
178-
179- }
180-
181- private void completeWork (JobParameters jobParameters , JobWorkItem jobWorkItem ) {
182- Intent intent = jobWorkItem .getIntent ();
183- if (intent != null && intent .hasExtra (INTENT_EXTRA_JWS_PERIODIC )) {
184- logger .info ("Periodic work item completed " );
185- jobParameters .completeWork (jobWorkItem );
186- //reschedule(jobWorkItem);
187- }
188- else {
189- logger .info ("work item completed" );
190- jobParameters .completeWork (jobWorkItem );
191-
192- }
193-
194- }
195-
196199 private void reschedule (JobWorkItem item ) {
197200 ServiceScheduler .PendingIntentFactory pendingIntentFactory = new ServiceScheduler
198201 .PendingIntentFactory (getApplicationContext ());
0 commit comments