New live notifications in settings

This commit is contained in:
tom79 2019-09-11 16:02:56 +02:00
parent 35977c9018
commit 088dcd1889
8 changed files with 172 additions and 209 deletions

View File

@ -55,7 +55,9 @@
<service <service
android:name="app.fedilab.android.services.LiveNotificationDelayedService" android:name="app.fedilab.android.services.LiveNotificationDelayedService"
android:exported="false" /> android:exported="false" />
<service
android:name="app.fedilab.android.services.LiveNotificationService"
android:exported="false" />
<service <service
android:name="app.fedilab.android.services.BackupStatusService" android:name="app.fedilab.android.services.BackupStatusService"
android:exported="false" /> android:exported="false" />
@ -70,7 +72,6 @@
android:name="app.fedilab.android.services.RestartLiveNotificationReceiver" android:name="app.fedilab.android.services.RestartLiveNotificationReceiver"
android:exported="false"> android:exported="false">
<intent-filter> <intent-filter>
<action android:name="LiveNotificationDelayedService" />
<action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter> </intent-filter>
</receiver> </receiver>

View File

@ -42,7 +42,6 @@ import android.provider.Settings;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
@ -65,39 +64,30 @@ 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;
import com.google.android.material.navigation.NavigationView;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import org.apache.poi.sl.usermodel.Line;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import app.fedilab.android.R; import app.fedilab.android.R;
import app.fedilab.android.activities.LanguageActivity;
import app.fedilab.android.activities.MainActivity; import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.activities.SettingsActivity; import app.fedilab.android.activities.SettingsActivity;
import app.fedilab.android.animatemenu.interfaces.ScreenShotable; import app.fedilab.android.animatemenu.interfaces.ScreenShotable;
import app.fedilab.android.asynctasks.DownloadTrackingDomainsAsyncTask; import app.fedilab.android.asynctasks.DownloadTrackingDomainsAsyncTask;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask; import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
import app.fedilab.android.client.Entities.Account; import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.filelister.FileListerDialog; import app.fedilab.android.filelister.FileListerDialog;
import app.fedilab.android.filelister.OnFileSelectedListener; import app.fedilab.android.filelister.OnFileSelectedListener;
import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.Helper;
import app.fedilab.android.services.LiveNotificationDelayedService; import app.fedilab.android.services.LiveNotificationDelayedService;
import app.fedilab.android.services.LiveNotificationService;
import app.fedilab.android.services.StopLiveNotificationReceiver; import app.fedilab.android.services.StopLiveNotificationReceiver;
import app.fedilab.android.sqlite.AccountDAO; import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.Sqlite; import app.fedilab.android.sqlite.Sqlite;
@ -106,10 +96,8 @@ import mabbas007.tagsedittext.TagsEditText;
import static android.app.Activity.RESULT_OK; import static android.app.Activity.RESULT_OK;
import static android.content.Context.ACTIVITY_SERVICE;
import static android.content.Context.MODE_PRIVATE; import static android.content.Context.MODE_PRIVATE;
import static app.fedilab.android.fragments.ContentSettingsFragment.type.ADMIN; import static app.fedilab.android.fragments.ContentSettingsFragment.type.ADMIN;
import static app.fedilab.android.fragments.ContentSettingsFragment.type.BATTERY;
import static app.fedilab.android.fragments.ContentSettingsFragment.type.COMPOSE; import static app.fedilab.android.fragments.ContentSettingsFragment.type.COMPOSE;
import static app.fedilab.android.fragments.ContentSettingsFragment.type.INTERFACE; import static app.fedilab.android.fragments.ContentSettingsFragment.type.INTERFACE;
import static app.fedilab.android.fragments.ContentSettingsFragment.type.NOTIFICATIONS; import static app.fedilab.android.fragments.ContentSettingsFragment.type.NOTIFICATIONS;
@ -131,7 +119,6 @@ public class ContentSettingsFragment extends Fragment implements ScreenShotable
NOTIFICATIONS, NOTIFICATIONS,
INTERFACE, INTERFACE,
COMPOSE, COMPOSE,
BATTERY,
LANGUAGE, LANGUAGE,
MENU MENU
} }
@ -250,7 +237,7 @@ public class ContentSettingsFragment extends Fragment implements ScreenShotable
LinearLayout settings_admin = rootView.findViewById(R.id.settings_admin); LinearLayout settings_admin = rootView.findViewById(R.id.settings_admin);
LinearLayout settings_interface = rootView.findViewById(R.id.settings_interface); LinearLayout settings_interface = rootView.findViewById(R.id.settings_interface);
LinearLayout settings_compose = rootView.findViewById(R.id.settings_compose); LinearLayout settings_compose = rootView.findViewById(R.id.settings_compose);
LinearLayout settings_battery = rootView.findViewById(R.id.settings_battery);
String title = ""; String title = "";
if (type == null || type.equals(TIMELINES)) { if (type == null || type.equals(TIMELINES)) {
@ -265,9 +252,6 @@ public class ContentSettingsFragment extends Fragment implements ScreenShotable
} else if (type == INTERFACE) { } else if (type == INTERFACE) {
settings_interface.setVisibility(View.VISIBLE); settings_interface.setVisibility(View.VISIBLE);
title = context.getString(R.string.u_interface); title = context.getString(R.string.u_interface);
} else if (type == BATTERY) {
title = context.getString(R.string.battery);
settings_battery.setVisibility(View.VISIBLE);
} else if (type == COMPOSE) { } else if (type == COMPOSE) {
settings_compose.setVisibility(View.VISIBLE); settings_compose.setVisibility(View.VISIBLE);
title = context.getString(R.string.compose); title = context.getString(R.string.compose);
@ -1130,24 +1114,32 @@ public class ContentSettingsFragment extends Fragment implements ScreenShotable
} }
}); });
boolean livenotif = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
final CheckBox set_live_notif = rootView.findViewById(R.id.set_live_notify); boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true);
set_live_notif.setChecked(livenotif); final SwitchCompat switchCompatNotify = rootView.findViewById(R.id.set_notify);
set_live_notif.setOnClickListener(new View.OnClickListener() { switchCompatNotify.setChecked(notify);
final LinearLayout notification_settings = rootView.findViewById(R.id.notification_settings);
if (notify)
notification_settings.setVisibility(View.VISIBLE);
else
notification_settings.setVisibility(View.GONE);
switchCompatNotify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onClick(View v) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Save the state here
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, set_live_notif.isChecked()); editor.putBoolean(Helper.SET_NOTIFY, isChecked);
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING, set_live_notif.isChecked());
editor.apply(); editor.apply();
if (set_live_notif.isChecked()) { if (isChecked) {
notification_settings.setVisibility(View.VISIBLE);
try { try {
Intent streamingIntent = new Intent(context, LiveNotificationDelayedService.class); Intent streamingIntent = new Intent(context, LiveNotificationDelayedService.class);
context.startService(streamingIntent); context.startService(streamingIntent);
} catch (Exception ignored) { } catch (Exception ignored) {
ignored.printStackTrace(); ignored.printStackTrace();
} }
} else { }else {
notification_settings.setVisibility(View.GONE);
context.sendBroadcast(new Intent(context, StopLiveNotificationReceiver.class)); context.sendBroadcast(new Intent(context, StopLiveNotificationReceiver.class));
if (Build.VERSION.SDK_INT >= 26) { if (Build.VERSION.SDK_INT >= 26) {
NotificationManager notif = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)); NotificationManager notif = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
@ -1158,22 +1150,57 @@ public class ContentSettingsFragment extends Fragment implements ScreenShotable
} }
} }
}); });
final LinearLayout set_live_notify_text = rootView.findViewById(R.id.set_live_notify_text);
set_live_notify_text.setOnClickListener(v -> set_live_notif.performClick());
boolean keep_background_process = sharedpreferences.getBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, true);
final CheckBox set_keep_background_process = rootView.findViewById(R.id.set_keep_background_process);
set_keep_background_process.setChecked(keep_background_process);
set_keep_background_process.setOnClickListener(new View.OnClickListener() { //Live notification mode
final Spinner set_live_type = rootView.findViewById(R.id.set_live_type);
String[] labels = {context.getString(R.string.live_notif), context.getString(R.string.live_delayed), context.getString(R.string.no_live_notif)};
ArrayAdapter<String> adapterLive = new ArrayAdapter<>(context,
android.R.layout.simple_spinner_dropdown_item,labels );
LinearLayout live_notif_per_account = rootView.findViewById(R.id.live_notif_per_account);
set_live_type.setAdapter(adapterLive);
if( Helper.liveNotifType(context) == Helper.NOTIF_NONE){
live_notif_per_account.setVisibility(View.GONE);
}
set_live_type.setSelection(Helper.liveNotifType(context));
set_live_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @Override
public void onClick(View v) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences.Editor editor = sharedpreferences.edit(); if (count2 > 0) {
editor.putBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, set_keep_background_process.isChecked()); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.apply(); switch (position) {
case Helper.NOTIF_LIVE:
editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
editor.putBoolean(Helper.SET_DELAYED_NOTIFICATIONS, false);
live_notif_per_account.setVisibility(View.VISIBLE);
editor.apply();
break;
case Helper.NOTIF_DELAYED:
editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, false);
editor.putBoolean(Helper.SET_DELAYED_NOTIFICATIONS, true);
live_notif_per_account.setVisibility(View.VISIBLE);
editor.apply();
break;
case Helper.NOTIF_NONE:
editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, false);
editor.putBoolean(Helper.SET_DELAYED_NOTIFICATIONS, false);
live_notif_per_account.setVisibility(View.GONE);
editor.apply();
break;
}
}
count2++;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
} }
}); });
final LinearLayout set_keep_background_process_text = rootView.findViewById(R.id.set_keep_background_process_text);
set_keep_background_process_text.setOnClickListener(v -> set_keep_background_process.performClick());
boolean capitalize = sharedpreferences.getBoolean(Helper.SET_CAPITALIZE, true); boolean capitalize = sharedpreferences.getBoolean(Helper.SET_CAPITALIZE, true);
@ -1708,27 +1735,7 @@ public class ContentSettingsFragment extends Fragment implements ScreenShotable
}); });
boolean notify = sharedpreferences.getBoolean(Helper.SET_NOTIFY, true);
final SwitchCompat switchCompatNotify = rootView.findViewById(R.id.set_notify);
switchCompatNotify.setChecked(notify);
final LinearLayout notification_settings = rootView.findViewById(R.id.notification_settings);
if (notify)
notification_settings.setVisibility(View.VISIBLE);
else
notification_settings.setVisibility(View.GONE);
switchCompatNotify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Save the state here
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Helper.SET_NOTIFY, isChecked);
editor.apply();
if (isChecked)
notification_settings.setVisibility(View.VISIBLE);
else
notification_settings.setVisibility(View.GONE);
}
});
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true); boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);

View File

@ -344,6 +344,7 @@ public class Helper {
public static final String INSTANCE_VERSION = "instance_version"; public static final String INSTANCE_VERSION = "instance_version";
public static final String NEWS_ACCOUNT_ID = "news_account_id"; public static final String NEWS_ACCOUNT_ID = "news_account_id";
public static final String SET_LIVE_NOTIFICATIONS = "set_live_notifications"; public static final String SET_LIVE_NOTIFICATIONS = "set_live_notifications";
public static final String SET_DELAYED_NOTIFICATIONS = "set_delayed_notifications";
public static final String SET_DISABLE_GIF = "set_disable_gif"; public static final String SET_DISABLE_GIF = "set_disable_gif";
public static final String SET_DISABLE_ANIMATED_EMOJI = "set_disable_animated_emoji"; public static final String SET_DISABLE_ANIMATED_EMOJI = "set_disable_animated_emoji";
public static final String SET_CAPITALIZE = "set_capitalize"; public static final String SET_CAPITALIZE = "set_capitalize";
@ -358,7 +359,6 @@ public class Helper {
public static final String SET_CUSTOM_SHARING_URL = "set_custom_sharing_url"; public static final String SET_CUSTOM_SHARING_URL = "set_custom_sharing_url";
public static final String SET_NOTIF_SOUND = "set_notif_sound"; public static final String SET_NOTIF_SOUND = "set_notif_sound";
public static final String SET_ENABLE_TIME_SLOT = "set_enable_time_slot"; public static final String SET_ENABLE_TIME_SLOT = "set_enable_time_slot";
public static final String SET_KEEP_BACKGROUND_PROCESS = "set_keep_background_process";
public static final String SET_CLEAR_CACHE_EXIT = "set_clear_cache_exit"; public static final String SET_CLEAR_CACHE_EXIT = "set_clear_cache_exit";
public static final String SET_DISPLAY_EMOJI = "set_display_emoji"; public static final String SET_DISPLAY_EMOJI = "set_display_emoji";
public static final String SET_DISPLAY_CARD = "set_display_card"; public static final String SET_DISPLAY_CARD = "set_display_card";
@ -406,6 +406,10 @@ public class Helper {
public static final int THEME_DARK = 2; public static final int THEME_DARK = 2;
public static final int THEME_BLACK = 3; public static final int THEME_BLACK = 3;
public static final int NOTIF_LIVE = 2;
public static final int NOTIF_DELAYED = 1;
public static final int NOTIF_NONE = 0;
public static final int LED_COLOUR = 0; public static final int LED_COLOUR = 0;
public static final int TRANS_YANDEX = 0; public static final int TRANS_YANDEX = 0;
@ -545,6 +549,19 @@ public class Helper {
} }
public static int liveNotifType(Context context){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean live = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, false );
boolean delayed = sharedpreferences.getBoolean(Helper.SET_DELAYED_NOTIFICATIONS, true );
if( delayed){
return Helper.NOTIF_DELAYED;
}else if( live ){
return Helper.NOTIF_LIVE;
}else {
return Helper.NOTIF_NONE;
}
}
/** /**
* Converts emojis in input to unicode * Converts emojis in input to unicode
* *

View File

@ -79,7 +79,6 @@ public class LiveNotificationDelayedService extends Service {
public static String CHANNEL_ID = "live_notifications"; public static String CHANNEL_ID = "live_notifications";
protected Account account; protected Account account;
boolean backgroundProcess;
private NotificationChannel channel; private NotificationChannel channel;
public static int totalAccount = 0; public static int totalAccount = 0;
public static int eventsCount = 0; public static int eventsCount = 0;
@ -89,13 +88,11 @@ public class LiveNotificationDelayedService extends Service {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
backgroundProcess = sharedpreferences.getBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, true);
} }
private void startStream() { private void startStream() {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
backgroundProcess = sharedpreferences.getBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, true);
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true); boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
if (liveNotifications) { if (liveNotifications) {
@ -129,38 +126,33 @@ public class LiveNotificationDelayedService extends Service {
if (intent == null || intent.getBooleanExtra("stop", false)) { if (intent == null || intent.getBooleanExtra("stop", false)) {
stopSelf(); stopSelf();
} }
if (backgroundProcess) { if (Build.VERSION.SDK_INT >= 26) {
if (Build.VERSION.SDK_INT >= 26) { 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(); SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction(); List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
totalAccount = 0; totalAccount = 0;
for (Account account : accountStreams) { for (Account account : accountStreams) {
if (account.getSocial() == null || account.getSocial().equals("MASTODON") || account.getSocial().equals("PLEROMA")) { if (account.getSocial() == null || account.getSocial().equals("MASTODON") || account.getSocial().equals("PLEROMA")) {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true); boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
if (allowStream) { if (allowStream) {
totalAccount++; totalAccount++;
}
} }
} }
android.app.Notification notification = 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, notification);
} }
startStream(); android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
return START_STICKY; .setContentTitle(getString(R.string.top_notification))
} else { .setSmallIcon(R.drawable.fedilab_notification_icon)
startStream(); .setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount), String.valueOf(eventsCount))).build();
return START_NOT_STICKY;
startForeground(1, notification);
} }
startStream();
return START_STICKY;
} }
@ -173,9 +165,7 @@ public class LiveNotificationDelayedService extends Service {
@Override @Override
public void onTaskRemoved(Intent rootIntent) { public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent); super.onTaskRemoved(rootIntent);
if (backgroundProcess) { restart();
restart();
}
} }
private void restart() { private void restart() {

View File

@ -95,7 +95,6 @@ public class LiveNotificationService extends Service implements NetworkStateRece
public static String CHANNEL_ID = "live_notifications"; public static String CHANNEL_ID = "live_notifications";
protected Account account; protected Account account;
boolean backgroundProcess;
private static HashMap<String, Thread> threads = new HashMap<>(); private static HashMap<String, Thread> threads = new HashMap<>();
private static HashMap<String, String> lastNotification = new HashMap<>(); private static HashMap<String, String> lastNotification = new HashMap<>();
private NetworkStateReceiver networkStateReceiver; private NetworkStateReceiver networkStateReceiver;
@ -111,13 +110,11 @@ public class LiveNotificationService extends Service implements NetworkStateRece
networkStateReceiver.addListener(this); networkStateReceiver.addListener(this);
registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION)); registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
backgroundProcess = sharedpreferences.getBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, true);
} }
private void startStream() { private void startStream() {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
backgroundProcess = sharedpreferences.getBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, true);
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true); boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
if (liveNotifications) { if (liveNotifications) {
@ -138,37 +135,33 @@ public class LiveNotificationService extends Service implements NetworkStateRece
if (intent == null || intent.getBooleanExtra("stop", false)) { if (intent == null || intent.getBooleanExtra("stop", false)) {
stopSelf(); stopSelf();
} }
if (backgroundProcess) { if (Build.VERSION.SDK_INT >= 26) {
if (Build.VERSION.SDK_INT >= 26) { 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(); SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction(); List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
totalAccount = 0; totalAccount = 0;
for (Account account : accountStreams) { for (Account account : accountStreams) {
if (account.getSocial() == null || account.getSocial().equals("MASTODON") || account.getSocial().equals("PLEROMA")) { if (account.getSocial() == null || account.getSocial().equals("MASTODON") || account.getSocial().equals("PLEROMA")) {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true); boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
if (allowStream) { if (allowStream) {
totalAccount++; totalAccount++;
}
} }
} }
android.app.Notification notification = 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, notification);
} }
return START_STICKY;
} else { android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
return START_NOT_STICKY; .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);
} }
return START_STICKY;
} }
@Override @Override
@ -188,9 +181,7 @@ public class LiveNotificationService extends Service implements NetworkStateRece
@Override @Override
public void onTaskRemoved(Intent rootIntent) { public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent); super.onTaskRemoved(rootIntent);
if (backgroundProcess) { restart();
restart();
}
} }
private void restart() { private void restart() {

View File

@ -35,7 +35,14 @@ public class RestartLiveNotificationReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true); boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
if (liveNotifications) { boolean delayedNotifications = sharedpreferences.getBoolean(Helper.SET_DELAYED_NOTIFICATIONS, true);
if (delayedNotifications) {
Intent streamingServiceIntent = new Intent(context.getApplicationContext(), LiveNotificationDelayedService.class);
try {
context.startService(streamingServiceIntent);
} catch (Exception ignored) {
}
}else if (liveNotifications) {
Intent streamingServiceIntent = new Intent(context.getApplicationContext(), LiveNotificationService.class); Intent streamingServiceIntent = new Intent(context.getApplicationContext(), LiveNotificationService.class);
try { try {
context.startService(streamingServiceIntent); context.startService(streamingServiceIntent);

View File

@ -537,7 +537,30 @@
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
</LinearLayout> </LinearLayout>
<!-- Choose stream -->
<LinearLayout <LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:textSize="16sp"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set_live_type_title" />
<Spinner
android:id="@+id/set_live_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/live_notif_per_account"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="@dimen/settings_checkbox_margin" android:layout_marginTop="@dimen/settings_checkbox_margin"
@ -571,6 +594,9 @@
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:visibility="gone" android:visibility="gone"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -1719,85 +1745,6 @@
</LinearLayout> </LinearLayout>
<!-- BATTERY -->
<LinearLayout
android:id="@+id/settings_battery"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="match_parent"
android:orientation="vertical">
<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_live_notify"
android:layout_width="wrap_content"
android:textSize="16sp"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/set_live_notify_text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:textSize="16sp"
android:text="@string/live_notif"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textColor="@color/mastodonC2"
android:text="@string/live_notif_indication"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</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_keep_background_process"
android:layout_width="wrap_content"
android:textSize="16sp"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/set_keep_background_process_text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:textSize="16sp"
android:text="@string/set_keep_background_process"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textColor="@color/mastodonC2"
android:text="@string/set_keep_background_process_indication"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!-- COMPOSE --> <!-- COMPOSE -->
<LinearLayout <LinearLayout
android:id="@+id/settings_compose" android:id="@+id/settings_compose"

View File

@ -660,7 +660,6 @@
<string name="comment">Comment</string> <string name="comment">Comment</string>
<string name="peertube_instance">Peertube instance</string> <string name="peertube_instance">Peertube instance</string>
<string name="set_display_direct">Display private messages timeline</string> <string name="set_display_direct">Display private messages timeline</string>
<string name="set_keep_background_process">Keep background process when app is closed</string>
<string name="no_comments">Be the first to leave a comment on this video with the top right button!</string> <string name="no_comments">Be the first to leave a comment on this video with the top right button!</string>
<string name="number_view_video">%s views</string> <string name="number_view_video">%s views</string>
<string name="duration_video">Duration: %s</string> <string name="duration_video">Duration: %s</string>
@ -1209,4 +1208,8 @@
<string name="set_invidious_host">Enter your custom host or leave blank for using invidio.us</string> <string name="set_invidious_host">Enter your custom host or leave blank for using invidio.us</string>
<string name="set_hide_status_bar">Hide Fedilab notification bar</string> <string name="set_hide_status_bar">Hide Fedilab notification bar</string>
<string name="set_hide_status_bar_indication">For hiding the remaining notification in the status bar, click on the eye icon button then uncheck: \"Display in status bar\"</string> <string name="set_hide_status_bar_indication">For hiding the remaining notification in the status bar, click on the eye icon button then uncheck: \"Display in status bar\"</string>
<string name="set_live_type_title">Enable delayed notifications</string>
<string name="set_live_type_indication">Notifications will be delayed every 30 seconds. That will allow to drain less battery.</string>
<string name="live_delayed">Live notifications delayed</string>
<string name="no_live_notif">No live notifications</string>
</resources> </resources>