/** * @param item * @param messageState -1 不发送信息 */ private void updateDownloadState(DownloadItem item, int messageState) { if (null == item) { return; } String url = item.url; switch (item.state) { case DownloadItem.DOWNLOAD_STATE_FINISH: DownDB.getInstance(App.getContext()) .upDate(item.name, url, "" + "", DownloadItem.DOWNLOAD_STATE_FINISH); FileUitls.moveFile(url, item.name, App.getContext()); if (DownloadingItems.contains(url)) { DownloadingItems.remove(url); } if (pauseItems.contains(url)) { pauseItems.remove(url); } break; case DownloadItem.DOWNLOAD_STATE_ERROR: DownDB.getInstance(App.getContext()).delItem(item.url); FileUitls.delFile(item.url, App.getContext()); if (DownloadingItems.contains(url)) { DownloadingItems.remove(url); } if (pauseItems.contains(url)) { pauseItems.remove(url); } break; } if (messageState != -1) { Message message = new Message(); message.what = messageState; message.obj = item; mHandler.sendMessage(message); } DownLoadManager.sendDownloadUpdateBroadCast(item); }
private void downLoadDatas(String urlStr, String name) { if (pauseItems.contains(urlStr)) { pauseItems.remove(urlStr); L.i(this, "pause delet ", urlStr, name); } L.i(this, "before download", DownloadingItems.toString()); if (DownloadingItems.contains(urlStr)) { L.i(this, "is download", urlStr, name); return; } L.i(this, "start download ", urlStr, name); DownloadingItems.add(urlStr); URL url = null; try { url = new URL(urlStr); } catch (MalformedURLException e) { e.printStackTrace(); L.e(this, "url", "error", urlStr, name); DownLoadManager.sendDownloadBroadCast(urlStr, DownLoadManager.ERROR_URL); return; } HttpURLConnection urlConnection = null; try { urlConnection = (HttpURLConnection) url.openConnection(); } catch (IOException e) { L.e(this, "url", "openConnection error", urlStr, name); e.printStackTrace(); DownLoadManager.sendDownloadBroadCast(urlStr, DownLoadManager.ERROR_NETWORK); return; } urlConnection.setConnectTimeout(8000); urlConnection.setReadTimeout(8000); long startRange = 0; long currentSize = 0; File file = FileUitls.getFile(urlStr, this); if (file.exists()) { startRange = file.length(); } else { startRange = 0; } urlConnection.setRequestProperty("Range", "bytes=" + startRange + "-"); float fileSize = 0; DownloadItem downLoadDatas = DownDB.getInstance(App.getContext()).selectItem(urlStr); if (startRange == 0) { fileSize = urlConnection.getContentLength(); if (null == downLoadDatas) { if (startRange == 0) { DownDB.getInstance(App.getContext()) .InsetData(name, urlStr, fileSize + "", DownloadItem.DOWNLOAD_STATE_NOTINT); } } } else { downLoadDatas = DownDB.getInstance(App.getContext()).selectItem(urlStr); fileSize = Float.parseFloat( TextUtils.isEmpty(downLoadDatas.totalSize) ? "0" : downLoadDatas.totalSize); } InputStream inputStream = null; try { inputStream = urlConnection.getInputStream(); } catch (IOException e) { e.printStackTrace(); L.e(this, "network error", urlStr, name); DownLoadManager.sendDownloadBroadCast(urlStr, DownLoadManager.ERROR_NETWORK); return; } byte[] b = new byte[1024]; int lenth = 0; int shouldNotifySize = 0; FileOutputStream fileout = null; try { fileout = new FileOutputStream(file, true); } catch (FileNotFoundException e) { e.printStackTrace(); L.e("download", " ERROR_FILE_NOFIND", urlStr, name); DownLoadManager.sendDownloadBroadCast(urlStr, DownLoadManager.ERROR_FILE_NOFIND); } L.i("download", "download begin"); DownLoadManager.sendDownloadBroadCast(urlStr, DownLoadManager.DOWNLOAD_BEGIN); try { while (null != inputStream && (lenth = inputStream.read(b)) != -1) { if (file.exists() && file.length() != 0 && file.length() < currentSize) { L.i("TAG", file.length() + ":" + currentSize + "", urlStr, name); L.i(this, file.length() + ":" + currentSize + "", "redownload"); downLoadDatas.state = DownloadItem.DOWNLOAD_STATE_ERROR; updateDownloadState(downLoadDatas, DOWNLOAD_FAIL); downLoadDatas(urlStr, name); return; } currentSize += lenth; fileout.write(b, 0, lenth); fileout.flush(); shouldNotifySize += lenth; if (pauseItems.contains(urlStr)) { fileout.close(); downLoadDatas.state = DownloadItem.DOWNLOAD_STATE_PAUSE; updateDownloadState(downLoadDatas, -1); return; } if (shouldNotifySize * 100 / fileSize >= 5) { shouldNotifySize = 0; downLoadDatas.currentSize = file.length() + ""; downLoadDatas.state = DownloadItem.DOWNLOAD_STATE_DOING; downLoadDatas.downloadPercent = (int) (((double) file.length() / fileSize) * 100); updateDownloadState(downLoadDatas, DOWNLOAD_UPDATE); } file = FileUitls.getFile(urlStr, this); } } catch (IOException e) { e.printStackTrace(); downLoadDatas.state = DownloadItem.DOWNLOAD_STATE_ERROR; updateDownloadState(downLoadDatas, DOWNLOAD_FAIL); L.e("TAG", urlStr, name, "redownload"); downLoadDatas(urlStr, name); return; } try { fileout.close(); } catch (IOException e) { e.printStackTrace(); L.e(this, "close error", urlStr, name); } if (file.exists()) { if (file.length() != 0 && file.length() < currentSize) { downLoadDatas.state = DownloadItem.DOWNLOAD_STATE_ERROR; updateDownloadState(downLoadDatas, DOWNLOAD_FAIL); L.e("TAG", urlStr, name, "redownload"); downLoadDatas(urlStr, name); return; } /** finsh donwload */ if (fileSize == file.length()) { L.i(this, "fileSize == file.length()DOWNLOAD_STATE_FINISH", urlStr, name); int downloadPercent = (int) (((double) file.length() / fileSize) * 100); downLoadDatas.state = DownloadItem.DOWNLOAD_STATE_FINISH; downLoadDatas.downloadPercent = downloadPercent; updateDownloadState(downLoadDatas, DOWNLOAD_COMPLETE); } else { downLoadDatas.state = DownloadItem.DOWNLOAD_STATE_ERROR; updateDownloadState(downLoadDatas, DOWNLOAD_FAIL); L.e("TAG", urlStr, name, "redownload"); downLoadDatas(downLoadDatas.url, downLoadDatas.name); } } L.i("download", "download success", urlStr, name); }