From 4c3224a59e9b343015fe3f933f450639283830c0 Mon Sep 17 00:00:00 2001 From: tom79 Date: Sun, 1 Sep 2019 18:11:57 +0200 Subject: [PATCH] Improve live notifications --- .../fragments/ContentSettingsFragment.java | 27 ++++++++++++ .../app/fedilab/android/helper/Helper.java | 1 + .../services/LiveNotificationService.java | 43 +++++++++++++++++-- .../res/layout/fragment_settings_reveal.xml | 29 +++++++++++++ app/src/main/res/values/strings.xml | 4 ++ 5 files changed, 100 insertions(+), 4 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 f6a128996..ca7379f0d 100644 --- a/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java +++ b/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java @@ -64,6 +64,7 @@ import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.SwitchCompat; +import androidx.core.app.NotificationCompat; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -1433,6 +1434,32 @@ public class ContentSettingsFragment extends Fragment implements ScreenShotable }); + boolean allow_live_notifications = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM+userId+instance, true); + TextView set_allow_live_notifications_title = rootView.findViewById(R.id.set_allow_live_notifications_title); + set_allow_live_notifications_title.setText(context.getString(R.string.set_allow_live_notifications, account.getAcct() + "@"+account.getInstance())); + final CheckBox set_allow_live_notifications = rootView.findViewById(R.id.set_allow_live_notifications); + set_allow_live_notifications.setChecked(allow_live_notifications); + set_allow_live_notifications.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putBoolean(Helper.SET_ALLOW_STREAM+userId+instance, set_allow_live_notifications.isChecked()); + editor.apply(); + if( set_allow_live_notifications.isChecked()){ + LiveNotificationService.totalAccount++; + }else{ + LiveNotificationService.totalAccount--; + } + boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true); + if( liveNotifications) { + Intent streamingServiceIntent = new Intent(context.getApplicationContext(), LiveNotificationService.class); + try { + context.startService(streamingServiceIntent); + }catch (Exception ignored){} + } + } + }); + boolean trans_forced = sharedpreferences.getBoolean(Helper.SET_TRANS_FORCED, false); final CheckBox set_trans_forced = rootView.findViewById(R.id.set_trans_forced); set_trans_forced.setChecked(trans_forced); 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 7825c2d8d..b327f2a6e 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -455,6 +455,7 @@ public class Helper { public static final String SET_TRUNCATE_TOOTS_SIZE = "set_truncate_toots_size"; public static final String SET_ART_WITH_NSFW = "set_art_with_nsfw"; public static final String SET_SECURITY_PROVIDER = "set_security_provider"; + public static final String SET_ALLOW_STREAM = "set_allow_stream"; //End points public static final String EP_AUTHORIZE = "/oauth/authorize"; 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 1b588af97..e621ac0f9 100644 --- a/app/src/main/java/app/fedilab/android/services/LiveNotificationService.java +++ b/app/src/main/java/app/fedilab/android/services/LiveNotificationService.java @@ -98,6 +98,9 @@ public class LiveNotificationService extends Service implements NetworkStateRece private static HashMap lastNotification = new HashMap<>(); private NetworkStateReceiver networkStateReceiver; private static HashMap webSocketFutures = new HashMap<>(); + private NotificationChannel channel; + public static int totalAccount = 0; + public static int eventsCount = 0; public void onCreate() { super.onCreate(); @@ -119,7 +122,9 @@ public class LiveNotificationService extends Service implements NetworkStateRece List accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction(); if (accountStreams != null) { for (final Account accountStream : accountStreams) { - startWork(accountStream); + if( accountStream.getSocial() == null || accountStream.getSocial().equals("MASTODON")|| accountStream.getSocial().equals("PLEROMA")) { + startWork(accountStream); + } } } } @@ -133,15 +138,28 @@ public class LiveNotificationService extends Service implements NetworkStateRece } if( backgroundProcess) { if (Build.VERSION.SDK_INT >= 26) { - NotificationChannel channel = new NotificationChannel(CHANNEL_ID, + channel = new NotificationChannel(CHANNEL_ID, "Live notifications", NotificationManager.IMPORTANCE_DEFAULT); ((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(); + totalAccount = 0; + for(Account account: accountStreams){ + if( account.getSocial() == null || account.getSocial().equals("MASTODON")|| account.getSocial().equals("PLEROMA")) { + final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM+account.getId()+account.getInstance(), true); + if( allowStream){ + totalAccount++; + } + } + } android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) - .setContentTitle("") - .setContentText("").build(); + .setContentTitle(getString(R.string.top_notification)) + .setSmallIcon(R.drawable.fedilab_notification_icon) + .setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount),String.valueOf(eventsCount))).build(); startForeground(1, notification); } @@ -281,6 +299,19 @@ public class LiveNotificationService extends Service implements NetworkStateRece try { switch (response.get("event").toString()) { case "notification": + eventsCount++; + if (Build.VERSION.SDK_INT >= 26) { + channel = new NotificationChannel(CHANNEL_ID, + "Live notifications", + NotificationManager.IMPORTANCE_DEFAULT); + ((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)) + .setSmallIcon(R.drawable.fedilab_notification_icon) + .setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount),String.valueOf(eventsCount))).build(); + + startForeground(1, notificationChannel); + } event = Helper.EventStreaming.NOTIFICATION; notification = API.parseNotificationResponse(getApplicationContext(), new JSONObject(response.get("payload").toString())); if( notification == null){ @@ -297,6 +328,10 @@ public class LiveNotificationService extends Service implements NetworkStateRece if( lastNotification.containsKey(key) && notification.getId().compareTo(Objects.requireNonNull(lastNotification.get(key))) <= 0){ canNotify = false; } + boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM+account.getId()+account.getInstance(), true); + if( !allowStream){ + canNotify = false; + } if ((userId == null || !userId.equals(account.getId()) || !activityRunning) && liveNotifications && canNotify && notify) { lastNotification.put(key, notification.getId()); boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true); diff --git a/app/src/main/res/layout/fragment_settings_reveal.xml b/app/src/main/res/layout/fragment_settings_reveal.xml index e8feb8c1a..fa91c4474 100644 --- a/app/src/main/res/layout/fragment_settings_reveal.xml +++ b/app/src/main/res/layout/fragment_settings_reveal.xml @@ -463,6 +463,35 @@ /> + + + + + + + + Files in cache Total notifications Hide menu items + Fedilab is running live notifications + For %1$s accounts with %2$s events + Live notifications for %1$s + Live notifications will be only disabled for this account. \ No newline at end of file