Check DND settings before show activity or play ring or vibrate.
This commit is contained in:
parent
c247935f1a
commit
a135e7efa2
5 changed files with 122 additions and 3 deletions
|
@ -29,7 +29,6 @@ import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
||||||
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
||||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||||
import org.thoughtcrime.securesms.recipients.RecipientId;
|
import org.thoughtcrime.securesms.recipients.RecipientId;
|
||||||
import org.thoughtcrime.securesms.util.Util;
|
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -204,6 +203,10 @@ public class MmsSmsDatabase extends Database {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean hasReceivedAnyCallsSince(long threadId, long timestamp) {
|
||||||
|
return DatabaseFactory.getSmsDatabase(context).hasReceivedAnyCallsSince(threadId, timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the position of the message with the provided timestamp in the query results you'd
|
* Retrieves the position of the message with the provided timestamp in the query results you'd
|
||||||
* get from calling {@link #getConversation(long)}.
|
* get from calling {@link #getConversation(long)}.
|
||||||
|
|
|
@ -487,6 +487,20 @@ public class SmsDatabase extends MessagingDatabase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean hasReceivedAnyCallsSince(long threadId, long timestamp) {
|
||||||
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||||
|
String[] projection = new String[]{SmsDatabase.TYPE};
|
||||||
|
String selection = THREAD_ID + " = ? AND " + DATE_RECEIVED + " > ? AND (" + TYPE + " = ? OR " + TYPE + " = ?)";
|
||||||
|
String[] selectionArgs = new String[]{String.valueOf(threadId),
|
||||||
|
String.valueOf(timestamp),
|
||||||
|
String.valueOf(Types.INCOMING_CALL_TYPE),
|
||||||
|
String.valueOf(Types.MISSED_CALL_TYPE)};
|
||||||
|
|
||||||
|
try (Cursor cursor = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, null)) {
|
||||||
|
return cursor != null && cursor.moveToFirst();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public @NonNull Pair<Long, Long> insertReceivedCall(@NonNull RecipientId address) {
|
public @NonNull Pair<Long, Long> insertReceivedCall(@NonNull RecipientId address) {
|
||||||
return insertCallLog(address, Types.INCOMING_CALL_TYPE, false);
|
return insertCallLog(address, Types.INCOMING_CALL_TYPE, false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -285,6 +285,10 @@ public class ThreadDatabase extends Database {
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasCalledSince(@NonNull Recipient recipient, long timestamp) {
|
||||||
|
return DatabaseFactory.getMmsSmsDatabase(context).hasReceivedAnyCallsSince(getThreadIdFor(recipient), timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
public List<MarkedMessageInfo> setRead(long threadId, boolean lastSeen) {
|
public List<MarkedMessageInfo> setRead(long threadId, boolean lastSeen) {
|
||||||
ContentValues contentValues = new ContentValues(1);
|
ContentValues contentValues = new ContentValues(1);
|
||||||
contentValues.put(READ, 1);
|
contentValues.put(READ, 1);
|
||||||
|
|
|
@ -0,0 +1,92 @@
|
||||||
|
package org.thoughtcrime.securesms.notifications;
|
||||||
|
|
||||||
|
import android.app.NotificationManager;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.provider.ContactsContract;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.RequiresApi;
|
||||||
|
import androidx.annotation.WorkerThread;
|
||||||
|
|
||||||
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||||
|
import org.thoughtcrime.securesms.logging.Log;
|
||||||
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||||
|
import org.thoughtcrime.securesms.util.ServiceUtil;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public final class DoNotDisturbUtil {
|
||||||
|
|
||||||
|
private static final String TAG = Log.tag(DoNotDisturbUtil.class);
|
||||||
|
|
||||||
|
private DoNotDisturbUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@WorkerThread
|
||||||
|
public static boolean shouldDisturbUserWithCall(@NonNull Context context, @NonNull Recipient recipient) {
|
||||||
|
if (Build.VERSION.SDK_INT <= 23) return true;
|
||||||
|
|
||||||
|
NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);
|
||||||
|
|
||||||
|
switch (notificationManager.getCurrentInterruptionFilter()) {
|
||||||
|
case NotificationManager.INTERRUPTION_FILTER_PRIORITY:
|
||||||
|
return handlePriority(context, notificationManager, recipient);
|
||||||
|
case NotificationManager.INTERRUPTION_FILTER_NONE:
|
||||||
|
case NotificationManager.INTERRUPTION_FILTER_ALARMS:
|
||||||
|
return false;
|
||||||
|
default:
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(23)
|
||||||
|
private static boolean handlePriority(@NonNull Context context, @NonNull NotificationManager notificationManager, @NonNull Recipient recipient) {
|
||||||
|
final NotificationManager.Policy policy = notificationManager.getNotificationPolicy();
|
||||||
|
final boolean areCallsPrioritized = (policy.priorityCategories & NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) != 0;
|
||||||
|
final boolean isRepeatCallerEnabled = (policy.priorityCategories & NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS) != 0;
|
||||||
|
|
||||||
|
if (!areCallsPrioritized && !isRepeatCallerEnabled) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (areCallsPrioritized && !isRepeatCallerEnabled) {
|
||||||
|
return isContactPriority(context, recipient, policy.priorityCallSenders);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!areCallsPrioritized) {
|
||||||
|
return isRepeatCaller(context, recipient);
|
||||||
|
}
|
||||||
|
|
||||||
|
return isContactPriority(context, recipient, policy.priorityCallSenders) || isRepeatCaller(context, recipient);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isContactPriority(@NonNull Context context, @NonNull Recipient recipient, int priority) {
|
||||||
|
switch (priority) {
|
||||||
|
case NotificationManager.Policy.PRIORITY_SENDERS_ANY:
|
||||||
|
return true;
|
||||||
|
case NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS:
|
||||||
|
return recipient.resolve().isSystemContact();
|
||||||
|
case NotificationManager.Policy.PRIORITY_SENDERS_STARRED:
|
||||||
|
return isContactStarred(context, recipient);
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.w(TAG, "Unknown priority " + priority);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isContactStarred(@NonNull Context context, @NonNull Recipient recipient) {
|
||||||
|
if (!recipient.resolve().isSystemContact()) return false;
|
||||||
|
|
||||||
|
try (Cursor cursor = context.getContentResolver().query(recipient.resolve().getContactUri(), new String[]{ContactsContract.Contacts.STARRED}, null, null, null)) {
|
||||||
|
if (cursor == null || !cursor.moveToFirst()) return false;
|
||||||
|
return cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.STARRED)) == 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isRepeatCaller(@NonNull Context context, @NonNull Recipient recipient) {
|
||||||
|
return DatabaseFactory.getThreadDatabase(context).hasCalledSince(recipient, System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(15));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -38,6 +38,7 @@ import org.thoughtcrime.securesms.database.RecipientDatabase.VibrateState;
|
||||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||||
import org.thoughtcrime.securesms.events.WebRtcViewModel;
|
import org.thoughtcrime.securesms.events.WebRtcViewModel;
|
||||||
import org.thoughtcrime.securesms.logging.Log;
|
import org.thoughtcrime.securesms.logging.Log;
|
||||||
|
import org.thoughtcrime.securesms.notifications.DoNotDisturbUtil;
|
||||||
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
||||||
import org.thoughtcrime.securesms.permissions.Permissions;
|
import org.thoughtcrime.securesms.permissions.Permissions;
|
||||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||||
|
@ -499,10 +500,15 @@ public class WebRtcCallService extends Service implements CallConnection.Observe
|
||||||
this.lockManager.updatePhoneState(LockManager.PhoneState.INTERACTIVE);
|
this.lockManager.updatePhoneState(LockManager.PhoneState.INTERACTIVE);
|
||||||
|
|
||||||
sendMessage(WebRtcViewModel.State.CALL_INCOMING, recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
sendMessage(WebRtcViewModel.State.CALL_INCOMING, recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
||||||
startCallCardActivityIfPossible();
|
|
||||||
|
boolean shouldDisturbUserWithCall = DoNotDisturbUtil.shouldDisturbUserWithCall(getApplicationContext(), recipient);
|
||||||
|
if (shouldDisturbUserWithCall) {
|
||||||
|
startCallCardActivityIfPossible();
|
||||||
|
}
|
||||||
|
|
||||||
audioManager.initializeAudioForCall();
|
audioManager.initializeAudioForCall();
|
||||||
|
|
||||||
if (TextSecurePreferences.isCallNotificationsEnabled(this)) {
|
if (shouldDisturbUserWithCall && TextSecurePreferences.isCallNotificationsEnabled(this)) {
|
||||||
Uri ringtone = recipient.resolve().getCallRingtone();
|
Uri ringtone = recipient.resolve().getCallRingtone();
|
||||||
VibrateState vibrateState = recipient.resolve().getCallVibrate();
|
VibrateState vibrateState = recipient.resolve().getCallVibrate();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue