diff options
5 files changed, 66 insertions, 37 deletions
diff --git a/services/core/Android.bp b/services/core/Android.bp index 7a26b21d6ac7..1bcc19845dfb 100644 --- a/services/core/Android.bp +++ b/services/core/Android.bp @@ -154,6 +154,10 @@ java_library { static_libs: ["services.core.priorityboosted"], } +java_library_host { + name: "core_cts_test_resources", + srcs: ["java/com/android/server/notification/SmallHash.java"] +} prebuilt_etc { name: "gps_debug.conf", diff --git a/services/core/java/com/android/server/notification/NotificationChannelLogger.java b/services/core/java/com/android/server/notification/NotificationChannelLogger.java index 83f4ebb41827..a7b18778f868 100644 --- a/services/core/java/com/android/server/notification/NotificationChannelLogger.java +++ b/services/core/java/com/android/server/notification/NotificationChannelLogger.java @@ -184,14 +184,14 @@ public interface NotificationChannelLogger { * @return Small hash of the channel ID, if present, or 0 otherwise. */ static int getIdHash(@NonNull NotificationChannel channel) { - return NotificationRecordLogger.smallHash(channel.getId()); + return SmallHash.hash(channel.getId()); } /** * @return Small hash of the channel ID, if present, or 0 otherwise. */ static int getIdHash(@NonNull NotificationChannelGroup group) { - return NotificationRecordLogger.smallHash(group.getId()); + return SmallHash.hash(group.getId()); } /** diff --git a/services/core/java/com/android/server/notification/NotificationRecordLogger.java b/services/core/java/com/android/server/notification/NotificationRecordLogger.java index 6c833f966d36..eba57304124a 100644 --- a/services/core/java/com/android/server/notification/NotificationRecordLogger.java +++ b/services/core/java/com/android/server/notification/NotificationRecordLogger.java @@ -359,43 +359,24 @@ public interface NotificationRecordLogger { * @return Small hash of the notification ID, and tag (if present). */ int getNotificationIdHash() { - return smallHash(Objects.hashCode(r.getSbn().getTag()) ^ r.getSbn().getId()); + return SmallHash.hash(Objects.hashCode(r.getSbn().getTag()) ^ r.getSbn().getId()); } /** * @return Small hash of the channel ID, if present, or 0 otherwise. */ int getChannelIdHash() { - return smallHash(r.getSbn().getNotification().getChannelId()); + return SmallHash.hash(r.getSbn().getNotification().getChannelId()); } /** * @return Small hash of the group ID, respecting group override if present. 0 otherwise. */ int getGroupIdHash() { - return smallHash(r.getSbn().getGroup()); + return SmallHash.hash(r.getSbn().getGroup()); } } - // "Small" hashes will be in the range [0, MAX_HASH). - int MAX_HASH = (1 << 13); - - /** - * Maps in to the range [0, MAX_HASH), keeping similar values distinct. - * @param in An arbitrary integer. - * @return in mod MAX_HASH, signs chosen to stay in the range [0, MAX_HASH). - */ - static int smallHash(int in) { - return Math.floorMod(in, MAX_HASH); - } - - /** - * @return Small hash of the string, if non-null, or 0 otherwise. - */ - static int smallHash(@Nullable String in) { - return smallHash(Objects.hashCode(in)); - } - } diff --git a/services/core/java/com/android/server/notification/SmallHash.java b/services/core/java/com/android/server/notification/SmallHash.java new file mode 100644 index 000000000000..4483a5ba8b76 --- /dev/null +++ b/services/core/java/com/android/server/notification/SmallHash.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2020 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 com.android.server.notification; + +import java.util.Objects; + +/** + * A simple hash function for use in privacy-sensitive logging. Few bits = lots of collisions. + * See {@link NotificationRecordLogger}. + */ +public class SmallHash { + // Hashes will be in the range [0, MAX_HASH). + public static final int MAX_HASH = (1 << 13); + + /** + * @return Small hash of the string, if non-null, or 0 otherwise. + */ + public static int hash(String in) { + return hash(Objects.hashCode(in)); + } + + /** + * Maps in to the range [0, MAX_HASH), keeping similar values distinct. + * @param in An arbitrary integer. + * @return in mod MAX_HASH, signs chosen to stay in the range [0, MAX_HASH). + */ + public static int hash(int in) { + return Math.floorMod(in, MAX_HASH); + } +} diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationRecordLoggerTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationRecordLoggerTest.java index 1e6270d78275..fd6804641d2c 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationRecordLoggerTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationRecordLoggerTest.java @@ -60,16 +60,16 @@ public class NotificationRecordLoggerTest extends UiServiceTestCase { @Test public void testSmallHash() { - assertEquals(0, NotificationRecordLogger.smallHash(0)); - final int maxHash = NotificationRecordLogger.MAX_HASH; + assertEquals(0, SmallHash.hash(0)); + final int maxHash = SmallHash.MAX_HASH; assertEquals(0, - NotificationRecordLogger.smallHash(maxHash)); + SmallHash.hash(maxHash)); assertEquals(0, - NotificationRecordLogger.smallHash(17 * maxHash)); + SmallHash.hash(17 * maxHash)); assertEquals(maxHash - 1, - NotificationRecordLogger.smallHash(maxHash - 1)); + SmallHash.hash(maxHash - 1)); assertEquals(maxHash - 1, - NotificationRecordLogger.smallHash(-1)); + SmallHash.hash(-1)); } @Test @@ -78,10 +78,10 @@ public class NotificationRecordLoggerTest extends UiServiceTestCase { getNotificationRecordPair(0, null).getNotificationIdHash()); assertEquals(1, getNotificationRecordPair(1, null).getNotificationIdHash()); - assertEquals(NotificationRecordLogger.MAX_HASH - 1, + assertEquals(SmallHash.MAX_HASH - 1, getNotificationRecordPair(-1, null).getNotificationIdHash()); final String tag = "someTag"; - final int hash = NotificationRecordLogger.smallHash(tag.hashCode()); + final int hash = SmallHash.hash(tag.hashCode()); assertEquals(hash, getNotificationRecordPair(0, tag).getNotificationIdHash()); // We xor the tag and hashcode together before compressing the range. The order of // operations doesn't matter if id is small. @@ -89,19 +89,19 @@ public class NotificationRecordLoggerTest extends UiServiceTestCase { getNotificationRecordPair(1, tag).getNotificationIdHash()); // But it does matter for an id with more 1 bits than fit in the small hash. assertEquals( - NotificationRecordLogger.smallHash(-1 ^ tag.hashCode()), + SmallHash.hash(-1 ^ tag.hashCode()), getNotificationRecordPair(-1, tag).getNotificationIdHash()); assertNotEquals(-1 ^ hash, - NotificationRecordLogger.smallHash(-1 ^ tag.hashCode())); + SmallHash.hash(-1 ^ tag.hashCode())); } @Test public void testGetChannelIdHash() { assertEquals( - NotificationRecordLogger.smallHash(CHANNEL_ID.hashCode()), + SmallHash.hash(CHANNEL_ID.hashCode()), getNotificationRecordPair(0, null).getChannelIdHash()); assertNotEquals( - NotificationRecordLogger.smallHash(CHANNEL_ID.hashCode()), + SmallHash.hash(CHANNEL_ID.hashCode()), CHANNEL_ID.hashCode()); } @@ -113,7 +113,7 @@ public class NotificationRecordLoggerTest extends UiServiceTestCase { final String group = "someGroup"; p.r.setOverrideGroupKey(group); assertEquals( - NotificationRecordLogger.smallHash(group.hashCode()), + SmallHash.hash(group.hashCode()), p.getGroupIdHash()); } } |