|
|
|
|
@ -8,7 +8,6 @@ import android.app.Service;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
|
import android.os.Binder;
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
import android.os.IBinder;
|
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
|
import android.support.v4.app.NotificationCompat;
|
|
|
|
|
@ -46,164 +45,225 @@ import static com.example.musicplayer.app.Constant.TYPE_DOWNLOADING;
|
|
|
|
|
* <pre>
|
|
|
|
|
* author : 残渊
|
|
|
|
|
* time : 2019/04/08
|
|
|
|
|
* desc : 下载服务,保证DownloadTask在后台运行
|
|
|
|
|
* desc : 下载服务,保证 DownloadTask 在后台运行
|
|
|
|
|
*
|
|
|
|
|
* 该类是一个服务类,用于处理歌曲的下载操作。
|
|
|
|
|
* 它使用 DownloadTask 进行下载任务的执行,通过 DownloadListener 监听下载进度和状态,
|
|
|
|
|
* 并使用 EventBus 进行事件的发布和订阅,以更新 UI 或通知其他部分下载的状态。
|
|
|
|
|
* 同时使用 LitePal 进行数据库操作,存储和更新下载信息。
|
|
|
|
|
* 还使用 Notification 进行通知的创建和更新,让用户了解下载的状态。
|
|
|
|
|
* </pre>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public class DownloadService extends Service {
|
|
|
|
|
// 日志标签
|
|
|
|
|
private static final String TAG = "DownloadService";
|
|
|
|
|
// 存储当前的下载任务
|
|
|
|
|
private DownloadTask downloadTask;
|
|
|
|
|
// 存储下载的 URL
|
|
|
|
|
private String downloadUrl;
|
|
|
|
|
// 自定义的 Binder 对象,用于与客户端通信
|
|
|
|
|
private DownloadBinder downloadBinder = new DownloadBinder();
|
|
|
|
|
private LinkedList<DownloadInfo> downloadQueue = new LinkedList<>();//等待队列
|
|
|
|
|
private int position = 0;//下载歌曲在下载歌曲列表的位置
|
|
|
|
|
// 存储下载等待队列,使用 LinkedList 方便在队列头部和尾部操作
|
|
|
|
|
private LinkedList<DownloadInfo> downloadQueue = new LinkedList<>();
|
|
|
|
|
// 下载歌曲在下载歌曲列表中的位置
|
|
|
|
|
private int position = 0;
|
|
|
|
|
// 下载监听器,监听下载进度和状态
|
|
|
|
|
private DownloadListener listener = new DownloadListener() {
|
|
|
|
|
// 下载进度更新时调用
|
|
|
|
|
@Override
|
|
|
|
|
public void onProgress(DownloadInfo downloadInfo) {
|
|
|
|
|
// 更新下载信息的状态为正在下载
|
|
|
|
|
downloadInfo.setStatus(Constant.DOWNLOAD_ING);
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(TYPE_DOWNLOADING, downloadInfo)); //通知下载模块
|
|
|
|
|
if(downloadInfo.getProgress()!=100){
|
|
|
|
|
getNotificationManager().notify(1, getNotification("正在下载: "+downloadInfo.getSongName(), downloadInfo.getProgress()));
|
|
|
|
|
}else {
|
|
|
|
|
if(downloadQueue.isEmpty()) getNotificationManager().notify(1, getNotification("下载成功",-1));
|
|
|
|
|
// 发布下载事件,通知下载模块
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(TYPE_DOWNLOADING, downloadInfo));
|
|
|
|
|
if (downloadInfo.getProgress()!= 100) {
|
|
|
|
|
// 更新通知显示正在下载及进度
|
|
|
|
|
getNotificationManager().notify(1, getNotification("正在下载: " + downloadInfo.getSongName(), downloadInfo.getProgress()));
|
|
|
|
|
} else {
|
|
|
|
|
if (downloadQueue.isEmpty()) {
|
|
|
|
|
// 当下载队列空且下载完成时,更新通知为下载成功
|
|
|
|
|
getNotificationManager().notify(1, getNotification("下载成功", -1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 下载成功时调用
|
|
|
|
|
@Override
|
|
|
|
|
public void onSuccess() {
|
|
|
|
|
downloadTask = null;
|
|
|
|
|
// 从下载队列中取出已完成的下载信息
|
|
|
|
|
DownloadInfo downloadInfo = downloadQueue.poll();
|
|
|
|
|
operateDb(downloadInfo); //操作数据库
|
|
|
|
|
start();//下载队列中的其它歌曲
|
|
|
|
|
//下载成功通知前台服务通知关闭,并创建一个下载成功的通知
|
|
|
|
|
// 操作数据库,可能是更新下载信息等操作
|
|
|
|
|
operateDb(downloadInfo);
|
|
|
|
|
// 开始下载队列中的下一个歌曲
|
|
|
|
|
start();
|
|
|
|
|
// 停止前台服务通知,并创建一个下载成功的通知
|
|
|
|
|
stopForeground(true);
|
|
|
|
|
if(downloadQueue.isEmpty()) getNotificationManager().notify(1, getNotification("下载成功",-1));
|
|
|
|
|
if (downloadQueue.isEmpty()) {
|
|
|
|
|
getNotificationManager().notify(1, getNotification("下载成功", -1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 下载完成时调用
|
|
|
|
|
@Override
|
|
|
|
|
public void onDownloaded() {
|
|
|
|
|
downloadTask = null;
|
|
|
|
|
// 显示下载完成的 Toast 提示
|
|
|
|
|
CommonUtil.showToast(DownloadService.this, "已下载");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 下载失败时调用
|
|
|
|
|
@Override
|
|
|
|
|
public void onFailed() {
|
|
|
|
|
downloadTask = null;
|
|
|
|
|
|
|
|
|
|
//下载失败通知前台服务通知关闭,并创建一个下载失败的通知
|
|
|
|
|
// 停止前台服务通知,并创建一个下载失败的通知
|
|
|
|
|
stopForeground(true);
|
|
|
|
|
getNotificationManager().notify(1, getNotification("下载失败",-1));
|
|
|
|
|
getNotificationManager().notify(1, getNotification("下载失败", -1));
|
|
|
|
|
// 显示下载失败的 Toast 提示
|
|
|
|
|
Toast.makeText(DownloadService.this, "下载失败", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 下载暂停时调用
|
|
|
|
|
@Override
|
|
|
|
|
public void onPaused() {
|
|
|
|
|
downloadTask = null;
|
|
|
|
|
DownloadInfo downloadInfo=downloadQueue.poll();//从下载列表中移除该歌曲
|
|
|
|
|
// 从下载队列中移除暂停的歌曲
|
|
|
|
|
DownloadInfo downloadInfo = downloadQueue.poll();
|
|
|
|
|
// 更新数据库中暂停的歌曲状态
|
|
|
|
|
updateDbOfPause(downloadInfo.getSongId());
|
|
|
|
|
getNotificationManager().notify(1, getNotification("下载已暂停:"+downloadInfo.getSongName(), -1));
|
|
|
|
|
start();//下载下载列表中的歌曲
|
|
|
|
|
// 更新通知为下载已暂停
|
|
|
|
|
getNotificationManager().notify(1, getNotification("下载已暂停:" + downloadInfo.getSongName(), -1));
|
|
|
|
|
// 开始下载队列中的下一个歌曲
|
|
|
|
|
start();
|
|
|
|
|
// 更新下载信息状态为暂停,并发布暂停事件
|
|
|
|
|
downloadInfo.setStatus(Constant.DOWNLOAD_PAUSED);
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(Constant.TYPE_DOWNLOAD_PAUSED, downloadInfo)); //下载暂停
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(Constant.TYPE_DOWNLOAD_PAUSED, downloadInfo));
|
|
|
|
|
// 显示下载已暂停的 Toast 提示
|
|
|
|
|
Toast.makeText(DownloadService.this, "下载已暂停", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 下载取消时调用
|
|
|
|
|
@Override
|
|
|
|
|
public void onCanceled() {
|
|
|
|
|
downloadTask = null;
|
|
|
|
|
// 停止前台服务通知
|
|
|
|
|
stopForeground(true);
|
|
|
|
|
// 显示下载已取消的 Toast 提示
|
|
|
|
|
Toast.makeText(DownloadService.this, "下载已取消", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 服务绑定方法,返回 Binder 对象
|
|
|
|
|
@Nullable
|
|
|
|
|
@Override
|
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
|
return downloadBinder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 自定义的 Binder 类,用于与客户端通信
|
|
|
|
|
public class DownloadBinder extends Binder {
|
|
|
|
|
// 开始下载方法
|
|
|
|
|
public void startDownload(DownloadInfo song) {
|
|
|
|
|
try {
|
|
|
|
|
postDownloadEvent(song);//通知正在下载界面
|
|
|
|
|
// 通知正在下载界面
|
|
|
|
|
postDownloadEvent(song);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
if (downloadTask != null) {
|
|
|
|
|
if (downloadTask!= null) {
|
|
|
|
|
// 已存在下载任务时的提示
|
|
|
|
|
CommonUtil.showToast(DownloadService.this, "已经加入下载队列");
|
|
|
|
|
} else {
|
|
|
|
|
// 开始下载的提示
|
|
|
|
|
CommonUtil.showToast(DownloadService.this, "开始下载");
|
|
|
|
|
start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 暂停下载方法
|
|
|
|
|
public void pauseDownload(String songId) {
|
|
|
|
|
//暂停的歌曲是否为当前下载的歌曲
|
|
|
|
|
if (downloadTask != null &&downloadQueue.peek().getSongId().equals(songId)) {
|
|
|
|
|
// 判断暂停的歌曲是否为当前正在下载的歌曲
|
|
|
|
|
if (downloadTask!= null && downloadQueue.peek().getSongId().equals(songId)) {
|
|
|
|
|
// 暂停当前下载任务
|
|
|
|
|
downloadTask.pauseDownload();
|
|
|
|
|
}else {//暂停的歌曲是下载队列的歌曲
|
|
|
|
|
//将该歌曲从下载队列中移除
|
|
|
|
|
} else {
|
|
|
|
|
// 暂停的歌曲在下载队列中
|
|
|
|
|
for (int i = 0; i < downloadQueue.size(); i++) {
|
|
|
|
|
DownloadInfo downloadInfo = downloadQueue.get(i);
|
|
|
|
|
if (downloadInfo.getSongId().equals(songId)) {
|
|
|
|
|
// 从下载队列中移除该歌曲
|
|
|
|
|
downloadQueue.remove(i);
|
|
|
|
|
// 更新数据库中该歌曲的暂停状态
|
|
|
|
|
updateDbOfPause(downloadInfo.getSongId());
|
|
|
|
|
// 更新下载信息状态为暂停,并发布暂停事件
|
|
|
|
|
downloadInfo.setStatus(Constant.DOWNLOAD_PAUSED);
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(Constant.TYPE_DOWNLOAD_PAUSED, downloadInfo)); //下载暂停
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(Constant.TYPE_DOWNLOAD_PAUSED, downloadInfo));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 取消下载方法
|
|
|
|
|
public void cancelDownload(DownloadInfo song) {
|
|
|
|
|
String songId = song.getSongId();
|
|
|
|
|
//如果该歌曲正在下载,则需要将downloadTask置为null
|
|
|
|
|
if (downloadTask != null && downloadQueue.peek().getSongId().equals(songId)) {
|
|
|
|
|
// 如果该歌曲正在下载,取消下载任务
|
|
|
|
|
if (downloadTask!= null && downloadQueue.peek().getSongId().equals(songId)) {
|
|
|
|
|
downloadTask.cancelDownload();
|
|
|
|
|
}
|
|
|
|
|
//将该歌曲从下载队列中移除
|
|
|
|
|
// 将该歌曲从下载队列中移除
|
|
|
|
|
for (int i = 0; i < downloadQueue.size(); i++) {
|
|
|
|
|
DownloadInfo downloadInfo = downloadQueue.get(i);
|
|
|
|
|
if (downloadInfo.getSongId().equals(songId)) downloadQueue.remove(i);
|
|
|
|
|
}
|
|
|
|
|
// 更新数据库
|
|
|
|
|
updateDb(songId);
|
|
|
|
|
// 删除数据库中的该歌曲信息
|
|
|
|
|
deleteDb(songId);
|
|
|
|
|
//取消下载需要将文件删除并将通知关闭
|
|
|
|
|
if (song.getUrl() != null) {
|
|
|
|
|
checkoutFile(song,song.getUrl()); //实际文件长度
|
|
|
|
|
// 取消下载需要将文件删除并关闭通知
|
|
|
|
|
if (song.getUrl()!= null) {
|
|
|
|
|
checkoutFile(song, song.getUrl());
|
|
|
|
|
}
|
|
|
|
|
//通知正在下载列表
|
|
|
|
|
// 发布下载取消事件
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(Constant.TYPE_DOWNLOAD_CANCELED));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 开始下载的方法
|
|
|
|
|
private void start() {
|
|
|
|
|
if (downloadTask == null && !downloadQueue.isEmpty()) {
|
|
|
|
|
if (downloadTask == null &&!downloadQueue.isEmpty()) {
|
|
|
|
|
// 从下载队列中取出要下载的歌曲信息
|
|
|
|
|
DownloadInfo downloadInfo = downloadQueue.peek();
|
|
|
|
|
// 从数据库中查找该歌曲信息
|
|
|
|
|
List<DownloadInfo> songList =
|
|
|
|
|
LitePal.where("songId = ?",downloadInfo.getSongId()).find(DownloadInfo.class);
|
|
|
|
|
LitePal.where("songId =?", downloadInfo.getSongId()).find(DownloadInfo.class);
|
|
|
|
|
DownloadInfo currentDownloadInfo = songList.get(0);
|
|
|
|
|
// 更新歌曲状态为准备下载
|
|
|
|
|
currentDownloadInfo.setStatus(Constant.DOWNLOAD_READY);
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(TYPE_DOWNLOADING,currentDownloadInfo));
|
|
|
|
|
// 发布下载事件
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(TYPE_DOWNLOADING, currentDownloadInfo));
|
|
|
|
|
downloadUrl = currentDownloadInfo.getUrl();
|
|
|
|
|
// 创建新的下载任务并执行
|
|
|
|
|
downloadTask = new DownloadTask(listener);
|
|
|
|
|
downloadTask.execute(currentDownloadInfo);
|
|
|
|
|
getNotificationManager().notify(1, getNotification("正在下载:"+downloadInfo.getSongName(), 0));
|
|
|
|
|
// 更新通知为正在下载
|
|
|
|
|
getNotificationManager().notify(1, getNotification("正在下载:" + downloadInfo.getSongName(), 0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取通知管理器
|
|
|
|
|
private NotificationManager getNotificationManager() {
|
|
|
|
|
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Notification getNotification(String title,int progress) {
|
|
|
|
|
// 创建通知的方法
|
|
|
|
|
private Notification getNotification(String title, int progress) {
|
|
|
|
|
// 创建跳转到主界面的意图
|
|
|
|
|
Intent intent = new Intent(this, MainActivity.class);
|
|
|
|
|
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
// 创建通知通道
|
|
|
|
|
String id = "channel_001";
|
|
|
|
|
String name = "下载通知";
|
|
|
|
|
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
|
|
|
|
|
@ -212,107 +272,132 @@ public class DownloadService extends Service {
|
|
|
|
|
builder.setSmallIcon(R.mipmap.icon);
|
|
|
|
|
builder.setContentIntent(pi);
|
|
|
|
|
builder.setContentTitle(title);
|
|
|
|
|
if(progress>0){
|
|
|
|
|
builder.setContentText(progress +"%");
|
|
|
|
|
if (progress > 0) {
|
|
|
|
|
builder.setContentText(progress + "%");
|
|
|
|
|
builder.setProgress(100, progress, false);
|
|
|
|
|
}
|
|
|
|
|
return builder.build();
|
|
|
|
|
} else {
|
|
|
|
|
// 兼容旧版本的通知创建
|
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default");
|
|
|
|
|
builder.setSmallIcon(R.mipmap.icon);
|
|
|
|
|
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.icon));
|
|
|
|
|
builder.setContentIntent(pi);
|
|
|
|
|
builder.setContentTitle(title);
|
|
|
|
|
if(progress>0){
|
|
|
|
|
builder.setContentText(progress +"%");
|
|
|
|
|
if (progress > 0) {
|
|
|
|
|
builder.setContentText(progress + "%");
|
|
|
|
|
builder.setProgress(100, progress, false);
|
|
|
|
|
}
|
|
|
|
|
return builder.build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 操作数据库的方法,可能是下载成功后的操作
|
|
|
|
|
private void operateDb(DownloadInfo downloadInfo) {
|
|
|
|
|
updateDb(downloadInfo.getSongId());
|
|
|
|
|
deleteDb(downloadInfo.getSongId());
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(Constant.TYPE_DOWNLOAD_SUCCESS));//通知已下载列表
|
|
|
|
|
EventBus.getDefault().post(new SongListNumEvent(Constant.LIST_TYPE_DOWNLOAD)); //通知主界面的下载个数需要改变
|
|
|
|
|
// 发布下载成功事件
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(Constant.TYPE_DOWNLOAD_SUCCESS));
|
|
|
|
|
// 发布歌曲列表数量改变事件
|
|
|
|
|
EventBus.getDefault().post(new SongListNumEvent(Constant.LIST_TYPE_DOWNLOAD));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//更新数据库中歌曲列表的位置,即下载完成歌曲后的位置都要减去1;
|
|
|
|
|
// 更新数据库中歌曲列表的位置
|
|
|
|
|
private void updateDb(String songId) {
|
|
|
|
|
long id = LitePal.select("id").where("songId = ?", songId).find(DownloadInfo.class).get(0).getId();
|
|
|
|
|
List<DownloadInfo> songIdList = LitePal.where("id > ?", id + "").find(DownloadInfo.class);
|
|
|
|
|
// 查找该歌曲在数据库中的 ID
|
|
|
|
|
long id = LitePal.select("id").where("songId =?", songId).find(DownloadInfo.class).get(0).getId();
|
|
|
|
|
// 查找该歌曲之后的歌曲列表
|
|
|
|
|
List<DownloadInfo> songIdList = LitePal.where("id >?", id + "").find(DownloadInfo.class);
|
|
|
|
|
for (DownloadInfo song : songIdList) {
|
|
|
|
|
// 更新位置,将后续歌曲位置减 1
|
|
|
|
|
song.setPosition(song.getPosition() - 1);
|
|
|
|
|
song.save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//暂停时更新列表歌曲状态
|
|
|
|
|
private void updateDbOfPause(String songId){
|
|
|
|
|
// 暂停时更新数据库中歌曲的状态
|
|
|
|
|
private void updateDbOfPause(String songId) {
|
|
|
|
|
List<DownloadInfo> statusList =
|
|
|
|
|
LitePal.where("songId = ?",songId).find(DownloadInfo.class,true);
|
|
|
|
|
LitePal.where("songId =?", songId).find(DownloadInfo.class, true);
|
|
|
|
|
DownloadInfo downloadInfo = statusList.get(0);
|
|
|
|
|
downloadInfo.setStatus(Constant.DOWNLOAD_PAUSED);
|
|
|
|
|
downloadInfo.save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//下载完成时要删除下载歌曲表中的数据以及关联表中的数据
|
|
|
|
|
// 下载完成时删除数据库中的数据
|
|
|
|
|
private void deleteDb(String songId) {
|
|
|
|
|
LitePal.deleteAll(DownloadInfo.class, "songId=?", songId);//删除已下载歌曲的相关列
|
|
|
|
|
// 删除已下载歌曲的相关列
|
|
|
|
|
LitePal.deleteAll(DownloadInfo.class, "songId=?", songId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发布下载事件
|
|
|
|
|
private void postDownloadEvent(DownloadInfo downloadInfo) {
|
|
|
|
|
//如果需要下载的表中有该条歌曲,则添加到下载队列后跳过
|
|
|
|
|
// 检查数据库中是否已存在该歌曲的下载信息
|
|
|
|
|
List<DownloadInfo> downloadInfoList =
|
|
|
|
|
LitePal.where("songId = ?",downloadInfo.getSongId()).find(DownloadInfo.class,true);
|
|
|
|
|
if (downloadInfoList.size() != 0){
|
|
|
|
|
LitePal.where("songId =?", downloadInfo.getSongId()).find(DownloadInfo.class, true);
|
|
|
|
|
if (downloadInfoList.size()!= 0) {
|
|
|
|
|
DownloadInfo historyDownloadInfo = downloadInfoList.get(0);
|
|
|
|
|
// 更新状态为等待
|
|
|
|
|
historyDownloadInfo.setStatus(Constant.DOWNLOAD_WAIT);
|
|
|
|
|
historyDownloadInfo.save();
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(Constant.DOWNLOAD_PAUSED,historyDownloadInfo));
|
|
|
|
|
// 发布暂停事件
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(Constant.DOWNLOAD_PAUSED, historyDownloadInfo));
|
|
|
|
|
// 将歌曲添加到下载队列
|
|
|
|
|
downloadQueue.offer(historyDownloadInfo);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置歌曲在下载列表中的位置
|
|
|
|
|
position = LitePal.findAll(DownloadInfo.class).size();
|
|
|
|
|
downloadInfo.setPosition(position);
|
|
|
|
|
downloadInfo.setStatus(Constant.DOWNLOAD_WAIT); //等待
|
|
|
|
|
downloadInfo.setStatus(Constant.DOWNLOAD_WAIT);
|
|
|
|
|
downloadInfo.save();
|
|
|
|
|
downloadQueue.offer(downloadInfo);//将歌曲放到等待队列中
|
|
|
|
|
// 将歌曲添加到等待队列
|
|
|
|
|
downloadQueue.offer(downloadInfo);
|
|
|
|
|
// 发布添加下载事件
|
|
|
|
|
EventBus.getDefault().post(new DownloadEvent(Constant.TYPE_DOWNLOAD_ADD));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//获取歌曲实际大小,然后判断是否存在于文件中
|
|
|
|
|
public void checkoutFile(DownloadInfo song, String downloadUrl){
|
|
|
|
|
OkHttpClient client = new OkHttpClient();
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(downloadUrl)
|
|
|
|
|
.build();
|
|
|
|
|
Call call= client.newCall(request);
|
|
|
|
|
call.enqueue(new Callback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onFailure(Call call, IOException e) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onResponse(Call call, Response response) throws IOException {
|
|
|
|
|
if(response.isSuccessful()){
|
|
|
|
|
long size = response.body().contentLength();
|
|
|
|
|
String fileName = DownloadUtil.getSaveSongFile(song.getSinger(),song.getSongName(),song.getDuration(),song.getSongId(),size);
|
|
|
|
|
File downloadFile = new File(Api.STORAGE_SONG_FILE);
|
|
|
|
|
String directory = String.valueOf(downloadFile);
|
|
|
|
|
File file = new File(fileName, directory);
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
file.delete();
|
|
|
|
|
}
|
|
|
|
|
getNotificationManager().cancel(1);
|
|
|
|
|
stopForeground(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// 获取歌曲实际大小,并判断文件是否存在,可能用于取消下载时的清理
|
|
|
|
|
public void checkoutFile(DownloadInfo song, String downloadUrl) {
|
|
|
|
|
// 创建 OkHttpClient 实例,用于网络请求
|
|
|
|
|
OkHttpClient client = new OkHttpClient();
|
|
|
|
|
// 构建请求对象,指定请求的 URL
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(downloadUrl)
|
|
|
|
|
.build();
|
|
|
|
|
// 创建一个 Call 对象,用于执行请求
|
|
|
|
|
Call call = client.newCall(request);
|
|
|
|
|
// 将请求加入请求队列,并设置回调
|
|
|
|
|
call.enqueue(new Callback() {
|
|
|
|
|
// 请求失败时调用
|
|
|
|
|
@Override
|
|
|
|
|
public void onFailure(Call call, IOException e) {
|
|
|
|
|
// 此处暂时未处理请求失败的情况,可以根据需求添加相应的处理逻辑
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// 请求成功时调用
|
|
|
|
|
@Override
|
|
|
|
|
public void onResponse(Call call, Response response) throws IOException {
|
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
|
// 获取响应体的内容长度,即文件的实际大小
|
|
|
|
|
long size = response.body().contentLength();
|
|
|
|
|
// 根据歌曲信息和文件大小生成保存歌曲的文件名
|
|
|
|
|
String fileName = DownloadUtil.getSaveSongFile(song.getSinger(), song.getSongName(), song.getDuration(), song.getSongId(), size);
|
|
|
|
|
// 获取存储歌曲文件的目录
|
|
|
|
|
File downloadFile = new File(Api.STORAGE_SONG_FILE);
|
|
|
|
|
String directory = String.valueOf(downloadFile);
|
|
|
|
|
// 构建文件对象
|
|
|
|
|
File file = new File(fileName, directory);
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
// 如果文件存在,删除文件
|
|
|
|
|
file.delete();
|
|
|
|
|
}
|
|
|
|
|
// 取消通知
|
|
|
|
|
getNotificationManager().cancel(1);
|
|
|
|
|
// 停止服务的前台运行状态
|
|
|
|
|
stopForeground(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|