Improve live notifications
This commit is contained in:
parent
ad63835daf
commit
4c3224a59e
|
@ -64,6 +64,7 @@ import android.widget.Toast;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.appcompat.app.AlertDialog;
|
import androidx.appcompat.app.AlertDialog;
|
||||||
import androidx.appcompat.widget.SwitchCompat;
|
import androidx.appcompat.widget.SwitchCompat;
|
||||||
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
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);
|
boolean trans_forced = sharedpreferences.getBoolean(Helper.SET_TRANS_FORCED, false);
|
||||||
final CheckBox set_trans_forced = rootView.findViewById(R.id.set_trans_forced);
|
final CheckBox set_trans_forced = rootView.findViewById(R.id.set_trans_forced);
|
||||||
set_trans_forced.setChecked(trans_forced);
|
set_trans_forced.setChecked(trans_forced);
|
||||||
|
|
|
@ -455,6 +455,7 @@ public class Helper {
|
||||||
public static final String SET_TRUNCATE_TOOTS_SIZE = "set_truncate_toots_size";
|
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_ART_WITH_NSFW = "set_art_with_nsfw";
|
||||||
public static final String SET_SECURITY_PROVIDER = "set_security_provider";
|
public static final String SET_SECURITY_PROVIDER = "set_security_provider";
|
||||||
|
public static final String SET_ALLOW_STREAM = "set_allow_stream";
|
||||||
//End points
|
//End points
|
||||||
public static final String EP_AUTHORIZE = "/oauth/authorize";
|
public static final String EP_AUTHORIZE = "/oauth/authorize";
|
||||||
|
|
||||||
|
|
|
@ -98,6 +98,9 @@ public class LiveNotificationService extends Service implements NetworkStateRece
|
||||||
private static HashMap<String, String> lastNotification = new HashMap<>();
|
private static HashMap<String, String> lastNotification = new HashMap<>();
|
||||||
private NetworkStateReceiver networkStateReceiver;
|
private NetworkStateReceiver networkStateReceiver;
|
||||||
private static HashMap<String, WebSocket> webSocketFutures = new HashMap<>();
|
private static HashMap<String, WebSocket> webSocketFutures = new HashMap<>();
|
||||||
|
private NotificationChannel channel;
|
||||||
|
public static int totalAccount = 0;
|
||||||
|
public static int eventsCount = 0;
|
||||||
|
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
@ -119,11 +122,13 @@ public class LiveNotificationService extends Service implements NetworkStateRece
|
||||||
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
|
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
|
||||||
if (accountStreams != null) {
|
if (accountStreams != null) {
|
||||||
for (final Account accountStream : accountStreams) {
|
for (final Account accountStream : accountStreams) {
|
||||||
|
if( accountStream.getSocial() == null || accountStream.getSocial().equals("MASTODON")|| accountStream.getSocial().equals("PLEROMA")) {
|
||||||
startWork(accountStream);
|
startWork(accountStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
@ -133,15 +138,28 @@ public class LiveNotificationService extends Service implements NetworkStateRece
|
||||||
}
|
}
|
||||||
if( backgroundProcess) {
|
if( backgroundProcess) {
|
||||||
if (Build.VERSION.SDK_INT >= 26) {
|
if (Build.VERSION.SDK_INT >= 26) {
|
||||||
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
|
channel = new NotificationChannel(CHANNEL_ID,
|
||||||
"Live notifications",
|
"Live notifications",
|
||||||
NotificationManager.IMPORTANCE_DEFAULT);
|
NotificationManager.IMPORTANCE_DEFAULT);
|
||||||
|
|
||||||
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
|
((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)
|
android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
|
||||||
.setContentTitle("")
|
.setContentTitle(getString(R.string.top_notification))
|
||||||
.setContentText("").build();
|
.setSmallIcon(R.drawable.fedilab_notification_icon)
|
||||||
|
.setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount),String.valueOf(eventsCount))).build();
|
||||||
|
|
||||||
startForeground(1, notification);
|
startForeground(1, notification);
|
||||||
}
|
}
|
||||||
|
@ -281,6 +299,19 @@ public class LiveNotificationService extends Service implements NetworkStateRece
|
||||||
try {
|
try {
|
||||||
switch (response.get("event").toString()) {
|
switch (response.get("event").toString()) {
|
||||||
case "notification":
|
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;
|
event = Helper.EventStreaming.NOTIFICATION;
|
||||||
notification = API.parseNotificationResponse(getApplicationContext(), new JSONObject(response.get("payload").toString()));
|
notification = API.parseNotificationResponse(getApplicationContext(), new JSONObject(response.get("payload").toString()));
|
||||||
if( notification == null){
|
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){
|
if( lastNotification.containsKey(key) && notification.getId().compareTo(Objects.requireNonNull(lastNotification.get(key))) <= 0){
|
||||||
canNotify = false;
|
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) {
|
if ((userId == null || !userId.equals(account.getId()) || !activityRunning) && liveNotifications && canNotify && notify) {
|
||||||
lastNotification.put(key, notification.getId());
|
lastNotification.put(key, notification.getId());
|
||||||
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
|
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
|
||||||
|
|
|
@ -463,6 +463,35 @@
|
||||||
/>
|
/>
|
||||||
</LinearLayout>
|
</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
|
<LinearLayout
|
||||||
android:id="@+id/notification_settings"
|
android:id="@+id/notification_settings"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
|
|
@ -1198,4 +1198,8 @@
|
||||||
<string name="file_cache">Files in cache</string>
|
<string name="file_cache">Files in cache</string>
|
||||||
<string name="total_notifications">Total notifications</string>
|
<string name="total_notifications">Total notifications</string>
|
||||||
<string name="hide_menu_items">Hide menu items</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>
|
</resources>
|
Loading…
Reference in New Issue