-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathTaskService.java
More file actions
228 lines (174 loc) · 6.18 KB
/
TaskService.java
File metadata and controls
228 lines (174 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package fr.gaulupeau.apps.Poche.service;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.Process;
import android.util.Log;
import androidx.annotation.Nullable;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import fr.gaulupeau.apps.Poche.service.tasks.SimpleTask;
@SuppressLint("Registered") // subclassed
public abstract class TaskService extends Service {
public static final String ACTION_SIMPLE_TASK = "action_simple_task";
public static final String PARAM_FOREGROUND = "param_foreground";
public class TaskServiceBinder extends Binder {
public void enqueue(ParameterizedRunnable parameterizedRunnable) {
TaskService.this.enqueueTask(parameterizedRunnable, true);
}
}
/**
* The time to wait for more tasks before stopping the service (in milliseconds).
* The setting is not precise.
*/
private static final int WAIT_TIME = 1000;
private final String tag;
private Thread taskThread;
private final Object startIdLock = new Object();
private volatile int lastStartId;
private final BlockingQueue<ParameterizedRunnable> taskQueue = new LinkedBlockingQueue<>();
private volatile boolean running;
public static Intent newStartIntent(Context context,
Class<? extends TaskService> serviceClass) {
return new Intent(context, serviceClass);
}
public static Intent newSimpleTaskIntent(Context context,
Class<? extends TaskService> serviceClass,
SimpleTask task) {
Intent intent = newStartIntent(context, serviceClass);
intent.setAction(ACTION_SIMPLE_TASK);
intent.putExtra(SimpleTask.SIMPLE_TASK, task);
return intent;
}
public TaskService(String name) {
this.tag = name;
}
protected int getThreadPriority() {
return Process.THREAD_PRIORITY_BACKGROUND;
}
@Override
public void onCreate() {
Log.d(tag, "onCreate()");
running = true;
taskThread = new Thread(this::run, tag + "-taskThread");
taskThread.start();
}
@Override
public void onDestroy() {
Log.d(tag, "onDestroy()");
running = false;
if (taskThread != null) {
taskThread.interrupt();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(tag, "onStartCommand()");
if (intent.getBooleanExtra(PARAM_FOREGROUND, false)) {
setForeground(true);
}
if (ACTION_SIMPLE_TASK.equals(intent.getAction())) {
ParameterizedRunnable task = taskFromSimpleTask(SimpleTask.fromIntent(intent));
if (task != null) {
enqueueTask(task, false);
}
}
synchronized (startIdLock) {
lastStartId = startId;
}
return START_NOT_STICKY;
}
private ParameterizedRunnable taskFromSimpleTask(SimpleTask simpleTask) {
if (simpleTask == null) {
Log.d(tag, "taskFromActionRequest() request is null");
return null;
}
return simpleTask::run;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(tag, "onBind()");
return new TaskServiceBinder();
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(tag, "onUnbind()");
return true;
}
@Override
public void onRebind(Intent intent) {
Log.d(tag, "onRebind()");
}
private void run() {
Process.setThreadPriority(getThreadPriority());
while (running) {
ParameterizedRunnable task;
try {
task = taskQueue.poll(WAIT_TIME, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Log.d(tag, "run() poll interrupted");
break;
}
if (task != null) {
try {
Log.v(tag, "run() running a task");
task.run(this);
Log.v(tag, "run() finished a task");
} catch (Exception e) {
Log.e(tag, "run() exception during task execution", e);
}
}
synchronized (startIdLock) {
if (taskQueue.isEmpty()) {
Log.d(tag, "run() no more tasks; notifying that we are ready to stop");
readyToStop();
}
}
}
if (!taskQueue.isEmpty()) {
Log.w(tag, "run() stopping, but the queue is not empty");
}
}
private void readyToStop() {
Log.d(tag, "readyToStop()");
setForeground(false);
if (!stopSelfResult(lastStartId)) {
Log.d(tag, "readyToStop() startId didn't match");
}
}
private void enqueueTask(ParameterizedRunnable task, boolean ensureStarted) {
Log.d(tag, "enqueueTask()");
Objects.requireNonNull(task);
Log.v(tag, "enqueueTask() enqueueing task");
taskQueue.add(task);
if (ensureStarted) {
Log.v(tag, "enqueueTask() starting service");
ensureStarted();
Log.v(tag, "enqueueTask() started service");
}
}
private void ensureStarted() {
Log.d(tag, "ensureStarted()");
startService(newStartIntent(this, getClass()));
}
private void setForeground(boolean foreground) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return; // normal services are good enough before Oreo
}
if (foreground) {
startForeground(getForegroundNotificationId(), getForegroundNotification());
} else {
stopForeground(true);
}
}
protected abstract int getForegroundNotificationId();
protected abstract Notification getForegroundNotification();
}