101101import com .zulip .android .networking .AsyncGetEvents ;
102102import com .zulip .android .networking .AsyncSend ;
103103import com .zulip .android .networking .AsyncStatusUpdate ;
104+ import com .zulip .android .networking .UploadProgressRequest ;
104105import com .zulip .android .networking .ZulipAsyncPushTask ;
105106import com .zulip .android .networking .response .UploadResponse ;
106107import com .zulip .android .util .ActivityTransitionAnim ;
133134import java .util .Locale ;
134135import java .util .concurrent .Callable ;
135136
136- import okhttp3 .MediaType ;
137137import okhttp3 .MultipartBody ;
138- import okhttp3 .RequestBody ;
139138import retrofit2 .Call ;
140139import 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
0 commit comments