Skip to content

Commit 3f4d138

Browse files
András KuruczAndroid Build Coastguard Worker
authored andcommitted
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. (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:8a5223fcd59e805da510c31c5f8462c5d01ec6d5) Merged-In: I2617de6f9e8a7dbfd8fbeff589a7d592f00d87c5 Change-Id: I2617de6f9e8a7dbfd8fbeff589a7d592f00d87c5
1 parent 67d1b35 commit 3f4d138

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

core/java/android/content/pm/ShortcutInfo.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ public final class ShortcutInfo implements Parcelable {
283283
*/
284284
public static final int DISABLED_REASON_OTHER_RESTORE_ISSUE = 103;
285285

286+
/**
287+
* The maximum length of Shortcut ID. IDs will be truncated at this limit.
288+
* @hide
289+
*/
290+
public static final int MAX_ID_LENGTH = 1000;
291+
286292
/** @hide */
287293
@IntDef(prefix = { "DISABLED_REASON_" }, value = {
288294
DISABLED_REASON_NOT_DISABLED,
@@ -475,8 +481,7 @@ public static boolean isDisabledForRestoreIssue(@DisabledReason int disabledReas
475481

476482
private ShortcutInfo(Builder b) {
477483
mUserId = b.mContext.getUserId();
478-
479-
mId = Preconditions.checkStringNotEmpty(b.mId, "Shortcut ID must be provided");
484+
mId = getSafeId(Preconditions.checkStringNotEmpty(b.mId, "Shortcut ID must be provided"));
480485

481486
// Note we can't do other null checks here because SM.updateShortcuts() takes partial
482487
// information.
@@ -582,6 +587,14 @@ private static Person[] clonePersons(Person[] persons) {
582587
return ret;
583588
}
584589

590+
@NonNull
591+
private static String getSafeId(@NonNull String id) {
592+
if (id.length() > MAX_ID_LENGTH) {
593+
return id.substring(0, MAX_ID_LENGTH);
594+
}
595+
return id;
596+
}
597+
585598
/**
586599
* Throws if any of the mandatory fields is not set.
587600
*
@@ -2336,7 +2349,8 @@ private ShortcutInfo(Parcel source) {
23362349
final ClassLoader cl = getClass().getClassLoader();
23372350

23382351
mUserId = source.readInt();
2339-
mId = source.readString8();
2352+
mId = getSafeId(Preconditions.checkStringNotEmpty(source.readString8(),
2353+
"Shortcut ID must be provided"));
23402354
mPackageName = source.readString8();
23412355
mActivity = source.readParcelable(cl, android.content.ComponentName.class);
23422356
mFlags = source.readInt();

services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ public void testShortcutInfoMissingMandatoryFields() {
228228
});
229229
}
230230

231+
public void testShortcutIdTruncated() {
232+
ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(),
233+
"s".repeat(Short.MAX_VALUE)).build();
234+
235+
assertTrue(
236+
"id must be truncated to MAX_ID_LENGTH",
237+
si.getId().length() <= ShortcutInfo.MAX_ID_LENGTH);
238+
}
239+
231240
public void testShortcutInfoParcel() {
232241
setCaller(CALLING_PACKAGE_1, USER_10);
233242
ShortcutInfo si = parceled(new ShortcutInfo.Builder(mClientContext)

0 commit comments

Comments
 (0)