use of android.app.NotificationManager in project SimplifyReader by chentao0707.
the class DownloadManager method deleteDownloadeds.
public boolean deleteDownloadeds(final ArrayList<DownloadInfo> infos) {
Logger.d(TAG, "deleteDownloadeds() : ArrayList");
if (infos == null || infos.size() == 0)
return true;
String nId = YoukuPlayerApplication.getPreference(KEY_LAST_NOTIFY_TASKID);
for (DownloadInfo info : infos) {
downloadedData.remove(info.videoid);
if (nId.equals(info.taskId)) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NOTIFY_ID);
YoukuPlayerApplication.savePreference(KEY_LAST_NOTIFY_TASKID, "");
}
}
new Thread() {
public void run() {
for (DownloadInfo info : infos) {
PlayerUtil.deleteFile(new File(info.savePath));
}
}
;
}.start();
return true;
}
use of android.app.NotificationManager in project SimplifyReader by chentao0707.
the class FileDownloadThread method downloadSegment.
/**
* 下载分片
*
* @param info
* @return true下载完成;false下载失败或取消下载
*/
private boolean downloadSegment(DownloadInfo info) {
Logger.d("DownloadFlow", "FileDownloadThread: downloadSegment()");
File f = checkAndGetFile(info);
if (f == null) {
cancel = true;
info.setState(DownloadInfo.STATE_EXCEPTION);
return false;
}
// 当前分片的结束位置
final long endPosition = info.segsSize[info.segId - 1];
// 当前分片的下载进度位置
long curPosition = info.segDownloadedSize;
if (curPosition >= endPosition) {
// 当前分片已下载完成
return true;
}
InputStream is = getInputStreamFromURL(info, download.canUseAcc());
if (is == null) {
cancel = true;
info.setState(DownloadInfo.STATE_EXCEPTION);
return false;
}
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(new FileOutputStream(f, true));
int len = 0;
byte[] buf = new byte[BUFFER_SIZE];
while (cancel == false && curPosition < endPosition && info.getState() == DownloadInfo.STATE_DOWNLOADING && (len = bis.read(buf, 0, BUFFER_SIZE)) != -1 && cancel == false && info.getState() == DownloadInfo.STATE_DOWNLOADING) {
// 因为read()是耗时操作,所以需要二次判断
bos.write(buf, 0, len);
curPosition += len;
if (curPosition > endPosition) {
Logger.d(TAG, "curPosition > endPosition,curPosition:" + curPosition + ",endPosition:" + endPosition);
info.segDownloadedSize += (len - (curPosition - endPosition) + 1);
info.downloadedSize += (len - (curPosition - endPosition) + 1);
} else {
info.segDownloadedSize = curPosition;
info.downloadedSize += len;
}
info.setProgress(((double) info.downloadedSize * 100) / info.size);
if (info.retry != 0)
info.retry = 0;
while (pause) {
try {
sleep(500L);
} catch (InterruptedException e) {
}
}
}
if (curPosition >= endPosition)
return true;
} catch (SocketTimeoutException e) {
Logger.e("DownloadFlow", "FileDownloadThread: downloadSegment(): " + e.toString());
Logger.e(TAG, e);
if (info.getState() != DownloadInfo.STATE_PAUSE && info.getState() != DownloadInfo.STATE_CANCEL) {
if (Util.hasInternet()) {
info.setExceptionId(DownloadInfo.EXCEPTION_TIMEOUT);
if (info.retry == 0) {
PlayerUtil.showTips(info.getExceptionInfo());
}
} else {
info.setExceptionId(DownloadInfo.EXCEPTION_NO_NETWORK);
}
cancel = true;
info.setState(DownloadInfo.STATE_EXCEPTION);
}
} catch (SocketException e) {
Logger.e("DownloadFlow", "FileDownloadThread: downloadSegment(): " + e.toString());
Logger.e(TAG, e);
if (info.getState() != DownloadInfo.STATE_PAUSE && info.getState() != DownloadInfo.STATE_CANCEL) {
if (Util.hasInternet()) {
info.setExceptionId(DownloadInfo.EXCEPTION_TIMEOUT);
if (info.retry == 0) {
PlayerUtil.showTips(info.getExceptionInfo());
}
} else {
info.setExceptionId(DownloadInfo.EXCEPTION_NO_NETWORK);
}
cancel = true;
info.setState(DownloadInfo.STATE_EXCEPTION);
}
} catch (FileNotFoundException e) {
// SD卡被拔出
Logger.e("DownloadFlow", "FileDownloadThread: downloadSegment(): " + e.toString());
Logger.e(TAG, e);
NotificationManager nm = (NotificationManager) YoukuPlayerApplication.context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(IDownload.NOTIFY_ID);
} catch (IOException e) {
Logger.e("DownloadFlow", "FileDownloadThread: downloadSegment(): " + e.toString());
Logger.e(TAG, e);
if (info.getState() != DownloadInfo.STATE_PAUSE && info.getState() != DownloadInfo.STATE_CANCEL) {
// if (info.createTime < DownloadManager222.deleteAllTimestamp)
// {
// 如果删除后立马又创建了该视频,则需要重新删掉
// download.deleteCache(info);
// } else {
String[] temp = info.savePath.split(YoukuPlayerApplication.getDownloadPath());
SDCardManager m = new SDCardManager(temp[0]);
if (!m.exist()) {
info.setExceptionId(DownloadInfo.EXCEPTION_NO_SDCARD);
PlayerUtil.showTips(info.getExceptionInfo());
} else if (m.getFreeSize() - info.size <= 0) {
info.setExceptionId(DownloadInfo.EXCEPTION_NO_SPACE);
PlayerUtil.showTips(info.getExceptionInfo());
}
cancel = true;
info.setState(DownloadInfo.STATE_EXCEPTION);
// }
}
} finally {
try {
if (bos != null)
bos.close();
if (bis != null)
bis.close();
if (is != null)
is.close();
} catch (IOException e) {
}
}
return false;
}
use of android.app.NotificationManager in project SimplifyReader by chentao0707.
the class DownloadManager method deleteAllDownloaded.
@SuppressWarnings({ "rawtypes", "unchecked" })
public boolean deleteAllDownloaded() {
Logger.d(TAG, "deleteAllDownloaded()");
if (getDownloadedData().size() == 0)
return true;
final HashMap<String, DownloadInfo> clone = (HashMap<String, DownloadInfo>) getDownloadedData().clone();
String nId = YoukuPlayerApplication.getPreference(KEY_LAST_NOTIFY_TASKID);
// 获得map的Iterator
Iterator iter = getDownloadedData().entrySet().iterator();
while (iter.hasNext()) {
Entry entry = (Entry) iter.next();
DownloadInfo info = (DownloadInfo) entry.getValue();
if (nId.equals(info.taskId)) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NOTIFY_ID);
YoukuPlayerApplication.savePreference(KEY_LAST_NOTIFY_TASKID, "");
}
}
new Thread() {
public void run() {
Iterator iter = clone.entrySet().iterator();
while (iter.hasNext()) {
Entry entry = (Entry) iter.next();
DownloadInfo info = (DownloadInfo) entry.getValue();
PlayerUtil.deleteFile(new File(info.savePath));
}
}
;
}.start();
getDownloadedData().clear();
return true;
}
use of android.app.NotificationManager in project SimplifyReader by chentao0707.
the class DownloadManager method deleteDownloaded.
public boolean deleteDownloaded(final DownloadInfo info) {
Logger.d(TAG, "deleteDownloaded() :" + info.title);
// info.setState(DownloadInfo.STATE_CANCEL);
downloadedData.remove(info.videoid);
if (YoukuPlayerApplication.getPreference(KEY_LAST_NOTIFY_TASKID).equals(info.taskId)) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NOTIFY_ID);
YoukuPlayerApplication.savePreference(KEY_LAST_NOTIFY_TASKID, "");
}
new Thread() {
public void run() {
PlayerUtil.deleteFile(new File(info.savePath));
}
;
}.start();
startNewTask();
return true;
}
use of android.app.NotificationManager in project SimplifyReader by chentao0707.
the class DownloadServiceManager method deleteDownloading.
@Override
public boolean deleteDownloading(String taskId) {
final DownloadInfo info = getDownloadingData().get(taskId);
// 如果是下载中的视频,改变状态自己就会删除
info.setState(DownloadInfo.STATE_CANCEL);
downloadingData.remove(taskId);
if (thread != null && thread.isStop() == false && taskId.equals(thread.getTaskId())) {
thread.cancel();
}
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (YoukuPlayerApplication.getPreference(KEY_LAST_NOTIFY_TASKID).equals(info.taskId)) {
nm.cancel(NOTIFY_ID);
YoukuPlayerApplication.savePreference(KEY_LAST_NOTIFY_TASKID, "");
}
new Thread() {
public void run() {
PlayerUtil.deleteFile(new File(info.savePath));
}
;
}.start();
startNewTask();
return true;
}
Aggregations