Update People Space widget on channel modification

Test: Buld and run, and unit test.
Change-Id: I209c3a7ac12fa48f744d8f784c598ad359bf2de6
This commit is contained in:
Flavio Fiszman
2020-11-10 17:24:55 +00:00
parent 5e687f4544
commit 04ffb5128b
7 changed files with 147 additions and 4 deletions

View File

@@ -14,6 +14,8 @@
package com.android.systemui.plugins;
import android.app.NotificationChannel;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService.RankingMap;
import android.service.notification.StatusBarNotification;
@@ -30,13 +32,32 @@ public interface NotificationListenerController extends Plugin {
void onListenerConnected(NotificationProvider provider);
/**
* @return whether plugin wants to skip the default callbacks.
*/
default boolean onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
return false;
}
/**
* @return whether plugin wants to skip the default callbacks.
*/
default boolean onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
return false;
}
/**
* Called when a notification channel is modified.
* @param modificationType One of {@link #NOTIFICATION_CHANNEL_OR_GROUP_ADDED},
* {@link #NOTIFICATION_CHANNEL_OR_GROUP_UPDATED},
* {@link #NOTIFICATION_CHANNEL_OR_GROUP_DELETED}.
* @return whether a plugin wants to skip the default callbacks.
*/
default boolean onNotificationChannelModified(
String pkgName, UserHandle user, NotificationChannel channel, int modificationType) {
return false;
}
default StatusBarNotification[] getActiveNotifications(
StatusBarNotification[] activeNotifications) {
return activeNotifications;

View File

@@ -16,9 +16,11 @@
package com.android.systemui.people.widget;
import android.app.NotificationChannel;
import android.content.ComponentName;
import android.content.Context;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
@@ -130,6 +132,18 @@ public class PeopleSpaceWidgetManager {
if (DEBUG) Log.d(TAG, "onNotificationsInitialized");
updateWidgets();
}
@Override
public void onNotificationChannelModified(
String pkgName,
UserHandle user,
NotificationChannel channel,
int modificationType) {
if (DEBUG) Log.d(TAG, "onNotificationChannelModified");
if (channel.isConversation()) {
updateWidgets();
}
}
};
}

View File

@@ -22,6 +22,7 @@ import static com.android.systemui.statusbar.phone.StatusBar.DEBUG;
import android.annotation.NonNull;
import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
@@ -158,6 +159,19 @@ public class NotificationListener extends NotificationListenerWithPlugins {
}
}
@Override
public void onNotificationChannelModified(
String pkgName, UserHandle user, NotificationChannel channel, int modificationType) {
if (DEBUG) Log.d(TAG, "onNotificationChannelModified");
if (!onPluginNotificationChannelModified(pkgName, user, channel, modificationType)) {
mMainHandler.post(() -> {
for (NotificationHandler handler : mNotificationHandlers) {
handler.onNotificationChannelModified(pkgName, user, channel, modificationType);
}
});
}
}
@Override
public void onSilentStatusBarIconsVisibilityChanged(boolean hideSilentStatusIcons) {
for (NotificationSettingsListener listener : mSettingsListeners) {
@@ -229,6 +243,14 @@ public class NotificationListener extends NotificationListenerWithPlugins {
void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap, int reason);
void onNotificationRankingUpdate(RankingMap rankingMap);
/** Called after a notification channel is modified. */
default void onNotificationChannelModified(
String pkgName,
UserHandle user,
NotificationChannel channel,
int modificationType) {
}
/**
* Called after the listener has connected to NoMan and posted any current notifications.
*/

View File

@@ -19,6 +19,8 @@ package com.android.systemui.statusbar.notification.collection.coalescer;
import static java.util.Objects.requireNonNull;
import android.annotation.MainThread;
import android.app.NotificationChannel;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService.Ranking;
import android.service.notification.NotificationListenerService.RankingMap;
import android.service.notification.StatusBarNotification;
@@ -158,6 +160,15 @@ public class GroupCoalescer implements Dumpable {
public void onNotificationsInitialized() {
mHandler.onNotificationsInitialized();
}
@Override
public void onNotificationChannelModified(
String pkgName,
UserHandle user,
NotificationChannel channel,
int modificationType) {
mHandler.onNotificationChannelModified(pkgName, user, channel, modificationType);
}
};
private void maybeEmitBatch(StatusBarNotification sbn) {

View File

@@ -14,9 +14,11 @@
package com.android.systemui.statusbar.phone;
import android.app.NotificationChannel;
import android.content.ComponentName;
import android.content.Context;
import android.os.RemoteException;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
@@ -78,7 +80,7 @@ public class NotificationListenerWithPlugins extends NotificationListenerService
/**
* Called when listener receives a onNotificationPosted.
* Returns true to indicate this callback should be skipped.
* Returns true if there's a plugin determining to skip the default callbacks.
*/
public boolean onPluginNotificationPosted(StatusBarNotification sbn,
final RankingMap rankingMap) {
@@ -92,7 +94,7 @@ public class NotificationListenerWithPlugins extends NotificationListenerService
/**
* Called when listener receives a onNotificationRemoved.
* Returns true to indicate this callback should be skipped.
* Returns true if there's a plugin determining to skip the default callbacks.
*/
public boolean onPluginNotificationRemoved(StatusBarNotification sbn,
final RankingMap rankingMap) {
@@ -104,6 +106,20 @@ public class NotificationListenerWithPlugins extends NotificationListenerService
return false;
}
/**
* Called when listener receives a onNotificationChannelModified.
* Returns true if there's a plugin determining to skip the default callbacks.
*/
public boolean onPluginNotificationChannelModified(
String pkgName, UserHandle user, NotificationChannel channel, int modificationType) {
for (NotificationListenerController plugin : mPlugins) {
if (plugin.onNotificationChannelModified(pkgName, user, channel, modificationType)) {
return true;
}
}
return false;
}
public RankingMap onPluginRankingUpdate(RankingMap rankingMap) {
return getCurrentRanking();
}

View File

@@ -16,6 +16,8 @@
package com.android.systemui.people.widget;
import static android.app.NotificationManager.IMPORTANCE_HIGH;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
@@ -26,8 +28,10 @@ import static org.mockito.Mockito.when;
import static java.util.Objects.requireNonNull;
import android.app.NotificationChannel;
import android.content.Context;
import android.os.RemoteException;
import android.os.UserHandle;
import android.testing.AndroidTestingRunner;
import androidx.test.filters.SmallTest;
@@ -56,6 +60,10 @@ public class PeopleSpaceWidgetManagerTest extends SysuiTestCase {
private static final String TEST_PACKAGE_A = "com.test.package_a";
private static final String TEST_PACKAGE_B = "com.test.package_b";
private static final String TEST_CHANNEL_ID = "channel_id";
private static final String TEST_CHANNEL_NAME = "channel_name";
private static final String TEST_PARENT_CHANNEL_ID = "parent_channel_id";
private static final String TEST_CONVERSATION_ID = "conversation_id";
private PeopleSpaceWidgetManager mManager;
@@ -100,7 +108,7 @@ public class PeopleSpaceWidgetManagerTest extends SysuiTestCase {
}
@Test
public void testNotifyAppWidgetIfWidgets() throws RemoteException {
public void testNotifyAppWidgetIfNotificationPosted() throws RemoteException {
int[] widgetIdsArray = {1};
when(mIAppWidgetService.getAppWidgetIds(any())).thenReturn(widgetIdsArray);
@@ -117,7 +125,7 @@ public class PeopleSpaceWidgetManagerTest extends SysuiTestCase {
}
@Test
public void testNotifyAppWidgetTwiceIfTwoNotifications() throws RemoteException {
public void testNotifyAppWidgetTwiceIfTwoNotificationsPosted() throws RemoteException {
int[] widgetIdsArray = {1, 2};
when(mIAppWidgetService.getAppWidgetIds(any())).thenReturn(widgetIdsArray);
@@ -149,4 +157,40 @@ public class PeopleSpaceWidgetManagerTest extends SysuiTestCase {
verify(mIAppWidgetService, times(2))
.notifyAppWidgetViewDataChanged(any(), eq(widgetIdsArray), anyInt());
}
@Test
public void testDoNotNotifyAppWidgetIfNonConversationChannelModified() throws RemoteException {
int[] widgetIdsArray = {1};
when(mIAppWidgetService.getAppWidgetIds(any())).thenReturn(widgetIdsArray);
NotificationChannel channel =
mNoMan.createNotificationChannel(TEST_CHANNEL_ID, TEST_CHANNEL_NAME);
mNoMan.issueChannelModification(TEST_PACKAGE_A,
UserHandle.getUserHandleForUid(0), channel, IMPORTANCE_HIGH);
mClock.advanceTime(MIN_LINGER_DURATION);
verify(mIAppWidgetService, never()).getAppWidgetIds(any());
verify(mIAppWidgetService, never()).notifyAppWidgetViewDataChanged(any(), any(), anyInt());
}
@Test
public void testNotifyAppWidgetIfConversationChannelModified() throws RemoteException {
int[] widgetIdsArray = {1};
when(mIAppWidgetService.getAppWidgetIds(any())).thenReturn(widgetIdsArray);
NotificationChannel channel =
mNoMan.createNotificationChannel(TEST_CHANNEL_ID, TEST_CHANNEL_NAME);
channel.setConversationId(TEST_PARENT_CHANNEL_ID, TEST_CONVERSATION_ID);
mNoMan.issueChannelModification(TEST_PACKAGE_A,
UserHandle.getUserHandleForUid(0), channel, IMPORTANCE_HIGH);
mClock.advanceTime(MIN_LINGER_DURATION);
verify(mIAppWidgetService, times(1)).getAppWidgetIds(any());
verify(mIAppWidgetService, times(1))
.notifyAppWidgetViewDataChanged(any(), eq(widgetIdsArray), anyInt());
}
}

View File

@@ -16,8 +16,12 @@
package com.android.systemui.statusbar.notification.collection;
import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static org.junit.Assert.assertNotNull;
import android.app.NotificationChannel;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService.Ranking;
import android.service.notification.NotificationListenerService.RankingMap;
import android.service.notification.StatusBarNotification;
@@ -72,6 +76,17 @@ public class NoManSimulator {
}
}
public NotificationChannel createNotificationChannel(String id, String name) {
return new NotificationChannel(id, name, IMPORTANCE_DEFAULT);
}
public void issueChannelModification(
String pkg, UserHandle user, NotificationChannel channel, int modificationType) {
for (NotificationHandler listener : mListeners) {
listener.onNotificationChannelModified(pkg, user, channel, modificationType);
}
}
public void setRanking(String key, Ranking ranking) {
mRankings.put(key, ranking);
}