1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 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.text.TextUtils; 30 import android.util.ArrayMap; 31 32 import java.util.Objects; 33 34 /** 35 * A util to look up messaging related functions for notifications. This is used for both the 36 * ranking and the actual layout. 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 ArrayMap<Integer, String> mDefaultSmsApp = new ArrayMap<>(); 43 NotificationMessagingUtil(Context context)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 @SuppressWarnings("deprecation") isDefaultMessagingApp(StatusBarNotification sbn)51 private boolean isDefaultMessagingApp(StatusBarNotification sbn) { 52 final int userId = sbn.getUserId(); 53 if (userId == UserHandle.USER_NULL || userId == UserHandle.USER_ALL) return false; 54 if (mDefaultSmsApp.get(userId) == null) { 55 cacheDefaultSmsApp(userId); 56 } 57 return Objects.equals(mDefaultSmsApp.get(userId), sbn.getPackageName()); 58 } 59 cacheDefaultSmsApp(int userId)60 private void cacheDefaultSmsApp(int userId) { 61 mDefaultSmsApp.put(userId, Settings.Secure.getStringForUser( 62 mContext.getContentResolver(), 63 Settings.Secure.SMS_DEFAULT_APPLICATION, userId)); 64 } 65 66 private final ContentObserver mSmsContentObserver = new ContentObserver( 67 new Handler(Looper.getMainLooper())) { 68 @Override 69 public void onChange(boolean selfChange, Uri uri, int userId) { 70 if (Settings.Secure.getUriFor(DEFAULT_SMS_APP_SETTING).equals(uri)) { 71 cacheDefaultSmsApp(userId); 72 } 73 } 74 }; 75 isImportantMessaging(StatusBarNotification sbn, int importance)76 public boolean isImportantMessaging(StatusBarNotification sbn, int importance) { 77 if (importance < NotificationManager.IMPORTANCE_LOW) { 78 return false; 79 } 80 81 Class<? extends Notification.Style> style = sbn.getNotification().getNotificationStyle(); 82 if (Notification.MessagingStyle.class.equals(style)) { 83 return true; 84 } 85 86 if (Notification.CATEGORY_MESSAGE.equals(sbn.getNotification().category) 87 && isDefaultMessagingApp(sbn)) { 88 return true; 89 } 90 91 return false; 92 } 93 } 94