/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.app; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.SdkConstant; import android.app.Notification.Builder; import android.content.ComponentName; import android.content.Context; import android.content.pm.ParceledListSlice; import android.graphics.drawable.Icon; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Parcel; import android.os.Parcelable; import android.os.RemoteException; import android.os.ServiceManager; import android.os.StrictMode; import android.os.UserHandle; import android.provider.Settings.Global; import android.service.notification.NotificationListenerService.Ranking; import android.service.notification.StatusBarNotification; import android.service.notification.ZenModeConfig; import android.util.ArraySet; import android.util.Log; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; /** * Class to notify the user of events that happen. This is how you tell * the user that something has happened in the background. {@more} * * Notifications can take different forms: *
* Each of the notify methods takes an int id parameter and optionally a * {@link String} tag parameter, which may be {@code null}. These parameters * are used to form a pair (tag, id), or ({@code null}, id) if tag is * unspecified. This pair identifies this notification from your app to the * system, so that pair should be unique within your app. If you call one * of the notify methods with a (tag, id) pair that is currently active and * a new set of notification parameters, it will be updated. For example, * if you pass a new status bar icon, the old icon in the status bar will * be replaced with the new one. This is also the same tag and id you pass * to the {@link #cancel(int)} or {@link #cancel(String, int)} method to clear * this notification. * *
* You do not instantiate this class directly; instead, retrieve it through * {@link android.content.Context#getSystemService}. * *
For a guide to creating notifications, read the * Status Bar Notifications * developer guide.
*
* Throws a SecurityException if policy access is granted to this package.
* See {@link #isNotificationPolicyAccessGranted}.
*/
public Map
* Throws a SecurityException if policy access is granted to this package.
* See {@link #isNotificationPolicyAccessGranted}.
*
*
* Returns null if there are no zen rules that match the given id, or if the calling package
* doesn't own the matching rule. See {@link AutomaticZenRule#getOwner}.
*/
public AutomaticZenRule getAutomaticZenRule(String id) {
INotificationManager service = getService();
try {
return service.getAutomaticZenRule(id);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Creates the given zen rule.
*
*
* Throws a SecurityException if policy access is granted to this package.
* See {@link #isNotificationPolicyAccessGranted}.
*
* @param automaticZenRule the rule to create.
* @return The id of the newly created rule; null if the rule could not be created.
*/
public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) {
INotificationManager service = getService();
try {
return service.addAutomaticZenRule(automaticZenRule);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Updates the given zen rule.
*
*
* Throws a SecurityException if policy access is granted to this package.
* See {@link #isNotificationPolicyAccessGranted}.
*
*
* Callers can only update rules that they own. See {@link AutomaticZenRule#getOwner}.
* @param id The id of the rule to update
* @param automaticZenRule the rule to update.
* @return Whether the rule was successfully updated.
*/
public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) {
INotificationManager service = getService();
try {
return service.updateAutomaticZenRule(id, automaticZenRule);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Deletes the automatic zen rule with the given id.
*
*
* Throws a SecurityException if policy access is granted to this package.
* See {@link #isNotificationPolicyAccessGranted}.
*
*
* Callers can only delete rules that they own. See {@link AutomaticZenRule#getOwner}.
* @param id the id of the rule to delete.
* @return Whether the rule was successfully deleted.
*/
public boolean removeAutomaticZenRule(String id) {
INotificationManager service = getService();
try {
return service.removeAutomaticZenRule(id);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Deletes all automatic zen rules owned by the given package.
*
* @hide
*/
public boolean removeAutomaticZenRules(String packageName) {
INotificationManager service = getService();
try {
return service.removeAutomaticZenRules(packageName);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Returns the user specified importance for notifications from the calling package.
*
* @return An importance level, such as {@link #IMPORTANCE_DEFAULT}.
*/
public @Importance int getImportance() {
INotificationManager service = getService();
try {
return service.getPackageImportance(mContext.getPackageName());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Returns whether notifications from the calling package are blocked.
*/
public boolean areNotificationsEnabled() {
INotificationManager service = getService();
try {
return service.areNotificationsEnabled(mContext.getPackageName());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Checks the ability to read/modify notification policy for the calling package.
*
*
* Returns true if the calling package can read/modify notification policy.
*
*
* Request policy access by sending the user to the activity that matches the system intent
* action {@link android.provider.Settings#ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS}.
*
*
* Use {@link #ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED} to listen for
* user grant or denial of this access.
*/
public boolean isNotificationPolicyAccessGranted() {
INotificationManager service = getService();
try {
return service.isNotificationPolicyAccessGranted(mContext.getOpPackageName());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/** @hide */
public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {
INotificationManager service = getService();
try {
return service.isNotificationPolicyAccessGrantedForPackage(pkg);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Gets the current notification policy.
*
*
* Only available if policy access is granted to this package.
* See {@link #isNotificationPolicyAccessGranted}.
*/
public Policy getNotificationPolicy() {
INotificationManager service = getService();
try {
return service.getNotificationPolicy(mContext.getOpPackageName());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Sets the current notification policy.
*
*
* Only available if policy access is granted to this package.
* See {@link #isNotificationPolicyAccessGranted}.
*
* @param policy The new desired policy.
*/
public void setNotificationPolicy(@NonNull Policy policy) {
checkRequired("policy", policy);
INotificationManager service = getService();
try {
service.setNotificationPolicy(mContext.getOpPackageName(), policy);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/** @hide */
public void setNotificationPolicyAccessGranted(String pkg, boolean granted) {
INotificationManager service = getService();
try {
service.setNotificationPolicyAccessGranted(pkg, granted);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/** @hide */
public ArraySet
* The interruption filter defines which notifications are allowed to interrupt the user
* (e.g. via sound & vibration) and is applied globally.
* @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when
* unavailable.
*/
public final @InterruptionFilter int getCurrentInterruptionFilter() {
final INotificationManager service = getService();
try {
return zenModeToInterruptionFilter(service.getZenMode());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Sets the current notification interruption filter.
*
*
* The interruption filter defines which notifications are allowed to interrupt the user
* (e.g. via sound & vibration) and is applied globally.
* @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when
* unavailable.
*
*
* Only available if policy access is granted to this package.
* See {@link #isNotificationPolicyAccessGranted}.
*/
public final void setInterruptionFilter(int interruptionFilter) {
final INotificationManager service = getService();
try {
service.setInterruptionFilter(mContext.getOpPackageName(), interruptionFilter);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/** @hide */
public static int zenModeToInterruptionFilter(int zen) {
switch (zen) {
case Global.ZEN_MODE_OFF: return INTERRUPTION_FILTER_ALL;
case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return INTERRUPTION_FILTER_PRIORITY;
case Global.ZEN_MODE_ALARMS: return INTERRUPTION_FILTER_ALARMS;
case Global.ZEN_MODE_NO_INTERRUPTIONS: return INTERRUPTION_FILTER_NONE;
default: return INTERRUPTION_FILTER_UNKNOWN;
}
}
/** @hide */
public static int zenModeFromInterruptionFilter(int interruptionFilter, int defValue) {
switch (interruptionFilter) {
case INTERRUPTION_FILTER_ALL: return Global.ZEN_MODE_OFF;
case INTERRUPTION_FILTER_PRIORITY: return Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
case INTERRUPTION_FILTER_ALARMS: return Global.ZEN_MODE_ALARMS;
case INTERRUPTION_FILTER_NONE: return Global.ZEN_MODE_NO_INTERRUPTIONS;
default: return defValue;
}
}
}
tag
and id
supplied to
* {@link #notify(String, int, Notification) notify()}
* (via {@link StatusBarNotification#getTag() getTag()} and
* {@link StatusBarNotification#getId() getId()}) as well as a copy of the original
* {@link Notification} object (via {@link StatusBarNotification#getNotification()}).
*
* @return An array of {@link StatusBarNotification}.
*/
public StatusBarNotification[] getActiveNotifications() {
final INotificationManager service = getService();
final String pkg = mContext.getPackageName();
try {
final ParceledListSlice