Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2eb5797

Browse files
author
Tianhua Ran
committedSep 10, 2018
refactor: stop foreground service after all tasks finished in Android O
Closes lingochamp#1096
1 parent ba4cacc commit 2eb5797

File tree

6 files changed

+34
-1
lines changed

6 files changed

+34
-1
lines changed
 

‎library/src/main/java/com/liulishuo/filedownloader/FileDownloadList.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ public boolean remove(final BaseDownloadTask.IRunningTask willRemoveDownload,
177177
boolean succeed;
178178
synchronized (mList) {
179179
succeed = mList.remove(willRemoveDownload);
180+
if (succeed && mList.size() == 0) {
181+
if (FileDownloadServiceProxy.getImpl().madeServiceForeground()) {
182+
FileDownloader.getImpl().stopForeground(true);
183+
}
184+
}
180185
}
181186
if (FileDownloadLog.NEED_LOG) {
182187
if (mList.size() == 0) {
@@ -206,7 +211,6 @@ public boolean remove(final BaseDownloadTask.IRunningTask willRemoveDownload,
206211
default:
207212
// ignored
208213
}
209-
210214
} else {
211215
FileDownloadLog.e(this, "remove error, not exist: %s %d", willRemoveDownload,
212216
removeByStatus);

‎library/src/main/java/com/liulishuo/filedownloader/FileDownloadServiceProxy.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,9 @@ public boolean clearTaskData(int id) {
149149
public void clearAllTaskData() {
150150
handler.clearAllTaskData();
151151
}
152+
153+
@Override
154+
public boolean madeServiceForeground() {
155+
return handler.madeServiceForeground();
156+
}
152157
}

‎library/src/main/java/com/liulishuo/filedownloader/FileDownloadServiceSharedTransmit.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class FileDownloadServiceSharedTransmit implements
4444

4545
private static final Class<?> SERVICE_CLASS = SharedMainProcessService.class;
4646

47+
private boolean madeServiceForeground = false;
48+
4749
@Override
4850
public boolean start(String url, String path, boolean pathAsDirectory,
4951
int callbackProgressTimes,
@@ -147,8 +149,10 @@ public void bindStartByContext(Context context, Runnable connectedRunnable) {
147149
if (FileDownloadUtils.needMakeServiceForeground(context)) {
148150
if (FileDownloadLog.NEED_LOG) FileDownloadLog.d(this, "start foreground service");
149151
context.startForegroundService(i);
152+
madeServiceForeground = true;
150153
} else {
151154
context.startService(i);
155+
madeServiceForeground = false;
152156
}
153157
}
154158

@@ -177,6 +181,7 @@ public void stopForeground(boolean removeNotification) {
177181
}
178182

179183
handler.stopForeground(removeNotification);
184+
madeServiceForeground = false;
180185
}
181186

182187
@Override
@@ -206,6 +211,11 @@ public void clearAllTaskData() {
206211
handler.clearAllTaskData();
207212
}
208213

214+
@Override
215+
public boolean madeServiceForeground() {
216+
return madeServiceForeground;
217+
}
218+
209219
private FDServiceSharedHandler handler;
210220

211221
@Override

‎library/src/main/java/com/liulishuo/filedownloader/FileDownloadServiceUIGuard.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ public void stopForeground(boolean removeNotification) {
248248
getService().stopForeground(removeNotification);
249249
} catch (RemoteException e) {
250250
e.printStackTrace();
251+
} finally {
252+
madeServiceForeground = false;
251253
}
252254
}
253255

‎library/src/main/java/com/liulishuo/filedownloader/IFileDownloadServiceProxy.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@ boolean start(final String url, final String path, final boolean pathAsDirectory
6161
boolean clearTaskData(int id);
6262

6363
void clearAllTaskData();
64+
65+
boolean madeServiceForeground();
6466
}

‎library/src/main/java/com/liulishuo/filedownloader/services/BaseFileServiceUIGuard.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public abstract class BaseFileServiceUIGuard<CALLBACK extends Binder, INTERFACE
4545
private final CALLBACK callback;
4646
private volatile INTERFACE service;
4747
private final Class<?> serviceClass;
48+
protected boolean madeServiceForeground = false;
4849

4950
private final HashMap<String, Object> uiCacheMap = new HashMap<>();
5051

@@ -158,8 +159,10 @@ public void bindStartByContext(final Context context, final Runnable connectedRu
158159
if (FileDownloadUtils.needMakeServiceForeground(context)) {
159160
if (FileDownloadLog.NEED_LOG) FileDownloadLog.d(this, "start foreground service");
160161
context.startForegroundService(i);
162+
madeServiceForeground = true;
161163
} else {
162164
context.startService(i);
165+
madeServiceForeground = false;
163166
}
164167
}
165168

@@ -185,13 +188,20 @@ public void unbindByContext(final Context context) {
185188
context.stopService(i);
186189
}
187190

191+
@Override
192+
public boolean madeServiceForeground() {
193+
return madeServiceForeground;
194+
}
195+
188196
public void startService(final Context context) {
189197
Intent i = new Intent(context, serviceClass);
190198
if (FileDownloadUtils.needMakeServiceForeground(context)) {
191199
if (FileDownloadLog.NEED_LOG) FileDownloadLog.d(this, "start foreground service");
192200
context.startForegroundService(i);
201+
madeServiceForeground = true;
193202
} else {
194203
context.startService(i);
204+
madeServiceForeground = false;
195205
}
196206
}
197207

0 commit comments

Comments
 (0)
Failed to load comments.