Truncate ShortcutInfo Id

Creating Conversation with a ShortcutId longer than 65_535 (max unsigned short), we did not save the conversation settings into the notification_policy.xml due to a restriction in FastDataOutput.
This put us to a state where the user changing the importance or turning off the notifications for the given conversation had no effect on notification behavior.

Fixes: 273729476
Test: atest ShortcutManagerTest2
Test: Create a test app which creates a Conversation with a long shortcutId. Go to the Conversation Settings and turn off Notifications. Post a new Notification to this Conversation and see if it is displayed.
Change-Id: I2617de6f9e8a7dbfd8fbeff589a7d592f00d87c5
Merged-In: I2617de6f9e8a7dbfd8fbeff589a7d592f00d87c5
This commit is contained in:
András Kurucz
2023-04-21 09:45:07 +00:00
parent cd993dd7e0
commit 6a278c3998
2 changed files with 27 additions and 3 deletions

View File

@@ -261,6 +261,12 @@ public final class ShortcutInfo implements Parcelable {
*/ */
public static final int DISABLED_REASON_OTHER_RESTORE_ISSUE = 103; public static final int DISABLED_REASON_OTHER_RESTORE_ISSUE = 103;
/**
* The maximum length of Shortcut ID. IDs will be truncated at this limit.
* @hide
*/
public static final int MAX_ID_LENGTH = 1000;
/** @hide */ /** @hide */
@IntDef(prefix = { "DISABLED_REASON_" }, value = { @IntDef(prefix = { "DISABLED_REASON_" }, value = {
DISABLED_REASON_NOT_DISABLED, DISABLED_REASON_NOT_DISABLED,
@@ -436,8 +442,7 @@ public final class ShortcutInfo implements Parcelable {
private ShortcutInfo(Builder b) { private ShortcutInfo(Builder b) {
mUserId = b.mContext.getUserId(); mUserId = b.mContext.getUserId();
mId = getSafeId(Preconditions.checkStringNotEmpty(b.mId, "Shortcut ID must be provided"));
mId = Preconditions.checkStringNotEmpty(b.mId, "Shortcut ID must be provided");
// Note we can't do other null checks here because SM.updateShortcuts() takes partial // Note we can't do other null checks here because SM.updateShortcuts() takes partial
// information. // information.
@@ -539,6 +544,14 @@ public final class ShortcutInfo implements Parcelable {
return ret; return ret;
} }
@NonNull
private static String getSafeId(@NonNull String id) {
if (id.length() > MAX_ID_LENGTH) {
return id.substring(0, MAX_ID_LENGTH);
}
return id;
}
/** /**
* Throws if any of the mandatory fields is not set. * Throws if any of the mandatory fields is not set.
* *
@@ -2090,7 +2103,8 @@ public final class ShortcutInfo implements Parcelable {
final ClassLoader cl = getClass().getClassLoader(); final ClassLoader cl = getClass().getClassLoader();
mUserId = source.readInt(); mUserId = source.readInt();
mId = source.readString8(); mId = getSafeId(Preconditions.checkStringNotEmpty(source.readString8(),
"Shortcut ID must be provided"));
mPackageName = source.readString8(); mPackageName = source.readString8();
mActivity = source.readParcelable(cl); mActivity = source.readParcelable(cl);
mFlags = source.readInt(); mFlags = source.readInt();

View File

@@ -53,6 +53,7 @@ import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.util.Collections;
import java.util.Locale; import java.util.Locale;
/** /**
@@ -223,6 +224,15 @@ public class ShortcutManagerTest2 extends BaseShortcutManagerTest {
}); });
} }
public void testShortcutIdTruncated() {
ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(),
String.join("", Collections.nCopies(Short.MAX_VALUE, "s"))).build();
assertTrue(
"id must be truncated to MAX_ID_LENGTH",
si.getId().length() <= ShortcutInfo.MAX_ID_LENGTH);
}
public void testShortcutInfoParcel() { public void testShortcutInfoParcel() {
setCaller(CALLING_PACKAGE_1, USER_10); setCaller(CALLING_PACKAGE_1, USER_10);
ShortcutInfo si = parceled(new ShortcutInfo.Builder(mClientContext) ShortcutInfo si = parceled(new ShortcutInfo.Builder(mClientContext)