Home | History | Annotate | Download | only in notification
      1 /*
      2  * Copyright (C) 2015 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 package com.android.settings.notification;
     17 
     18 import android.app.INotificationManager;
     19 import android.app.Notification;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.pm.ApplicationInfo;
     23 import android.content.pm.PackageInfo;
     24 import android.content.pm.PackageManager;
     25 import android.graphics.drawable.Drawable;
     26 import android.os.ServiceManager;
     27 import android.os.UserHandle;
     28 import android.service.notification.NotificationListenerService;
     29 import android.util.Log;
     30 
     31 import com.android.internal.widget.LockPatternUtils;
     32 import com.android.settingslib.Utils;
     33 
     34 public class NotificationBackend {
     35     private static final String TAG = "NotificationBackend";
     36 
     37     static INotificationManager sINM = INotificationManager.Stub.asInterface(
     38             ServiceManager.getService(Context.NOTIFICATION_SERVICE));
     39 
     40     public AppRow loadAppRow(Context context, PackageManager pm, ApplicationInfo app) {
     41         final AppRow row = new AppRow();
     42         row.pkg = app.packageName;
     43         row.uid = app.uid;
     44         try {
     45             row.label = app.loadLabel(pm);
     46         } catch (Throwable t) {
     47             Log.e(TAG, "Error loading application label for " + row.pkg, t);
     48             row.label = row.pkg;
     49         }
     50         row.icon = app.loadIcon(pm);
     51         row.banned = getNotificationsBanned(row.pkg, row.uid);
     52         row.appImportance = getImportance(row.pkg, row.uid);
     53         row.appBypassDnd = getBypassZenMode(row.pkg, row.uid);
     54         row.appVisOverride = getVisibilityOverride(row.pkg, row.uid);
     55         row.lockScreenSecure = new LockPatternUtils(context).isSecure(
     56                 UserHandle.myUserId());
     57         return row;
     58     }
     59 
     60     public AppRow loadAppRow(Context context, PackageManager pm, PackageInfo app) {
     61         final AppRow row = loadAppRow(context, pm, app.applicationInfo);
     62         row.systemApp = Utils.isSystemPackage(context.getResources(), pm, app);
     63         return row;
     64     }
     65 
     66     public boolean getNotificationsBanned(String pkg, int uid) {
     67         try {
     68             final boolean enabled = sINM.areNotificationsEnabledForPackage(pkg, uid);
     69             return !enabled;
     70         } catch (Exception e) {
     71             Log.w(TAG, "Error calling NoMan", e);
     72             return false;
     73         }
     74     }
     75 
     76     public boolean getBypassZenMode(String pkg, int uid) {
     77         try {
     78             return sINM.getPriority(pkg, uid) == Notification.PRIORITY_MAX;
     79         } catch (Exception e) {
     80             Log.w(TAG, "Error calling NoMan", e);
     81             return false;
     82         }
     83     }
     84 
     85     public boolean setBypassZenMode(String pkg, int uid, boolean bypassZen) {
     86         try {
     87             sINM.setPriority(pkg, uid,
     88                     bypassZen ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT);
     89             return true;
     90         } catch (Exception e) {
     91             Log.w(TAG, "Error calling NoMan", e);
     92             return false;
     93         }
     94     }
     95 
     96     public int getVisibilityOverride(String pkg, int uid) {
     97         try {
     98             return sINM.getVisibilityOverride(pkg, uid);
     99         } catch (Exception e) {
    100             Log.w(TAG, "Error calling NoMan", e);
    101             return NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE;
    102         }
    103     }
    104 
    105     public boolean setVisibilityOverride(String pkg, int uid, int override) {
    106         try {
    107             sINM.setVisibilityOverride(pkg, uid, override);
    108             return true;
    109         } catch (Exception e) {
    110             Log.w(TAG, "Error calling NoMan", e);
    111             return false;
    112         }
    113     }
    114 
    115     public boolean setImportance(String pkg, int uid, int importance) {
    116         try {
    117             sINM.setImportance(pkg, uid, importance);
    118             return true;
    119         } catch (Exception e) {
    120             Log.w(TAG, "Error calling NoMan", e);
    121             return false;
    122         }
    123     }
    124 
    125     public int getImportance(String pkg, int uid) {
    126         try {
    127             return sINM.getImportance(pkg, uid);
    128         } catch (Exception e) {
    129             Log.w(TAG, "Error calling NoMan", e);
    130             return NotificationListenerService.Ranking.IMPORTANCE_UNSPECIFIED;
    131         }
    132     }
    133 
    134     static class Row {
    135         public String section;
    136     }
    137 
    138     public static class AppRow extends Row {
    139         public String pkg;
    140         public int uid;
    141         public Drawable icon;
    142         public CharSequence label;
    143         public Intent settingsIntent;
    144         public boolean banned;
    145         public boolean first;  // first app in section
    146         public boolean systemApp;
    147         public int appImportance;
    148         public boolean appBypassDnd;
    149         public int appVisOverride;
    150         public boolean lockScreenSecure;
    151     }
    152 }
    153