Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4db9092a authored by Yuri Lin's avatar Yuri Lin
Browse files

Update handling of vibration effects that don't need cropping

This change splits the addition of vibration effects into three cases:
- has equivalent vibration pattern, and that pattern needs trimming: delegate to setVibrationPattern to trim & re-convert to a vibration effect
- has equivalent vibration pattern, which is under the size limit: set vibration pattern directly, and store the effect passed in by the caller
- has no equivalent vibration pattern: trim effect if possible

Also: when there is no equivalent vibration pattern, set it to null, thus making sure to override any value previously set by setVibrationPattern.

This fixes some tests that were expecting the effect to be exactly the same as what was passed in, but we were instead producing an equivalent one with an extra "0"-duration at the beginning, and otherwise avoids doing some redundant conversions.

Also parameterizes the unit test to run with both values of notif_channel_crop_vibration_effects, to avoid running into any further potential test issues when the flag turns on.

Bug: 345881518
Bug: 365538915
Fixes: 364960520
Test: NotificationChannelTest (both CTS and unit)
Flag: android.app.notif_channel_crop_vibration_effects
Change-Id: I3073e88a95169b049b051b37936e3fb9e8c16e9f
parent 2b05348a
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -761,14 +761,22 @@ public final class NotificationChannel implements Parcelable {
        this.mVibrationEnabled = effect != null;
        this.mVibrationEffect = effect;
        if (Flags.notifChannelCropVibrationEffects() && effect != null) {
            // Try converting to a vibration pattern and trimming that array. If not convertible
            // to a pattern directly, try trimming the vibration effect if possible and storing
            // that version instead.
            long[] pattern = effect.computeCreateWaveformOffOnTimingsOrNull();
            if (pattern != null) {
                // If this effect has an equivalent pattern, AND the pattern needs to be truncated
                // due to being too long, we delegate to setVibrationPattern to re-generate the
                // effect as well. Otherwise, we use the effect (already set above) and converted
                // pattern directly.
                if (pattern.length > MAX_VIBRATION_LENGTH) {
                    setVibrationPattern(pattern);
                } else {
                    this.mVibrationPattern = pattern;
                }
            } else {
                // If not convertible to a pattern directly, try trimming the vibration effect if
                // possible and storing that version instead.
                this.mVibrationEffect = getTrimmedVibrationEffect(mVibrationEffect);
                this.mVibrationPattern = null;
            }
        } else {
            this.mVibrationPattern =
+23 −3
Original line number Diff line number Diff line
@@ -47,12 +47,13 @@ import android.os.RemoteException;
import android.os.VibrationEffect;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.annotations.Presubmit;
import android.platform.test.annotations.UsesFlags;
import android.platform.test.flag.junit.FlagsParameterization;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.MediaStore.Audio.AudioColumns;
import android.test.mock.MockContentResolver;
import android.util.Xml;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import com.android.modules.utils.TypedXmlPullParser;
@@ -61,6 +62,7 @@ import com.android.modules.utils.TypedXmlSerializer;
import com.google.common.base.Strings;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -71,14 +73,28 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

@RunWith(AndroidJUnit4.class)
import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
import platform.test.runner.parameterized.Parameters;

@RunWith(ParameterizedAndroidJunit4.class)
@UsesFlags(android.app.Flags.class)
@SmallTest
@Presubmit
public class NotificationChannelTest {
    @ClassRule
    public static final SetFlagsRule.ClassRule mSetFlagsClassRule = new SetFlagsRule.ClassRule();

    @Parameters(name = "{0}")
    public static List<FlagsParameterization> getParams() {
        return FlagsParameterization.allCombinationsOf(
                Flags.FLAG_NOTIF_CHANNEL_CROP_VIBRATION_EFFECTS);
    }

    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
    public final SetFlagsRule mSetFlagsRule;

    private final String CLASS = "android.app.NotificationChannel";

@@ -86,6 +102,10 @@ public class NotificationChannelTest {
    ContentProvider mContentProvider;
    IContentProvider mIContentProvider;

    public NotificationChannelTest(FlagsParameterization flags) {
        mSetFlagsRule = mSetFlagsClassRule.createSetFlagsRule(flags);
    }

    @Before
    public void setUp() throws Exception {
        mContext = mock(Context.class);