Defer new message notification when calling

This commit is contained in:
M66B 2022-06-18 16:52:50 +02:00
parent 425a057af6
commit 9710b7cddb
6 changed files with 81 additions and 94 deletions

View File

@ -4943,7 +4943,6 @@ class Core {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean badge = prefs.getBoolean("badge", true);
boolean notify_background_only = prefs.getBoolean("notify_background_only", false);
boolean notify_suppress_in_call = prefs.getBoolean("notify_suppress_in_call", false);
boolean notify_summary = prefs.getBoolean("notify_summary", false);
boolean notify_preview = prefs.getBoolean("notify_preview", true);
boolean notify_preview_only = prefs.getBoolean("notify_preview_only", false);
@ -4998,14 +4997,6 @@ class Core {
continue;
}
if (notify_suppress_in_call && message.notifying == 0 &&
MediaPlayerHelper.isInCall(context)) {
Log.i("Notify call=" + message.id);
if (!message.ui_ignored)
db.message().setMessageUiIgnored(message.id, true);
continue;
}
long group = (pro && message.accountNotify ? message.account : 0);
if (!message.folderUnified)
group = -message.folder;

View File

@ -468,6 +468,9 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared
}
});
swNotifySuppressInCall.setVisibility(
Build.VERSION.SDK_INT < Build.VERSION_CODES.S
? View.GONE : View.VISIBLE);
swNotifySuppressInCall.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {

View File

@ -678,39 +678,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
};
});
final TwoStateOwner cowner = new TwoStateOwner(this, "liveUnseenNotify");
if (BuildConfig.DEBUG &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Widgets will be disabled too
AudioManager am = Helper.getSystemService(this, AudioManager.class);
AudioManager.OnModeChangedListener listener = new AudioManager.OnModeChangedListener() {
@Override
public void onModeChanged(int mode) {
boolean inCall = MediaPlayerHelper.isInCall(mode);
boolean notify_suppress_in_call = prefs.getBoolean("notify_suppress_in_call", false);
Log.i("Owner inCall=" + inCall + " suppress=" + notify_suppress_in_call);
cowner.setEnabled(!notify_suppress_in_call || !inCall);
}
};
listener.onModeChanged(am.getMode()); // Init
getLifecycle().addObserver(new LifecycleObserver() {
private boolean registered = false;
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onStateChanged() {
if (ServiceSynchronize.this.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
am.addOnModeChangedListener(executor, listener);
registered = true;
} else if (registered) {
am.removeOnModeChangedListener(listener);
registered = false;
}
}
});
}
final TwoStateOwner cowner = new TwoStateOwner(this, "liveSynchronizing");
db.folder().liveSynchronizing().observe(this, new Observer<List<TupleFolderSync>>() {
private List<Long> lastAccounts = new ArrayList<>();
@ -784,15 +752,79 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
}
});
// New message notifications batching
Core.NotificationData notificationData = new Core.NotificationData(this);
MutableLiveData<List<TupleMessageEx>> mutableUnseenNotify = new MutableLiveData<>();
db.message().liveUnseenNotify().observe(cowner, new Observer<List<TupleMessageEx>>() {
@Override
public void onChanged(List<TupleMessageEx> messages) {
mutableUnseenNotify.setValue(messages);
}
});
final TwoStateOwner mowner = new TwoStateOwner(this, "mutableUnseenNotify");
AudioManager am = Helper.getSystemService(this, AudioManager.class);
if (am == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
mowner.start();
Log.i("Audio mode legacy");
} else {
AudioManager.OnModeChangedListener listener = new AudioManager.OnModeChangedListener() {
@Override
public void onModeChanged(int mode) {
getMainHandler().post(new RunnableEx("AudioMode") {
@Override
public void delegate() {
boolean incall = MediaPlayerHelper.isInCall(mode);
boolean suppress = prefs.getBoolean("notify_suppress_in_call", false);
boolean start = (!suppress || !incall);
Log.i("Audio mode start=" + start +
" incall=" + incall +
" suppress=" + suppress);
if (start)
mowner.start();
else
mowner.stop();
}
});
}
};
listener.onModeChanged(am.getMode()); // Init
getLifecycle().addObserver(new LifecycleObserver() {
private boolean registered = false;
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onStateChanged() {
try {
if (ServiceSynchronize.this.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
if (!registered) {
am.addOnModeChangedListener(executor, listener);
registered = true;
}
} else {
if (registered) {
am.removeOnModeChangedListener(listener);
registered = false;
}
}
Log.i("Audio mode registered=" + registered);
} catch (Throwable ex) {
Log.e(ex);
}
}
});
}
mutableUnseenNotify.observe(mowner, new Observer<List<TupleMessageEx>>() {
private final ExecutorService executor =
Helper.getBackgroundExecutor(1, "notify");
@Override
public void onChanged(final List<TupleMessageEx> messages) {
executor.submit(new RunnableEx("liveUnseenNotify") {
executor.submit(new RunnableEx("mutableUnseenNotify") {
@Override
public void delegate() {
try {
@ -809,6 +841,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
}
});
// Message count widgets
db.message().liveWidgetUnseen(null).observe(cowner, new Observer<List<TupleMessageStats>>() {
private Integer lastCount = null;
private List<TupleMessageStats> last = null;
@ -874,6 +908,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
}
});
// Message list widgets
db.message().liveWidgetUnified().observe(cowner, new Observer<List<TupleMessageWidgetCount>>() {
private List<TupleMessageWidgetCount> last = null;

View File

@ -37,8 +37,6 @@ import java.util.Objects;
public class TwoStateOwner implements LifecycleOwner {
private String name;
private boolean enabled = true;
private Boolean start = null;
private boolean owned = true;
private Object condition;
private LifecycleRegistry registry;
@ -79,27 +77,6 @@ public class TwoStateOwner implements LifecycleOwner {
});
}
void setEnabled(boolean value) {
Log.i("Owner enable=" + value + " state=" + registry.getCurrentState().name());
if (enabled && !value) {
boolean started = registry.getCurrentState().isAtLeast(Lifecycle.State.STARTED);
stop();
start = (started ? true : null);
Log.i("Owner stopped start=" + start);
}
enabled = value;
if (enabled && start != null) {
Log.i("Owner restore start=" + start);
if (start)
start();
else
stop();
start = null;
}
}
private void create() {
if (owned) {
// Initialize
@ -122,21 +99,15 @@ public class TwoStateOwner implements LifecycleOwner {
}
void start() {
if (enabled) {
Lifecycle.State state = registry.getCurrentState();
if (!state.equals(Lifecycle.State.STARTED) && !state.equals(Lifecycle.State.DESTROYED))
setState(Lifecycle.State.STARTED);
} else
start = true;
Lifecycle.State state = registry.getCurrentState();
if (!state.equals(Lifecycle.State.STARTED) && !state.equals(Lifecycle.State.DESTROYED))
setState(Lifecycle.State.STARTED);
}
void stop() {
if (enabled) {
Lifecycle.State state = registry.getCurrentState();
if (!state.equals(Lifecycle.State.CREATED) && !state.equals(Lifecycle.State.DESTROYED))
setState(Lifecycle.State.CREATED);
} else
start = false;
Lifecycle.State state = registry.getCurrentState();
if (!state.equals(Lifecycle.State.CREATED) && !state.equals(Lifecycle.State.DESTROYED))
setState(Lifecycle.State.CREATED);
}
void restart() {

View File

@ -549,19 +549,6 @@
app:layout_constraintTop_toBottomOf="@id/tvNotifyKnownPro"
app:switchPadding="12dp" />
<TextView
android:id="@+id/tvNotifySuppressHint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginEnd="48dp"
android:text="@string/title_advanced_notify_suppress_hint"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swNotifySuppressInCall" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swNotifySummary"
android:layout_width="0dp"
@ -570,7 +557,7 @@
android:text="@string/title_advanced_notify_summary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvNotifySuppressHint"
app:layout_constraintTop_toBottomOf="@id/swNotifySuppressInCall"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat

View File

@ -617,7 +617,7 @@
<string name="title_advanced_unseen_ignored">Let the number of new messages match the number of notifications</string>
<string name="title_advanced_notify_background">Show notifications when in the background only</string>
<string name="title_advanced_notify_known">Show notifications for contacts only</string>
<string name="title_advanced_notify_suppress_in_call">Suppress notifications when calling</string>
<string name="title_advanced_notify_suppress_in_call">Delay notifications while on a call</string>
<string name="title_advanced_notify_summary">Show summary notification only</string>
<string name="title_advanced_notify_preview">Show message preview in notifications</string>
<string name="title_advanced_notify_preview_all">Preview all text</string>
@ -879,7 +879,6 @@
<string name="title_advanced_badge_hint">Only available on supported launchers</string>
<string name="title_advanced_notify_action_hint">At most three actions will be shown</string>
<string name="title_advanced_notify_suppress_hint">Suppressed notifications will not be shown again later</string>
<string name="title_advanced_notify_remove_hint">New message notifications will always be removed on being swiped away and on marking messages read</string>
<string name="title_advanced_notify_manage_hint">Tap on the channel name to set the channel properties</string>
<string name="title_advanced_notify_manage_default_hint">To set the default sound, etc</string>