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

Skip to content

Commit c1db4c1

Browse files
author
Elias Naur
committed
Only add content type if speficied. Use different notification id for the final notification.
1 parent d8d2c0e commit c1db4c1

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

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/UploadService.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import android.os.PowerManager;
1818
import android.app.IntentService;
19+
import android.app.NotificationManager;
1920
import android.content.Intent;
2021
import android.support.v4.app.NotificationCompat;
2122
import android.support.v4.app.NotificationCompat.Builder;
@@ -33,6 +34,7 @@ public class UploadService extends IntentService {
3334
private static final String TAG = "AndroidUploadService";
3435

3536
private static final int UPLOAD_NOTIFICATION_ID = 1234; // Something unique
37+
private static final int UPLOAD_NOTIFICATION_ID_DONE = 1235; // Something unique
3638
private static final int BUFFER_SIZE = 4096;
3739
private static final String NEW_LINE = "\r\n";
3840
private static final String TWO_HYPHENS = "--";
@@ -56,6 +58,7 @@ public class UploadService extends IntentService {
5658
public static final String SERVER_RESPONSE_CODE = "serverResponseCode";
5759
public static final String SERVER_RESPONSE_MESSAGE = "serverResponseMessage";
5860

61+
private NotificationManager notificationManager;
5962
private Builder notification;
6063
private PowerManager.WakeLock wakeLock;
6164
private UploadNotificationConfig notificationConfig;
@@ -98,8 +101,8 @@ public UploadService() {
98101
@Override
99102
public void onCreate() {
100103
super.onCreate();
101-
notification = new NotificationCompat.Builder(this)
102-
.setOngoing(true);
104+
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
105+
notification = new NotificationCompat.Builder(this);
103106
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
104107
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
105108
}
@@ -360,35 +363,39 @@ private void createNotification() {
360363
notification.setContentTitle(notificationConfig.getTitle())
361364
.setContentText(notificationConfig.getMessage())
362365
.setSmallIcon(notificationConfig.getIconResourceID())
363-
.setProgress(100, 0, true);
366+
.setProgress(100, 0, true)
367+
.setOngoing(true);
364368
startForeground(UPLOAD_NOTIFICATION_ID, notification.build());
365369
}
366370

367371
private void updateNotificationProgress(final int progress) {
368372
notification.setContentTitle(notificationConfig.getTitle())
369373
.setContentText(notificationConfig.getMessage())
370374
.setSmallIcon(notificationConfig.getIconResourceID())
371-
.setProgress(100, progress, false);
375+
.setProgress(100, progress, false)
376+
.setOngoing(true);
372377
startForeground(UPLOAD_NOTIFICATION_ID, notification.build());
373378
}
374379

375380
private void updateNotificationCompleted() {
381+
stopForeground(notificationConfig.isAutoClearOnSuccess());
376382
if (!notificationConfig.isAutoClearOnSuccess()) {
377383
notification.setContentTitle(notificationConfig.getTitle())
378384
.setContentText(notificationConfig.getCompleted())
379385
.setSmallIcon(notificationConfig.getIconResourceID())
380-
.setProgress(0, 0, false);
381-
startForeground(UPLOAD_NOTIFICATION_ID, notification.build());
386+
.setProgress(0, 0, false)
387+
.setOngoing(false);
388+
notificationManager.notify(UPLOAD_NOTIFICATION_ID_DONE, notification.build());
382389
}
383-
stopForeground(notificationConfig.isAutoClearOnSuccess());
384390
}
385391

386392
private void updateNotificationError() {
393+
stopForeground(false);
387394
notification.setContentTitle(notificationConfig.getTitle())
388395
.setContentText(notificationConfig.getError())
389396
.setSmallIcon(notificationConfig.getIconResourceID())
390-
.setProgress(0, 0, false);
391-
startForeground(UPLOAD_NOTIFICATION_ID, notification.build());
392-
stopForeground(false);
397+
.setProgress(0, 0, false)
398+
.setOngoing(false);
399+
notificationManager.notify(UPLOAD_NOTIFICATION_ID_DONE, notification.build());
393400
}
394401
}

0 commit comments

Comments
 (0)