@Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.downloader); _downloadingText = (TextView) findViewById(R.id.downloadDownloadingText); _infoText = (TextView) findViewById(R.id.downloadInfoText); _progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar); _progressText = (TextView) findViewById(R.id.downloadProgressText); _progressBar.setMax(100); _handler = new DownloadMessageHandler(this); final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); _wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, DownloadActivity.class.getName()); scheduleDownloadingTextAnimation(); final Intent intent = getIntent(); final EpisodeModel episode = (EpisodeModel) intent.getSerializableExtra(getString(R.string.key_episode)); final ProgrammeModel programme = episode.getProgramme(); final String programmeName = programme.getName(); final String name = episode.getName(); final Date date = episode.getDate(); final String dateStr = DATE_FORMAT.format(date); setTitle(getString(R.string.download_title) + " - " + programmeName); _infoText.setText(programmeName + " " + dateStr + "\n" + name); new Downloader(this, episode, 10, _wifiLock, _handler).start(); }
@Override public void run() { _wifiLock.acquire(); final String asxUrl = _episode.getAsxUrl(); AsxModel asx; try { asx = AsxModel.createModelFromUrl(asxUrl); } catch (IOException e) { e.printStackTrace(); _handler.sendDownloadFailedMessage(); return; } final List<AsxEntryModel> entries = asx.getEntries(); final String[] playlist = new String[entries.size()]; for (int i = 0; i < playlist.length; i++) { final AsxEntryModel entry = entries.get(i); final String ref = entry.getRef(); playlist[i] = ref; } long totalSize = 10L; for (final String url : playlist) { try { final MMSInputStream mmsStream = new MMSInputStream(url); totalSize += mmsStream.getSize(); mmsStream.close(); } catch (final Exception e) { Log.e(TAG, e.getMessage()); _handler.sendDownloadFailedMessage(); _wifiLock.release(); return; } } _handler.sendTotalSizeUpdateMessage(totalSize); for (final String url : playlist) { _threadFinishedCount = 0; try { final MMSInputStream mmsStream = new MMSInputStream(url); final long size = mmsStream.getSize(); final double length = mmsStream.getLength(); final List<FileOutputStream> files; final String filePath; try { final String externalStorageState = Environment.getExternalStorageState(); if (externalStorageState.equals(Environment.MEDIA_REMOVED)) { _handler.sendDownloadFailedMessage(); _wifiLock.release(); mmsStream.close(); return; } final File sdCardRoot = Environment.getExternalStorageDirectory(); final String sdCardPath = sdCardRoot.getPath(); final File rthkFolder = new File(sdCardPath + "/RthkArchivePlayer"); if (!rthkFolder.exists()) { rthkFolder.mkdirs(); } final String rthkPath = rthkFolder.getPath(); final String filename = URLEncoder.encode(url, "utf-8"); filePath = rthkPath + "/" + filename; files = new ArrayList<FileOutputStream>(_threadNum); String ext = ""; for (int i = 0; i < _threadNum; i++, ext = ".part" + i) { final FileOutputStream stream = new FileOutputStream(filePath + ext); files.add(stream); } } catch (final Exception e) { Log.e(TAG, e.getMessage()); _handler.sendDownloadFailedMessage(); _wifiLock.release(); return; } int streamNum = 0; final long stepSize = size / _threadNum; long startPos = -1L; long readCap = -1L; DownloadThread downloadThread = null; for (final FileOutputStream file : files) { final MMSInputStream stream; if (streamNum == 0) { stream = mmsStream; startPos = 0L; } else { stream = new MMSInputStream(url); final long pos = stream.seekByte(stepSize * streamNum); readCap = pos - startPos; downloadThread.setReadCapacity(readCap); downloadThread.start(); startPos = pos; } downloadThread = new DownloadThread(stream, file, streamNum); streamNum++; if (streamNum == _threadNum) { downloadThread.setReadCapacity(size * 2); downloadThread.start(); } } synchronized (this) { while (_threadFinishedCount < _threadNum) { try { wait(); } catch (final InterruptedException e) { e.printStackTrace(); } } } final FileOutputStream baseFile = new FileOutputStream(filePath, true); ; for (int i = 1; i < _threadNum; i++) { final String path = filePath + ".part" + i; try { final File file = new File(path); final FileInputStream inputStream = new FileInputStream(file); final byte[] buffer = new byte[1024]; int read = 0; while ((read = inputStream.read(buffer)) > 0) { baseFile.write(buffer, 0, read); } inputStream.close(); file.delete(); } catch (final IOException e) { Log.e(TAG, e.getMessage()); } } baseFile.close(); final AsfFileDbAdapter dbAdapter = new AsfFileDbAdapter(_context); dbAdapter.open(); dbAdapter.createFile(url, filePath, length, size); dbAdapter.close(); } catch (final IOException e) { Log.e(TAG, e.getMessage()); _handler.sendDownloadFailedMessage(); _wifiLock.release(); return; } } _wifiLock.release(); _handler.sendDownloadSuccessMessage(); }