Create custom channels

This commit is contained in:
stom79 2018-09-16 18:03:24 +02:00
parent 32d00e04e3
commit 8a5cee5447
7 changed files with 76 additions and 13 deletions

View File

@ -368,6 +368,15 @@ public class Helper {
NONE
}
public enum NotifType{
FOLLLOW,
MENTION,
BOOST,
FAV,
BACKUP,
STORE,
TOOT
}
private static boolean isPerformingSearch = false;
/**
@ -784,7 +793,7 @@ public class Helper {
* @param title String title of the notification
* @param message String message for the notification
*/
public static void notify_user(Context context, Intent intent, int notificationId, Bitmap icon, String title, String message ) {
public static void notify_user(Context context, Intent intent, int notificationId, Bitmap icon, NotifType notifType, String title, String message ) {
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
// prepare intent which is triggered if the user click on the notification
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
@ -792,9 +801,43 @@ public class Helper {
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// build notification
String channelId = "channel_"+ String.valueOf(notificationId);
String channelId;
String channelTitle;
switch(notifType){
case BOOST:
channelId = "channel_boost";
channelTitle = context.getString(R.string.channel_notif_boost);
break;
case FAV:
channelId = "channel_fav";
channelTitle = context.getString(R.string.channel_notif_fav);
break;
case FOLLLOW:
channelId = "channel_follow";
channelTitle = context.getString(R.string.channel_notif_follow);
break;
case MENTION:
channelId = "channel_mention";
channelTitle = context.getString(R.string.channel_notif_mention);
break;
case BACKUP:
channelId = "channel_backup";
channelTitle = context.getString(R.string.channel_notif_backup);
break;
case STORE:
channelId = "channel_store";
channelTitle = context.getString(R.string.channel_notif_media);
break;
case TOOT:
channelId = "channel_toot";
channelTitle = context.getString(R.string.channel_notif_toot);
break;
default:
channelId = "channel_boost";
channelTitle = context.getString(R.string.channel_notif_boost);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, title, NotificationManager.IMPORTANCE_DEFAULT);
NotificationChannel channel = new NotificationChannel(channelId, channelTitle, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(channel);
@ -899,7 +942,7 @@ public class Helper {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
notify_user(context, intent, notificationIdTmp, BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_launcher), context.getString(R.string.save_over), context.getString(R.string.download_from, fileName));
R.mipmap.ic_launcher), NotifType.STORE, context.getString(R.string.save_over), context.getString(R.string.download_from, fileName));
Toast.makeText(context, R.string.toast_saved,Toast.LENGTH_LONG).show();
return false;
}
@ -907,7 +950,7 @@ public class Helper {
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
notify_user(context, intent, notificationIdTmp, resource, context.getString(R.string.save_over), context.getString(R.string.download_from, fileName));
notify_user(context, intent, notificationIdTmp, resource, NotifType.STORE, context.getString(R.string.save_over), context.getString(R.string.download_from, fileName));
Toast.makeText(context, R.string.toast_saved,Toast.LENGTH_LONG).show();
}
});

View File

@ -193,7 +193,7 @@ public class HomeTimelineSyncJob extends Job {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
notify_user(getContext(), intent, notificationId, BitmapFactory.decodeResource(getContext().getResources(),
R.drawable.mastodonlogo), finalTitle, finalMessage);
R.drawable.mastodonlogo), Helper.NotifType.TOOT, finalTitle, finalMessage);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_HOMETIMELINE_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), statuses.get(0).getId());
editor.apply();
@ -203,7 +203,7 @@ public class HomeTimelineSyncJob extends Job {
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
notify_user(getContext(), intent, notificationId, resource, finalTitle, finalMessage);
notify_user(getContext(), intent, notificationId, resource, Helper.NotifType.TOOT, finalTitle, finalMessage);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_HOMETIMELINE_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), statuses.get(0).getId());
editor.apply();

View File

@ -167,9 +167,12 @@ public class NotificationsSyncJob extends Job {
String title = null;
final String message;
String targeted_account = null;
Helper.NotifType notifType = Helper.NotifType.MENTION;
for(Notification notification: notifications){
switch (notification.getType()){
case "mention":
notifType = Helper.NotifType.MENTION;
if(notif_mention){
newMentions++;
if( notificationUrl == null){
@ -182,6 +185,7 @@ public class NotificationsSyncJob extends Job {
}
break;
case "reblog":
notifType = Helper.NotifType.BOOST;
if(notif_share){
newShare++;
if( notificationUrl == null){
@ -195,6 +199,7 @@ public class NotificationsSyncJob extends Job {
}
break;
case "favourite":
notifType = Helper.NotifType.FAV;
if(notif_add){
newAdds++;
if( notificationUrl == null){
@ -207,6 +212,7 @@ public class NotificationsSyncJob extends Job {
}
break;
case "follow":
notifType = Helper.NotifType.FOLLLOW;
if(notif_follow){
newFollows++;
if( notificationUrl == null){
@ -247,6 +253,7 @@ public class NotificationsSyncJob extends Job {
Handler mainHandler = new Handler(Looper.getMainLooper());
final String finalNotificationUrl = notificationUrl;
Helper.NotifType finalNotifType = notifType;
Runnable myRunnable = new Runnable() {
@Override
public void run() {
@ -263,7 +270,7 @@ public class NotificationsSyncJob extends Job {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
notify_user(getContext(), intent, notificationId, BitmapFactory.decodeResource(getContext().getResources(),
R.drawable.mastodonlogo), finalTitle, message);
R.drawable.mastodonlogo), finalNotifType, finalTitle, message);
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
if( lastNotif == null || Long.parseLong(notifications.get(0).getId()) > Long.parseLong(lastNotif)){
SharedPreferences.Editor editor = sharedpreferences.edit();
@ -276,7 +283,7 @@ public class NotificationsSyncJob extends Job {
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
notify_user(getContext(), intent, notificationId, resource, finalTitle, message);
notify_user(getContext(), intent, notificationId, resource, finalNotifType, finalTitle, message);
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
if( lastNotif == null || Long.parseLong(notifications.get(0).getId()) > Long.parseLong(lastNotif)){
SharedPreferences.Editor editor = sharedpreferences.edit();

View File

@ -128,7 +128,7 @@ public class BackupStatusInDataBaseService extends IntentService {
int notificationId = ((notif_id + 4) > 2147483647) ? (int) (2147483647 - notif_id - 4) : (int) (notif_id + 4);
String title = getString(R.string.data_backup_toots, account.getAcct());
notify_user(getApplicationContext(), mainActivity, notificationId, BitmapFactory.decodeResource(getResources(),
R.drawable.mastodonlogo), title, message);
R.drawable.mastodonlogo), Helper.NotifType.BACKUP, title, message);
} catch (Exception e) {
e.printStackTrace();
message = getString(R.string.data_export_error, account.getAcct());

View File

@ -186,7 +186,7 @@ public class BackupStatusService extends IntentService {
int notificationId = ((notif_id + 3) > 2147483647) ? (int) (2147483647 - notif_id - 3) : (int) (notif_id + 3);
String title = getString(R.string.data_export_toots, account.getAcct());
notify_user(getApplicationContext(), intentOpen, notificationId, BitmapFactory.decodeResource(getResources(),
R.drawable.mastodonlogo), title, message);
R.drawable.mastodonlogo),Helper.NotifType.BACKUP, title, message);
} catch (Exception e) {
e.printStackTrace();
message = getString(R.string.data_export_error, account.getAcct());

View File

@ -403,6 +403,7 @@ public class LiveNotificationService extends Service {
boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String targeted_account = null;
Helper.NotifType notifType = Helper.NotifType.MENTION;
if((userId == null || !userId.equals(account.getId()) || activityPaused) && liveNotifications && canNotify && notify) {
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
@ -413,6 +414,7 @@ public class LiveNotificationService extends Service {
if( somethingToPush && notification != null){
switch (notification.getType()){
case "mention":
notifType = Helper.NotifType.MENTION;
if(notif_mention){
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
title = String.format("%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getString(R.string.notif_mention));
@ -421,6 +423,7 @@ public class LiveNotificationService extends Service {
}
break;
case "reblog":
notifType = Helper.NotifType.BOOST;
if(notif_share){
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
title = String.format("%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getString(R.string.notif_reblog));
@ -429,6 +432,7 @@ public class LiveNotificationService extends Service {
}
break;
case "favourite":
notifType = Helper.NotifType.FAV;
if(notif_add){
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
title = String.format("%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getString(R.string.notif_favourite));
@ -437,6 +441,7 @@ public class LiveNotificationService extends Service {
}
break;
case "follow":
notifType = Helper.NotifType.FOLLLOW;
if(notif_follow){
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
title = String.format("%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getString(R.string.notif_follow));
@ -463,6 +468,7 @@ public class LiveNotificationService extends Service {
Handler mainHandler = new Handler(Looper.getMainLooper());
Helper.NotifType finalNotifType = notifType;
Runnable myRunnable = new Runnable() {
@Override
public void run() {
@ -481,7 +487,7 @@ public class LiveNotificationService extends Service {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
notify_user(getApplicationContext(), intent, notificationId, BitmapFactory.decodeResource(getResources(),
R.drawable.mastodonlogo), finalTitle, "@"+account.getAcct()+"@"+account.getInstance());
R.drawable.mastodonlogo), finalNotifType, finalTitle, "@"+account.getAcct()+"@"+account.getInstance());
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
if (lastNotif == null || Long.parseLong(notification.getId()) > Long.parseLong(lastNotif)) {
SharedPreferences.Editor editor = sharedpreferences.edit();
@ -494,7 +500,7 @@ public class LiveNotificationService extends Service {
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
notify_user(getApplicationContext(), intent, notificationId, resource, finalTitle, "@"+account.getAcct()+"@"+account.getInstance());
notify_user(getApplicationContext(), intent, notificationId, resource, finalNotifType, finalTitle, "@"+account.getAcct()+"@"+account.getInstance());
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
if (lastNotif == null || Long.parseLong(notification.getId()) > Long.parseLong(lastNotif)) {
SharedPreferences.Editor editor = sharedpreferences.edit();

View File

@ -610,6 +610,13 @@
<item>SOCKS</item>
</string-array>
<string name="channel_notif_follow">New follow</string>
<string name="channel_notif_boost">New Boost</string>
<string name="channel_notif_fav">New Favourite</string>
<string name="channel_notif_mention">New Mention</string>
<string name="channel_notif_toot">New Toot</string>
<string name="channel_notif_backup">Toots Backup</string>
<string name="channel_notif_media">Media Download</string>
<string-array name="filter_expire">
<item>Never</item>