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.PackageManager;
     24 import android.graphics.drawable.Drawable;
     25 import android.os.ServiceManager;
     26 import android.service.notification.NotificationListenerService;
     27 import android.util.Log;
     28 
     29 public class NotificationBackend {
     30     private static final String TAG = "NotificationBackend";
     31 
     32     static INotificationManager sINM = INotificationManager.Stub.asInterface(
     33             ServiceManager.getService(Context.NOTIFICATION_SERVICE));
     34 
     35     public AppRow loadAppRow(PackageManager pm, ApplicationInfo app) {
     36         final AppRow row = new AppRow();
     37         row.pkg = app.packageName;
     38         row.uid = app.uid;
     39         try {
     40             row.label = app.loadLabel(pm);
     41         } catch (Throwable t) {
     42             Log.e(TAG, "Error loading application label for " + row.pkg, t);
     43             row.label = row.pkg;
     44         }
     45         row.icon = app.loadIcon(pm);
     46         row.banned = getNotificationsBanned(row.pkg, row.uid);
     47         row.priority = getHighPriority(row.pkg, row.uid);
     48         row.peekable = getPeekable(row.pkg, row.uid);
     49         row.sensitive = getSensitive(row.pkg, row.uid);
     50         return row;
     51     }
     52 
     53     public boolean setNotificationsBanned(String pkg, int uid, boolean banned) {
     54         try {
     55             sINM.setNotificationsEnabledForPackage(pkg, uid, !banned);
     56             return true;
     57         } catch (Exception e) {
     58            Log.w(TAG, "Error calling NoMan", e);
     59            return false;
     60         }
     61     }
     62 
     63     public boolean getNotificationsBanned(String pkg, int uid) {
     64         try {
     65             final boolean enabled = sINM.areNotificationsEnabledForPackage(pkg, uid);
     66             return !enabled;
     67         } catch (Exception e) {
     68             Log.w(TAG, "Error calling NoMan", e);
     69             return false;
     70         }
     71     }
     72 
     73     public boolean getHighPriority(String pkg, int uid) {
     74         try {
     75             return sINM.getPackagePriority(pkg, uid) == Notification.PRIORITY_MAX;
     76         } catch (Exception e) {
     77             Log.w(TAG, "Error calling NoMan", e);
     78             return false;
     79         }
     80     }
     81 
     82     public boolean setHighPriority(String pkg, int uid, boolean highPriority) {
     83         try {
     84             sINM.setPackagePriority(pkg, uid,
     85                     highPriority ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT);
     86             return true;
     87         } catch (Exception e) {
     88             Log.w(TAG, "Error calling NoMan", e);
     89             return false;
     90         }
     91     }
     92 
     93     public boolean getPeekable(String pkg, int uid) {
     94         try {
     95             return sINM.getPackagePeekable(pkg, uid);
     96         } catch (Exception e) {
     97             Log.w(TAG, "Error calling NoMan", e);
     98             return false;
     99         }
    100     }
    101 
    102     public boolean setPeekable(String pkg, int uid, boolean peekable) {
    103         try {
    104             sINM.setPackagePeekable(pkg, uid, peekable);
    105             return true;
    106         } catch (Exception e) {
    107            Log.w(TAG, "Error calling NoMan", e);
    108            return false;
    109         }
    110     }
    111 
    112     public boolean getSensitive(String pkg, int uid) {
    113         try {
    114             return sINM.getPackageVisibilityOverride(pkg, uid) == Notification.VISIBILITY_PRIVATE;
    115         } catch (Exception e) {
    116             Log.w(TAG, "Error calling NoMan", e);
    117             return false;
    118         }
    119     }
    120 
    121     public boolean setSensitive(String pkg, int uid, boolean sensitive) {
    122         try {
    123             sINM.setPackageVisibilityOverride(pkg, uid,
    124                     sensitive ? Notification.VISIBILITY_PRIVATE
    125                             : NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE);
    126             return true;
    127         } catch (Exception e) {
    128             Log.w(TAG, "Error calling NoMan", e);
    129             return false;
    130         }
    131     }
    132 
    133     static class Row {
    134         public String section;
    135     }
    136 
    137     public static class AppRow extends Row {
    138         public String pkg;
    139         public int uid;
    140         public Drawable icon;
    141         public CharSequence label;
    142         public Intent settingsIntent;
    143         public boolean banned;
    144         public boolean priority;
    145         public boolean peekable;
    146         public boolean sensitive;
    147         public boolean first;  // first app in section
    148     }
    149 
    150 }
    151