|
|
@ -16,7 +16,6 @@ |
|
|
|
package com.keylesspalace.tusky.util; |
|
|
|
package com.keylesspalace.tusky.util; |
|
|
|
|
|
|
|
|
|
|
|
import android.app.NotificationChannel; |
|
|
|
import android.app.NotificationChannel; |
|
|
|
import android.app.NotificationManager; |
|
|
|
|
|
|
|
import android.app.PendingIntent; |
|
|
|
import android.app.PendingIntent; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.Intent; |
|
|
|
import android.content.Intent; |
|
|
@ -33,6 +32,7 @@ import android.support.v4.content.ContextCompat; |
|
|
|
import android.util.Log; |
|
|
|
import android.util.Log; |
|
|
|
|
|
|
|
|
|
|
|
import com.keylesspalace.tusky.MainActivity; |
|
|
|
import com.keylesspalace.tusky.MainActivity; |
|
|
|
|
|
|
|
import com.keylesspalace.tusky.NotificationPullJobCreator; |
|
|
|
import com.keylesspalace.tusky.R; |
|
|
|
import com.keylesspalace.tusky.R; |
|
|
|
import com.keylesspalace.tusky.entity.Notification; |
|
|
|
import com.keylesspalace.tusky.entity.Notification; |
|
|
|
import com.keylesspalace.tusky.receiver.NotificationClearBroadcastReceiver; |
|
|
|
import com.keylesspalace.tusky.receiver.NotificationClearBroadcastReceiver; |
|
|
@ -46,10 +46,12 @@ import java.io.IOException; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
public class NotificationMaker { |
|
|
|
public class NotificationManager { |
|
|
|
private static final String TAG = "NotificationMaker"; |
|
|
|
private static final String TAG = "NotificationManager"; |
|
|
|
|
|
|
|
|
|
|
|
/** notification channels used on Android O+ **/ |
|
|
|
/** |
|
|
|
|
|
|
|
* notification channels used on Android O+ |
|
|
|
|
|
|
|
**/ |
|
|
|
private static final String CHANNEL_MENTION = "CHANNEL_MENTION"; |
|
|
|
private static final String CHANNEL_MENTION = "CHANNEL_MENTION"; |
|
|
|
private static final String CHANNEL_FOLLOW = "CHANNEL_FOLLOW"; |
|
|
|
private static final String CHANNEL_FOLLOW = "CHANNEL_FOLLOW"; |
|
|
|
private static final String CHANNEL_BOOST = "CHANNEL_BOOST"; |
|
|
|
private static final String CHANNEL_BOOST = "CHANNEL_BOOST"; |
|
|
@ -102,7 +104,7 @@ public class NotificationMaker { |
|
|
|
|
|
|
|
|
|
|
|
notificationPreferences.edit() |
|
|
|
notificationPreferences.edit() |
|
|
|
.putString("current", currentNotifications.toString()) |
|
|
|
.putString("current", currentNotifications.toString()) |
|
|
|
.commit(); |
|
|
|
.apply(); |
|
|
|
|
|
|
|
|
|
|
|
Intent resultIntent = new Intent(context, MainActivity.class); |
|
|
|
Intent resultIntent = new Intent(context, MainActivity.class); |
|
|
|
resultIntent.putExtra("tab_position", 1); |
|
|
|
resultIntent.putExtra("tab_position", 1); |
|
|
@ -160,16 +162,17 @@ public class NotificationMaker { |
|
|
|
builder.setCategory(android.app.Notification.CATEGORY_SOCIAL); |
|
|
|
builder.setCategory(android.app.Notification.CATEGORY_SOCIAL); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
NotificationManager notificationManager = |
|
|
|
android.app.NotificationManager notificationManager = (android.app.NotificationManager) |
|
|
|
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
|
|
|
context.getSystemService(Context.NOTIFICATION_SERVICE); |
|
|
|
|
|
|
|
//noinspection ConstantConditions
|
|
|
|
notificationManager.notify(notifyId, builder.build()); |
|
|
|
notificationManager.notify(notifyId, builder.build()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void createNotificationChannels(Context context) { |
|
|
|
public static void createNotificationChannels(Context context) { |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
|
|
|
|
|
|
|
|
|
|
|
NotificationManager mNotificationManager = |
|
|
|
android.app.NotificationManager mNotificationManager = |
|
|
|
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
|
|
|
(android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
|
|
|
|
|
|
|
|
|
|
|
String[] channelIds = new String[]{CHANNEL_MENTION, CHANNEL_FOLLOW, CHANNEL_BOOST, CHANNEL_FAVOURITE}; |
|
|
|
String[] channelIds = new String[]{CHANNEL_MENTION, CHANNEL_FOLLOW, CHANNEL_BOOST, CHANNEL_FAVOURITE}; |
|
|
|
int[] channelNames = { |
|
|
|
int[] channelNames = { |
|
|
@ -191,7 +194,7 @@ public class NotificationMaker { |
|
|
|
String id = channelIds[i]; |
|
|
|
String id = channelIds[i]; |
|
|
|
String name = context.getString(channelNames[i]); |
|
|
|
String name = context.getString(channelNames[i]); |
|
|
|
String description = context.getString(channelDescriptions[i]); |
|
|
|
String description = context.getString(channelDescriptions[i]); |
|
|
|
int importance = NotificationManager.IMPORTANCE_DEFAULT; |
|
|
|
int importance = android.app.NotificationManager.IMPORTANCE_DEFAULT; |
|
|
|
NotificationChannel channel = new NotificationChannel(id, name, importance); |
|
|
|
NotificationChannel channel = new NotificationChannel(id, name, importance); |
|
|
|
|
|
|
|
|
|
|
|
channel.setDescription(description); |
|
|
|
channel.setDescription(description); |
|
|
@ -201,11 +204,23 @@ public class NotificationMaker { |
|
|
|
channels.add(channel); |
|
|
|
channels.add(channel); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//noinspection ConstantConditions
|
|
|
|
mNotificationManager.createNotificationChannels(channels); |
|
|
|
mNotificationManager.createNotificationChannels(channels); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void clearNotifications(Context context) { |
|
|
|
|
|
|
|
SharedPreferences notificationPreferences = |
|
|
|
|
|
|
|
context.getSharedPreferences("Notifications", Context.MODE_PRIVATE); |
|
|
|
|
|
|
|
notificationPreferences.edit().putString("current", "[]").apply(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
android.app.NotificationManager manager = (android.app.NotificationManager) |
|
|
|
|
|
|
|
context.getSystemService(Context.NOTIFICATION_SERVICE); |
|
|
|
|
|
|
|
//noinspection ConstantConditions
|
|
|
|
|
|
|
|
manager.cancel(NotificationPullJobCreator.NOTIFY_ID); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static boolean filterNotification(SharedPreferences preferences, |
|
|
|
private static boolean filterNotification(SharedPreferences preferences, |
|
|
|
Notification notification) { |
|
|
|
Notification notification) { |
|
|
|
|
|
|
|
|
|
|
@ -241,6 +256,7 @@ public class NotificationMaker { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("SameParameterValue") |
|
|
|
private static String truncateWithEllipses(String string, int limit) { |
|
|
|
private static String truncateWithEllipses(String string, int limit) { |
|
|
|
if (string.length() < limit) { |
|
|
|
if (string.length() < limit) { |
|
|
|
return string; |
|
|
|
return string; |