|
|
@ -44,6 +44,9 @@ import java.util.concurrent.Executors;
|
|
|
|
// Results will not be delivered to destroyed fragments
|
|
|
|
// Results will not be delivered to destroyed fragments
|
|
|
|
|
|
|
|
|
|
|
|
public abstract class SimpleTask<T> implements LifecycleObserver {
|
|
|
|
public abstract class SimpleTask<T> implements LifecycleObserver {
|
|
|
|
|
|
|
|
private boolean count = true;
|
|
|
|
|
|
|
|
private int executing = 0;
|
|
|
|
|
|
|
|
|
|
|
|
private static final List<SimpleTask> tasks = new ArrayList<>();
|
|
|
|
private static final List<SimpleTask> tasks = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
private static final ExecutorService executor = Executors.newFixedThreadPool(
|
|
|
|
private static final ExecutorService executor = Executors.newFixedThreadPool(
|
|
|
@ -51,6 +54,11 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
|
|
|
|
|
|
|
|
|
|
|
|
static final String ACTION_TASK_COUNT = BuildConfig.APPLICATION_ID + ".ACTION_TASK_COUNT";
|
|
|
|
static final String ACTION_TASK_COUNT = BuildConfig.APPLICATION_ID + ".ACTION_TASK_COUNT";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SimpleTask<T> setCount(boolean count) {
|
|
|
|
|
|
|
|
this.count = count;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void execute(Context context, LifecycleOwner owner, @NonNull Bundle args, @NonNull String name) {
|
|
|
|
public void execute(Context context, LifecycleOwner owner, @NonNull Bundle args, @NonNull String name) {
|
|
|
|
run(context, owner, args, name);
|
|
|
|
run(context, owner, args, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -75,14 +83,14 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
|
|
|
|
final Handler handler = new Handler();
|
|
|
|
final Handler handler = new Handler();
|
|
|
|
|
|
|
|
|
|
|
|
// prevent garbage collection
|
|
|
|
// prevent garbage collection
|
|
|
|
int count;
|
|
|
|
|
|
|
|
synchronized (tasks) {
|
|
|
|
synchronized (tasks) {
|
|
|
|
tasks.add(this);
|
|
|
|
tasks.add(this);
|
|
|
|
count = tasks.size();
|
|
|
|
if (count)
|
|
|
|
|
|
|
|
executing++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
|
|
|
|
lbm.sendBroadcast(new Intent(ACTION_TASK_COUNT).putExtra("count", count));
|
|
|
|
lbm.sendBroadcast(new Intent(ACTION_TASK_COUNT).putExtra("count", executing));
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
onPreExecute(args);
|
|
|
|
onPreExecute(args);
|
|
|
@ -164,15 +172,15 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void cleanup(Context context) {
|
|
|
|
private void cleanup(Context context) {
|
|
|
|
int count;
|
|
|
|
|
|
|
|
synchronized (tasks) {
|
|
|
|
synchronized (tasks) {
|
|
|
|
tasks.remove(this);
|
|
|
|
tasks.remove(this);
|
|
|
|
count = tasks.size();
|
|
|
|
if (count)
|
|
|
|
|
|
|
|
executing--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
|
|
|
|
lbm.sendBroadcast(new Intent(ACTION_TASK_COUNT).putExtra("count", count));
|
|
|
|
lbm.sendBroadcast(new Intent(ACTION_TASK_COUNT).putExtra("count", executing));
|
|
|
|
Log.i("Remaining tasks=" + count);
|
|
|
|
Log.i("Remaining tasks=" + tasks.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected void onPreExecute(Bundle args) {
|
|
|
|
protected void onPreExecute(Bundle args) {
|
|
|
|