Home | History | Annotate | Download | only in util
      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.ArrayMap;
     30 
     31 import java.util.Objects;
     32 
     33 /**
     34  * A util to look up messaging related functions for notifications. This is used for both the
     35  * ranking and the actual layout.
     36  */
     37 public class NotificationMessagingUtil {
     38 
     39     private static final String DEFAULT_SMS_APP_SETTING = Settings.Secure.SMS_DEFAULT_APPLICATION;
     40     private final Context mContext;
     41     private ArrayMap<Integer, String> mDefaultSmsApp = new ArrayMap<>();
     42 
     43     public NotificationMessagingUtil(Context context) {
     44         mContext = context;
     45         mContext.getContentResolver().registerContentObserver(
     46                 Settings.Secure.getUriFor(DEFAULT_SMS_APP_SETTING), false, mSmsContentObserver);
     47     }
     48 
     49     public boolean isImportantMessaging(StatusBarNotification sbn, int importance) {
     50         if (importance < NotificationManager.IMPORTANCE_LOW) {
     51             return false;
     52         }
     53 
     54         return hasMessagingStyle(sbn) || (isCategoryMessage(sbn) && isDefaultMessagingApp(sbn));
     55     }
     56 
     57     public boolean isMessaging(StatusBarNotification sbn) {
     58         return hasMessagingStyle(sbn) || isDefaultMessagingApp(sbn) || isCategoryMessage(sbn);
     59     }
     60 
     61     @SuppressWarnings("deprecation")
     62     private boolean isDefaultMessagingApp(StatusBarNotification sbn) {
     63         final int userId = sbn.getUserId();
     64         if (userId == UserHandle.USER_NULL || userId == UserHandle.USER_ALL) return false;
     65         if (mDefaultSmsApp.get(userId) == null) {
     66             cacheDefaultSmsApp(userId);
     67         }
     68         return Objects.equals(mDefaultSmsApp.get(userId), sbn.getPackageName());
     69     }
     70 
     71     private void cacheDefaultSmsApp(int userId) {
     72         mDefaultSmsApp.put(userId, Settings.Secure.getStringForUser(
     73                 mContext.getContentResolver(),
     74                 Settings.Secure.SMS_DEFAULT_APPLICATION, userId));
     75     }
     76 
     77     private final ContentObserver mSmsContentObserver = new ContentObserver(
     78             new Handler(Looper.getMainLooper())) {
     79         @Override
     80         public void onChange(boolean selfChange, Uri uri, int userId) {
     81             if (Settings.Secure.getUriFor(DEFAULT_SMS_APP_SETTING).equals(uri)) {
     82                 cacheDefaultSmsApp(userId);
     83             }
     84         }
     85     };
     86 
     87     private boolean hasMessagingStyle(StatusBarNotification sbn) {
     88         Class<? extends Notification.Style> style = sbn.getNotification().getNotificationStyle();
     89         return Notification.MessagingStyle.class.equals(style);
     90     }
     91 
     92     private boolean isCategoryMessage(StatusBarNotification sbn) {
     93         return Notification.CATEGORY_MESSAGE.equals(sbn.getNotification().category);
     94     }
     95 }
     96