-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check DND settings before show activity or play ring or vibrate.
- Loading branch information
1 parent
c247935
commit a135e7e
Showing
5 changed files
with
122 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/org/thoughtcrime/securesms/notifications/DoNotDisturbUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters