Improve live notifications

This commit is contained in:
tom79 2019-09-01 18:11:57 +02:00
parent ad63835daf
commit 4c3224a59e
5 changed files with 100 additions and 4 deletions

View File

@ -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);

View File

@ -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";

View File

@ -98,6 +98,9 @@ public class LiveNotificationService extends Service implements NetworkStateRece
private static HashMap<String, String> lastNotification = new HashMap<>();
private NetworkStateReceiver networkStateReceiver;
private static HashMap<String, WebSocket> 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<Account> 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<Account> 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);

View File

@ -463,6 +463,35 @@
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/settings_checkbox_margin"
android:layout_marginBottom="@dimen/settings_checkbox_margin"
android:orientation="horizontal">
<CheckBox
android:id="@+id/set_allow_live_notifications"
android:layout_width="wrap_content"
android:textSize="16sp"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:textSize="16sp"
android:id="@+id/set_allow_live_notifications_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textColor="@color/mastodonC2"
android:text="@string/set_allow_live_notifications_indication"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/notification_settings"
android:layout_width="match_parent"

View File

@ -1198,4 +1198,8 @@
<string name="file_cache">Files in cache</string>
<string name="total_notifications">Total notifications</string>
<string name="hide_menu_items">Hide menu items</string>
<string name="top_notification">Fedilab is running live notifications</string>
<string name="top_notification_message">For %1$s accounts with %2$s events</string>
<string name="set_allow_live_notifications">Live notifications for %1$s</string>
<string name="set_allow_live_notifications_indication">Live notifications will be only disabled for this account.</string>
</resources>