Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit a7c1a44

Browse files
Sam1301kunall17
authored andcommitted
Add a cancel button to progress notifications.
1 parent e58235e commit a7c1a44

File tree

5 files changed

+71
-9
lines changed

5 files changed

+71
-9
lines changed

app/src/main/java/com/zulip/android/activities/ZulipActivity.java

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,16 @@ private MultipartBody.Part prepareFilePart(String partName, final File file, int
11271127
private NotificationManager mNotificationManager;
11281128
private NotificationCompat.Builder mBuilder;
11291129

1130+
private HashMap<Integer, Call<UploadResponse>> mCancelHashMap;
1131+
1132+
public void cancelRequest(int notificationId) throws NullPointerException{
1133+
Call<UploadResponse> call = mCancelHashMap.get(notificationId);
1134+
if (call != null) {
1135+
call.cancel();
1136+
mCancelHashMap.remove(notificationId);
1137+
}
1138+
}
1139+
11301140
/**
11311141
* Modifies notification {@link Notifications} of specifies {@param notificationId}
11321142
* and sets its {@param content}.
@@ -1142,6 +1152,7 @@ private void setNotification(int notificationId, String content) {
11421152
.setOngoing(false)
11431153
// Removes the progress bar
11441154
.setProgress(0, 0, false);
1155+
mBuilder.mActions.clear();
11451156
PendingIntent contentIntent = PendingIntent.getActivity(
11461157
getApplicationContext(),
11471158
0,
@@ -1158,13 +1169,15 @@ private void setNotification(int notificationId, String content) {
11581169
* @param notificationId
11591170
* @param percentage
11601171
*/
1161-
private void progressNotification(int notificationId, int percentage, String progress, String title) {
1172+
private void progressNotification(int notificationId, int percentage, String progress, String title, PendingIntent pendingIntent) {
11621173
mBuilder.setSmallIcon(android.R.drawable.stat_sys_upload)
11631174
.setContentTitle(title)
11641175
.setContentText(progress)
11651176
.setAutoCancel(false)
11661177
.setOngoing(true)
11671178
.setProgress(100, percentage, false);
1179+
mBuilder.mActions.clear();
1180+
mBuilder.addAction(R.drawable.ic_cancel_black_24dp, getString(R.string.cancel_content_desp), pendingIntent);
11681181
mNotificationManager.notify(notificationId, mBuilder.build());
11691182
}
11701183

@@ -1188,6 +1201,7 @@ private void endNotification(int notificationId, String content) {
11881201
new Intent(),
11891202
PendingIntent.FLAG_UPDATE_CURRENT);
11901203
mBuilder.setContentIntent(contentIntent);
1204+
mBuilder.mActions.clear();
11911205
mNotificationManager.notify(notificationId, mBuilder.build());
11921206
}
11931207

@@ -1207,6 +1221,12 @@ private void uploadFile(final File file) {
12071221
// generate unique notification Id for this upload
12081222
final int notifId = (int) (new Date().getTime() % Integer.MAX_VALUE);
12091223

1224+
// cancel pending intent
1225+
Intent actionIntent = new Intent(this, GcmBroadcastReceiver.class);
1226+
actionIntent.putExtra("id", notifId);
1227+
actionIntent.setAction(Constants.CANCEL);
1228+
final PendingIntent piAction = PendingIntent.getBroadcast(this, notifId, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
1229+
12101230
// update notification after every one second
12111231
final long startTime = System.currentTimeMillis();
12121232
final int[] counter = {0};
@@ -1215,7 +1235,7 @@ private void uploadFile(final File file) {
12151235
public void onProgressUpdate(int percentage, String progress, int notificationId) {
12161236
if (System.currentTimeMillis() - startTime >= 1000 * counter[0]) {
12171237
// update notification
1218-
progressNotification(notificationId, percentage, progress, file.getName());
1238+
progressNotification(notificationId, percentage, progress, file.getName(), piAction);
12191239
counter[0]++;
12201240
}
12211241
}
@@ -1224,12 +1244,16 @@ public void onProgressUpdate(int percentage, String progress, int notificationId
12241244
// MultipartBody.Part is used to send also the actual file name
12251245
MultipartBody.Part body = prepareFilePart("file", file, notifId, progressListener);
12261246

1227-
// start notification
1228-
setNotification(notifId, getString(R.string.init_notif_title) + getString(R.string.to_string) + getNotifTitle());
1229-
12301247
// finally, execute the request
12311248
// create upload service client
12321249
Call<UploadResponse> call = ((ZulipApp) getApplicationContext()).getUploadServices().upload(body);
1250+
if (mCancelHashMap == null) {
1251+
mCancelHashMap = new HashMap<>();
1252+
}
1253+
mCancelHashMap.put(notifId, call);
1254+
Toast.makeText(this, R.string.upload_started_str, Toast.LENGTH_SHORT).show();
1255+
// start notification
1256+
setNotification(notifId, getString(R.string.init_notif_title) + getString(R.string.to_string) + getNotifTitle());
12331257
call.enqueue(new DefaultCallback<UploadResponse>() {
12341258
@Override
12351259
public void onSuccess(Call<UploadResponse> call, Response<UploadResponse> response) {
@@ -1250,8 +1274,13 @@ public void onSuccess(Call<UploadResponse> call, Response<UploadResponse> respon
12501274
} else {
12511275
endNotification(notifId, getString(R.string.failed_to_upload));
12521276
}
1253-
mNotificationManager.cancel(notifId);
1254-
1277+
if (!(mCancelHashMap != null && mCancelHashMap.size() == 1)) {
1278+
mNotificationManager.cancel(notifId);
1279+
}
1280+
if (mCancelHashMap != null) {
1281+
mCancelHashMap.remove(notifId);
1282+
mCancelHashMap = mCancelHashMap.isEmpty() ? null : mCancelHashMap;
1283+
}
12551284
}
12561285

12571286
@Override
@@ -1268,6 +1297,10 @@ public void onFailure(Call<UploadResponse> call, Throwable t) {
12681297
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && isDestroyed()) {
12691298
return;
12701299
}
1300+
if (call.isCanceled()) {
1301+
mNotificationManager.cancel(notifId);
1302+
return;
1303+
}
12711304
endNotification(notifId, getString(R.string.failed_to_upload));
12721305
mNotificationManager.cancel(notifId);
12731306
ZLog.logException(t);
@@ -2501,6 +2534,11 @@ protected void onDestroy() {
25012534
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
25022535
}
25032536
mNotificationManager.cancelAll();
2537+
if (mCancelHashMap != null) {
2538+
for (Call call : mCancelHashMap.values()) {
2539+
call.cancel();
2540+
}
2541+
}
25042542
}
25052543

25062544
/**

app/src/main/java/com/zulip/android/gcm/GcmBroadcastReceiver.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import android.util.Log;
99

1010
import com.google.android.gms.gcm.GoogleCloudMessaging;
11+
import com.zulip.android.ZulipApp;
12+
import com.zulip.android.util.Constants;
13+
import com.zulip.android.util.ZLog;
1114

1215
// This class receives GCM messages from the cloud. It then passes them through a series of steps
1316
// to get a notification on the menu bar. See
@@ -22,13 +25,24 @@
2225
public class GcmBroadcastReceiver extends BroadcastReceiver {
2326

2427
private static final String TAG = "GCM";
25-
2628
public static String getGCMReceiverAction(Context appContext) {
2729
return appContext.getPackageName() + ".PushMessage.BROADCAST";
2830
}
2931

3032
@Override
3133
public void onReceive(Context context, Intent intent) {
34+
// handle cancel notification action on uploads
35+
final String action = intent.getAction();
36+
if (Constants.CANCEL.equals(action)) {
37+
int notifId = intent.getIntExtra("id", 0);
38+
try {
39+
ZulipApp.get().getZulipActivity().cancelRequest(notifId);
40+
} catch (NullPointerException e) {
41+
ZLog.log("onReceive: app destroyed but notification visible");
42+
return;
43+
}
44+
}
45+
3246
Bundle extras = intent.getExtras();
3347
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
3448
// The getMessageType() intent parameter must be the intent you received

app/src/main/java/com/zulip/android/util/Constants.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.zulip.android.util;
22

3+
import static android.R.string.cancel;
4+
35
/**
46
* List of all Constants used in the projects
57
*/
@@ -16,7 +18,6 @@ public class Constants {
1618
public static String DATE_FORMAT = "dd/MM/yyyy";
1719
public static final int HIDE_FAB_AFTER_SEC = 5;
1820
public static final int REQUEST_PICK_FILE = 3;
19-
2021
public static final long STATUS_UPDATER_INTERVAL = 25 * 1000; //in milliseconds
2122
public static final int INACTIVE_TIME_OUT = 2 * 60; //in milliseconds
2223
public static final int PEOPLE_DRAWER_ACTIVE_GROUP_ID = 0;
@@ -26,4 +27,5 @@ public class Constants {
2627
public static final int ALL_PEOPLE_ID = -1;
2728
// row number which is used to differentiate the '@-mentions' in people drawer
2829
public static int MENTIONS = -2;
30+
public static final String CANCEL = "Cancel";
2931
}

app/src/main/java/com/zulip/android/util/ZLog.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ public static void logException(Throwable e) {
1717
}
1818
}
1919

20+
public static void log(String message) {
21+
if (!BuildHelper.shouldLogToCrashlytics()) {
22+
Log.e("Error", message);
23+
} else {
24+
Crashlytics.log(message);
25+
}
26+
}
2027
}

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,5 @@
173173
<string name="photo_dialog">Take photo</string>
174174
<string name="pick_dialog">Pick a file</string>
175175
<string name="to_string">" to "</string>
176+
<string name="upload_started_str">Upload started</string>
176177
</resources>

0 commit comments

Comments
 (0)