From 42294e533b6b3d82eb1feb17b9db97cd9369d3cd Mon Sep 17 00:00:00 2001 From: Schoumi Date: Tue, 6 Sep 2016 16:37:36 +0200 Subject: [PATCH] Change infinite progress bar to a real one that show the real progress of the upload Close #4 --- app/build.gradle | 14 ++--- .../java/fr/mobdev/goblim/NetworkManager.java | 59 +++++++++++++------ .../goblim/activity/UploadActivity.java | 12 ++++ .../goblim/listener/NetworkAdapter.java | 5 ++ .../goblim/listener/NetworkListener.java | 1 + build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 7 files changed, 69 insertions(+), 28 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index b3b8184..d8dba10 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,15 +1,15 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 23 - buildToolsVersion "23.0.2" + compileSdkVersion 24 + buildToolsVersion "24.0.1" defaultConfig { applicationId "fr.mobdev.goblim" minSdkVersion 15 - targetSdkVersion 23 - versionCode 3 - versionName "2.0" + targetSdkVersion 24 + versionCode 4 + versionName "2.1" } buildTypes { @@ -22,6 +22,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:23.1.1' - compile 'com.android.support:design:23.1.1' + compile 'com.android.support:appcompat-v7:24.2.0' + compile 'com.android.support:design:24.2.0' } diff --git a/app/src/main/java/fr/mobdev/goblim/NetworkManager.java b/app/src/main/java/fr/mobdev/goblim/NetworkManager.java index 3c03f5b..e2f7f55 100644 --- a/app/src/main/java/fr/mobdev/goblim/NetworkManager.java +++ b/app/src/main/java/fr/mobdev/goblim/NetworkManager.java @@ -133,34 +133,57 @@ public class NetworkManager { conn.setRequestProperty("Accept", "*/*"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); - request = new DataOutputStream(conn.getOutputStream()); + int request_size = 0; //ask for JSON answer - request.writeBytes(hyphens + boundary + crlf); - request.writeBytes("Content-Disposition: form-data; name=\"format\"" + crlf); - request.writeBytes(crlf); - request.writeBytes("json" + crlf); + String answer = hyphens + boundary + crlf; + answer += "Content-Disposition: form-data; name=\"format\"" + crlf; + answer += crlf; + answer += "json" + crlf; + request_size += answer.length(); //ask for storage duration - request.writeBytes(hyphens + boundary + crlf); - request.writeBytes("Content-Disposition: form-data; name=\"delete-day\"" + crlf); - request.writeBytes(crlf); - request.writeBytes(nbDays + crlf); + String duration = hyphens + boundary + crlf; + duration += "Content-Disposition: form-data; name=\"delete-day\"" + crlf; + duration += crlf; + duration += nbDays + crlf; + request_size += duration.length(); //setup filename and say that octets follow - request.writeBytes(hyphens + boundary + crlf); - request.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"" + crlf); - request.writeBytes("Content-Type: application/octet-stream" + crlf); - request.writeBytes(crlf); - request.flush(); + String outputInformations = hyphens + boundary + crlf; + outputInformations += "Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"" + crlf; + outputInformations += "Content-Type: application/octet-stream" + crlf; + outputInformations += crlf; + request_size += outputInformations.length(); - //write image data - request.write(byteArray); + request_size += byteArray.length; //finish the format http post packet - request.writeBytes(crlf); - request.writeBytes(hyphens + boundary + hyphens + crlf); + String endHttp = crlf; + endHttp += hyphens + boundary + hyphens + crlf; + request_size += endHttp.length(); + + conn.setFixedLengthStreamingMode(request_size); + + //write data + request = new DataOutputStream(conn.getOutputStream()); + request.writeBytes(answer); + request.writeBytes(duration); + request.writeBytes(outputInformations); request.flush(); + //write in loop + int byteWriten = 0; + int blockSize = byteArray.length / 100; + while(byteWriten < byteArray.length) { + if(byteArray.length - byteWriten < blockSize) + blockSize = byteArray.length - byteWriten; + request.write(byteArray,byteWriten,blockSize); + byteWriten+=blockSize; + listener.uploadProgress(byteWriten,byteArray.length); + } + request.writeBytes(endHttp); + request.flush(); + //get answer stream = conn.getInputStream(); diff --git a/app/src/main/java/fr/mobdev/goblim/activity/UploadActivity.java b/app/src/main/java/fr/mobdev/goblim/activity/UploadActivity.java index a53b8e6..d471236 100644 --- a/app/src/main/java/fr/mobdev/goblim/activity/UploadActivity.java +++ b/app/src/main/java/fr/mobdev/goblim/activity/UploadActivity.java @@ -157,6 +157,16 @@ public class UploadActivity extends AppCompatActivity { } }); } + + @Override + public void uploadProgress(final int progress, final int length) { + runOnUiThread(new Runnable() { + @Override + public void run() { + progressDialog.setProgress(progress*100/length); + } + }); + } }; //prepare for upload @@ -308,6 +318,8 @@ public class UploadActivity extends AppCompatActivity { @Override public void run() { progressDialog = new ProgressDialog(UploadActivity.this); + progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + progressDialog.setMax(100); progressDialog.setMessage(getString(R.string.upload_progress)); progressDialog.setCancelable(false); progressDialog.show(); diff --git a/app/src/main/java/fr/mobdev/goblim/listener/NetworkAdapter.java b/app/src/main/java/fr/mobdev/goblim/listener/NetworkAdapter.java index 3631560..6cf5215 100644 --- a/app/src/main/java/fr/mobdev/goblim/listener/NetworkAdapter.java +++ b/app/src/main/java/fr/mobdev/goblim/listener/NetworkAdapter.java @@ -41,4 +41,9 @@ public class NetworkAdapter implements NetworkListener{ public void deleteError(String Error) { //do nothing } + + @Override + public void uploadProgress(int byteWriten, int length) { + + } } diff --git a/app/src/main/java/fr/mobdev/goblim/listener/NetworkListener.java b/app/src/main/java/fr/mobdev/goblim/listener/NetworkListener.java index ce4cadc..07074ee 100644 --- a/app/src/main/java/fr/mobdev/goblim/listener/NetworkListener.java +++ b/app/src/main/java/fr/mobdev/goblim/listener/NetworkListener.java @@ -27,4 +27,5 @@ public interface NetworkListener extends EventListener{ void fileUploadError(String error); void deleteSucceed(); void deleteError(String error); + void uploadProgress(int byteWriten, int length); } diff --git a/build.gradle b/build.gradle index e0b366a..77ce66e 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.5.0' + classpath 'com.android.tools.build:gradle:2.1.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0c71e76..2a2532f 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Apr 10 15:27:10 PDT 2013 +#Mon Sep 05 14:49:01 CEST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip