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));
}
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();
}
}
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();
}
});
}
});
}
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();
}
});
}
});
}
Aggregations