Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 79a230c

Browse files
committed
Merge pull request gotev#4 from eliasnaur/master
New features and improvements: - Support custom HTTP method in addition to the standard (POST) - Only add content type if specified - Use different notification ID for the final notification - Use wake lock to keep upload running while the device is sleeping - Use startForegrounf and stopForeground to lower the risks of being killed by the system during upload - Use unique ID for notifications - Only show success notification if response code is 2XX - Upload IDs, so that broadcasts for different files can be identified - Remove extra boundary at the end of the file upload - Don't trust self signed SSL certificates by default
2 parents e1a9e64 + 596b8cb commit 79a230c

7 files changed

+98
-110
lines changed

libs/android-support-v4.jar

26.2 KB
Binary file not shown.

project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
1212

1313
# Project target.
14-
target=android-19
14+
target=Google Inc.:Google APIs:19
1515
android.library=true

src/com/alexbbb/uploadservice/AbstractUploadServiceReceiver.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,23 @@ public void onReceive(Context context, Intent intent) {
2222
if (intent != null) {
2323
if (UploadService.BROADCAST_ACTION.equals(intent.getAction())) {
2424
final int status = intent.getIntExtra(UploadService.STATUS, 0);
25+
final String uploadId = intent.getStringExtra(UploadService.UPLOAD_ID);
2526

2627
switch (status) {
2728
case UploadService.STATUS_ERROR:
2829
final Exception exception = (Exception) intent.getSerializableExtra(UploadService.ERROR_EXCEPTION);
29-
onError(exception);
30+
onError(uploadId, exception);
3031
break;
3132

3233
case UploadService.STATUS_COMPLETED:
3334
final int responseCode = intent.getIntExtra(UploadService.SERVER_RESPONSE_CODE, 0);
3435
final String responseMsg = intent.getStringExtra(UploadService.SERVER_RESPONSE_MESSAGE);
35-
onCompleted(responseCode, responseMsg);
36+
onCompleted(uploadId, responseCode, responseMsg);
3637
break;
3738

3839
case UploadService.STATUS_IN_PROGRESS:
3940
final int progress = intent.getIntExtra(UploadService.PROGRESS, 0);
40-
onProgress(progress);
41+
onProgress(uploadId, progress);
4142
break;
4243

4344
default:
@@ -52,18 +53,18 @@ public void onReceive(Context context, Intent intent) {
5253
* Called when the upload progress changes.
5354
* @param progress value from 0 to 100
5455
*/
55-
public abstract void onProgress(final int progress);
56+
public abstract void onProgress(final String uploadId, final int progress);
5657

5758
/**
5859
* Called when an error happens during the upload.
5960
* @param exception exception that caused the error
6061
*/
61-
public abstract void onError(final Exception exception);
62+
public abstract void onError(final String uploadId, final Exception exception);
6263

6364
/**
6465
* Called when the upload is completed successfully.
6566
* @param serverResponseCode status code returned by the server
6667
* @param serverResponseMessage string containing the response received from the server
6768
*/
68-
public abstract void onCompleted(final int serverResponseCode, final String serverResponseMessage);
69+
public abstract void onCompleted(final String uploadId, final int serverResponseCode, final String serverResponseMessage);
6970
}

src/com/alexbbb/uploadservice/AllCertificatesTruster.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/com/alexbbb/uploadservice/FileToUpload.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,20 @@ public final InputStream getStream() throws FileNotFoundException {
5050
}
5151

5252
public byte[] getMultipartHeader() throws UnsupportedEncodingException {
53-
String header = "Content-Disposition: form-data; name=\""
54-
+ paramName + "\"; filename=\"" + fileName + "\""
55-
+ NEW_LINE + "Content-Type: " + contentType + NEW_LINE + NEW_LINE;
56-
return header.getBytes("UTF-8");
53+
StringBuilder builder = new StringBuilder()
54+
.append("Content-Disposition: form-data; name=\"")
55+
.append(paramName)
56+
.append("\"; filename=\"")
57+
.append(fileName)
58+
.append("\"")
59+
.append(NEW_LINE);
60+
if (contentType != null) {
61+
builder.append("Content-Type: ")
62+
.append(contentType)
63+
.append(NEW_LINE);
64+
}
65+
builder.append(NEW_LINE);
66+
return builder.toString().getBytes("UTF-8");
5767
}
5868

5969
public long length() {

src/com/alexbbb/uploadservice/UploadRequest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616
public class UploadRequest {
1717

1818
private UploadNotificationConfig notificationConfig;
19+
private String method = "POST";
1920
private final Context context;
21+
private final String uploadId;
2022
private final String url;
2123
private final ArrayList<FileToUpload> filesToUpload;
2224
private final ArrayList<NameValue> headers;
2325
private final ArrayList<NameValue> parameters;
2426

25-
public UploadRequest(final Context context, final String serverUrl) {
27+
public UploadRequest(final Context context, final String uploadId, final String serverUrl) {
2628
this.context = context;
29+
this.uploadId = uploadId;
2730
notificationConfig = new UploadNotificationConfig();
2831
url = serverUrl;
2932
filesToUpload = new ArrayList<FileToUpload>();
@@ -87,6 +90,18 @@ public void addArrayParameter(final String paramName, final List<String> list) {
8790
}
8891
}
8992

93+
public void setMethod(String method) {
94+
this.method = method;
95+
}
96+
97+
protected String getMethod() {
98+
return method;
99+
}
100+
101+
protected String getUploadId() {
102+
return uploadId;
103+
}
104+
90105
protected String getServerUrl() {
91106
return url;
92107
}

0 commit comments

Comments
 (0)