66import android .annotation .TargetApi ;
77import android .app .AlertDialog ;
88import android .app .DatePickerDialog ;
9+ import android .app .NotificationManager ;
10+ import android .app .PendingIntent ;
911import android .app .SearchManager ;
1012import android .content .BroadcastReceiver ;
1113import android .content .ClipData ;
4446import android .support .v4 .widget .DrawerLayout ;
4547import android .support .v4 .widget .SimpleCursorAdapter ;
4648import android .support .v7 .app .AppCompatDelegate ;
49+ import android .support .v7 .app .NotificationCompat ;
4750import android .support .v7 .widget .LinearLayoutManager ;
4851import android .support .v7 .widget .RecyclerView ;
4952import android .support .v7 .widget .Toolbar ;
@@ -557,6 +560,8 @@ public Cursor runQuery(CharSequence charSequence) {
557560 setupSnackBar ();
558561 //Hides Keyboard if it was open with focus on an editText before restart of the activity
559562 this .getWindow ().setSoftInputMode (WindowManager .LayoutParams .SOFT_INPUT_STATE_HIDDEN );
563+
564+ mNotificationManager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
560565 }
561566
562567 /**
@@ -1099,6 +1104,49 @@ private MultipartBody.Part prepareFilePart(String partName, File file) {
10991104 return MultipartBody .Part .createFormData (partName , file .getName (), requestFile );
11001105 }
11011106
1107+ NotificationManager mNotificationManager ;
1108+
1109+ /**
1110+ * TODO: add description
1111+ *
1112+ * @param notificationId
1113+ * @param title
1114+ * @param content
1115+ */
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 ));
1123+ PendingIntent contentIntent = PendingIntent .getActivity (
1124+ getApplicationContext (),
1125+ 0 ,
1126+ new Intent (),
1127+ PendingIntent .FLAG_UPDATE_CURRENT );
1128+ builder .setContentIntent (contentIntent );
1129+ mNotificationManager .notify (notificationId , builder .build ());
1130+ }
1131+
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 ));
1140+
1141+ PendingIntent contentIntent = PendingIntent .getActivity (
1142+ getApplicationContext (),
1143+ 0 ,
1144+ new Intent (),
1145+ PendingIntent .FLAG_UPDATE_CURRENT );
1146+ builder .setContentIntent (contentIntent );
1147+ mNotificationManager .notify (notificationId , builder .build ());
1148+ }
1149+
11021150 /**
11031151 * Function to upload file asynchronously to the server using retrofit callback
11041152 * upload {@link com.zulip.android.service.ZulipServices#upload(MultipartBody.Part)}
@@ -1112,6 +1160,10 @@ private void uploadFile(final File file) {
11121160
11131161 final String loadingMsg = getResources ().getString (R .string .uploading_message );
11141162
1163+ // start notification
1164+ // TODO: handle different notif ids
1165+ setNotification (100 , getString (R .string .notif_title ), getString (R .string .init_notif_title ));
1166+
11151167 // finally, execute the request
11161168 // create upload service client
11171169 Call <UploadResponse > call = ((ZulipApp ) getApplicationContext ()).getZulipServices ().upload (body );
@@ -1122,6 +1174,7 @@ public void onSuccess(Call<UploadResponse> call, Response<UploadResponse> respon
11221174 UploadResponse uploadResponse = response .body ();
11231175 filePathOnServer = uploadResponse .getUri ();
11241176 if (!filePathOnServer .equals ("" )) {
1177+ endNotification (100 , getString (R .string .notif_title ), getString (R .string .finish_notif_title ));
11251178 // remove loading message from the screen
11261179 sendingMessage (false , loadingMsg );
11271180
@@ -2354,6 +2407,10 @@ protected void onDestroy() {
23542407 statusUpdateHandler .removeMessages (0 );
23552408 statusUpdateHandler .removeCallbacks (statusUpdateRunnable );
23562409 }
2410+ if (mNotificationManager == null ) {
2411+ mNotificationManager = (NotificationManager ) this .getSystemService (Context .NOTIFICATION_SERVICE );
2412+ mNotificationManager .cancelAll ();
2413+ }
23572414 }
23582415
23592416 /**
0 commit comments