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

Commit 290eb284 authored by Lais Andrade's avatar Lais Andrade
Browse files

Add receiver to user switch to VibrationSettings

The current user settings held by the vibrator manager service
is not updating on user switch. Adding a receiver to trigger settings
update on that broadcast.

Fix: 186142123
Test: VibrationSettingsTest
Change-Id: I177bc6fd0c12185526ce82faf066a76901520468
parent d6f7eb33
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -19,7 +19,10 @@ package com.android.server.vibrator;
import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.IUidObserver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.media.AudioManager;
@@ -61,6 +64,9 @@ final class VibrationSettings {
    private final SettingsObserver mSettingObserver;
    @VisibleForTesting
    final UidObserver mUidObserver;
    @VisibleForTesting
    final UserObserver mUserReceiver;


    @GuardedBy("mLock")
    private final List<OnVibratorSettingsChanged> mListeners = new ArrayList<>();
@@ -94,6 +100,7 @@ final class VibrationSettings {
        mContext = context;
        mSettingObserver = new SettingsObserver(handler);
        mUidObserver = new UidObserver();
        mUserReceiver = new UserObserver();

        VibrationEffect clickEffect = createEffectFromResource(
                com.android.internal.R.array.config_virtualKeyVibePattern);
@@ -150,6 +157,7 @@ final class VibrationSettings {
                    }
                });

        mContext.registerReceiver(mUserReceiver, new IntentFilter(Intent.ACTION_USER_SWITCHED));
        registerSettingsObserver(Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES));
        registerSettingsObserver(Settings.System.getUriFor(Settings.System.VIBRATE_WHEN_RINGING));
        registerSettingsObserver(Settings.Global.getUriFor(Settings.Global.APPLY_RAMPING_RINGER));
@@ -457,6 +465,17 @@ final class VibrationSettings {
        }
    }

    /** Implementation of {@link BroadcastReceiver} to update settings on current user change. */
    @VisibleForTesting
    final class UserObserver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
                updateSettings();
            }
        }
    }

    /** Implementation of {@link ContentObserver} to be registered to a setting {@link Uri}. */
    @VisibleForTesting
    final class UidObserver extends IUidObserver.Stub {
+21 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Handler;
import android.os.PowerManagerInternal;
@@ -69,6 +70,7 @@ import org.mockito.junit.MockitoRule;
public class VibrationSettingsTest {

    private static final int UID = 1;
    private static final int USER_OPERATION_TIMEOUT_MILLIS = 60_000; // 1 min
    private static final PowerSaveState NORMAL_POWER_STATE = new PowerSaveState.Builder().build();
    private static final PowerSaveState LOW_POWER_STATE = new PowerSaveState.Builder()
            .setBatterySaverEnabled(true).build();
@@ -407,6 +409,25 @@ public class VibrationSettingsTest {
                mVibrationSettings.getCurrentIntensity(VibrationAttributes.USAGE_RINGTONE));
    }

    @Test
    public void getCurrentIntensity_updateTriggeredAfterUserSwitched() {
        mFakeVibrator.setDefaultRingVibrationIntensity(Vibrator.VIBRATION_INTENSITY_OFF);
        setUserSetting(Settings.System.RING_VIBRATION_INTENSITY,
                Vibrator.VIBRATION_INTENSITY_HIGH);
        assertEquals(Vibrator.VIBRATION_INTENSITY_HIGH,
                mVibrationSettings.getCurrentIntensity(VibrationAttributes.USAGE_RINGTONE));

        // Switching user is not working with FakeSettingsProvider.
        // Testing the broadcast flow manually.
        Settings.System.putIntForUser(mContextSpy.getContentResolver(),
                Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW,
                UserHandle.USER_CURRENT);
        mVibrationSettings.mUserReceiver.onReceive(mContextSpy,
                new Intent(Intent.ACTION_USER_SWITCHED));
        assertEquals(Vibrator.VIBRATION_INTENSITY_LOW,
                mVibrationSettings.getCurrentIntensity(VibrationAttributes.USAGE_RINGTONE));
    }

    @Test
    public void getFallbackEffect_returnsEffectsFromSettings() {
        assertNotNull(mVibrationSettings.getFallbackEffect(VibrationEffect.EFFECT_TICK));