Skip to content

Commit 889caf9

Browse files
committed
Added NotificationChannelManager to support notifications on Android 8.0 | Close #121.
1 parent 262e9e4 commit 889caf9

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

FlowCrypt/src/main/java/com/flowcrypt/email/FlowCryptApplication.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.support.v4.app.FragmentManager;
1212
import android.support.v7.preference.PreferenceManager;
1313

14+
import com.flowcrypt.email.ui.NotificationChannelManager;
1415
import com.flowcrypt.email.util.SharedPreferencesHelper;
1516
import com.squareup.leakcanary.LeakCanary;
1617

@@ -65,6 +66,8 @@ public class FlowCryptApplication extends Application {
6566
@Override
6667
public void onCreate() {
6768
super.onCreate();
69+
NotificationChannelManager.registerNotificationChannels(this);
70+
6871
intiLeakCanary();
6972
FragmentManager.enableDebugLogging(BuildConfig.DEBUG);
7073
}

FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/AttachmentNotificationManager.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.flowcrypt.email.R;
2020
import com.flowcrypt.email.api.email.model.AttachmentInfo;
21+
import com.flowcrypt.email.ui.NotificationChannelManager;
2122

2223
/**
2324
* This manager is responsible for displaying attachment notifications.
@@ -45,7 +46,8 @@ public AttachmentNotificationManager(Context context) {
4546
* @param attachmentInfo {@link AttachmentInfo} object which contains a detail information about an attachment.
4647
*/
4748
public void attachmentAddedToLoadQueue(Context context, int startId, AttachmentInfo attachmentInfo) {
48-
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
49+
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,
50+
NotificationChannelManager.CHANNEL_ID_ATTACHMENTS)
4951
.setWhen(startId)
5052
.setShowWhen(false)
5153
.setProgress(0, 0, true)
@@ -72,7 +74,8 @@ public void attachmentAddedToLoadQueue(Context context, int startId, AttachmentI
7274
*/
7375
public void updateLoadingProgress(Context context, int startId, AttachmentInfo attachmentInfo,
7476
int progress, long timeLeftInMillisecond) {
75-
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
77+
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,
78+
NotificationChannelManager.CHANNEL_ID_ATTACHMENTS)
7679
.setProgress(MAX_FILE_SIZE_IN_PERCENTAGE, progress, timeLeftInMillisecond == 0)
7780
.setWhen(startId)
7881
.setShowWhen(false)
@@ -103,7 +106,8 @@ public void downloadComplete(Context context, int startId, AttachmentInfo attach
103106

104107
PendingIntent pendingIntentOpenFile = PendingIntent.getActivity(context, 0, intentOpenFile, 0);
105108

106-
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
109+
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,
110+
NotificationChannelManager.CHANNEL_ID_ATTACHMENTS)
107111
.setProgress(0, 0, false)
108112
.setAutoCancel(true)
109113
.setOngoing(false)
@@ -128,7 +132,8 @@ public void downloadComplete(Context context, int startId, AttachmentInfo attach
128132
* @param e The {@link Exception} which contains a detail information about happened error..
129133
*/
130134
public void errorHappened(Context context, int startId, AttachmentInfo attachmentInfo, Exception e) {
131-
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
135+
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,
136+
NotificationChannelManager.CHANNEL_ID_ATTACHMENTS)
132137
.setProgress(0, 0, false)
133138
.setAutoCancel(true)
134139
.setOngoing(false)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Business Source License 1.0 © 2017 FlowCrypt Limited ([email protected]).
3+
* Use limitations apply. See https://github.com/FlowCrypt/flowcrypt-android/blob/master/LICENSE
4+
* Contributors: DenBond7
5+
*/
6+
7+
package com.flowcrypt.email.ui;
8+
9+
import android.app.NotificationChannel;
10+
import android.app.NotificationManager;
11+
import android.content.Context;
12+
import android.os.Build;
13+
import android.support.annotation.RequiresApi;
14+
15+
import com.flowcrypt.email.R;
16+
17+
/**
18+
* This manager does job of register {@link NotificationChannel} of the app. The {@link NotificationChannel} was
19+
* added in the {@link Build.VERSION_CODES#O} and doesn't work on previous Android versions.
20+
*
21+
* @author Denis Bondarenko
22+
* Date: 17.10.2017
23+
* Time: 12:12
24+
25+
*/
26+
27+
public class NotificationChannelManager {
28+
public static final String CHANNEL_ID_ATTACHMENTS = "Attachments";
29+
30+
/**
31+
* Register {@link NotificationChannel}(s) of the app in the system.
32+
*
33+
* @param context Interface to global information about an application environment.
34+
*/
35+
public static void registerNotificationChannels(Context context) {
36+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
37+
NotificationManager notificationManager =
38+
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
39+
40+
if (notificationManager != null) {
41+
notificationManager.createNotificationChannel(generateAttachmentsNotificationChannel(context));
42+
}
43+
}
44+
}
45+
46+
/**
47+
* Generate attachments notification channel.
48+
*
49+
* @param context Interface to global information about an application environment.
50+
* @return Generated {@link NotificationChannel}
51+
*/
52+
@RequiresApi(api = Build.VERSION_CODES.O)
53+
private static NotificationChannel generateAttachmentsNotificationChannel(Context context) {
54+
CharSequence name = context.getString(R.string.attachments);
55+
String description = context.getString(R.string.download_attachments_notification_channel);
56+
int importance = NotificationManager.IMPORTANCE_DEFAULT;
57+
58+
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_ATTACHMENTS, name, importance);
59+
notificationChannel.setDescription(description);
60+
notificationChannel.enableLights(false);
61+
notificationChannel.enableVibration(false);
62+
63+
return notificationChannel;
64+
}
65+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,6 @@
199199
<string name="experimental">Experimental</string>
200200
<string name="experimental_settings">Experimental Settings</string>
201201
<string name="simulate_app_crash">Simulate app crash</string>
202+
<string name="attachments">Attachments</string>
203+
<string name="download_attachments_notification_channel">The download attachments notification channel</string>
202204
</resources>

0 commit comments

Comments
 (0)