Merge "Pull out library needed for Notification CTS test." into rvc-dev am: dce5a57376 am: 1471f01988
Change-Id: I5e2449b1b887379176e47de0a7b22ec25b686a24
This commit is contained in:
@@ -150,6 +150,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",
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user