From ea3bed1c7f50b0480979e1d42433c25c2130174f Mon Sep 17 00:00:00 2001 From: tom79 Date: Thu, 26 Sep 2019 18:58:43 +0200 Subject: [PATCH] Group notifications --- .../fragments/ContentSettingsFragment.java | 14 +-- .../fragments/DisplayStatusFragment.java | 1 - .../app/fedilab/android/helper/Helper.java | 29 +++-- .../LiveNotificationDelayedService.java | 102 +++++++++--------- .../services/LiveNotificationService.java | 3 +- 5 files changed, 81 insertions(+), 68 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java b/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java index 6f9485187..a6ce979cb 100644 --- a/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java +++ b/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java @@ -1944,7 +1944,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot public void onClick(View v) { Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); - intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_boost_new"); + intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_boost"); startActivity(intent); } }); @@ -1954,7 +1954,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot public void onClick(View v) { Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); - intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_fav_new"); + intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_fav"); startActivity(intent); } }); @@ -1964,7 +1964,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot public void onClick(View v) { Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); - intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_follow_new"); + intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_follow"); startActivity(intent); } }); @@ -1974,7 +1974,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot public void onClick(View v) { Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); - intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_mention_new"); + intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_mention"); startActivity(intent); } }); @@ -1984,7 +1984,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot public void onClick(View v) { Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); - intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_poll_new"); + intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_poll"); startActivity(intent); } }); @@ -1994,7 +1994,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot public void onClick(View v) { Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); - intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_backup_new"); + intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_backup"); startActivity(intent); } }); @@ -2004,7 +2004,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot public void onClick(View v) { Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); - intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_store_new"); + intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_store"); startActivity(intent); } }); diff --git a/app/src/main/java/app/fedilab/android/fragments/DisplayStatusFragment.java b/app/src/main/java/app/fedilab/android/fragments/DisplayStatusFragment.java index 7628d7886..f7d4f261b 100644 --- a/app/src/main/java/app/fedilab/android/fragments/DisplayStatusFragment.java +++ b/app/src/main/java/app/fedilab/android/fragments/DisplayStatusFragment.java @@ -34,7 +34,6 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index 2826d6da5..0ba4c774b 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -21,6 +21,7 @@ import android.annotation.SuppressLint; import android.app.Activity; import android.app.DownloadManager; import android.app.FragmentManager; +import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; @@ -1144,39 +1145,39 @@ public class Helper { switch (notifType) { case BOOST: - channelId = "channel_boost_new" + account.getAcct() + "@" + account.getInstance(); + channelId = "channel_boost" + account.getAcct() + "@" + account.getInstance(); channelTitle = context.getString(R.string.channel_notif_boost); break; case FAV: - channelId = "channel_fav_new"; + channelId = "channel_fav"; channelTitle = context.getString(R.string.channel_notif_fav); break; case FOLLLOW: - channelId = "channel_follow_new"; + channelId = "channel_follow"; channelTitle = context.getString(R.string.channel_notif_follow); break; case MENTION: - channelId = "channel_mention_new"; + channelId = "channel_mention"; channelTitle = context.getString(R.string.channel_notif_mention); break; case POLL: - channelId = "channel_poll_new"; + channelId = "channel_poll"; channelTitle = context.getString(R.string.channel_notif_poll); break; case BACKUP: - channelId = "channel_backup_new"; + channelId = "channel_backup"; channelTitle = context.getString(R.string.channel_notif_backup); break; case STORE: - channelId = "channel_store_new"; + channelId = "channel_store"; channelTitle = context.getString(R.string.channel_notif_media); break; case TOOT: - channelId = "channel_toot_new"; + channelId = "channel_toot"; channelTitle = context.getString(R.string.channel_notif_toot); break; default: - channelId = "channel_boost_new"; + channelId = "channel_boost"; channelTitle = context.getString(R.string.channel_notif_boost); } NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId) @@ -1250,6 +1251,16 @@ public class Helper { notificationBuilder.setContentTitle(title); notificationBuilder.setLargeIcon(icon); notificationManager.notify(notificationId, notificationBuilder.build()); + + Notification summaryNotification = + new NotificationCompat.Builder(context, channelId) + .setContentTitle(account.getAcct()+"@"+account.getInstance()) + .setContentText(channelTitle) + .setSmallIcon(getNotificationIcon(context)) + .setGroup(account.getAcct() + "@" + account.getInstance()) + .setGroupSummary(true) + .build(); + notificationManager.notify(0, summaryNotification); } diff --git a/app/src/main/java/app/fedilab/android/services/LiveNotificationDelayedService.java b/app/src/main/java/app/fedilab/android/services/LiveNotificationDelayedService.java index eb375921b..cc5e9cd72 100644 --- a/app/src/main/java/app/fedilab/android/services/LiveNotificationDelayedService.java +++ b/app/src/main/java/app/fedilab/android/services/LiveNotificationDelayedService.java @@ -53,6 +53,8 @@ import java.lang.ref.WeakReference; import java.util.HashMap; import java.util.List; import java.util.Objects; +import java.util.Timer; +import java.util.TimerTask; import app.fedilab.android.R; import app.fedilab.android.activities.MainActivity; @@ -87,9 +89,11 @@ public class LiveNotificationDelayedService extends Service { public static int eventsCount = 0; public static HashMap since_ids = new HashMap<>(); private boolean fetch; + private LiveNotificationDelayedService liveNotificationDelayedService; public void onCreate() { super.onCreate(); + liveNotificationDelayedService = this; } private void startStream() { @@ -99,20 +103,25 @@ public class LiveNotificationDelayedService extends Service { List accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction(); if (accountStreams != null) { fetch = true; - Handler handler = new Handler(); - handler.postDelayed(new Runnable() { - public void run() { - for (final Account accountStream : accountStreams) { - if (accountStream.getSocial() == null || accountStream.getSocial().equals("MASTODON") || accountStream.getSocial().equals("PLEROMA")) { - new Fetch(new WeakReference<>(getApplicationContext()), accountStream).execute(); - } - } - fetch = (Helper.liveNotifType(getApplicationContext()) == Helper.NOTIF_DELAYED); - if (!fetch) { - handler.removeMessages(0); - } - } - }, 30000); + Timer t = new Timer(); + t.scheduleAtFixedRate(new TimerTask() { + + @Override + public void run() { + for (final Account accountStream : accountStreams) { + if (accountStream.getSocial() == null || accountStream.getSocial().equals("MASTODON") || accountStream.getSocial().equals("PLEROMA")) { + new Fetch(new WeakReference<>(liveNotificationDelayedService), accountStream).execute(); + } + } + fetch = (Helper.liveNotifType(getApplicationContext()) == Helper.NOTIF_DELAYED); + if( !fetch){ + t.cancel(); + } + } + + }, + 0, + 30000); } } } @@ -122,9 +131,9 @@ public class LiveNotificationDelayedService extends Service { private Account accountFetch; private String key, last_notifid; - private WeakReference contextWeakReference; + private WeakReference contextWeakReference; - Fetch(WeakReference contextWeakReference, Account account){ + Fetch(WeakReference contextWeakReference, Account account){ this.accountFetch = account; this.contextWeakReference =contextWeakReference; key = account.getAcct() + "@" + account.getInstance(); @@ -136,11 +145,13 @@ public class LiveNotificationDelayedService extends Service { @Override protected APIResponse doInBackground(Void... params) { + return new API(this.contextWeakReference.get(), accountFetch.getInstance(), accountFetch.getToken()).getNotificationsSince(DisplayNotificationsFragment.Type.ALL, last_notifid, false); } @Override protected void onPostExecute(APIResponse apiResponse) { + if( apiResponse.getNotifications() != null && apiResponse.getNotifications().size() > 0){ since_ids.put(key, apiResponse.getNotifications().get(0).getId()); for (Notification notification : apiResponse.getNotifications()) { @@ -161,7 +172,7 @@ public class LiveNotificationDelayedService extends Service { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Live notifications", NotificationManager.IMPORTANCE_DEFAULT); - channel.setShowBadge(false); + ((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel); SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); List accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction(); @@ -214,7 +225,7 @@ public class LiveNotificationDelayedService extends Service { } - private static void onRetrieveStreaming(WeakReference contextWeakReference, Account account, Notification notification) { + private static void onRetrieveStreaming(WeakReference contextWeakReference, Account account, Notification notification) { Bundle b = new Bundle(); boolean canSendBroadCast = true; @@ -227,7 +238,6 @@ public class LiveNotificationDelayedService extends Service { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Live notifications", NotificationManager.IMPORTANCE_DEFAULT); - channel.setShowBadge(false); ((NotificationManager) Objects.requireNonNull(contextWeakReference.get().getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel); android.app.Notification notificationChannel = new NotificationCompat.Builder(contextWeakReference.get(), CHANNEL_ID) .setContentTitle(contextWeakReference.get().getString(R.string.top_notification)) @@ -336,39 +346,32 @@ public class LiveNotificationDelayedService extends Service { intent.putExtra(Helper.INTENT_TARGETED_ACCOUNT, targeted_account); } final String finalMessage = message; - Handler mainHandler = new Handler(Looper.getMainLooper()); Helper.NotifType finalNotifType = notifType; - Runnable myRunnable = new Runnable() { - @Override - public void run() { - if (finalMessage != null) { - Glide.with(contextWeakReference.get()) - .asBitmap() - .load(notification.getAccount().getAvatar()) - .listener(new RequestListener() { - @Override - public boolean onResourceReady(Bitmap resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - return false; - } + if (finalMessage != null) { + Glide.with(contextWeakReference.get()) + .asBitmap() + .load(notification.getAccount().getAvatar()) + .listener(new RequestListener() { + @Override + public boolean onResourceReady(Bitmap resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + return false; + } - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - Helper.notify_user(contextWeakReference.get(), account, intent, BitmapFactory.decodeResource(contextWeakReference.get().getResources(), - getMainLogo(contextWeakReference.get())), finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage); - return false; - } - }) - .into(new SimpleTarget() { - @Override - public void onResourceReady(@NonNull Bitmap resource, Transition transition) { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + Helper.notify_user(contextWeakReference.get(), account, intent, BitmapFactory.decodeResource(contextWeakReference.get().getResources(), + getMainLogo(contextWeakReference.get())), finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage); + return false; + } + }) + .into(new SimpleTarget() { + @Override + public void onResourceReady(@NonNull Bitmap resource, Transition transition) { - Helper.notify_user(contextWeakReference.get(), account, intent, resource, finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage); - } - }); - } - } - }; - mainHandler.post(myRunnable); + Helper.notify_user(contextWeakReference.get(), account, intent, resource, finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage); + } + }); + } } } @@ -384,6 +387,7 @@ public class LiveNotificationDelayedService extends Service { editor.apply(); } } catch (Exception ignored) { + ignored.printStackTrace(); } } diff --git a/app/src/main/java/app/fedilab/android/services/LiveNotificationService.java b/app/src/main/java/app/fedilab/android/services/LiveNotificationService.java index 3b6e24591..e2acc3031 100644 --- a/app/src/main/java/app/fedilab/android/services/LiveNotificationService.java +++ b/app/src/main/java/app/fedilab/android/services/LiveNotificationService.java @@ -144,7 +144,7 @@ public class LiveNotificationService extends Service implements NetworkStateRece channel = new NotificationChannel(CHANNEL_ID, "Live notifications", NotificationManager.IMPORTANCE_DEFAULT); - channel.setShowBadge(false); + ((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel); SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); List accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction(); @@ -301,7 +301,6 @@ public class LiveNotificationService extends Service implements NetworkStateRece channel = new NotificationChannel(CHANNEL_ID, "Live notifications", NotificationManager.IMPORTANCE_DEFAULT); - channel.setShowBadge(false); ((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel); android.app.Notification notificationChannel = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle(getString(R.string.top_notification))