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.util.SparseArray;
30 
31 import java.util.Collection;
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 SparseArray<String> mDefaultSmsApp = new SparseArray<>();
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 
isImportantMessaging(StatusBarNotification sbn, int importance)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 
isMessaging(StatusBarNotification sbn)58     public boolean isMessaging(StatusBarNotification sbn) {
59         return hasMessagingStyle(sbn) || isDefaultMessagingApp(sbn) || isCategoryMessage(sbn);
60     }
61 
62     @SuppressWarnings("deprecation")
isDefaultMessagingApp(StatusBarNotification sbn)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 
cacheDefaultSmsApp(int userId)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 
hasMessagingStyle(StatusBarNotification sbn)88     private boolean hasMessagingStyle(StatusBarNotification sbn) {
89         return sbn.getNotification().isStyle(Notification.MessagingStyle.class);
90     }
91 
isCategoryMessage(StatusBarNotification sbn)92     private boolean isCategoryMessage(StatusBarNotification sbn) {
93         return Notification.CATEGORY_MESSAGE.equals(sbn.getNotification().category);
94     }
95 }
96