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
17package com.android.internal.util;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.Context;
22import android.database.ContentObserver;
23import android.net.Uri;
24import android.os.Handler;
25import android.os.Looper;
26import android.os.UserHandle;
27import android.provider.Settings;
28import android.service.notification.StatusBarNotification;
29import android.text.TextUtils;
30import android.util.ArrayMap;
31
32import 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 */
38public 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
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")
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
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
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