Search in sources :

Example 1 with ProgressListener

use of org.fdroid.fdroid.ProgressListener in project fdroidclient by f-droid.

the class DownloaderService method handleIntent.

/**
 * This method is invoked on the worker thread with a request to process.
 * Only one Intent is processed at a time, but the processing happens on a
 * worker thread that runs independently from other application logic.
 * So, if this code takes a long time, it will hold up other requests to
 * the same DownloaderService, but it will not hold up anything else.
 * When all requests have been handled, the DownloaderService stops itself,
 * so you should not ever call {@link #stopSelf}.
 * <p/>
 * Downloads are put into subdirectories based on hostname/port of each repo
 * to prevent files with the same names from conflicting.  Each repo enforces
 * unique APK file names on the server side.
 *
 * @param intent The {@link Intent} passed via {@link
 *               android.content.Context#startService(Intent)}.
 * @see org.fdroid.fdroid.IndexV1Updater#update()
 */
private void handleIntent(Intent intent) {
    final Uri uri = intent.getData();
    final long repoId = intent.getLongExtra(Downloader.EXTRA_REPO_ID, 0);
    final Uri canonicalUrl = Uri.parse(intent.getStringExtra(Downloader.EXTRA_CANONICAL_URL));
    final SanitizedFile localFile = ApkCache.getApkDownloadPath(this, canonicalUrl);
    sendBroadcast(uri, Downloader.ACTION_STARTED, localFile, repoId, canonicalUrl);
    try {
        activeCanonicalUrl = canonicalUrl.toString();
        downloader = DownloaderFactory.create(this, uri, localFile);
        downloader.setListener(new ProgressListener() {

            @Override
            public void onProgress(long bytesRead, long totalBytes) {
                Intent intent = new Intent(Downloader.ACTION_PROGRESS);
                intent.setData(canonicalUrl);
                intent.putExtra(Downloader.EXTRA_BYTES_READ, bytesRead);
                intent.putExtra(Downloader.EXTRA_TOTAL_BYTES, totalBytes);
                localBroadcastManager.sendBroadcast(intent);
            }
        });
        downloader.setTimeout(timeout);
        downloader.download();
        if (downloader.isNotFound()) {
            sendBroadcast(uri, Downloader.ACTION_INTERRUPTED, localFile, getString(R.string.download_404), repoId, canonicalUrl);
        } else {
            sendBroadcast(uri, Downloader.ACTION_COMPLETE, localFile, repoId, canonicalUrl);
        }
    } catch (InterruptedException e) {
        sendBroadcast(uri, Downloader.ACTION_INTERRUPTED, localFile, repoId, canonicalUrl);
    } catch (ConnectException | HttpRetryException | NoRouteToHostException | SocketTimeoutException | SSLHandshakeException | SSLKeyException | SSLPeerUnverifiedException | SSLProtocolException | ProtocolException | UnknownHostException e) {
        // if the above list of exceptions changes, also change it in IndexV1Updater.update()
        Log.e(TAG, "CONNECTION_FAILED: " + e.getLocalizedMessage());
        sendBroadcast(uri, Downloader.ACTION_CONNECTION_FAILED, localFile, repoId, canonicalUrl);
    } catch (IOException e) {
        e.printStackTrace();
        sendBroadcast(uri, Downloader.ACTION_INTERRUPTED, localFile, e.getLocalizedMessage(), repoId, canonicalUrl);
    } finally {
        if (downloader != null) {
            downloader.close();
        }
    }
    downloader = null;
    activeCanonicalUrl = null;
}
Also used : SSLProtocolException(javax.net.ssl.SSLProtocolException) ProtocolException(java.net.ProtocolException) UnknownHostException(java.net.UnknownHostException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) Intent(android.content.Intent) IOException(java.io.IOException) SSLKeyException(javax.net.ssl.SSLKeyException) Uri(android.net.Uri) HttpRetryException(java.net.HttpRetryException) NoRouteToHostException(java.net.NoRouteToHostException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SSLProtocolException(javax.net.ssl.SSLProtocolException) SocketTimeoutException(java.net.SocketTimeoutException) ProgressListener(org.fdroid.fdroid.ProgressListener) SanitizedFile(org.fdroid.fdroid.data.SanitizedFile) ConnectException(java.net.ConnectException)

Example 2 with ProgressListener

use of org.fdroid.fdroid.ProgressListener in project fdroidclient by f-droid.

the class HttpDownloaderTest method downloadThenCancel.

@Test
public void downloadThenCancel() throws IOException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(2);
    Uri uri = Uri.parse("https://f-droid.org/repo/index.jar");
    File destFile = File.createTempFile("dl-", "");
    final HttpDownloader httpDownloader = new HttpDownloader(uri, destFile);
    httpDownloader.setListener(new ProgressListener() {

        @Override
        public void onProgress(long bytesRead, long totalBytes) {
            receivedProgress = true;
            latch.countDown();
        }
    });
    new Thread() {

        @Override
        public void run() {
            try {
                httpDownloader.download();
                fail();
            } catch (IOException e) {
                e.printStackTrace();
                fail();
            } catch (InterruptedException e) {
            // success!
            }
        }
    }.start();
    // either 2 progress reports or 100 seconds
    latch.await(100, TimeUnit.SECONDS);
    httpDownloader.cancelDownload();
    assertTrue(receivedProgress);
    destFile.deleteOnExit();
}
Also used : ProgressListener(org.fdroid.fdroid.ProgressListener) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) File(java.io.File) Test(org.junit.Test)

Example 3 with ProgressListener

use of org.fdroid.fdroid.ProgressListener in project fdroidclient by f-droid.

the class HttpDownloaderTest method downloadUninterruptedTestWithProgress.

@Test
public void downloadUninterruptedTestWithProgress() throws IOException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    String urlString = "https://f-droid.org/repo/index.jar";
    receivedProgress = false;
    Uri uri = Uri.parse(urlString);
    File destFile = File.createTempFile("dl-", "");
    final HttpDownloader httpDownloader = new HttpDownloader(uri, destFile);
    httpDownloader.setListener(new ProgressListener() {

        @Override
        public void onProgress(long bytesRead, long totalBytes) {
            receivedProgress = true;
        }
    });
    new Thread() {

        @Override
        public void run() {
            try {
                httpDownloader.download();
                latch.countDown();
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
                fail();
            }
        }
    }.start();
    // either 2 progress reports or 100 seconds
    latch.await(100, TimeUnit.SECONDS);
    assertTrue(destFile.exists());
    assertTrue(destFile.canRead());
    assertTrue(receivedProgress);
    destFile.deleteOnExit();
}
Also used : ProgressListener(org.fdroid.fdroid.ProgressListener) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) File(java.io.File) Test(org.junit.Test)

Aggregations

Uri (android.net.Uri)3 ProgressListener (org.fdroid.fdroid.ProgressListener)3 File (java.io.File)2 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 Intent (android.content.Intent)1 ConnectException (java.net.ConnectException)1 HttpRetryException (java.net.HttpRetryException)1 NoRouteToHostException (java.net.NoRouteToHostException)1 ProtocolException (java.net.ProtocolException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 SSLKeyException (javax.net.ssl.SSLKeyException)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 SSLProtocolException (javax.net.ssl.SSLProtocolException)1 SanitizedFile (org.fdroid.fdroid.data.SanitizedFile)1