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

Commit f7ffbaa8 authored by Lais Andrade's avatar Lais Andrade Committed by Android (Google) Code Review
Browse files

Merge "Fix broken vibration vendor effect tests in staging" into main

parents de93c539 52425a25
Loading
Loading
Loading
Loading
+54 −4
Original line number Diff line number Diff line
@@ -345,7 +345,10 @@ public abstract class VibrationEffect implements Parcelable {
    @FlaggedApi(FLAG_VENDOR_VIBRATION_EFFECTS)
    @RequiresPermission(android.Manifest.permission.VIBRATE_VENDOR_EFFECTS)
    public static VibrationEffect createVendorEffect(@NonNull PersistableBundle effect) {
        return new VendorEffect(effect, VendorEffect.DEFAULT_STRENGTH, VendorEffect.DEFAULT_SCALE);
        VibrationEffect vendorEffect = new VendorEffect(effect, VendorEffect.DEFAULT_STRENGTH,
                VendorEffect.DEFAULT_SCALE);
        vendorEffect.validate();
        return vendorEffect;
    }

    /**
@@ -1204,9 +1207,7 @@ public abstract class VibrationEffect implements Parcelable {
            }
            return mEffectStrength == other.mEffectStrength
                    && (Float.compare(mLinearScale, other.mLinearScale) == 0)
                    // Make sure it calls unparcel for both before calling BaseBundle.kindofEquals.
                    && mVendorData.size() == other.mVendorData.size()
                    && BaseBundle.kindofEquals(mVendorData, other.mVendorData);
                    && isPersistableBundleEquals(mVendorData, other.mVendorData);
        }

        @Override
@@ -1243,6 +1244,55 @@ public abstract class VibrationEffect implements Parcelable {
            out.writeFloat(mLinearScale);
        }

        /**
         * Compares two {@link PersistableBundle} objects are equals.
         */
        private static boolean isPersistableBundleEquals(
                PersistableBundle first, PersistableBundle second) {
            if (first == second) {
                return true;
            }
            if (first == null || second == null || first.size() != second.size()) {
                return false;
            }
            for (String key : first.keySet()) {
                if (!isPersistableBundleSupportedValueEquals(first.get(key), second.get(key))) {
                    return false;
                }
            }
            return true;
        }

        /**
         * Compares two values which type is supported by {@link PersistableBundle}.
         *
         * <p>If the type isn't supported. The equality is done by {@link Object#equals(Object)}.
         */
        private static boolean isPersistableBundleSupportedValueEquals(
                Object first, Object second) {
            if (first == second) {
                return true;
            } else if (first == null || second == null
                    || !first.getClass().equals(second.getClass())) {
                return false;
            } else if (first instanceof PersistableBundle) {
                return isPersistableBundleEquals(
                        (PersistableBundle) first, (PersistableBundle) second);
            } else if (first instanceof int[]) {
                return Arrays.equals((int[]) first, (int[]) second);
            } else if (first instanceof long[]) {
                return Arrays.equals((long[]) first, (long[]) second);
            } else if (first instanceof double[]) {
                return Arrays.equals((double[]) first, (double[]) second);
            } else if (first instanceof boolean[]) {
                return Arrays.equals((boolean[]) first, (boolean[]) second);
            } else if (first instanceof String[]) {
                return Arrays.equals((String[]) first, (String[]) second);
            } else {
                return Objects.equals(first, second);
            }
        }

        @NonNull
        public static final Creator<VendorEffect> CREATOR =
                new Creator<VendorEffect>() {
+5 −2
Original line number Diff line number Diff line
@@ -1603,7 +1603,7 @@ public class VibratorManagerServiceTest {
    @Test
    @RequiresFlagsEnabled(android.os.vibrator.Flags.FLAG_VENDOR_VIBRATION_EFFECTS)
    public void vibrate_vendorEffectsWithPermission_successful() throws Exception {
        // Deny permission to vibrate with vendor effects
        // Grant permission to vibrate with vendor effects
        grantPermission(android.Manifest.permission.VIBRATE_VENDOR_EFFECTS);
        mockVibrators(1);
        FakeVibratorControllerProvider fakeVibrator = mVibratorProviders.get(1);
@@ -1767,6 +1767,9 @@ public class VibratorManagerServiceTest {
    })
    public void vibrate_withIntensitySettingsAndAdaptiveHaptics_appliesSettingsToVendorEffects()
            throws Exception {
        // Grant permission to vibrate with vendor effects
        grantPermission(android.Manifest.permission.VIBRATE_VENDOR_EFFECTS);

        setUserSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
                Vibrator.VIBRATION_INTENSITY_LOW);

@@ -1789,7 +1792,7 @@ public class VibratorManagerServiceTest {

        assertThat(fakeVibrator.getAllVendorEffects()).hasSize(1);
        VibrationEffect.VendorEffect scaled = fakeVibrator.getAllVendorEffects().get(0);
        assertThat(scaled.getEffectStrength()).isEqualTo(VibrationEffect.EFFECT_STRENGTH_STRONG);
        assertThat(scaled.getEffectStrength()).isEqualTo(VibrationEffect.EFFECT_STRENGTH_LIGHT);
        assertThat(scaled.getLinearScale()).isEqualTo(0.4f);
    }