@Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); while (info.redirect < MAX_REDIRECTS) { HttpURLConnection conn = null; try { conn = (HttpURLConnection) new URL(info.realUrl).openConnection(); conn.setInstanceFollowRedirects(false); conn.setConnectTimeout(DEFAULT_TIMEOUT); conn.setReadTimeout(DEFAULT_TIMEOUT); addRequestHeaders(conn); final int code = conn.getResponseCode(); switch (code) { case HTTP_OK: case HTTP_PARTIAL: dlInit(conn, code); return; case HTTP_MOVED_PERM: case HTTP_MOVED_TEMP: case HTTP_SEE_OTHER: case HTTP_NOT_MODIFIED: case HTTP_TEMP_REDIRECT: final String location = conn.getHeaderField("location"); if (TextUtils.isEmpty(location)) throw new DLException("Can not obtain real url from location in header."); info.realUrl = location; info.redirect++; continue; default: if (info.hasListener) info.listener.onError(code, conn.getResponseMessage(), info); EventBus.getDefault().post(info, "onError"); DLManager.getInstance(context).removeDLTask(info.baseUrl); info.state = DLState.FAIL; try { mDLInfoDao.update(info); } catch (SQLException e1) { e1.printStackTrace(); } return; } } catch (Exception e) { if (info.hasListener) info.listener.onError(ERROR_OPEN_CONNECT, e.toString(), info); EventBus.getDefault().post(info, "onError"); DLManager.getInstance(context).removeDLTask(info.baseUrl); info.state = DLState.FAIL; try { mDLInfoDao.update(info); } catch (SQLException e1) { e1.printStackTrace(); } return; } finally { if (null != conn) conn.disconnect(); } } throw new RuntimeException("Too many redirects"); }
@Override public synchronized void onStop(DLThreadInfo threadInfo) { // DLDBManager.getInstance(context).updateThreadInfo(threadInfo); try { mDLThreadInfoDao.update(threadInfo); } catch (SQLException e) { e.printStackTrace(); } count++; if (count >= info.threads.size()) { LogUtils.e("All the threads was stopped."); info.currentBytes = totalProgress; info.state = DLState.PAUSE; DLManager.getInstance(context).addStopTask(info).removeDLTask(info.baseUrl); // DLDBManager.getInstance(context).updateTaskInfo(info); try { mDLInfoDao.update(info); } catch (SQLException e) { e.printStackTrace(); } count = 0; if (info.hasListener) info.listener.onStop(info); EventBus.getDefault().post(info, "onStop"); } }
@Override public synchronized void onFinish(DLThreadInfo threadInfo) { LogUtils.e("已经完成:" + info.appName + " 进度:" + info.progress + " " + threadInfo); if (null == threadInfo) { if (info.hasListener) { try { info.state = DLState.COMPLETE; mDLInfoDao.update(info); } catch (SQLException e) { e.printStackTrace(); } info.listener.onProgress(info); info.listener.onFinish(info); } EventBus.getDefault().post(info, "onProgress"); EventBus.getDefault().post(info, "onFinish"); return; } info.removeDLThread(threadInfo); // DLDBManager.getInstance(context).deleteThreadInfo(threadInfo.id); try { mDLThreadInfoDao.delete(DBCons.TB_THREAD_ID, threadInfo.id); } catch (SQLException e) { e.printStackTrace(); } LogUtils.e("Thread size " + info.threads.size()); if (info.threads.isEmpty()) { LogUtils.e("Task was finished."); DLManager.getInstance(context).removeDLTask(info.baseUrl); // DLDBManager.getInstance(context).deleteTaskInfo(info.baseUrl); try { info.state = DLState.COMPLETE; mDLInfoDao.update(info); } catch (SQLException e) { e.printStackTrace(); } LogUtils.e("info.hasListener=" + info.hasListener); if (info.hasListener) { info.listener.onProgress(info); info.listener.onFinish(info); } EventBus.getDefault().post(info, "onProgress"); EventBus.getDefault().post(info, "onFinish"); DLManager.getInstance(context).addDLTask(); } }