Merge "notification ranking infrastructure"

This commit is contained in:
Chris Wren
2014-05-12 20:48:43 +00:00
committed by Android (Google) Code Review
16 changed files with 868 additions and 535 deletions

View File

@@ -659,8 +659,8 @@ public class Notification implements Parcelable
/**
* @hide
* Extra added by NotificationManagerService to indicate whether a NotificationScorer
* modified the Notifications's score.
* Extra added by NotificationManagerService to indicate whether
* the Notifications's score has been modified.
*/
public static final String EXTRA_SCORE_MODIFIED = "android.scoreModified";

View File

@@ -17,11 +17,15 @@
package android.service.notification;
import android.service.notification.StatusBarNotification;
import android.service.notification.NotificationOrderUpdate;
/** @hide */
oneway interface INotificationListener
{
void onListenerConnected(in String[] notificationKeys);
void onNotificationPosted(in StatusBarNotification notification);
void onNotificationRemoved(in StatusBarNotification notification);
void onListenerConnected(in NotificationOrderUpdate update);
void onNotificationPosted(in StatusBarNotification notification,
in NotificationOrderUpdate update);
void onNotificationRemoved(in StatusBarNotification notification,
in NotificationOrderUpdate update);
void onNotificationOrderUpdate(in NotificationOrderUpdate update);
}

View File

@@ -22,10 +22,13 @@ import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.util.Log;
import java.util.Comparator;
import java.util.HashMap;
/**
* A service that receives calls from the system when new notifications are posted or removed.
* <p>To extend this class, you must declare the service in your manifest file with
@@ -46,6 +49,7 @@ public abstract class NotificationListenerService extends Service {
+ "[" + getClass().getSimpleName() + "]";
private INotificationListenerWrapper mWrapper = null;
private String[] mNotificationKeys;
private INotificationManager mNoMan;
@@ -95,6 +99,15 @@ public abstract class NotificationListenerService extends Service {
// optional
}
/**
* Implement this method to be notified when the notification order cahnges.
*
* Call {@link #getOrderedNotificationKeys()} to retrieve the new order.
*/
public void onNotificationOrderUpdate() {
// optional
}
private final INotificationManager getNotificationInterface() {
if (mNoMan == null) {
mNoMan = INotificationManager.Stub.asInterface(
@@ -202,7 +215,7 @@ public abstract class NotificationListenerService extends Service {
* Request the list of outstanding notifications (that is, those that are visible to the
* current user). Useful when you don't know what's already been posted.
*
* @return An array of active notifications.
* @return An array of active notifications, sorted in natural order.
*/
public StatusBarNotification[] getActiveNotifications() {
return getActiveNotifications(null /*all*/);
@@ -213,7 +226,8 @@ public abstract class NotificationListenerService extends Service {
* current user). Useful when you don't know what's already been posted.
*
* @param keys A specific list of notification keys, or {@code null} for all.
* @return An array of active notifications.
* @return An array of active notifications, sorted in natural order
* if {@code keys} is {@code null}.
*/
public StatusBarNotification[] getActiveNotifications(String[] keys) {
if (!isBound()) return null;
@@ -226,21 +240,15 @@ public abstract class NotificationListenerService extends Service {
}
/**
* Request the list of outstanding notification keys(that is, those that are visible to the
* current user). You can use the notification keys for subsequent retrieval via
* Request the list of notification keys in their current natural order.
* You can use the notification keys for subsequent retrieval via
* {@link #getActiveNotifications(String[]) or dismissal via
* {@link #cancelNotifications(String[]).
*
* @return An array of active notification keys.
* @return An array of active notification keys, in their natural order.
*/
public String[] getActiveNotificationKeys() {
if (!isBound()) return null;
try {
return getNotificationInterface().getActiveNotificationKeysFromListener(mWrapper);
} catch (android.os.RemoteException ex) {
Log.v(TAG, "Unable to contact notification manager", ex);
}
return null;
public String[] getOrderedNotificationKeys() {
return mNotificationKeys;
}
@Override
@@ -261,28 +269,60 @@ public abstract class NotificationListenerService extends Service {
private class INotificationListenerWrapper extends INotificationListener.Stub {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
public void onNotificationPosted(StatusBarNotification sbn,
NotificationOrderUpdate update) {
try {
NotificationListenerService.this.onNotificationPosted(sbn);
// protect subclass from concurrent modifications of (@link mNotificationKeys}.
synchronized (mWrapper) {
updateNotificationKeys(update);
NotificationListenerService.this.onNotificationPosted(sbn);
}
} catch (Throwable t) {
Log.w(TAG, "Error running onNotificationPosted", t);
Log.w(TAG, "Error running onOrderedNotificationPosted", t);
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
public void onNotificationRemoved(StatusBarNotification sbn,
NotificationOrderUpdate update) {
try {
NotificationListenerService.this.onNotificationRemoved(sbn);
// protect subclass from concurrent modifications of (@link mNotificationKeys}.
synchronized (mWrapper) {
updateNotificationKeys(update);
NotificationListenerService.this.onNotificationRemoved(sbn);
}
} catch (Throwable t) {
Log.w(TAG, "Error running onNotificationRemoved", t);
}
}
@Override
public void onListenerConnected(String[] notificationKeys) {
public void onListenerConnected(NotificationOrderUpdate update) {
try {
NotificationListenerService.this.onListenerConnected(notificationKeys);
// protect subclass from concurrent modifications of (@link mNotificationKeys}.
synchronized (mWrapper) {
updateNotificationKeys(update);
NotificationListenerService.this.onListenerConnected(mNotificationKeys);
}
} catch (Throwable t) {
Log.w(TAG, "Error running onListenerConnected", t);
}
}
@Override
public void onNotificationOrderUpdate(NotificationOrderUpdate update)
throws RemoteException {
try {
// protect subclass from concurrent modifications of (@link mNotificationKeys}.
synchronized (mWrapper) {
updateNotificationKeys(update);
NotificationListenerService.this.onNotificationOrderUpdate();
}
} catch (Throwable t) {
Log.w(TAG, "Error running onNotificationOrderUpdate", t);
}
}
}
private void updateNotificationKeys(NotificationOrderUpdate update) {
// TODO: avoid garbage by comparing the lists
mNotificationKeys = update.getOrderedKeys();
}
}

View File

@@ -0,0 +1,19 @@
/**
* Copyright (c) 2014, 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.service.notification;
parcelable NotificationOrderUpdate;

View File

@@ -0,0 +1,62 @@
/*
* Copyright (C) 2014 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.service.notification;
import android.os.Parcel;
import android.os.Parcelable;
public class NotificationOrderUpdate implements Parcelable {
// TODO replace this with an update instead of the whole array
private final String[] mKeys;
/** @hide */
public NotificationOrderUpdate(String[] keys) {
this.mKeys = keys;
}
public NotificationOrderUpdate(Parcel in) {
this.mKeys = in.readStringArray();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeStringArray(this.mKeys);
}
public static final Parcelable.Creator<NotificationOrderUpdate> CREATOR
= new Parcelable.Creator<NotificationOrderUpdate>() {
public NotificationOrderUpdate createFromParcel(Parcel parcel) {
return new NotificationOrderUpdate(parcel);
}
public NotificationOrderUpdate[] newArray(int size) {
return new NotificationOrderUpdate[size];
}
};
/**
* @hide
* @return ordered list of keys
*/
String[] getOrderedKeys() {
return mKeys;
}
}