Search in sources :

Example 1 with ProgressCallback

use of com.koushikdutta.ion.ProgressCallback in project ion by koush.

the class ProgressTests method testProgress.

public void testProgress() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final Semaphore progressSemaphore = new Semaphore(0);
    final Md5 md5 = Md5.createInstance();
    Ion.with(getContext()).load("https://raw.githubusercontent.com/koush/AndroidAsync/master/AndroidAsync/test/assets/6691924d7d24237d3b3679310157d640").setHandler(null).setTimeout(600000).setLogging("testProgress", Log.VERBOSE).progress(new ProgressCallback() {

        @Override
        public void onProgress(long downloaded, long total) {
            // depending on gzip, etc. the total may vary... the actual length of the uncompressed data
            // is 100000
            assertTrue(total > 90000 && total < 110000);
            progressSemaphore.release();
        }
    }).write(new ByteArrayOutputStream()).setCallback(new FutureCallback<ByteArrayOutputStream>() {

        @Override
        public void onCompleted(Exception e, ByteArrayOutputStream result) {
            byte[] bytes = result.toByteArray();
            md5.update(new ByteBufferList(bytes));
            assertEquals(md5.digest(), dataNameAndHash);
            semaphore.release();
        }
    });
    assertTrue(semaphore.tryAcquire(600000, TimeUnit.MILLISECONDS));
    assertTrue(progressSemaphore.tryAcquire(10000, TimeUnit.MILLISECONDS));
}
Also used : ByteBufferList(com.koushikdutta.async.ByteBufferList) ProgressCallback(com.koushikdutta.ion.ProgressCallback) Semaphore(java.util.concurrent.Semaphore) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with ProgressCallback

use of com.koushikdutta.ion.ProgressCallback in project ion by koush.

the class ProgressTests method testUpload.

public void testUpload() throws Exception {
    AsyncHttpServer httpServer = new AsyncHttpServer();
    try {
        httpServer.listen(Ion.getDefault(getContext()).getServer(), 5000);
        httpServer.post("/", new HttpServerRequestCallback() {

            @Override
            public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
                MultipartFormDataBody multipartFormDataBody = (MultipartFormDataBody) request.getBody();
                multipartFormDataBody.setMultipartCallback(new MultipartFormDataBody.MultipartCallback() {

                    @Override
                    public void onPart(Part part) {
                    }
                });
                multipartFormDataBody.setEndCallback(new CompletedCallback() {

                    @Override
                    public void onCompleted(Exception ex) {
                        response.send("Got parts!");
                    }
                });
            }
        });
        final Semaphore semaphore = new Semaphore(0);
        Ion.with(getContext()).load("http://localhost:5000/").uploadProgress(new ProgressCallback() {

            @Override
            public void onProgress(long downloaded, long total) {
                semaphore.release();
            }
        }).setMultipartParameter("foo", "bar").asString().get();
        assertTrue(semaphore.tryAcquire());
    } finally {
        Ion.getDefault(getContext()).getServer().stop();
    }
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) Part(com.koushikdutta.async.http.body.Part) ProgressCallback(com.koushikdutta.ion.ProgressCallback) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) Semaphore(java.util.concurrent.Semaphore) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Example 3 with ProgressCallback

use of com.koushikdutta.ion.ProgressCallback in project ion by koush.

the class ProgressBarDownload method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Enable global Ion logging
    Ion.getDefault(this).configure().setLogging("ion-sample", Log.DEBUG);
    setContentView(R.layout.progress);
    download = (Button) findViewById(R.id.download);
    downloadCount = (TextView) findViewById(R.id.download_count);
    progressBar = (ProgressBar) findViewById(R.id.progress);
    download.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (downloading != null && !downloading.isCancelled()) {
                resetDownload();
                return;
            }
            download.setText("Cancel");
            // this is a 180MB zip file to test with
            downloading = Ion.with(ProgressBarDownload.this).load("http://developer.clockworkmod.com/downloads/51/4883/cm-10.1-20130512-CPUFREQ-m7.zip").progressBar(progressBar).progressHandler(new ProgressCallback() {

                @Override
                public void onProgress(long downloaded, long total) {
                    downloadCount.setText("" + downloaded + " / " + total);
                }
            }).write(getFileStreamPath("zip-" + System.currentTimeMillis() + ".zip")).setCallback(new FutureCallback<File>() {

                @Override
                public void onCompleted(Exception e, File result) {
                    resetDownload();
                    if (e != null) {
                        Toast.makeText(ProgressBarDownload.this, "Error downloading file", Toast.LENGTH_LONG).show();
                        return;
                    }
                    Toast.makeText(ProgressBarDownload.this, "File upload complete", Toast.LENGTH_LONG).show();
                }
            });
        }
    });
}
Also used : ProgressCallback(com.koushikdutta.ion.ProgressCallback) TextView(android.widget.TextView) View(android.view.View) File(java.io.File)

Example 4 with ProgressCallback

use of com.koushikdutta.ion.ProgressCallback in project ion by koush.

the class ProgressBarUpload method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Enable global Ion logging
    Ion.getDefault(this).configure().setLogging("ion-sample", Log.DEBUG);
    setContentView(R.layout.progress_upload);
    upload = (Button) findViewById(R.id.upload);
    uploadCount = (TextView) findViewById(R.id.upload_count);
    progressBar = (ProgressBar) findViewById(R.id.progress);
    upload.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (uploading != null && !uploading.isCancelled()) {
                resetUpload();
                return;
            }
            File f = getFileStreamPath("largefile");
            try {
                RandomAccessFile rf = new RandomAccessFile(f, "rw");
                rf.setLength(1024 * 1024 * 2);
            } catch (Exception e) {
                System.err.println(e);
            }
            File echoedFile = getFileStreamPath("echo");
            upload.setText("Cancel");
            // this is a 180MB zip file to test with
            uploading = Ion.with(ProgressBarUpload.this).load("http://koush.clockworkmod.com/test/echo").uploadProgressBar(progressBar).uploadProgressHandler(new ProgressCallback() {

                @Override
                public void onProgress(long downloaded, long total) {
                    uploadCount.setText("" + downloaded + " / " + total);
                }
            }).setMultipartFile("largefile", f).write(echoedFile).setCallback(new FutureCallback<File>() {

                @Override
                public void onCompleted(Exception e, File result) {
                    resetUpload();
                    if (e != null) {
                        Toast.makeText(ProgressBarUpload.this, "Error uploading file", Toast.LENGTH_LONG).show();
                        return;
                    }
                    Toast.makeText(ProgressBarUpload.this, "File upload complete", Toast.LENGTH_LONG).show();
                }
            });
        }
    });
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ProgressCallback(com.koushikdutta.ion.ProgressCallback) TextView(android.widget.TextView) View(android.view.View) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

ProgressCallback (com.koushikdutta.ion.ProgressCallback)4 View (android.view.View)2 TextView (android.widget.TextView)2 File (java.io.File)2 Semaphore (java.util.concurrent.Semaphore)2 ByteBufferList (com.koushikdutta.async.ByteBufferList)1 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)1 Part (com.koushikdutta.async.http.body.Part)1 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)1 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)1 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)1 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1