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 aeeedac

Browse files
committedJul 3, 2016
refactor(completed-task): FileDownloader doesn't store completed tasks in Database anymore, add FileDownloadUtils#isFileNameConverted
Refs lingochamp#176, lingochamp#172
1 parent 4598bfe commit aeeedac

22 files changed

+169
-480
lines changed
 

‎demo/src/main/java/com/liulishuo/filedownloader/demo/TasksManagerDemoActivity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public void onBindViewHolder(TaskItemViewHolder holder, int position) {
356356

357357

358358
if (TasksManager.getImpl().isReady()) {
359-
final int status = TasksManager.getImpl().getStatus(model.getId());
359+
final int status = TasksManager.getImpl().getStatus(model.getId(), model.getPath());
360360
if (status == FileDownloadStatus.pending || status == FileDownloadStatus.started ||
361361
status == FileDownloadStatus.connected) {
362362
// start task, but file not created yet
@@ -514,8 +514,8 @@ public boolean isDownloaded(final int status) {
514514
return status == FileDownloadStatus.completed;
515515
}
516516

517-
public int getStatus(final int id) {
518-
return FileDownloader.getImpl().getStatus(id);
517+
public int getStatus(final int id, String path) {
518+
return FileDownloader.getImpl().getStatus(id, path);
519519
}
520520

521521
public long getTotal(final int id) {

‎demo/src/main/res/values-zh/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<string name="performance_test_output">写文件性能测试</string>
3333

3434
<string name="tasks_manager_demo_title">任务管理器案例</string>
35-
<string name="tasks_manager_demo_name" formatted="false">任务: %s</string>
35+
<string name="tasks_manager_demo_name" formatted="false">任务: %d</string>
3636
<string name="tasks_manager_demo_status_loading">状态: 加载中...</string>
3737
<string name="tasks_manager_demo_status_pending">状态: 队列中</string>
3838
<string name="tasks_manager_demo_status_started">状态: 开始下载</string>

‎demo/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
<string name="tasks_manager_demo_title">Tasks Manager Demo</string>
18-
<string name="tasks_manager_demo_name" formatted="false">task: %s</string>
18+
<string name="tasks_manager_demo_name" formatted="false">task: %d</string>
1919
<string name="tasks_manager_demo_status_loading">Status: loading...</string>
2020
<string name="tasks_manager_demo_status_pending">Status: pending</string>
2121
<string name="tasks_manager_demo_status_started">Status: started</string>

‎library/src/main/aidl/com/liulishuo/filedownloader/i/IFileDownloadIPCService.aidl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ interface IFileDownloadIPCService {
1111
oneway void registerCallback(in IFileDownloadIPCCallback callback);
1212
oneway void unregisterCallback(in IFileDownloadIPCCallback callback);
1313

14-
MessageSnapshot checkReuse(String url, String path);
15-
MessageSnapshot checkReuse2(int id);
1614
boolean checkDownloading(String url, String path);
1715
// why not use `oneway` to optimize the performance of the below `start` method? because if we
1816
// use `oneway` it will be very hard to decide how is the binder thread going according to the context.
@@ -29,12 +27,10 @@ interface IFileDownloadIPCService {
2927

3028
long getSofar(int downloadId);
3129
long getTotal(int downloadId);
32-
int getStatus(int downloadId);
30+
byte getStatus(int downloadId);
3331
boolean isIdle();
3432

3533
oneway void startForeground(int id, in Notification notification);
3634
oneway void stopForeground(boolean removeNotification);
3735

38-
boolean setTaskCompleted(String url, String path, long totalBytes);
39-
boolean setTaskCompleted1(in List<FileDownloadTaskAtom> taskList);
4036
}

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ public abstract class BaseDownloadTask {
8484

8585
private boolean isForceReDownload = false;
8686

87-
/**
88-
* 如果{@link #isForceReDownload}为false
89-
* 并且检查文件是正确的{@link com.liulishuo.filedownloader.services.FileDownloadMgr#checkReuse(int, FileDownloadModel)}
90-
* 则不启动下载直接成功返回,此时该变量为true
91-
*/
9287
private boolean isReusedOldFile = false;
9388

9489
volatile boolean using = false;
@@ -502,7 +497,7 @@ public boolean pause() {
502497
*
503498
* @return The identify id for this task.
504499
* @see FileDownloader#pause(int)
505-
* @see FileDownloader#getStatus(int)
500+
* @see FileDownloader#getStatus(String, String)
506501
* @see FileDownloader#getTotal(int)
507502
* @see FileDownloader#getSoFar(int)
508503
*/
@@ -652,7 +647,7 @@ public Throwable getEx() {
652647

653648

654649
/**
655-
* @return Is reused downloaded old file, 是否是使用了已经存在的有效文件,而非启动下载
650+
* @return Whether reused the downloaded file by past.
656651
* @see #isReusedOldFile
657652
*/
658653
public boolean isReusedOldFile() {
@@ -1082,9 +1077,6 @@ private void update(final MessageSnapshot snapshot) {
10821077
break;
10831078
case FileDownloadStatus.completed:
10841079
this.isReusedOldFile = snapshot.isReusedDownloadedFile();
1085-
if (snapshot.isReusedDownloadedFile()) {
1086-
this.etag = snapshot.getEtag();
1087-
}
10881080
// only carry total data back
10891081
this.soFarBytes = snapshot.getLargeTotalBytes();
10901082
this.totalBytes = snapshot.getLargeTotalBytes();

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,10 @@
1818
import android.app.Notification;
1919
import android.content.Context;
2020

21-
import com.liulishuo.filedownloader.message.MessageSnapshot;
2221
import com.liulishuo.filedownloader.model.FileDownloadHeader;
23-
import com.liulishuo.filedownloader.model.FileDownloadTaskAtom;
2422
import com.liulishuo.filedownloader.services.FDServiceSharedHandler;
2523
import com.liulishuo.filedownloader.util.FileDownloadProperties;
2624

27-
import java.util.List;
28-
2925
/**
3026
* Created by Jacksgong on 4/17/16.
3127
* <p/>
@@ -75,16 +71,6 @@ public boolean pause(int id) {
7571
return handler.pause(id);
7672
}
7773

78-
@Override
79-
public MessageSnapshot isDownloaded(String url, String path) {
80-
return handler.isDownloaded(url, path);
81-
}
82-
83-
@Override
84-
public MessageSnapshot isDownloaded(int id) {
85-
return handler.isDownloaded(id);
86-
}
87-
8874
@Override
8975
public boolean isDownloading(String url, String path) {
9076
return handler.isDownloading(url, path);
@@ -101,7 +87,7 @@ public long getTotal(int id) {
10187
}
10288

10389
@Override
104-
public int getStatus(int id) {
90+
public byte getStatus(int id) {
10591
return handler.getStatus(id);
10692
}
10793

@@ -145,16 +131,6 @@ public void stopForeground(boolean removeNotification) {
145131
handler.stopForeground(removeNotification);
146132
}
147133

148-
@Override
149-
public boolean setTaskCompleted(String url, String path, long totalBytes) {
150-
return handler.setTaskCompleted(url, path, totalBytes);
151-
}
152-
153-
@Override
154-
public boolean setTaskCompleted(List<FileDownloadTaskAtom> taskAtomList) {
155-
return handler.setTaskCompleted(taskAtomList);
156-
}
157-
158134
@Override
159135
public boolean setMaxNetworkThreadCount(int count) {
160136
return handler.setMaxNetworkThreadCount(count);

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

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
import android.content.Intent;
2121

2222
import com.liulishuo.filedownloader.event.DownloadServiceConnectChangedEvent;
23-
import com.liulishuo.filedownloader.message.MessageSnapshot;
2423
import com.liulishuo.filedownloader.model.FileDownloadHeader;
25-
import com.liulishuo.filedownloader.model.FileDownloadTaskAtom;
2624
import com.liulishuo.filedownloader.services.FDServiceSharedHandler;
2725
import com.liulishuo.filedownloader.services.FDServiceSharedHandler.FileDownloadServiceSharedConnection;
2826
import com.liulishuo.filedownloader.services.FileDownloadService.SharedMainProcessService;
@@ -68,24 +66,6 @@ public boolean pause(int id) {
6866
return handler.pause(id);
6967
}
7068

71-
@Override
72-
public MessageSnapshot isDownloaded(String url, String path) {
73-
if (!isConnected()) {
74-
return DownloadServiceNotConnectedHelper.isDownloaded(url, path);
75-
}
76-
77-
return handler.checkReuse(url, path);
78-
}
79-
80-
@Override
81-
public MessageSnapshot isDownloaded(int id) {
82-
if (!isConnected()) {
83-
return DownloadServiceNotConnectedHelper.isDownloaded(id);
84-
}
85-
86-
return handler.checkReuse2(id);
87-
}
88-
8969
@Override
9070
public boolean isDownloading(String url, String path) {
9171
if (!isConnected()) {
@@ -114,7 +94,7 @@ public long getTotal(int id) {
11494
}
11595

11696
@Override
117-
public int getStatus(int id) {
97+
public byte getStatus(int id) {
11898
if (!isConnected()) {
11999
return DownloadServiceNotConnectedHelper.getStatus(id);
120100
}
@@ -191,24 +171,6 @@ public void stopForeground(boolean removeNotification) {
191171
handler.stopForeground(removeNotification);
192172
}
193173

194-
@Override
195-
public boolean setTaskCompleted(String url, String path, long totalBytes) {
196-
if (!isConnected()) {
197-
return DownloadServiceNotConnectedHelper.setTaskCompleted(url, path, totalBytes);
198-
}
199-
200-
return handler.setTaskCompleted(url, path, totalBytes);
201-
}
202-
203-
@Override
204-
public boolean setTaskCompleted(List<FileDownloadTaskAtom> taskAtomList) {
205-
if (!isConnected()) {
206-
return DownloadServiceNotConnectedHelper.setTaskCompleted(taskAtomList);
207-
}
208-
209-
return handler.setTaskCompleted1(taskAtomList);
210-
}
211-
212174
@Override
213175
public boolean setMaxNetworkThreadCount(int count) {
214176
if (!isConnected()) {

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

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@
2626
import com.liulishuo.filedownloader.message.MessageSnapshotFlow;
2727
import com.liulishuo.filedownloader.model.FileDownloadHeader;
2828
import com.liulishuo.filedownloader.model.FileDownloadStatus;
29-
import com.liulishuo.filedownloader.model.FileDownloadTaskAtom;
3029
import com.liulishuo.filedownloader.services.BaseFileServiceUIGuard;
3130
import com.liulishuo.filedownloader.services.FileDownloadService.SeparateProcessService;
3231
import com.liulishuo.filedownloader.util.DownloadServiceNotConnectedHelper;
3332

34-
import java.util.List;
35-
3633

3734
/**
3835
* Created by Jacksgong on 9/23/15.
@@ -127,36 +124,6 @@ public boolean pause(final int id) {
127124
return false;
128125
}
129126

130-
@Override
131-
public MessageSnapshot isDownloaded(final String url, final String path) {
132-
if (!isConnected()) {
133-
return DownloadServiceNotConnectedHelper.isDownloaded(url, path);
134-
}
135-
136-
try {
137-
return getService().checkReuse(url, path);
138-
} catch (RemoteException e) {
139-
e.printStackTrace();
140-
}
141-
142-
return null;
143-
}
144-
145-
@Override
146-
public MessageSnapshot isDownloaded(final int id) {
147-
if (!isConnected()) {
148-
return DownloadServiceNotConnectedHelper.isDownloaded(id);
149-
}
150-
151-
try {
152-
return getService().checkReuse2(id);
153-
} catch (RemoteException e) {
154-
e.printStackTrace();
155-
}
156-
157-
return null;
158-
}
159-
160127
@Override
161128
public boolean isDownloading(final String url, final String path) {
162129
if (!isConnected()) {
@@ -205,12 +172,12 @@ public long getTotal(final int id) {
205172
}
206173

207174
@Override
208-
public int getStatus(final int id) {
175+
public byte getStatus(final int id) {
209176
if (!isConnected()) {
210177
return DownloadServiceNotConnectedHelper.getStatus(id);
211178
}
212179

213-
int status = FileDownloadStatus.INVALID_STATUS;
180+
byte status = FileDownloadStatus.INVALID_STATUS;
214181
try {
215182
status = getService().getStatus(id);
216183
} catch (RemoteException e) {
@@ -280,36 +247,6 @@ public void stopForeground(boolean removeNotification) {
280247
}
281248
}
282249

283-
@Override
284-
public boolean setTaskCompleted(String url, String path, long totalBytes) {
285-
if (!isConnected()) {
286-
return DownloadServiceNotConnectedHelper.setTaskCompleted(url, path, totalBytes);
287-
}
288-
289-
try {
290-
return getService().setTaskCompleted(url, path, totalBytes);
291-
} catch (RemoteException e) {
292-
e.printStackTrace();
293-
}
294-
295-
return false;
296-
}
297-
298-
@Override
299-
public boolean setTaskCompleted(List<FileDownloadTaskAtom> taskAtomList) {
300-
if (!isConnected()) {
301-
return DownloadServiceNotConnectedHelper.setTaskCompleted(taskAtomList);
302-
}
303-
304-
try {
305-
return getService().setTaskCompleted1(taskAtomList);
306-
} catch (RemoteException e) {
307-
e.printStackTrace();
308-
}
309-
310-
return false;
311-
}
312-
313250
@Override
314251
public boolean setMaxNetworkThreadCount(int count) {
315252
if (!isConnected()) {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
import com.liulishuo.filedownloader.event.IDownloadEvent;
2424
import com.liulishuo.filedownloader.message.MessageSnapshot;
2525
import com.liulishuo.filedownloader.message.MessageSnapshotFlow;
26+
import com.liulishuo.filedownloader.message.MessageSnapshotTaker;
2627
import com.liulishuo.filedownloader.util.FileDownloadHelper;
2728
import com.liulishuo.filedownloader.util.FileDownloadLog;
2829
import com.liulishuo.filedownloader.util.FileDownloadUtils;
2930

31+
import java.io.File;
3032
import java.util.ArrayList;
3133
import java.util.List;
3234

@@ -119,13 +121,13 @@ protected boolean _checkCanReuse() {
119121
return false;
120122
}
121123

122-
final MessageSnapshot snapshot = FileDownloadServiceProxy.getImpl().isDownloaded(getId());
123-
if (snapshot != null) {
124-
MessageSnapshotFlow.getImpl().inflow(snapshot);
124+
final File file = new File(getPath());
125+
if (file.exists()) {
126+
MessageSnapshotFlow.getImpl().inflow(MessageSnapshotTaker.
127+
catchCanReusedOldFile(getId(), file));
125128
return true;
126129
}
127130

128-
129131
return super._checkCanReuse();
130132
}
131133

There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.