104
104
import com .zulip .android .networking .UploadProgressRequest ;
105
105
import com .zulip .android .networking .ZulipAsyncPushTask ;
106
106
import com .zulip .android .networking .response .UploadResponse ;
107
- import com .zulip .android .util .ActivityTransitionAnim ;
108
107
import com .zulip .android .networking .util .DefaultCallback ;
108
+ import com .zulip .android .util .ActivityTransitionAnim ;
109
109
import com .zulip .android .util .AnimationHelper ;
110
110
import com .zulip .android .util .CommonProgressDialog ;
111
111
import com .zulip .android .util .Constants ;
@@ -350,6 +350,9 @@ public void onClick(View v) {
350
350
etSearchPeople = (EditText ) findViewById (R .id .people_drawer_search );
351
351
ivSearchPeopleCancel = (ImageView ) findViewById (R .id .iv_people__search_cancel_button );
352
352
onTextChangeOfPeopleSearchEditText ();
353
+ mNotificationManager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
354
+ mBuilder = (NotificationCompat .Builder ) new NotificationCompat .Builder (this )
355
+ .setColor (getColor (R .color .notif_background ));
353
356
ivSearchPeopleCancel .setOnClickListener (new View .OnClickListener () {
354
357
@ Override
355
358
public void onClick (View v ) {
@@ -559,11 +562,6 @@ public Cursor runQuery(CharSequence charSequence) {
559
562
setupSnackBar ();
560
563
//Hides Keyboard if it was open with focus on an editText before restart of the activity
561
564
this .getWindow ().setSoftInputMode (WindowManager .LayoutParams .SOFT_INPUT_STATE_HIDDEN );
562
-
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 ));
567
565
}
568
566
569
567
/**
@@ -872,6 +870,15 @@ private void combineList(List<PeopleDrawerList> drawerLists) {
872
870
protected void onNewIntent (Intent intent ) {
873
871
super .onNewIntent (intent );
874
872
873
+ if (mNotificationManager == null ) {
874
+ mNotificationManager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
875
+ }
876
+ if (mBuilder == null ) {
877
+ mBuilder = (NotificationCompat .Builder ) new NotificationCompat .Builder (this )
878
+ .setContentTitle (getString (R .string .notif_title ))
879
+ .setColor (getColor (R .color .notif_background ));
880
+ }
881
+
875
882
// Get action and MIME type of intent
876
883
String action = intent .getAction ();
877
884
String type = intent .getType ();
@@ -891,13 +898,6 @@ protected void onNewIntent(Intent intent) {
891
898
892
899
if (action == null ) {
893
900
if (!TextUtils .isEmpty (filePath )) {
894
- // Update UI to indicate image is being loaded
895
- // hide fab and display chatbox
896
- displayFAB (false );
897
- displayChatBox (true );
898
- String loadingMsg = getResources ().getString (R .string .uploading_message );
899
- sendingMessage (true , loadingMsg );
900
-
901
901
// start upload of photo
902
902
File photoFile = new File (filePath );
903
903
uploadFile (photoFile );
@@ -1094,13 +1094,13 @@ private void startFileUpload() {
1094
1094
}
1095
1095
1096
1096
@ NonNull
1097
- private MultipartBody .Part prepareFilePart (String partName , File file , int notificationId ) {
1097
+ private MultipartBody .Part prepareFilePart (String partName , final File file , int notificationId ) {
1098
1098
// create UploadProgressRequest instance from file
1099
1099
UploadProgressRequest request = new UploadProgressRequest (file , new UploadProgressRequest .UploadCallbacks () {
1100
1100
@ Override
1101
1101
public void onProgressUpdate (int percentage , String progress , int notificationId ) {
1102
1102
// update notification
1103
- progressNotification (notificationId , percentage , progress );
1103
+ progressNotification (notificationId , percentage , progress , file . getName () );
1104
1104
}
1105
1105
}, notificationId );
1106
1106
@@ -1119,10 +1119,12 @@ public void onProgressUpdate(int percentage, String progress, int notificationId
1119
1119
*/
1120
1120
private void setNotification (int notificationId , String content ) {
1121
1121
mBuilder .setSmallIcon (android .R .drawable .stat_sys_upload )
1122
+ .setContentTitle (getString (R .string .notif_title ))
1122
1123
.setContentText (content )
1123
1124
.setAutoCancel (false )
1125
+ .setOngoing (false )
1124
1126
// Removes the progress bar
1125
- .setProgress (0 ,0 , false );
1127
+ .setProgress (0 , 0 , false );
1126
1128
PendingIntent contentIntent = PendingIntent .getActivity (
1127
1129
getApplicationContext (),
1128
1130
0 ,
@@ -1138,10 +1140,12 @@ private void setNotification(int notificationId, String content) {
1138
1140
* @param notificationId
1139
1141
* @param percentage
1140
1142
*/
1141
- private void progressNotification (int notificationId , int percentage , String progress ) {
1143
+ private void progressNotification (int notificationId , int percentage , String progress , String title ) {
1142
1144
mBuilder .setSmallIcon (android .R .drawable .stat_sys_upload )
1145
+ .setContentTitle (title )
1143
1146
.setContentText (progress )
1144
1147
.setAutoCancel (false )
1148
+ .setOngoing (true )
1145
1149
.setProgress (100 , percentage , false );
1146
1150
mNotificationManager .notify (notificationId , mBuilder .build ());
1147
1151
}
@@ -1154,10 +1158,12 @@ private void progressNotification(int notificationId, int percentage, String pro
1154
1158
*/
1155
1159
private void endNotification (int notificationId , String content ) {
1156
1160
mBuilder .setSmallIcon (R .drawable .ic_done_white_24dp )
1161
+ .setContentTitle (getString (R .string .notif_title ))
1157
1162
.setContentText (content )
1158
1163
.setAutoCancel (true )
1164
+ .setOngoing (false )
1159
1165
// Removes the progress bar
1160
- .setProgress (0 ,0 , false );
1166
+ .setProgress (0 , 0 , false );
1161
1167
PendingIntent contentIntent = PendingIntent .getActivity (
1162
1168
getApplicationContext (),
1163
1169
0 ,
@@ -1174,7 +1180,14 @@ private void endNotification(int notificationId, String content) {
1174
1180
* @param file on local storage
1175
1181
*/
1176
1182
private void uploadFile (final File file ) {
1177
- final int notifId = (int ) ((new Date ().getTime () / 1000L ) % Integer .MAX_VALUE );
1183
+ // check if file size is greater than 10MB
1184
+ if (file .length () / Math .pow (1024 , 2 ) > 10 ) {
1185
+ Toast .makeText (this , R .string .upload_big_file , Toast .LENGTH_SHORT ).show ();
1186
+ return ;
1187
+ }
1188
+
1189
+ // generate unique notification Id for this upload
1190
+ final int notifId = (int ) (new Date ().getTime () % Integer .MAX_VALUE );
1178
1191
1179
1192
// MultipartBody.Part is used to send also the actual file name
1180
1193
MultipartBody .Part body = prepareFilePart ("file" , file , notifId );
@@ -1190,36 +1203,37 @@ private void uploadFile(final File file) {
1190
1203
call .enqueue (new DefaultCallback <UploadResponse >() {
1191
1204
@ Override
1192
1205
public void onSuccess (Call <UploadResponse > call , Response <UploadResponse > response ) {
1193
- String filePathOnServer = "" ;
1194
- UploadResponse uploadResponse = response . body () ;
1195
- filePathOnServer = uploadResponse . getUri ();
1196
- if (! filePathOnServer . equals ( "" )) {
1197
- endNotification ( notifId , getString ( R . string . finish_notif_title ));
1198
- // remove loading message from the screen
1199
- sendingMessage ( false , loadingMsg );
1200
-
1201
- // print message to compose box
1202
- messageEt . append ( " [" + file . getName ( ) + "](" +
1203
- UrlHelper . addHost ( filePathOnServer ) + ")" );
1204
- } else {
1205
- // remove loading message from the screen
1206
- sendingMessage ( false , loadingMsg );
1207
- Toast . makeText ( ZulipActivity . this , R . string . failed_to_upload , Toast . LENGTH_SHORT ). show ();
1206
+ if (! isDestroyed ()) {
1207
+ String filePathOnServer = "" ;
1208
+ UploadResponse uploadResponse = response . body ();
1209
+ filePathOnServer = uploadResponse . getUri ();
1210
+ if (! filePathOnServer . equals ( "" )) {
1211
+ endNotification ( notifId , getString ( R . string . finish_notif_title ));
1212
+
1213
+ // add uploaded file url on server to composed message
1214
+ messageEt . append ( " \n [" + file . getName () + "](" +
1215
+ UrlHelper . addHost ( filePathOnServer ) + ")" );
1216
+ displayFAB ( false );
1217
+ displayChatBox ( true );
1218
+ } else {
1219
+ endNotification ( notifId , getString ( R . string . failed_to_upload ) );
1220
+ }
1208
1221
}
1209
1222
}
1210
1223
1211
1224
@ Override
1212
1225
public void onError (Call <UploadResponse > call , Response <UploadResponse > response ) {
1213
- // remove loading message from the screen
1214
- sendingMessage ( false , loadingMsg );
1215
- Toast . makeText ( ZulipActivity . this , R . string . failed_to_upload , Toast . LENGTH_SHORT ). show ();
1226
+ if (! isDestroyed ()) {
1227
+ endNotification ( notifId , getString ( R . string . failed_to_upload ) );
1228
+ }
1216
1229
}
1217
1230
1218
1231
@ Override
1219
1232
public void onFailure (Call <UploadResponse > call , Throwable t ) {
1220
- // remove loading message from the screen
1221
- sendingMessage (false , loadingMsg );
1222
- ZLog .logException (t );
1233
+ if (!isDestroyed ()) {
1234
+ endNotification (notifId , getString (R .string .failed_to_upload ));
1235
+ ZLog .logException (t );
1236
+ }
1223
1237
}
1224
1238
});
1225
1239
}
0 commit comments