Merge "Make VibratorWrapper injected by dagger (14/n)" into main

This commit is contained in:
Anushree Ganjam authored and Android (Google) Code Review committed 2024-11-04 21:36:19 +00:00
commit 2f73d60b30
4 files changed
+35 -35

No files matched your search

@@ -26,6 +26,7 @@ import com.android.launcher3.util.PackageManagerHelper;
import com.android.launcher3.util.PluginManagerWrapper;
import com.android.launcher3.util.ScreenOnTracker;
import com.android.launcher3.util.SettingsCache;
import com.android.launcher3.util.VibratorWrapper;
import com.android.launcher3.util.window.RefreshRateTracker;
import com.android.launcher3.widget.custom.CustomWidgetManager;
@@ -48,8 +49,9 @@ public interface LauncherBaseAppComponent {
RefreshRateTracker getRefreshRateTracker();
ScreenOnTracker getScreenOnTracker();
SettingsCache getSettingsCache();
PluginManagerWrapper getPluginManagerWrapper();
PackageManagerHelper getPackageManagerHelper();
PluginManagerWrapper getPluginManagerWrapper();
VibratorWrapper getVibratorWrapper();
/** Builder for LauncherBaseAppComponent. */
interface Builder {
@@ -28,6 +28,8 @@ import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import androidx.annotation.UiThread;
import com.android.launcher3.dagger.ApplicationContext;
import com.android.launcher3.dagger.LauncherAppSingleton;
import com.android.launcher3.dagger.LauncherBaseAppComponent;
@@ -140,7 +142,9 @@ public class SettingsCache extends ContentObserver implements SafeCloseable {
* Does not de-dupe if you add same listeners for the same key multiple times.
* Unregister once complete using {@link #unregister(Uri, OnChangeListener)}
*/
@UiThread
public void register(Uri uri, OnChangeListener changeListener) {
Preconditions.assertUIThread();
if (mListenerMap.containsKey(uri)) {
mListenerMap.get(uri).add(changeListener);
} else {
@@ -19,6 +19,7 @@ import static android.os.VibrationEffect.Composition.PRIMITIVE_LOW_TICK;
import static android.os.VibrationEffect.createPredefined;
import static android.provider.Settings.System.HAPTIC_FEEDBACK_ENABLED;
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
import android.annotation.SuppressLint;
@@ -31,13 +32,20 @@ import android.provider.Settings;
import androidx.annotation.VisibleForTesting;
import com.android.launcher3.dagger.ApplicationContext;
import com.android.launcher3.dagger.LauncherAppSingleton;
import com.android.launcher3.dagger.LauncherBaseAppComponent;
import javax.inject.Inject;
/**
* Wrapper around {@link Vibrator} to easily perform haptic feedback where necessary.
*/
public class VibratorWrapper implements SafeCloseable {
@LauncherAppSingleton
public class VibratorWrapper {
public static final MainThreadInitializedObject<VibratorWrapper> INSTANCE =
new MainThreadInitializedObject<>(VibratorWrapper::new);
public static final DaggerSingletonObject<VibratorWrapper> INSTANCE =
new DaggerSingletonObject<>(LauncherBaseAppComponent::getVibratorWrapper);
public static final AudioAttributes VIBRATION_ATTRS = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
@@ -59,38 +67,29 @@ public class VibratorWrapper implements SafeCloseable {
private final Vibrator mVibrator;
private final boolean mHasVibrator;
private final SettingsCache mSettingsCache;
@VisibleForTesting
final SettingsCache.OnChangeListener mHapticChangeListener =
isEnabled -> mIsHapticFeedbackEnabled = isEnabled;
private boolean mIsHapticFeedbackEnabled;
private VibratorWrapper(Context context) {
this(context.getSystemService(Vibrator.class), SettingsCache.INSTANCE.get(context));
}
@Inject
public VibratorWrapper(@ApplicationContext Context context, SettingsCache settingsCache,
DaggerSingletonTracker tracker) {
@VisibleForTesting
VibratorWrapper(Vibrator vibrator, SettingsCache settingsCache) {
mVibrator = vibrator;
mVibrator = context.getSystemService(Vibrator.class);
mHasVibrator = mVibrator.hasVibrator();
mSettingsCache = settingsCache;
if (mHasVibrator) {
mSettingsCache.register(HAPTIC_FEEDBACK_URI, mHapticChangeListener);
mIsHapticFeedbackEnabled = mSettingsCache.getValue(HAPTIC_FEEDBACK_URI, 0);
MAIN_EXECUTOR.execute(
() -> settingsCache.register(HAPTIC_FEEDBACK_URI, mHapticChangeListener));
mIsHapticFeedbackEnabled = settingsCache.getValue(HAPTIC_FEEDBACK_URI, 0);
tracker.addCloseable(
() -> settingsCache.unregister(HAPTIC_FEEDBACK_URI, mHapticChangeListener));
} else {
mIsHapticFeedbackEnabled = false;
}
}
@Override
public void close() {
if (mHasVibrator) {
mSettingsCache.unregister(HAPTIC_FEEDBACK_URI, mHapticChangeListener);
}
}
/**
* This should be used to cancel a haptic in case where the haptic shouldn't be vibrating. For
* example, when no animation is happening but a vibrator happens to be vibrating still.
@@ -21,8 +21,8 @@ import android.os.VibrationEffect
import android.os.VibrationEffect.Composition.PRIMITIVE_LOW_TICK
import android.os.VibrationEffect.Composition.PRIMITIVE_TICK
import android.os.Vibrator
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.launcher3.util.LauncherModelHelper.SandboxModelContext
import com.android.launcher3.util.VibratorWrapper.HAPTIC_FEEDBACK_URI
import com.android.launcher3.util.VibratorWrapper.OVERVIEW_HAPTIC
import com.android.launcher3.util.VibratorWrapper.VIBRATION_ATTRS
@@ -41,25 +41,27 @@ import org.mockito.kotlin.never
import org.mockito.kotlin.same
@SmallTest
@RunWith(AndroidJUnit4::class)
@RunWith(LauncherMultivalentJUnit::class)
class VibratorWrapperTest {
@Mock private lateinit var settingsCache: SettingsCache
@Mock private lateinit var vibrator: Vibrator
private lateinit var vibrator: Vibrator
private val context: SandboxModelContext = SandboxModelContext()
@Captor private lateinit var vibrationEffectCaptor: ArgumentCaptor<VibrationEffect>
@Mock private lateinit var tracker: DaggerSingletonTracker
private lateinit var underTest: VibratorWrapper
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
vibrator = context.spyService(Vibrator::class.java)
`when`(settingsCache.getValue(HAPTIC_FEEDBACK_URI, 0)).thenReturn(true)
`when`(vibrator.hasVibrator()).thenReturn(true)
`when`(vibrator.areAllPrimitivesSupported(PRIMITIVE_TICK)).thenReturn(true)
`when`(vibrator.areAllPrimitivesSupported(PRIMITIVE_LOW_TICK)).thenReturn(true)
`when`(vibrator.getPrimitiveDurations(PRIMITIVE_LOW_TICK)).thenReturn(intArrayOf(10))
underTest = VibratorWrapper(vibrator, settingsCache)
underTest = VibratorWrapper(context, settingsCache, tracker)
}
@Test
@@ -67,13 +69,6 @@ class VibratorWrapperTest {
verify(settingsCache).register(HAPTIC_FEEDBACK_URI, underTest.mHapticChangeListener)
}
@Test
fun close_unregister_onChangeListener() {
underTest.close()
verify(settingsCache).unregister(HAPTIC_FEEDBACK_URI, underTest.mHapticChangeListener)
}
@Test
fun vibrate() {
underTest.vibrate(OVERVIEW_HAPTIC)
@@ -117,7 +112,7 @@ class VibratorWrapperTest {
@Test
fun haptic_feedback_disabled_no_vibrate() {
`when`(vibrator.hasVibrator()).thenReturn(false)
underTest = VibratorWrapper(vibrator, settingsCache)
underTest = VibratorWrapper(context, settingsCache, tracker)
underTest.vibrate(OVERVIEW_HAPTIC)