17d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek/* 27d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * Copyright (C) 2017 The Android Open Source Project 37d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * 47d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * Licensed under the Apache License, Version 2.0 (the "License"); 57d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * you may not use this file except in compliance with the License. 67d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * You may obtain a copy of the License at 77d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * 87d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * http://www.apache.org/licenses/LICENSE-2.0 97d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * 107d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * Unless required by applicable law or agreed to in writing, software 117d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * distributed under the License is distributed on an "AS IS" BASIS, 127d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * See the License for the specific language governing permissions and 147d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * limitations under the License 157d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek */ 167d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek 177d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekpackage com.android.internal.util; 187d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek 197d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekimport android.app.Notification; 207d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekimport android.app.NotificationManager; 217d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekimport android.content.Context; 227d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekimport android.database.ContentObserver; 237d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekimport android.net.Uri; 247d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekimport android.os.Handler; 257d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekimport android.os.Looper; 267d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekimport android.os.UserHandle; 277d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekimport android.provider.Settings; 287d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekimport android.service.notification.StatusBarNotification; 297d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekimport android.text.TextUtils; 307d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekimport android.util.ArrayMap; 317d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek 327d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekimport java.util.Objects; 337d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek 347d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek/** 357d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * A util to look up messaging related functions for notifications. This is used for both the 367d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek * ranking and the actual layout. 377d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek */ 387d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinekpublic class NotificationMessagingUtil { 397d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek 407d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek private static final String DEFAULT_SMS_APP_SETTING = Settings.Secure.SMS_DEFAULT_APPLICATION; 417d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek private final Context mContext; 427d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek private ArrayMap<Integer, String> mDefaultSmsApp = new ArrayMap<>(); 437d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek 447d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek public NotificationMessagingUtil(Context context) { 457d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek mContext = context; 467d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek mContext.getContentResolver().registerContentObserver( 477d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek Settings.Secure.getUriFor(DEFAULT_SMS_APP_SETTING), false, mSmsContentObserver); 487d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek } 497d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek 507d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek @SuppressWarnings("deprecation") 517d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek private boolean isDefaultMessagingApp(StatusBarNotification sbn) { 527d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek final int userId = sbn.getUserId(); 537d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek if (userId == UserHandle.USER_NULL || userId == UserHandle.USER_ALL) return false; 547d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek if (mDefaultSmsApp.get(userId) == null) { 557d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek cacheDefaultSmsApp(userId); 567d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek } 577d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek return Objects.equals(mDefaultSmsApp.get(userId), sbn.getPackageName()); 587d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek } 597d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek 607d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek private void cacheDefaultSmsApp(int userId) { 617d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek mDefaultSmsApp.put(userId, Settings.Secure.getStringForUser( 627d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek mContext.getContentResolver(), 637d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek Settings.Secure.SMS_DEFAULT_APPLICATION, userId)); 647d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek } 657d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek 667d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek private final ContentObserver mSmsContentObserver = new ContentObserver( 677d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek new Handler(Looper.getMainLooper())) { 687d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek @Override 697d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek public void onChange(boolean selfChange, Uri uri, int userId) { 707d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek if (Settings.Secure.getUriFor(DEFAULT_SMS_APP_SETTING).equals(uri)) { 717d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek cacheDefaultSmsApp(userId); 727d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek } 737d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek } 747d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek }; 757d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek 767d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek public boolean isImportantMessaging(StatusBarNotification sbn, int importance) { 777d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek if (importance < NotificationManager.IMPORTANCE_LOW) { 787d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek return false; 797d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek } 807d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek 817d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek Class<? extends Notification.Style> style = sbn.getNotification().getNotificationStyle(); 827d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek if (Notification.MessagingStyle.class.equals(style)) { 837d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek return true; 847d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek } 857d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek 867d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek if (Notification.CATEGORY_MESSAGE.equals(sbn.getNotification().category) 877d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek && isDefaultMessagingApp(sbn)) { 887d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek return true; 897d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek } 907d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek 917d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek return false; 927d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek } 937d1009b3bd3b69768f293eb73109c52b8da8ec8eSelim Cinek} 94