Change infinite progress bar to a real one that show the real progress

of the upload

Close #4
This commit is contained in:
Schoumi 2016-09-06 16:37:36 +02:00
父節點 3fec21c32d
當前提交 42294e533b
共有 7 個檔案被更改,包括 69 行新增28 行删除

查看文件

@ -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'
}

查看文件

@ -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();

查看文件

@ -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();

查看文件

@ -41,4 +41,9 @@ public class NetworkAdapter implements NetworkListener{
public void deleteError(String Error) {
//do nothing
}
@Override
public void uploadProgress(int byteWriten, int length) {
}
}

查看文件

@ -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);
}

查看文件

@ -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

查看文件

@ -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