Skip to content

Commit

Permalink
Better setup / teardown for JobService (#745)
Browse files Browse the repository at this point in the history
* Better setup / teardown for JobService

* Move to lazy pattern
  • Loading branch information
natario1 authored and rogerhu committed Oct 10, 2017
1 parent 2b7bb5b commit 300c3ec
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions Parse/src/main/java/com/parse/PushServiceApi26.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -44,7 +45,8 @@ static boolean run(Context context, Intent intent) {
// Execute in the next second.
Bundle extra = new Bundle(1);
extra.putParcelable(INTENT_KEY, intent);
int did = scheduler.schedule(new JobInfo.Builder(JOB_SERVICE_ID, new ComponentName(context, PushServiceApi26.class))
ComponentName component = new ComponentName(context, PushServiceApi26.class);
int did = scheduler.schedule(new JobInfo.Builder(JOB_SERVICE_ID, component)
.setMinimumLatency(1L)
.setOverrideDeadline(1000L)
.setRequiresCharging(false)
Expand All @@ -58,19 +60,13 @@ static boolean run(Context context, Intent intent) {
// We delegate the intent to a PushHandler running in a streamlined executor.
private ExecutorService executor;
private PushHandler handler;
private int jobsCount;

// Our manifest file is OK.
static boolean isSupported() {
return true;
}

@Override
public void onCreate() {
super.onCreate();
executor = Executors.newSingleThreadExecutor();
handler = PushServiceUtils.createPushHandler();
}

@Override
public boolean onStartJob(final JobParameters jobParameters) {
if (ParsePlugins.Android.get() == null) {
Expand All @@ -85,13 +81,18 @@ public boolean onStartJob(final JobParameters jobParameters) {

final Bundle params = jobParameters.getTransientExtras();
final Intent intent = params.getParcelable(INTENT_KEY);
executor.execute(new Runnable() {
jobsCount++;
getExecutor().execute(new Runnable() {
@Override
public void run() {
try {
handler.handlePush(intent);
getHandler().handlePush(intent);
} finally {
jobFinished(jobParameters, false);
jobsCount--;
if (jobsCount == 0) {
tearDown();
}
}
}
});
Expand All @@ -104,13 +105,19 @@ public boolean onStopJob(JobParameters jobParameters) {
return true;
}

@Override
public void onDestroy() {
if (executor != null) {
executor.shutdown();
executor = null;
handler = null;
}
super.onDestroy();
private Executor getExecutor() {
if (executor == null) executor = Executors.newSingleThreadExecutor();
return executor;
}

private PushHandler getHandler() {
if (handler == null) handler = PushServiceUtils.createPushHandler();
return handler;
}

private void tearDown() {
if (executor != null) executor.shutdown();
executor = null;
handler = null;
}
}

0 comments on commit 300c3ec

Please sign in to comment.