| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.util; |
| 18 |
|
| 19 |
import android.app.Notification; |
| 20 |
import android.app.NotificationManager; |
| 21 |
import android.content.Context; |
| 22 |
import android.database.ContentObserver; |
| 23 |
import android.net.Uri; |
| 24 |
import android.os.Handler; |
| 25 |
import android.os.Looper; |
| 26 |
import android.os.UserHandle; |
| 27 |
import android.provider.Settings; |
| 28 |
import android.service.notification.StatusBarNotification; |
| 29 |
import android.util.SparseArray; |
| 30 |
|
| 31 |
import java.util.Collection; |
| 32 |
import java.util.Objects; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class NotificationMessagingUtil { |
| 39 |
|
| 40 |
private static final String DEFAULT_SMS_APP_SETTING = Settings.Secure.SMS_DEFAULT_APPLICATION; |
| 41 |
private final Context mContext; |
| 42 |
private SparseArray<String> mDefaultSmsApp = new SparseArray<>(); |
| 43 |
|
| 44 |
public NotificationMessagingUtil(Context context) { |
| 45 |
mContext = context; |
| 46 |
mContext.getContentResolver().registerContentObserver( |
| 47 |
Settings.Secure.getUriFor(DEFAULT_SMS_APP_SETTING), false, mSmsContentObserver); |
| 48 |
} |
| 49 |
|
| 50 |
public boolean isImportantMessaging(StatusBarNotification sbn, int importance) { |
| 51 |
if (importance < NotificationManager.IMPORTANCE_LOW) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
return hasMessagingStyle(sbn) || (isCategoryMessage(sbn) && isDefaultMessagingApp(sbn)); |
| 56 |
} |
| 57 |
|
| 58 |
public boolean isMessaging(StatusBarNotification sbn) { |
| 59 |
return hasMessagingStyle(sbn) || isDefaultMessagingApp(sbn) || isCategoryMessage(sbn); |
| 60 |
} |
| 61 |
|
| 62 |
@SuppressWarnings("deprecation") |
| 63 |
private boolean isDefaultMessagingApp(StatusBarNotification sbn) { |
| 64 |
final int userId = sbn.getUserId(); |
| 65 |
if (userId == UserHandle.USER_NULL || userId == UserHandle.USER_ALL) return false; |
| 66 |
if (mDefaultSmsApp.get(userId) == null) { |
| 67 |
cacheDefaultSmsApp(userId); |
| 68 |
} |
| 69 |
return Objects.equals(mDefaultSmsApp.get(userId), sbn.getPackageName()); |
| 70 |
} |
| 71 |
|
| 72 |
private void cacheDefaultSmsApp(int userId) { |
| 73 |
mDefaultSmsApp.put(userId, Settings.Secure.getStringForUser( |
| 74 |
mContext.getContentResolver(), |
| 75 |
Settings.Secure.SMS_DEFAULT_APPLICATION, userId)); |
| 76 |
} |
| 77 |
|
| 78 |
private final ContentObserver mSmsContentObserver = new ContentObserver( |
| 79 |
new Handler(Looper.getMainLooper())) { |
| 80 |
@Override |
| 81 |
public void onChange(boolean selfChange, Collection<Uri> uris, int flags, int userId) { |
| 82 |
if (uris.contains(Settings.Secure.getUriFor(DEFAULT_SMS_APP_SETTING))) { |
| 83 |
cacheDefaultSmsApp(userId); |
| 84 |
} |
| 85 |
} |
| 86 |
}; |
| 87 |
|
| 88 |
private boolean hasMessagingStyle(StatusBarNotification sbn) { |
| 89 |
return sbn.getNotification().isStyle(Notification.MessagingStyle.class); |
| 90 |
} |
| 91 |
|
| 92 |
private boolean isCategoryMessage(StatusBarNotification sbn) { |
| 93 |
return Notification.CATEGORY_MESSAGE.equals(sbn.getNotification().category); |
| 94 |
} |
| 95 |
} |