6
6
import android .annotation .TargetApi ;
7
7
import android .app .AlertDialog ;
8
8
import android .app .DatePickerDialog ;
9
+ import android .app .NotificationManager ;
10
+ import android .app .PendingIntent ;
9
11
import android .app .SearchManager ;
10
12
import android .content .BroadcastReceiver ;
11
13
import android .content .ClipData ;
44
46
import android .support .v4 .widget .DrawerLayout ;
45
47
import android .support .v4 .widget .SimpleCursorAdapter ;
46
48
import android .support .v7 .app .AppCompatDelegate ;
49
+ import android .support .v7 .app .NotificationCompat ;
47
50
import android .support .v7 .widget .LinearLayoutManager ;
48
51
import android .support .v7 .widget .RecyclerView ;
49
52
import android .support .v7 .widget .Toolbar ;
@@ -557,6 +560,8 @@ public Cursor runQuery(CharSequence charSequence) {
557
560
setupSnackBar ();
558
561
//Hides Keyboard if it was open with focus on an editText before restart of the activity
559
562
this .getWindow ().setSoftInputMode (WindowManager .LayoutParams .SOFT_INPUT_STATE_HIDDEN );
563
+
564
+ mNotificationManager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
560
565
}
561
566
562
567
/**
@@ -1099,6 +1104,49 @@ private MultipartBody.Part prepareFilePart(String partName, File file) {
1099
1104
return MultipartBody .Part .createFormData (partName , file .getName (), requestFile );
1100
1105
}
1101
1106
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
+
1102
1150
/**
1103
1151
* Function to upload file asynchronously to the server using retrofit callback
1104
1152
* upload {@link com.zulip.android.service.ZulipServices#upload(MultipartBody.Part)}
@@ -1112,6 +1160,10 @@ private void uploadFile(final File file) {
1112
1160
1113
1161
final String loadingMsg = getResources ().getString (R .string .uploading_message );
1114
1162
1163
+ // start notification
1164
+ // TODO: handle different notif ids
1165
+ setNotification (100 , getString (R .string .notif_title ), getString (R .string .init_notif_title ));
1166
+
1115
1167
// finally, execute the request
1116
1168
// create upload service client
1117
1169
Call <UploadResponse > call = ((ZulipApp ) getApplicationContext ()).getZulipServices ().upload (body );
@@ -1122,6 +1174,7 @@ public void onSuccess(Call<UploadResponse> call, Response<UploadResponse> respon
1122
1174
UploadResponse uploadResponse = response .body ();
1123
1175
filePathOnServer = uploadResponse .getUri ();
1124
1176
if (!filePathOnServer .equals ("" )) {
1177
+ endNotification (100 , getString (R .string .notif_title ), getString (R .string .finish_notif_title ));
1125
1178
// remove loading message from the screen
1126
1179
sendingMessage (false , loadingMsg );
1127
1180
@@ -2354,6 +2407,10 @@ protected void onDestroy() {
2354
2407
statusUpdateHandler .removeMessages (0 );
2355
2408
statusUpdateHandler .removeCallbacks (statusUpdateRunnable );
2356
2409
}
2410
+ if (mNotificationManager == null ) {
2411
+ mNotificationManager = (NotificationManager ) this .getSystemService (Context .NOTIFICATION_SERVICE );
2412
+ mNotificationManager .cancelAll ();
2413
+ }
2357
2414
}
2358
2415
2359
2416
/**
0 commit comments