104104import com .zulip .android .networking .UploadProgressRequest ;
105105import com .zulip .android .networking .ZulipAsyncPushTask ;
106106import com .zulip .android .networking .response .UploadResponse ;
107- import com .zulip .android .util .ActivityTransitionAnim ;
108107import com .zulip .android .networking .util .DefaultCallback ;
108+ import com .zulip .android .util .ActivityTransitionAnim ;
109109import com .zulip .android .util .AnimationHelper ;
110110import com .zulip .android .util .CommonProgressDialog ;
111111import com .zulip .android .util .Constants ;
@@ -350,6 +350,9 @@ public void onClick(View v) {
350350 etSearchPeople = (EditText ) findViewById (R .id .people_drawer_search );
351351 ivSearchPeopleCancel = (ImageView ) findViewById (R .id .iv_people__search_cancel_button );
352352 onTextChangeOfPeopleSearchEditText ();
353+ mNotificationManager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
354+ mBuilder = (NotificationCompat .Builder ) new NotificationCompat .Builder (this )
355+ .setColor (getColor (R .color .notif_background ));
353356 ivSearchPeopleCancel .setOnClickListener (new View .OnClickListener () {
354357 @ Override
355358 public void onClick (View v ) {
@@ -559,11 +562,6 @@ public Cursor runQuery(CharSequence charSequence) {
559562 setupSnackBar ();
560563 //Hides Keyboard if it was open with focus on an editText before restart of the activity
561564 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 ));
567565 }
568566
569567 /**
@@ -872,6 +870,15 @@ private void combineList(List<PeopleDrawerList> drawerLists) {
872870 protected void onNewIntent (Intent intent ) {
873871 super .onNewIntent (intent );
874872
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+
875882 // Get action and MIME type of intent
876883 String action = intent .getAction ();
877884 String type = intent .getType ();
@@ -891,13 +898,6 @@ protected void onNewIntent(Intent intent) {
891898
892899 if (action == null ) {
893900 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-
901901 // start upload of photo
902902 File photoFile = new File (filePath );
903903 uploadFile (photoFile );
@@ -1094,13 +1094,13 @@ private void startFileUpload() {
10941094 }
10951095
10961096 @ NonNull
1097- private MultipartBody .Part prepareFilePart (String partName , File file , int notificationId ) {
1097+ private MultipartBody .Part prepareFilePart (String partName , final File file , int notificationId ) {
10981098 // create UploadProgressRequest instance from file
10991099 UploadProgressRequest request = new UploadProgressRequest (file , new UploadProgressRequest .UploadCallbacks () {
11001100 @ Override
11011101 public void onProgressUpdate (int percentage , String progress , int notificationId ) {
11021102 // update notification
1103- progressNotification (notificationId , percentage , progress );
1103+ progressNotification (notificationId , percentage , progress , file . getName () );
11041104 }
11051105 }, notificationId );
11061106
@@ -1119,10 +1119,12 @@ public void onProgressUpdate(int percentage, String progress, int notificationId
11191119 */
11201120 private void setNotification (int notificationId , String content ) {
11211121 mBuilder .setSmallIcon (android .R .drawable .stat_sys_upload )
1122+ .setContentTitle (getString (R .string .notif_title ))
11221123 .setContentText (content )
11231124 .setAutoCancel (false )
1125+ .setOngoing (false )
11241126 // Removes the progress bar
1125- .setProgress (0 ,0 , false );
1127+ .setProgress (0 , 0 , false );
11261128 PendingIntent contentIntent = PendingIntent .getActivity (
11271129 getApplicationContext (),
11281130 0 ,
@@ -1138,10 +1140,12 @@ private void setNotification(int notificationId, String content) {
11381140 * @param notificationId
11391141 * @param percentage
11401142 */
1141- private void progressNotification (int notificationId , int percentage , String progress ) {
1143+ private void progressNotification (int notificationId , int percentage , String progress , String title ) {
11421144 mBuilder .setSmallIcon (android .R .drawable .stat_sys_upload )
1145+ .setContentTitle (title )
11431146 .setContentText (progress )
11441147 .setAutoCancel (false )
1148+ .setOngoing (true )
11451149 .setProgress (100 , percentage , false );
11461150 mNotificationManager .notify (notificationId , mBuilder .build ());
11471151 }
@@ -1154,10 +1158,12 @@ private void progressNotification(int notificationId, int percentage, String pro
11541158 */
11551159 private void endNotification (int notificationId , String content ) {
11561160 mBuilder .setSmallIcon (R .drawable .ic_done_white_24dp )
1161+ .setContentTitle (getString (R .string .notif_title ))
11571162 .setContentText (content )
11581163 .setAutoCancel (true )
1164+ .setOngoing (false )
11591165 // Removes the progress bar
1160- .setProgress (0 ,0 , false );
1166+ .setProgress (0 , 0 , false );
11611167 PendingIntent contentIntent = PendingIntent .getActivity (
11621168 getApplicationContext (),
11631169 0 ,
@@ -1174,7 +1180,14 @@ private void endNotification(int notificationId, String content) {
11741180 * @param file on local storage
11751181 */
11761182 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 );
11781191
11791192 // MultipartBody.Part is used to send also the actual file name
11801193 MultipartBody .Part body = prepareFilePart ("file" , file , notifId );
@@ -1190,36 +1203,37 @@ private void uploadFile(final File file) {
11901203 call .enqueue (new DefaultCallback <UploadResponse >() {
11911204 @ Override
11921205 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+ }
12081221 }
12091222 }
12101223
12111224 @ Override
12121225 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+ }
12161229 }
12171230
12181231 @ Override
12191232 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+ }
12231237 }
12241238 });
12251239 }
0 commit comments