@ -17,6 +17,7 @@ package com.keylesspalace.tusky.util;
import android.app.NotificationChannel ;
import android.app.NotificationChannelGroup ;
import android.app.NotificationManager ;
import android.app.PendingIntent ;
import android.content.Context ;
import android.content.Intent ;
@ -47,12 +48,12 @@ import java.io.IOException;
import java.util.ArrayList ;
import java.util.List ;
public class NotificationManag er {
public class NotificationHelp er {
/** constants used in Intents */
public static final String ACCOUNT_ID = "account_id" ;
private static final String TAG = "NotificationManag er" ;
private static final String TAG = "NotificationHelp er" ;
/** notification channels used on Android O+ **/
private static final String CHANNEL_MENTION = "CHANNEL_MENTION" ;
@ -71,7 +72,7 @@ public class NotificationManager {
public static void make ( final Context context , Notification body , AccountEntity account ) {
if ( ! filterNotification ( account , body ) ) {
if ( ! filterNotification ( account , body , context ) ) {
return ;
}
@ -166,8 +167,7 @@ public class NotificationManager {
builder . setVisibility ( NotificationCompat . VISIBILITY_PRIVATE ) ;
builder . setCategory ( NotificationCompat . CATEGORY_SOCIAL ) ;
android . app . NotificationManager notificationManager = ( android . app . NotificationManager )
context . getSystemService ( Context . NOTIFICATION_SERVICE ) ;
NotificationManager notificationManager = ( NotificationManager ) context . getSystemService ( Context . NOTIFICATION_SERVICE ) ;
//noinspection ConstantConditions
notificationManager . notify ( ( int ) account . getId ( ) , builder . build ( ) ) ;
@ -176,8 +176,7 @@ public class NotificationManager {
public static void createNotificationChannelsForAccount ( AccountEntity account , Context context ) {
if ( Build . VERSION . SDK_INT > = Build . VERSION_CODES . O ) {
android . app . NotificationManager mNotificationManager =
( android . app . NotificationManager ) context . getSystemService ( Context . NOTIFICATION_SERVICE ) ;
NotificationManager notificationManager = ( NotificationManager ) context . getSystemService ( Context . NOTIFICATION_SERVICE ) ;
String [ ] channelIds = new String [ ] {
CHANNEL_MENTION + account . getIdentifier ( ) ,
@ -202,17 +201,18 @@ public class NotificationManager {
NotificationChannelGroup channelGroup = new NotificationChannelGroup ( account . getIdentifier ( ) , account . getFullName ( ) ) ;
//noinspection ConstantConditions
mN otificationManager. createNotificationChannelGroup ( channelGroup ) ;
n otificationManager. createNotificationChannelGroup ( channelGroup ) ;
for ( int i = 0 ; i < channelIds . length ; i + + ) {
String id = channelIds [ i ] ;
String name = context . getString ( channelNames [ i ] ) ;
String description = context . getString ( channelDescriptions [ i ] ) ;
int importance = android . app . NotificationManager . IMPORTANCE_DEFAULT ;
int importance = NotificationManager . IMPORTANCE_DEFAULT ;
NotificationChannel channel = new NotificationChannel ( id , name , importance ) ;
channel . setDescription ( description ) ;
channel . enableLights ( true ) ;
channel . setLightColor ( 0xFF2B90D9 ) ;
channel . enableVibration ( true ) ;
channel . setShowBadge ( true ) ;
channel . setGroup ( account . getIdentifier ( ) ) ;
@ -220,7 +220,7 @@ public class NotificationManager {
}
//noinspection ConstantConditions
mN otificationManager. createNotificationChannels ( channels ) ;
n otificationManager. createNotificationChannels ( channels ) ;
}
}
@ -228,11 +228,10 @@ public class NotificationManager {
public static void deleteNotificationChannelsForAccount ( AccountEntity account , Context context ) {
if ( Build . VERSION . SDK_INT > = Build . VERSION_CODES . O ) {
android . app . NotificationManager mNotificationManager =
( android . app . NotificationManager ) context . getSystemService ( Context . NOTIFICATION_SERVICE ) ;
NotificationManager notificationManager = ( NotificationManager ) context . getSystemService ( Context . NOTIFICATION_SERVICE ) ;
//noinspection ConstantConditions
mN otificationManager. deleteNotificationChannelGroup ( account . getIdentifier ( ) ) ;
n otificationManager. deleteNotificationChannelGroup ( account . getIdentifier ( ) ) ;
}
}
@ -241,17 +240,42 @@ public class NotificationManager {
// delete the notification channels that where used before the multi account mode was introduced to avoid confusion
if ( Build . VERSION . SDK_INT > = Build . VERSION_CODES . O ) {
android . app . NotificationManager mNotificationManager =
( android . app . NotificationManager ) context . getSystemService ( Context . NOTIFICATION_SERVICE ) ;
NotificationManager notificationManager = ( NotificationManager ) context . getSystemService ( Context . NOTIFICATION_SERVICE ) ;
//noinspection ConstantConditions
mN otificationManager. deleteNotificationChannel ( CHANNEL_MENTION ) ;
mN otificationManager. deleteNotificationChannel ( CHANNEL_FAVOURITE ) ;
mN otificationManager. deleteNotificationChannel ( CHANNEL_BOOST ) ;
mN otificationManager. deleteNotificationChannel ( CHANNEL_FOLLOW ) ;
n otificationManager. deleteNotificationChannel ( CHANNEL_MENTION ) ;
n otificationManager. deleteNotificationChannel ( CHANNEL_FAVOURITE ) ;
n otificationManager. deleteNotificationChannel ( CHANNEL_BOOST ) ;
n otificationManager. deleteNotificationChannel ( CHANNEL_FOLLOW ) ;
}
}
public static boolean areNotificationsEnabled ( Context context ) {
if ( Build . VERSION . SDK_INT > = Build . VERSION_CODES . O ) {
// on Android >= O, notifications are enabled, if at least one channel is enabled
NotificationManager notificationManager = ( NotificationManager ) context . getSystemService ( Context . NOTIFICATION_SERVICE ) ;
//noinspection ConstantConditions
if ( notificationManager . areNotificationsEnabled ( ) ) {
for ( NotificationChannel channel : notificationManager . getNotificationChannels ( ) ) {
if ( channel . getImportance ( ) > NotificationManager . IMPORTANCE_NONE ) {
Log . d ( TAG , "NotificationsEnabled" ) ;
return true ;
}
}
}
Log . d ( TAG , "NotificationsDisabled" ) ;
return false ;
} else {
// on Android < O, notifications are enabled, if at least one account has notification enabled
return TuskyApplication . getAccountManager ( ) . areNotificationsEnabled ( ) ;
}
}
public static void clearNotificationsForActiveAccount ( Context context ) {
AccountManager accountManager = TuskyApplication . getAccountManager ( ) ;
AccountEntity account = accountManager . getActiveAccount ( ) ;
@ -259,18 +283,21 @@ public class NotificationManager {
account . setActiveNotifications ( "[]" ) ;
accountManager . saveAccount ( account ) ;
android . app . NotificationManager manager = ( android . app . NotificationManager )
context . getSystemService ( Context . NOTIFICATION_SERVICE ) ;
NotificationManager notificationManager = ( NotificationManager ) context . getSystemService ( Context . NOTIFICATION_SERVICE ) ;
//noinspection ConstantConditions
m anager. cancel ( ( int ) account . getId ( ) ) ;
notificationM anager. cancel ( ( int ) account . getId ( ) ) ;
}
}
private static boolean filterNotification ( AccountEntity account ,
Notification notification ) {
private static boolean filterNotification ( AccountEntity account , Notification notification ,
Context context ) {
if ( Build . VERSION . SDK_INT > = Build . VERSION_CODES . O ) {
return true ; //do not filter on Android O or newer, the system does it for us
NotificationManager notificationManager = ( NotificationManager ) context . getSystemService ( Context . NOTIFICATION_SERVICE ) ;
//noinspection ConstantConditions
NotificationChannel channel = notificationManager . getNotificationChannel ( getChannelId ( account , notification ) ) ;
return channel . getImportance ( ) > NotificationManager . IMPORTANCE_NONE ;
}
switch ( notification . type ) {
@ -317,7 +344,7 @@ public class NotificationManager {
}
if ( account . getNotificationLight ( ) ) {
builder . setLights ( 0xFF00FF8F , 300 , 1000 ) ;
builder . setLights ( 0xFF2B90D9 , 300 , 1000 ) ;
}
}