-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathUploadNotification.java
More file actions
133 lines (116 loc) · 5.18 KB
/
UploadNotification.java
File metadata and controls
133 lines (116 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.spoon.backgroundfileupload;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import androidx.annotation.IntegerRes;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.work.ForegroundInfo;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import android.content.pm.ServiceInfo;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
public class UploadNotification {
private Context context;
private static final int notificationId = new Random().nextInt();
public static String notificationTitle = "Default title";
public static String notificationRetryText = "Please check your internet connection";
@IntegerRes
public static int notificationIconRes = 0;
public static String notificationIntentActivity;
public static NotificationManager notificationManager = null;
public static NotificationCompat.Builder notificationBuilder = null;
@RequiresApi(api = Build.VERSION_CODES.O)
UploadNotification(Context context) {
this.context = context;
notificationBuilder = getUploadNotification(context);
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(new NotificationChannel(
UploadTask.NOTIFICATION_CHANNEL_ID,
UploadTask.NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW
));
}
public void updateProgress() {
List<WorkInfo> workInfo;
try {
workInfo = WorkManager.getInstance(context)
.getWorkInfosByTag(FileTransferBackground.getCurrentTag(context))
.get();
} catch (ExecutionException | InterruptedException e) {
Log.w(UploadTask.TAG, "getForegroundInfo: Problem while retrieving task list:", e);
workInfo = Collections.emptyList();
}
int uploadDone = 0;
int uploadCount = 0;
for (WorkInfo info : workInfo) {
if (info.getState().isFinished()) {
uploadDone++;
}
uploadCount++;
}
float totalProgressStore = ((float) uploadDone) / uploadCount;
notificationBuilder.setProgress(100, (int) (totalProgressStore * 100f), false);
notificationManager.notify(UploadNotification.notificationId, notificationBuilder.build());
}
public static void configure(final String title, @IntegerRes final int icon, final String intentActivity) {
notificationTitle = title;
notificationIconRes = icon;
notificationIntentActivity = intentActivity;
}
public static Notification createNotification(NotificationCompat.Builder notificationBuilder) {
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
return notification;
}
@RequiresApi(api = Build.VERSION_CODES.O)
private static NotificationCompat.Builder getUploadNotification(final Context context) {
Class<?> mainActivityClass = null;
try {
mainActivityClass = Class.forName(notificationIntentActivity);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Intent notificationIntent = new Intent(context, mainActivityClass);
int pendingIntentFlag;
if (Build.VERSION.SDK_INT >= 23) {
pendingIntentFlag = PendingIntent.FLAG_IMMUTABLE;
} else {
pendingIntentFlag = 0;
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, pendingIntentFlag);
// TODO: click intent open app
@SuppressLint("ResourceType")
NotificationCompat.Builder uploadNotificationBuilder = new NotificationCompat.Builder(context,
UploadTask.NOTIFICATION_CHANNEL_ID)
.setContentTitle(notificationTitle)
.setTicker(notificationTitle)
.setSmallIcon(notificationIconRes)
.setColor(Color.rgb(57, 100, 150))
.setContentIntent(pendingIntent)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setProgress(100, 0, false)
.setChannelId(UploadTask.NOTIFICATION_CHANNEL_ID)
.addAction(notificationIconRes, "Open", pendingIntent);
return uploadNotificationBuilder;
}
public ForegroundInfo getForegroundInfo() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
return new ForegroundInfo(notificationId, notificationBuilder.build(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
}
return new ForegroundInfo(notificationId, notificationBuilder.build());
}
}