Loading core/java/com/android/internal/logging/InstanceIdSequence.java +5 −5 Original line number Original line Diff line number Diff line Loading @@ -25,7 +25,7 @@ import java.security.SecureRandom; import java.util.Random; import java.util.Random; /** /** * Generates random InstanceIds in range [0, instanceIdMax) for passing to * Generates random InstanceIds in range [1, instanceIdMax] for passing to * UiEventLogger.logWithInstanceId(). Holds a SecureRandom, which self-seeds on * UiEventLogger.logWithInstanceId(). Holds a SecureRandom, which self-seeds on * first use; try to give it a long lifetime. Safe for concurrent use. * first use; try to give it a long lifetime. Safe for concurrent use. */ */ Loading @@ -34,12 +34,12 @@ public class InstanceIdSequence { private final Random mRandom = new SecureRandom(); private final Random mRandom = new SecureRandom(); /** /** * Constructs a sequence with identifiers [0, instanceIdMax). Capped at INSTANCE_ID_MAX. * Constructs a sequence with identifiers [1, instanceIdMax]. Capped at INSTANCE_ID_MAX. * @param instanceIdMax Limiting value of identifiers. Normally positive: otherwise you get * @param instanceIdMax Limiting value of identifiers. Normally positive: otherwise you get * an all-zero sequence. * an all-1 sequence. */ */ public InstanceIdSequence(int instanceIdMax) { public InstanceIdSequence(int instanceIdMax) { mInstanceIdMax = min(max(0, instanceIdMax), InstanceId.INSTANCE_ID_MAX); mInstanceIdMax = min(max(1, instanceIdMax), InstanceId.INSTANCE_ID_MAX); } } /** /** Loading @@ -47,7 +47,7 @@ public class InstanceIdSequence { * @return new InstanceId * @return new InstanceId */ */ public InstanceId newInstanceId() { public InstanceId newInstanceId() { return newInstanceIdInternal(mRandom.nextInt(mInstanceIdMax)); return newInstanceIdInternal(1 + mRandom.nextInt(mInstanceIdMax)); } } /** /** Loading services/tests/uiservicestests/src/com/android/internal/logging/InstanceIdSequenceFake.java +3 −3 Original line number Original line Diff line number Diff line Loading @@ -17,7 +17,7 @@ package com.android.internal.logging; package com.android.internal.logging; /** /** * A fake implementation of InstanceIdSequence that returns 0, 1, 2, ... * A fake implementation of InstanceIdSequence that returns 1, 2, ... */ */ public class InstanceIdSequenceFake extends InstanceIdSequence { public class InstanceIdSequenceFake extends InstanceIdSequence { Loading @@ -25,13 +25,13 @@ public class InstanceIdSequenceFake extends InstanceIdSequence { super(instanceIdMax); super(instanceIdMax); } } private int mNextId = 0; private int mNextId = 1; @Override @Override public InstanceId newInstanceId() { public InstanceId newInstanceId() { synchronized (this) { synchronized (this) { if (mNextId >= mInstanceIdMax) { if (mNextId >= mInstanceIdMax) { mNextId = 0; mNextId = 1; } } return newInstanceIdInternal(mNextId++); return newInstanceIdInternal(mNextId++); } } Loading services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +11 −9 Original line number Original line Diff line number Diff line Loading @@ -1163,7 +1163,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals(PKG, call.r.getSbn().getPackageName()); assertEquals(PKG, call.r.getSbn().getPackageName()); assertEquals(0, call.r.getSbn().getId()); assertEquals(0, call.r.getSbn().getId()); assertEquals(tag, call.r.getSbn().getTag()); assertEquals(tag, call.r.getSbn().getTag()); assertEquals(0, call.getInstanceId()); // Fake instance IDs are assigned in order assertEquals(1, call.getInstanceId()); // Fake instance IDs are assigned in order } } @Test @Test Loading @@ -1185,14 +1185,14 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals( assertEquals( NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, mNotificationRecordLogger.get(0).event); mNotificationRecordLogger.get(0).event); assertEquals(0, mNotificationRecordLogger.get(0).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); assertTrue(mNotificationRecordLogger.get(1).shouldLogReported); assertTrue(mNotificationRecordLogger.get(1).shouldLogReported); assertEquals( assertEquals( NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_UPDATED, NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_UPDATED, mNotificationRecordLogger.get(1).event); mNotificationRecordLogger.get(1).event); // Instance ID doesn't change on update of an active notification // Instance ID doesn't change on update of an active notification assertEquals(0, mNotificationRecordLogger.get(1).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(1).getInstanceId()); } } @Test @Test Loading Loading @@ -1247,19 +1247,19 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, mNotificationRecordLogger.get(0).event); mNotificationRecordLogger.get(0).event); assertTrue(mNotificationRecordLogger.get(0).shouldLogReported); assertTrue(mNotificationRecordLogger.get(0).shouldLogReported); assertEquals(0, mNotificationRecordLogger.get(0).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); assertEquals( assertEquals( NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_APP_CANCEL, NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_APP_CANCEL, mNotificationRecordLogger.get(1).event); mNotificationRecordLogger.get(1).event); assertEquals(0, mNotificationRecordLogger.get(1).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(1).getInstanceId()); assertEquals( assertEquals( NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, mNotificationRecordLogger.get(2).event); mNotificationRecordLogger.get(2).event); assertTrue(mNotificationRecordLogger.get(2).shouldLogReported); assertTrue(mNotificationRecordLogger.get(2).shouldLogReported); // New instance ID because notification was canceled before re-post // New instance ID because notification was canceled before re-post assertEquals(1, mNotificationRecordLogger.get(2).getInstanceId()); assertEquals(2, mNotificationRecordLogger.get(2).getInstanceId()); } } @Test @Test Loading Loading @@ -3452,6 +3452,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test @Test public void testStats_dismissalSurface() throws Exception { public void testStats_dismissalSurface() throws Exception { final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); r.getSbn().setInstanceId(mNotificationInstanceIdSequence.newInstanceId()); mService.addNotification(r); mService.addNotification(r); final NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 0, 1, true); final NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 0, 1, true); Loading @@ -3469,7 +3470,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals( assertEquals( NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_USER_AOD, NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_USER_AOD, mNotificationRecordLogger.get(0).event); mNotificationRecordLogger.get(0).event); assertEquals(0, mNotificationRecordLogger.get(0).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); } } @Test @Test Loading Loading @@ -4343,6 +4344,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { final NotificationRecord r = generateNotificationRecord( final NotificationRecord r = generateNotificationRecord( mTestNotificationChannel, 1, null, true); mTestNotificationChannel, 1, null, true); r.setTextChanged(true); r.setTextChanged(true); r.getSbn().setInstanceId(mNotificationInstanceIdSequence.newInstanceId()); mService.addNotification(r); mService.addNotification(r); mService.mNotificationDelegate.onNotificationVisibilityChanged(new NotificationVisibility[] mService.mNotificationDelegate.onNotificationVisibilityChanged(new NotificationVisibility[] Loading @@ -4352,7 +4354,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals(1, mNotificationRecordLogger.getCalls().size()); assertEquals(1, mNotificationRecordLogger.getCalls().size()); assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_OPEN, assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_OPEN, mNotificationRecordLogger.get(0).event); mNotificationRecordLogger.get(0).event); assertEquals(0, mNotificationRecordLogger.get(0).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); mService.mNotificationDelegate.onNotificationVisibilityChanged( mService.mNotificationDelegate.onNotificationVisibilityChanged( new NotificationVisibility[]{}, new NotificationVisibility[]{}, Loading @@ -4363,7 +4365,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals(2, mNotificationRecordLogger.getCalls().size()); assertEquals(2, mNotificationRecordLogger.getCalls().size()); assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_CLOSE, assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_CLOSE, mNotificationRecordLogger.get(1).event); mNotificationRecordLogger.get(1).event); assertEquals(0, mNotificationRecordLogger.get(1).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(1).getInstanceId()); } } @Test @Test Loading Loading
core/java/com/android/internal/logging/InstanceIdSequence.java +5 −5 Original line number Original line Diff line number Diff line Loading @@ -25,7 +25,7 @@ import java.security.SecureRandom; import java.util.Random; import java.util.Random; /** /** * Generates random InstanceIds in range [0, instanceIdMax) for passing to * Generates random InstanceIds in range [1, instanceIdMax] for passing to * UiEventLogger.logWithInstanceId(). Holds a SecureRandom, which self-seeds on * UiEventLogger.logWithInstanceId(). Holds a SecureRandom, which self-seeds on * first use; try to give it a long lifetime. Safe for concurrent use. * first use; try to give it a long lifetime. Safe for concurrent use. */ */ Loading @@ -34,12 +34,12 @@ public class InstanceIdSequence { private final Random mRandom = new SecureRandom(); private final Random mRandom = new SecureRandom(); /** /** * Constructs a sequence with identifiers [0, instanceIdMax). Capped at INSTANCE_ID_MAX. * Constructs a sequence with identifiers [1, instanceIdMax]. Capped at INSTANCE_ID_MAX. * @param instanceIdMax Limiting value of identifiers. Normally positive: otherwise you get * @param instanceIdMax Limiting value of identifiers. Normally positive: otherwise you get * an all-zero sequence. * an all-1 sequence. */ */ public InstanceIdSequence(int instanceIdMax) { public InstanceIdSequence(int instanceIdMax) { mInstanceIdMax = min(max(0, instanceIdMax), InstanceId.INSTANCE_ID_MAX); mInstanceIdMax = min(max(1, instanceIdMax), InstanceId.INSTANCE_ID_MAX); } } /** /** Loading @@ -47,7 +47,7 @@ public class InstanceIdSequence { * @return new InstanceId * @return new InstanceId */ */ public InstanceId newInstanceId() { public InstanceId newInstanceId() { return newInstanceIdInternal(mRandom.nextInt(mInstanceIdMax)); return newInstanceIdInternal(1 + mRandom.nextInt(mInstanceIdMax)); } } /** /** Loading
services/tests/uiservicestests/src/com/android/internal/logging/InstanceIdSequenceFake.java +3 −3 Original line number Original line Diff line number Diff line Loading @@ -17,7 +17,7 @@ package com.android.internal.logging; package com.android.internal.logging; /** /** * A fake implementation of InstanceIdSequence that returns 0, 1, 2, ... * A fake implementation of InstanceIdSequence that returns 1, 2, ... */ */ public class InstanceIdSequenceFake extends InstanceIdSequence { public class InstanceIdSequenceFake extends InstanceIdSequence { Loading @@ -25,13 +25,13 @@ public class InstanceIdSequenceFake extends InstanceIdSequence { super(instanceIdMax); super(instanceIdMax); } } private int mNextId = 0; private int mNextId = 1; @Override @Override public InstanceId newInstanceId() { public InstanceId newInstanceId() { synchronized (this) { synchronized (this) { if (mNextId >= mInstanceIdMax) { if (mNextId >= mInstanceIdMax) { mNextId = 0; mNextId = 1; } } return newInstanceIdInternal(mNextId++); return newInstanceIdInternal(mNextId++); } } Loading
services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +11 −9 Original line number Original line Diff line number Diff line Loading @@ -1163,7 +1163,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals(PKG, call.r.getSbn().getPackageName()); assertEquals(PKG, call.r.getSbn().getPackageName()); assertEquals(0, call.r.getSbn().getId()); assertEquals(0, call.r.getSbn().getId()); assertEquals(tag, call.r.getSbn().getTag()); assertEquals(tag, call.r.getSbn().getTag()); assertEquals(0, call.getInstanceId()); // Fake instance IDs are assigned in order assertEquals(1, call.getInstanceId()); // Fake instance IDs are assigned in order } } @Test @Test Loading @@ -1185,14 +1185,14 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals( assertEquals( NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, mNotificationRecordLogger.get(0).event); mNotificationRecordLogger.get(0).event); assertEquals(0, mNotificationRecordLogger.get(0).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); assertTrue(mNotificationRecordLogger.get(1).shouldLogReported); assertTrue(mNotificationRecordLogger.get(1).shouldLogReported); assertEquals( assertEquals( NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_UPDATED, NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_UPDATED, mNotificationRecordLogger.get(1).event); mNotificationRecordLogger.get(1).event); // Instance ID doesn't change on update of an active notification // Instance ID doesn't change on update of an active notification assertEquals(0, mNotificationRecordLogger.get(1).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(1).getInstanceId()); } } @Test @Test Loading Loading @@ -1247,19 +1247,19 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, mNotificationRecordLogger.get(0).event); mNotificationRecordLogger.get(0).event); assertTrue(mNotificationRecordLogger.get(0).shouldLogReported); assertTrue(mNotificationRecordLogger.get(0).shouldLogReported); assertEquals(0, mNotificationRecordLogger.get(0).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); assertEquals( assertEquals( NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_APP_CANCEL, NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_APP_CANCEL, mNotificationRecordLogger.get(1).event); mNotificationRecordLogger.get(1).event); assertEquals(0, mNotificationRecordLogger.get(1).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(1).getInstanceId()); assertEquals( assertEquals( NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, mNotificationRecordLogger.get(2).event); mNotificationRecordLogger.get(2).event); assertTrue(mNotificationRecordLogger.get(2).shouldLogReported); assertTrue(mNotificationRecordLogger.get(2).shouldLogReported); // New instance ID because notification was canceled before re-post // New instance ID because notification was canceled before re-post assertEquals(1, mNotificationRecordLogger.get(2).getInstanceId()); assertEquals(2, mNotificationRecordLogger.get(2).getInstanceId()); } } @Test @Test Loading Loading @@ -3452,6 +3452,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test @Test public void testStats_dismissalSurface() throws Exception { public void testStats_dismissalSurface() throws Exception { final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); r.getSbn().setInstanceId(mNotificationInstanceIdSequence.newInstanceId()); mService.addNotification(r); mService.addNotification(r); final NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 0, 1, true); final NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 0, 1, true); Loading @@ -3469,7 +3470,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals( assertEquals( NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_USER_AOD, NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_USER_AOD, mNotificationRecordLogger.get(0).event); mNotificationRecordLogger.get(0).event); assertEquals(0, mNotificationRecordLogger.get(0).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); } } @Test @Test Loading Loading @@ -4343,6 +4344,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { final NotificationRecord r = generateNotificationRecord( final NotificationRecord r = generateNotificationRecord( mTestNotificationChannel, 1, null, true); mTestNotificationChannel, 1, null, true); r.setTextChanged(true); r.setTextChanged(true); r.getSbn().setInstanceId(mNotificationInstanceIdSequence.newInstanceId()); mService.addNotification(r); mService.addNotification(r); mService.mNotificationDelegate.onNotificationVisibilityChanged(new NotificationVisibility[] mService.mNotificationDelegate.onNotificationVisibilityChanged(new NotificationVisibility[] Loading @@ -4352,7 +4354,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals(1, mNotificationRecordLogger.getCalls().size()); assertEquals(1, mNotificationRecordLogger.getCalls().size()); assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_OPEN, assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_OPEN, mNotificationRecordLogger.get(0).event); mNotificationRecordLogger.get(0).event); assertEquals(0, mNotificationRecordLogger.get(0).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); mService.mNotificationDelegate.onNotificationVisibilityChanged( mService.mNotificationDelegate.onNotificationVisibilityChanged( new NotificationVisibility[]{}, new NotificationVisibility[]{}, Loading @@ -4363,7 +4365,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertEquals(2, mNotificationRecordLogger.getCalls().size()); assertEquals(2, mNotificationRecordLogger.getCalls().size()); assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_CLOSE, assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_CLOSE, mNotificationRecordLogger.get(1).event); mNotificationRecordLogger.get(1).event); assertEquals(0, mNotificationRecordLogger.get(1).getInstanceId()); assertEquals(1, mNotificationRecordLogger.get(1).getInstanceId()); } } @Test @Test Loading