Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit 5f6ffea

Browse files
Sam1301kunall17
authored andcommitted
Add Notifications with progress update.
1 parent 7db5a9e commit 5f6ffea

File tree

3 files changed

+140
-98
lines changed

3 files changed

+140
-98
lines changed

app/src/main/java/com/zulip/android/activities/ZulipActivity.java

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
import com.zulip.android.networking.AsyncGetEvents;
102102
import com.zulip.android.networking.AsyncSend;
103103
import com.zulip.android.networking.AsyncStatusUpdate;
104+
import com.zulip.android.networking.UploadProgressRequest;
104105
import com.zulip.android.networking.ZulipAsyncPushTask;
105106
import com.zulip.android.networking.response.UploadResponse;
106107
import com.zulip.android.util.ActivityTransitionAnim;
@@ -133,9 +134,7 @@
133134
import java.util.Locale;
134135
import java.util.concurrent.Callable;
135136

136-
import okhttp3.MediaType;
137137
import okhttp3.MultipartBody;
138-
import okhttp3.RequestBody;
139138
import retrofit2.Call;
140139
import retrofit2.Response;
141140

@@ -562,6 +561,9 @@ public Cursor runQuery(CharSequence charSequence) {
562561
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
563562

564563
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
564+
mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
565+
.setContentTitle(getString(R.string.notif_title))
566+
.setColor(getColor(R.color.notif_background));
565567
}
566568

567569
/**
@@ -1093,58 +1095,77 @@ private void startFileUpload() {
10931095

10941096
@NonNull
10951097
private MultipartBody.Part prepareFilePart(String partName, File file) {
1096-
// create RequestBody instance from file
1097-
RequestBody requestFile =
1098-
RequestBody.create(
1099-
MediaType.parse("multipart/form-data"),
1100-
file
1101-
);
1098+
// create UploadProgressRequest instance from file
1099+
// TODO: change for multiple uploads
1100+
UploadProgressRequest request = new UploadProgressRequest(file, new UploadProgressRequest.UploadCallbacks() {
1101+
@Override
1102+
public void onProgressUpdate(int percentage, String progress, int notificationId) {
1103+
// update notification
1104+
progressNotification(notificationId, percentage, progress);
1105+
}
1106+
}, 100);
11021107

11031108
// MultipartBody.Part is used to send also the actual file name
1104-
return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
1109+
return MultipartBody.Part.createFormData(partName, file.getName(), request);
11051110
}
11061111

1107-
NotificationManager mNotificationManager;
1112+
private NotificationManager mNotificationManager;
1113+
private NotificationCompat.Builder mBuilder;
11081114

11091115
/**
11101116
* TODO: add description
11111117
*
11121118
* @param notificationId
1113-
* @param title
11141119
* @param content
11151120
*/
1116-
private void setNotification(int notificationId, String title, String content) {
1117-
NotificationCompat.Builder builder =
1118-
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
1119-
.setSmallIcon(android.R.drawable.stat_sys_upload)
1120-
.setContentTitle(title)
1121-
.setContentText(content)
1122-
.setColor(getColor(R.color.notif_background));
1121+
private void setNotification(int notificationId, String content) {
1122+
mBuilder.setSmallIcon(android.R.drawable.stat_sys_upload)
1123+
.setContentText(content)
1124+
.setAutoCancel(false)
1125+
// Removes the progress bar
1126+
.setProgress(0,0,false);
11231127
PendingIntent contentIntent = PendingIntent.getActivity(
11241128
getApplicationContext(),
11251129
0,
11261130
new Intent(),
11271131
PendingIntent.FLAG_UPDATE_CURRENT);
1128-
builder.setContentIntent(contentIntent);
1129-
mNotificationManager.notify(notificationId, builder.build());
1132+
mBuilder.setContentIntent(contentIntent);
1133+
mNotificationManager.notify(notificationId, mBuilder.build());
11301134
}
11311135

1132-
private void endNotification(int notificationId, String title, String content) {
1133-
NotificationCompat.Builder builder =
1134-
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
1135-
.setSmallIcon(R.drawable.ic_done_white_24dp)
1136-
.setContentTitle(title)
1137-
.setContentText(content)
1138-
.setAutoCancel(true)
1139-
.setColor(getColor(R.color.notif_background));
1136+
/**
1137+
* TODO: add description
1138+
*
1139+
* @param notificationId
1140+
* @param percentage
1141+
*/
1142+
private void progressNotification(int notificationId, int percentage, String progress) {
1143+
mBuilder.setSmallIcon(android.R.drawable.stat_sys_upload)
1144+
.setContentText(progress)
1145+
.setAutoCancel(false)
1146+
.setProgress(100, percentage, false);
1147+
mNotificationManager.notify(notificationId, mBuilder.build());
1148+
}
11401149

1150+
/**
1151+
* TODO: add description
1152+
*
1153+
* @param notificationId
1154+
* @param content
1155+
*/
1156+
private void endNotification(int notificationId, String content) {
1157+
mBuilder.setSmallIcon(R.drawable.ic_done_white_24dp)
1158+
.setContentText(content)
1159+
.setAutoCancel(true)
1160+
// Removes the progress bar
1161+
.setProgress(0,0,false);
11411162
PendingIntent contentIntent = PendingIntent.getActivity(
11421163
getApplicationContext(),
11431164
0,
11441165
new Intent(),
11451166
PendingIntent.FLAG_UPDATE_CURRENT);
1146-
builder.setContentIntent(contentIntent);
1147-
mNotificationManager.notify(notificationId, builder.build());
1167+
mBuilder.setContentIntent(contentIntent);
1168+
mNotificationManager.notify(notificationId, mBuilder.build());
11481169
}
11491170

11501171
/**
@@ -1162,7 +1183,7 @@ private void uploadFile(final File file) {
11621183

11631184
// start notification
11641185
// TODO: handle different notif ids
1165-
setNotification(100, getString(R.string.notif_title), getString(R.string.init_notif_title));
1186+
setNotification(100, getString(R.string.init_notif_title));
11661187

11671188
// finally, execute the request
11681189
// create upload service client
@@ -1174,7 +1195,7 @@ public void onSuccess(Call<UploadResponse> call, Response<UploadResponse> respon
11741195
UploadResponse uploadResponse = response.body();
11751196
filePathOnServer = uploadResponse.getUri();
11761197
if (!filePathOnServer.equals("")) {
1177-
endNotification(100, getString(R.string.notif_title), getString(R.string.finish_notif_title));
1198+
endNotification(100, getString(R.string.finish_notif_title));
11781199
// remove loading message from the screen
11791200
sendingMessage(false, loadingMsg);
11801201

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.zulip.android.networking;
2+
3+
import android.os.Handler;
4+
import android.os.Looper;
5+
6+
import java.io.File;
7+
import java.io.FileInputStream;
8+
import java.io.IOException;
9+
10+
import okhttp3.MediaType;
11+
import okhttp3.RequestBody;
12+
import okio.BufferedSink;
13+
14+
/**
15+
* TODO: add description
16+
*/
17+
18+
public class UploadProgressRequest extends RequestBody {
19+
private File mFile;
20+
private UploadCallbacks mListener;
21+
private int mNotificationId;
22+
int counter = 0;
23+
24+
private static final int DEFAULT_BUFFER_SIZE = 2048;
25+
26+
public interface UploadCallbacks {
27+
void onProgressUpdate(int percentage, String progress, int notificationId);
28+
}
29+
30+
public UploadProgressRequest(final File file, final UploadCallbacks listener, int notificationId) {
31+
mFile = file;
32+
mListener = listener;
33+
mNotificationId = notificationId;
34+
}
35+
36+
@Override
37+
public MediaType contentType() {
38+
return MediaType.parse("multipart/form-data");
39+
}
40+
41+
@Override
42+
public void writeTo(BufferedSink sink) throws IOException {
43+
long fileLength = mFile.length();
44+
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
45+
FileInputStream in = new FileInputStream(mFile);
46+
long uploaded = 0;
47+
48+
// workaround logging interceptor disturbing progress update
49+
counter++;
50+
51+
try {
52+
int read;
53+
Handler handler = new Handler(Looper.getMainLooper());
54+
while ((read = in.read(buffer)) != -1) {
55+
uploaded += read;
56+
sink.write(buffer, 0, read);
57+
58+
// update progress on UI thread only when httpLoggingInterceptor
59+
// is not calling writeTo()
60+
if (counter % 2 == 0) {
61+
handler.post(new ProgressUpdater(uploaded, fileLength));
62+
}
63+
}
64+
} finally {
65+
in.close();
66+
}
67+
}
68+
69+
private class ProgressUpdater implements Runnable {
70+
private long mUploaded;
71+
private long mTotal;
72+
73+
public ProgressUpdater(long uploaded, long total) {
74+
mUploaded = uploaded;
75+
mTotal = total;
76+
}
77+
78+
@Override
79+
public void run() {
80+
double currentProgress = Math.round((mUploaded / Math.pow(1024, 2)) * 100) / 100.0;
81+
double totalProgress = Math.round((mTotal / Math.pow(1024, 2)) * 100) / 100.0;
82+
String progress = currentProgress + " MB"
83+
+ " / " + totalProgress + " MB";
84+
mListener.onProgressUpdate((int)(100 * mUploaded / mTotal), progress, mNotificationId);
85+
}
86+
}
87+
}

app/src/main/java/com/zulip/android/networking/UploadProgressResponse.java

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

0 commit comments

Comments
 (0)