diff options
| author | 2020-06-05 17:53:09 -0700 | |
|---|---|---|
| committer | 2020-06-08 17:22:38 -0700 | |
| commit | cbf1e41fecded6c70cbbed2c4312facc5a4ab2ae (patch) | |
| tree | 7e46519cc50bb2e62506c0dc0885ae5f953376b5 | |
| parent | a621dad0b63a219fa47b2cbf7158930569502947 (diff) | |
Move HapticSettingObserver to companion object.
MagnetizedObject used to register setting observer for each instance,
but it's never unregistered. MagnetizedObject doesn't have explicit
lifecycle calls, so there isn't a good place to unregister the observer.
Without unregistration MagnetizedObject will always be referenced by a
JNI global reference so that it can be called when the remote calls it.
It's not necessary to have an observer per instance, so let's do it in
the companion object instead then we don't need to worry about
unregistering it because it's long standing.
Bug: 149918957
Test: MagnetizedObject isn't referenced by any JNI global references.
Test: Device still vibrates when bubble stack is attracted by the
removal place when settings is on, and doesn't vibrate when settings is
off.
Change-Id: Ie9719baa3ff76bd08180b9ab32662eb1ca43de3a
| -rw-r--r-- | packages/SystemUI/src/com/android/systemui/util/magnetictarget/MagnetizedObject.kt | 59 |
1 files changed, 38 insertions, 21 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/util/magnetictarget/MagnetizedObject.kt b/packages/SystemUI/src/com/android/systemui/util/magnetictarget/MagnetizedObject.kt index e905e6772074..da7953d27f59 100644 --- a/packages/SystemUI/src/com/android/systemui/util/magnetictarget/MagnetizedObject.kt +++ b/packages/SystemUI/src/com/android/systemui/util/magnetictarget/MagnetizedObject.kt @@ -245,9 +245,6 @@ abstract class MagnetizedObject<T : Any>( */ var hapticsEnabled = true - /** Whether the HAPTIC_FEEDBACK_ENABLED setting is true. */ - private var systemHapticsEnabled = false - /** Default spring configuration to use for animating the object into a target. */ var springConfig = PhysicsAnimator.SpringConfig( SpringForce.STIFFNESS_MEDIUM, SpringForce.DAMPING_RATIO_NO_BOUNCY) @@ -259,24 +256,7 @@ abstract class MagnetizedObject<T : Any>( var flungIntoTargetSpringConfig = springConfig init { - val hapticSettingObserver = - object : ContentObserver(Handler.getMain()) { - override fun onChange(selfChange: Boolean) { - systemHapticsEnabled = - Settings.System.getIntForUser( - context.contentResolver, - Settings.System.HAPTIC_FEEDBACK_ENABLED, - 0, - UserHandle.USER_CURRENT) != 0 - } - } - - context.contentResolver.registerContentObserver( - Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_ENABLED), - true /* notifyForDescendants */, hapticSettingObserver) - - // Trigger the observer once to initialize systemHapticsEnabled. - hapticSettingObserver.onChange(false /* selfChange */) + initHapticSettingObserver(context) } /** @@ -623,6 +603,43 @@ abstract class MagnetizedObject<T : Any>( companion object { /** + * Whether the HAPTIC_FEEDBACK_ENABLED setting is true. + * + * We put it in the companion object because we need to register a settings observer and + * [MagnetizedObject] doesn't have an obvious lifecycle so we don't have a good time to + * remove that observer. Since this settings is shared among all instances we just let all + * instances read from this value. + */ + private var systemHapticsEnabled = false + private var hapticSettingObserverInitialized = false + + private fun initHapticSettingObserver(context: Context) { + if (hapticSettingObserverInitialized) { + return + } + + val hapticSettingObserver = + object : ContentObserver(Handler.getMain()) { + override fun onChange(selfChange: Boolean) { + systemHapticsEnabled = + Settings.System.getIntForUser( + context.contentResolver, + Settings.System.HAPTIC_FEEDBACK_ENABLED, + 0, + UserHandle.USER_CURRENT) != 0 + } + } + + context.contentResolver.registerContentObserver( + Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_ENABLED), + true /* notifyForDescendants */, hapticSettingObserver) + + // Trigger the observer once to initialize systemHapticsEnabled. + hapticSettingObserver.onChange(false /* selfChange */) + hapticSettingObserverInitialized = true + } + + /** * Magnetizes the given view. Magnetized views are attracted to one or more magnetic * targets. Magnetic targets attract objects that are dragged near them, and hold them there * unless they're moved away or released. Releasing objects inside a magnetic target |