Merge "Limit the size of NotificationChannel and NotificationChannelGroup" into rvc-qpr-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
a15093de05
@@ -78,8 +78,13 @@ public final class NotificationChannel implements Parcelable {
|
|||||||
/**
|
/**
|
||||||
* The maximum length for text fields in a NotificationChannel. Fields will be truncated at this
|
* The maximum length for text fields in a NotificationChannel. Fields will be truncated at this
|
||||||
* limit.
|
* limit.
|
||||||
|
* @hide
|
||||||
*/
|
*/
|
||||||
private static final int MAX_TEXT_LENGTH = 1000;
|
public static final int MAX_TEXT_LENGTH = 1000;
|
||||||
|
/**
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
public static final int MAX_VIBRATION_LENGTH = 1000;
|
||||||
|
|
||||||
private static final String TAG_CHANNEL = "channel";
|
private static final String TAG_CHANNEL = "channel";
|
||||||
private static final String ATT_NAME = "name";
|
private static final String ATT_NAME = "name";
|
||||||
@@ -236,17 +241,17 @@ public final class NotificationChannel implements Parcelable {
|
|||||||
*/
|
*/
|
||||||
protected NotificationChannel(Parcel in) {
|
protected NotificationChannel(Parcel in) {
|
||||||
if (in.readByte() != 0) {
|
if (in.readByte() != 0) {
|
||||||
mId = in.readString();
|
mId = getTrimmedString(in.readString());
|
||||||
} else {
|
} else {
|
||||||
mId = null;
|
mId = null;
|
||||||
}
|
}
|
||||||
if (in.readByte() != 0) {
|
if (in.readByte() != 0) {
|
||||||
mName = in.readString();
|
mName = getTrimmedString(in.readString());
|
||||||
} else {
|
} else {
|
||||||
mName = null;
|
mName = null;
|
||||||
}
|
}
|
||||||
if (in.readByte() != 0) {
|
if (in.readByte() != 0) {
|
||||||
mDesc = in.readString();
|
mDesc = getTrimmedString(in.readString());
|
||||||
} else {
|
} else {
|
||||||
mDesc = null;
|
mDesc = null;
|
||||||
}
|
}
|
||||||
@@ -255,18 +260,22 @@ public final class NotificationChannel implements Parcelable {
|
|||||||
mLockscreenVisibility = in.readInt();
|
mLockscreenVisibility = in.readInt();
|
||||||
if (in.readByte() != 0) {
|
if (in.readByte() != 0) {
|
||||||
mSound = Uri.CREATOR.createFromParcel(in);
|
mSound = Uri.CREATOR.createFromParcel(in);
|
||||||
|
mSound = Uri.parse(getTrimmedString(mSound.toString()));
|
||||||
} else {
|
} else {
|
||||||
mSound = null;
|
mSound = null;
|
||||||
}
|
}
|
||||||
mLights = in.readByte() != 0;
|
mLights = in.readByte() != 0;
|
||||||
mVibration = in.createLongArray();
|
mVibration = in.createLongArray();
|
||||||
|
if (mVibration != null && mVibration.length > MAX_VIBRATION_LENGTH) {
|
||||||
|
mVibration = Arrays.copyOf(mVibration, MAX_VIBRATION_LENGTH);
|
||||||
|
}
|
||||||
mUserLockedFields = in.readInt();
|
mUserLockedFields = in.readInt();
|
||||||
mFgServiceShown = in.readByte() != 0;
|
mFgServiceShown = in.readByte() != 0;
|
||||||
mVibrationEnabled = in.readByte() != 0;
|
mVibrationEnabled = in.readByte() != 0;
|
||||||
mShowBadge = in.readByte() != 0;
|
mShowBadge = in.readByte() != 0;
|
||||||
mDeleted = in.readByte() != 0;
|
mDeleted = in.readByte() != 0;
|
||||||
if (in.readByte() != 0) {
|
if (in.readByte() != 0) {
|
||||||
mGroup = in.readString();
|
mGroup = getTrimmedString(in.readString());
|
||||||
} else {
|
} else {
|
||||||
mGroup = null;
|
mGroup = null;
|
||||||
}
|
}
|
||||||
@@ -276,8 +285,8 @@ public final class NotificationChannel implements Parcelable {
|
|||||||
mAllowBubbles = in.readInt();
|
mAllowBubbles = in.readInt();
|
||||||
mImportanceLockedByOEM = in.readBoolean();
|
mImportanceLockedByOEM = in.readBoolean();
|
||||||
mOriginalImportance = in.readInt();
|
mOriginalImportance = in.readInt();
|
||||||
mParentId = in.readString();
|
mParentId = getTrimmedString(in.readString());
|
||||||
mConversationId = in.readString();
|
mConversationId = getTrimmedString(in.readString());
|
||||||
mDemoted = in.readBoolean();
|
mDemoted = in.readBoolean();
|
||||||
mImportantConvo = in.readBoolean();
|
mImportantConvo = in.readBoolean();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,8 +42,9 @@ public final class NotificationChannelGroup implements Parcelable {
|
|||||||
/**
|
/**
|
||||||
* The maximum length for text fields in a NotificationChannelGroup. Fields will be truncated at
|
* The maximum length for text fields in a NotificationChannelGroup. Fields will be truncated at
|
||||||
* this limit.
|
* this limit.
|
||||||
|
* @hide
|
||||||
*/
|
*/
|
||||||
private static final int MAX_TEXT_LENGTH = 1000;
|
public static final int MAX_TEXT_LENGTH = 1000;
|
||||||
|
|
||||||
private static final String TAG_GROUP = "channelGroup";
|
private static final String TAG_GROUP = "channelGroup";
|
||||||
private static final String ATT_NAME = "name";
|
private static final String ATT_NAME = "name";
|
||||||
@@ -89,13 +90,14 @@ public final class NotificationChannelGroup implements Parcelable {
|
|||||||
*/
|
*/
|
||||||
protected NotificationChannelGroup(Parcel in) {
|
protected NotificationChannelGroup(Parcel in) {
|
||||||
if (in.readByte() != 0) {
|
if (in.readByte() != 0) {
|
||||||
mId = in.readString();
|
mId = getTrimmedString(in.readString());
|
||||||
} else {
|
} else {
|
||||||
mId = null;
|
mId = null;
|
||||||
}
|
}
|
||||||
mName = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
|
mName = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
|
||||||
|
mName = getTrimmedString(mName.toString());
|
||||||
if (in.readByte() != 0) {
|
if (in.readByte() != 0) {
|
||||||
mDescription = in.readString();
|
mDescription = getTrimmedString(in.readString());
|
||||||
} else {
|
} else {
|
||||||
mDescription = null;
|
mDescription = null;
|
||||||
}
|
}
|
||||||
@@ -119,7 +121,7 @@ public final class NotificationChannelGroup implements Parcelable {
|
|||||||
} else {
|
} else {
|
||||||
dest.writeByte((byte) 0);
|
dest.writeByte((byte) 0);
|
||||||
}
|
}
|
||||||
TextUtils.writeToParcel(mName, dest, flags);
|
TextUtils.writeToParcel(mName.toString(), dest, flags);
|
||||||
if (mDescription != null) {
|
if (mDescription != null) {
|
||||||
dest.writeByte((byte) 1);
|
dest.writeByte((byte) 1);
|
||||||
dest.writeString(mDescription);
|
dest.writeString(mDescription);
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 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 android.app;
|
||||||
|
|
||||||
|
import static junit.framework.TestCase.assertEquals;
|
||||||
|
|
||||||
|
import android.os.Parcel;
|
||||||
|
import android.test.AndroidTestCase;
|
||||||
|
|
||||||
|
import androidx.test.filters.SmallTest;
|
||||||
|
import androidx.test.runner.AndroidJUnit4;
|
||||||
|
|
||||||
|
import com.google.common.base.Strings;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
@SmallTest
|
||||||
|
public class NotificationChannelGroupTest {
|
||||||
|
private final String CLASS = "android.app.NotificationChannelGroup";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLongStringFields() {
|
||||||
|
NotificationChannelGroup group = new NotificationChannelGroup("my_group_01", "groupName");
|
||||||
|
|
||||||
|
try {
|
||||||
|
String longString = Strings.repeat("A", 65536);
|
||||||
|
Field mName = Class.forName(CLASS).getDeclaredField("mName");
|
||||||
|
mName.setAccessible(true);
|
||||||
|
mName.set(group, longString);
|
||||||
|
Field mId = Class.forName(CLASS).getDeclaredField("mId");
|
||||||
|
mId.setAccessible(true);
|
||||||
|
mId.set(group, longString);
|
||||||
|
Field mDescription = Class.forName(CLASS).getDeclaredField("mDescription");
|
||||||
|
mDescription.setAccessible(true);
|
||||||
|
mDescription.set(group, longString);
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
Parcel parcel = Parcel.obtain();
|
||||||
|
group.writeToParcel(parcel, 0);
|
||||||
|
parcel.setDataPosition(0);
|
||||||
|
|
||||||
|
NotificationChannelGroup fromParcel =
|
||||||
|
NotificationChannelGroup.CREATOR.createFromParcel(parcel);
|
||||||
|
assertEquals(NotificationChannelGroup.MAX_TEXT_LENGTH, fromParcel.getId().length());
|
||||||
|
assertEquals(NotificationChannelGroup.MAX_TEXT_LENGTH, fromParcel.getName().length());
|
||||||
|
assertEquals(NotificationChannelGroup.MAX_TEXT_LENGTH,
|
||||||
|
fromParcel.getDescription().length());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 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 android.app;
|
||||||
|
|
||||||
|
import static junit.framework.TestCase.assertEquals;
|
||||||
|
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.Parcel;
|
||||||
|
|
||||||
|
import androidx.test.filters.SmallTest;
|
||||||
|
import androidx.test.runner.AndroidJUnit4;
|
||||||
|
|
||||||
|
import com.google.common.base.Strings;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
@SmallTest
|
||||||
|
public class NotificationChannelTest {
|
||||||
|
private final String CLASS = "android.app.NotificationChannel";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLongStringFields() {
|
||||||
|
NotificationChannel channel = new NotificationChannel("id", "name", 3);
|
||||||
|
|
||||||
|
try {
|
||||||
|
String longString = Strings.repeat("A", 65536);
|
||||||
|
Field mName = Class.forName(CLASS).getDeclaredField("mName");
|
||||||
|
mName.setAccessible(true);
|
||||||
|
mName.set(channel, longString);
|
||||||
|
Field mId = Class.forName(CLASS).getDeclaredField("mId");
|
||||||
|
mId.setAccessible(true);
|
||||||
|
mId.set(channel, longString);
|
||||||
|
Field mDesc = Class.forName(CLASS).getDeclaredField("mDesc");
|
||||||
|
mDesc.setAccessible(true);
|
||||||
|
mDesc.set(channel, longString);
|
||||||
|
Field mParentId = Class.forName(CLASS).getDeclaredField("mParentId");
|
||||||
|
mParentId.setAccessible(true);
|
||||||
|
mParentId.set(channel, longString);
|
||||||
|
Field mGroup = Class.forName(CLASS).getDeclaredField("mGroup");
|
||||||
|
mGroup.setAccessible(true);
|
||||||
|
mGroup.set(channel, longString);
|
||||||
|
Field mConversationId = Class.forName(CLASS).getDeclaredField("mConversationId");
|
||||||
|
mConversationId.setAccessible(true);
|
||||||
|
mConversationId.set(channel, longString);
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
Parcel parcel = Parcel.obtain();
|
||||||
|
channel.writeToParcel(parcel, 0);
|
||||||
|
parcel.setDataPosition(0);
|
||||||
|
|
||||||
|
NotificationChannel fromParcel = NotificationChannel.CREATOR.createFromParcel(parcel);
|
||||||
|
assertEquals(NotificationChannel.MAX_TEXT_LENGTH, fromParcel.getId().length());
|
||||||
|
assertEquals(NotificationChannel.MAX_TEXT_LENGTH, fromParcel.getName().length());
|
||||||
|
assertEquals(NotificationChannel.MAX_TEXT_LENGTH,
|
||||||
|
fromParcel.getDescription().length());
|
||||||
|
assertEquals(NotificationChannel.MAX_TEXT_LENGTH,
|
||||||
|
fromParcel.getParentChannelId().length());
|
||||||
|
assertEquals(NotificationChannel.MAX_TEXT_LENGTH,
|
||||||
|
fromParcel.getGroup().length());
|
||||||
|
assertEquals(NotificationChannel.MAX_TEXT_LENGTH,
|
||||||
|
fromParcel.getConversationId().length());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLongAlertFields() {
|
||||||
|
NotificationChannel channel = new NotificationChannel("id", "name", 3);
|
||||||
|
|
||||||
|
channel.setSound(Uri.parse("content://" + Strings.repeat("A",65536)),
|
||||||
|
Notification.AUDIO_ATTRIBUTES_DEFAULT);
|
||||||
|
channel.setVibrationPattern(new long[65550/2]);
|
||||||
|
|
||||||
|
Parcel parcel = Parcel.obtain();
|
||||||
|
channel.writeToParcel(parcel, 0);
|
||||||
|
parcel.setDataPosition(0);
|
||||||
|
|
||||||
|
NotificationChannel fromParcel = NotificationChannel.CREATOR.createFromParcel(parcel);
|
||||||
|
assertEquals(NotificationChannel.MAX_VIBRATION_LENGTH,
|
||||||
|
fromParcel.getVibrationPattern().length);
|
||||||
|
assertEquals(NotificationChannel.MAX_TEXT_LENGTH,
|
||||||
|
fromParcel.getSound().toString().length());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user