101
101
import com .zulip .android .networking .AsyncGetEvents ;
102
102
import com .zulip .android .networking .AsyncSend ;
103
103
import com .zulip .android .networking .AsyncStatusUpdate ;
104
+ import com .zulip .android .networking .UploadProgressRequest ;
104
105
import com .zulip .android .networking .ZulipAsyncPushTask ;
105
106
import com .zulip .android .networking .response .UploadResponse ;
106
107
import com .zulip .android .util .ActivityTransitionAnim ;
133
134
import java .util .Locale ;
134
135
import java .util .concurrent .Callable ;
135
136
136
- import okhttp3 .MediaType ;
137
137
import okhttp3 .MultipartBody ;
138
- import okhttp3 .RequestBody ;
139
138
import retrofit2 .Call ;
140
139
import retrofit2 .Response ;
141
140
@@ -562,6 +561,9 @@ public Cursor runQuery(CharSequence charSequence) {
562
561
this .getWindow ().setSoftInputMode (WindowManager .LayoutParams .SOFT_INPUT_STATE_HIDDEN );
563
562
564
563
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 ));
565
567
}
566
568
567
569
/**
@@ -1093,58 +1095,77 @@ private void startFileUpload() {
1093
1095
1094
1096
@ NonNull
1095
1097
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 );
1102
1107
1103
1108
// 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 );
1105
1110
}
1106
1111
1107
- NotificationManager mNotificationManager ;
1112
+ private NotificationManager mNotificationManager ;
1113
+ private NotificationCompat .Builder mBuilder ;
1108
1114
1109
1115
/**
1110
1116
* TODO: add description
1111
1117
*
1112
1118
* @param notificationId
1113
- * @param title
1114
1119
* @param content
1115
1120
*/
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 );
1123
1127
PendingIntent contentIntent = PendingIntent .getActivity (
1124
1128
getApplicationContext (),
1125
1129
0 ,
1126
1130
new Intent (),
1127
1131
PendingIntent .FLAG_UPDATE_CURRENT );
1128
- builder .setContentIntent (contentIntent );
1129
- mNotificationManager .notify (notificationId , builder .build ());
1132
+ mBuilder .setContentIntent (contentIntent );
1133
+ mNotificationManager .notify (notificationId , mBuilder .build ());
1130
1134
}
1131
1135
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
+ }
1140
1149
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 );
1141
1162
PendingIntent contentIntent = PendingIntent .getActivity (
1142
1163
getApplicationContext (),
1143
1164
0 ,
1144
1165
new Intent (),
1145
1166
PendingIntent .FLAG_UPDATE_CURRENT );
1146
- builder .setContentIntent (contentIntent );
1147
- mNotificationManager .notify (notificationId , builder .build ());
1167
+ mBuilder .setContentIntent (contentIntent );
1168
+ mNotificationManager .notify (notificationId , mBuilder .build ());
1148
1169
}
1149
1170
1150
1171
/**
@@ -1162,7 +1183,7 @@ private void uploadFile(final File file) {
1162
1183
1163
1184
// start notification
1164
1185
// 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 ));
1166
1187
1167
1188
// finally, execute the request
1168
1189
// create upload service client
@@ -1174,7 +1195,7 @@ public void onSuccess(Call<UploadResponse> call, Response<UploadResponse> respon
1174
1195
UploadResponse uploadResponse = response .body ();
1175
1196
filePathOnServer = uploadResponse .getUri ();
1176
1197
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 ));
1178
1199
// remove loading message from the screen
1179
1200
sendingMessage (false , loadingMsg );
1180
1201
0 commit comments