1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.wm.shell.common.magnetictarget
17 
18 import android.annotation.SuppressLint
19 import android.content.Context
20 import android.database.ContentObserver
21 import android.graphics.PointF
22 import android.os.Handler
23 import android.os.UserHandle
24 import android.os.VibrationEffect
25 import android.os.Vibrator
26 import android.provider.Settings
27 import android.view.MotionEvent
28 import android.view.VelocityTracker
29 import android.view.View
30 import android.view.ViewConfiguration
31 import androidx.dynamicanimation.animation.DynamicAnimation
32 import androidx.dynamicanimation.animation.FloatPropertyCompat
33 import androidx.dynamicanimation.animation.SpringForce
34 import com.android.wm.shell.animation.PhysicsAnimator
35 import kotlin.math.abs
36 import kotlin.math.hypot
37 
38 /**
39  * Utility class for creating 'magnetized' objects that are attracted to one or more magnetic
40  * targets. Magnetic targets attract objects that are dragged near them, and hold them there unless
41  * they're moved away or released. Releasing objects inside a magnetic target typically performs an
42  * action on the object.
43  *
44  * MagnetizedObject also supports flinging to targets, which will result in the object being pulled
45  * into the target and released as if it was dragged into it.
46  *
47  * To use this class, either construct an instance with an object of arbitrary type, or use the
48  * [MagnetizedObject.magnetizeView] shortcut method if you're magnetizing a view. Then, set
49  * [magnetListener] to receive event callbacks. In your touch handler, pass all MotionEvents
50  * that move this object to [maybeConsumeMotionEvent]. If that method returns true, consider the
51  * event consumed by the MagnetizedObject and don't move the object unless it begins returning false
52  * again.
53  *
54  * @param context Context, used to retrieve a Vibrator instance for vibration effects.
55  * @param underlyingObject The actual object that we're magnetizing.
56  * @param xProperty Property that sets the x value of the object's position.
57  * @param yProperty Property that sets the y value of the object's position.
58  */
59 abstract class MagnetizedObject<T : Any>(
60     val context: Context,
61 
62     /** The actual object that is animated. */
63     val underlyingObject: T,
64 
65     /** Property that gets/sets the object's X value. */
66     val xProperty: FloatPropertyCompat<in T>,
67 
68     /** Property that gets/sets the object's Y value. */
69     val yProperty: FloatPropertyCompat<in T>
70 ) {
71 
72     /** Return the width of the object. */
73     abstract fun getWidth(underlyingObject: T): Float
74 
75     /** Return the height of the object. */
76     abstract fun getHeight(underlyingObject: T): Float
77 
78     /**
79      * Fill the provided array with the location of the top-left of the object, relative to the
80      * entire screen. Compare to [View.getLocationOnScreen].
81      */
82     abstract fun getLocationOnScreen(underlyingObject: T, loc: IntArray)
83 
84     /** Methods for listening to events involving a magnetized object.  */
85     interface MagnetListener {
86 
87         /**
88          * Called when touch events move within the magnetic field of a target, causing the
89          * object to animate to the target and become 'stuck' there. The animation happens
90          * automatically here - you should not move the object. You can, however, change its state
91          * to indicate to the user that it's inside the target and releasing it will have an effect.
92          *
93          * [maybeConsumeMotionEvent] is now returning true and will continue to do so until a call
94          * to [onUnstuckFromTarget] or [onReleasedInTarget].
95          *
96          * @param target The target that the object is now stuck to.
97          */
98         fun onStuckToTarget(target: MagneticTarget)
99 
100         /**
101          * Called when the object is no longer stuck to a target. This means that either touch
102          * events moved outside of the magnetic field radius, or that a forceful fling out of the
103          * target was detected.
104          *
105          * The object won't be automatically animated out of the target, since you're responsible
106          * for moving the object again. You should move it (or animate it) using your own
107          * movement/animation logic.
108          *
109          * Reverse any effects applied in [onStuckToTarget] here.
110          *
111          * If [wasFlungOut] is true, [maybeConsumeMotionEvent] returned true for the ACTION_UP event
112          * that concluded the fling. If [wasFlungOut] is false, that means a drag gesture is ongoing
113          * and [maybeConsumeMotionEvent] is now returning false.
114          *
115          * @param target The target that this object was just unstuck from.
116          * @param velX The X velocity of the touch gesture when it exited the magnetic field.
117          * @param velY The Y velocity of the touch gesture when it exited the magnetic field.
118          * @param wasFlungOut Whether the object was unstuck via a fling gesture. This means that
119          * an ACTION_UP event was received, and that the gesture velocity was sufficient to conclude
120          * that the user wants to un-stick the object despite no touch events occurring outside of
121          * the magnetic field radius.
122          */
123         fun onUnstuckFromTarget(
124             target: MagneticTarget,
125             velX: Float,
126             velY: Float,
127             wasFlungOut: Boolean
128         )
129 
130         /**
131          * Called when the object is released inside a target, or flung towards it with enough
132          * velocity to reach it.
133          *
134          * @param target The target that the object was released in.
135          */
136         fun onReleasedInTarget(target: MagneticTarget)
137     }
138 
139     private val animator: PhysicsAnimator<T> = PhysicsAnimator.getInstance(underlyingObject)
140     private val objectLocationOnScreen = IntArray(2)
141 
142     /**
143      * Targets that have been added to this object. These will all be considered when determining
144      * magnetic fields and fling trajectories.
145      */
146     private val associatedTargets = ArrayList<MagneticTarget>()
147 
148     private val velocityTracker: VelocityTracker = VelocityTracker.obtain()
149     private val vibrator: Vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
150 
151     private var touchDown = PointF()
152     private var touchSlop = 0
153     private var movedBeyondSlop = false
154 
155     /** Whether touch events are presently occurring within the magnetic field area of a target. */
156     val objectStuckToTarget: Boolean
157         get() = targetObjectIsStuckTo != null
158 
159     /** The target the object is stuck to, or null if the object is not stuck to any target. */
160     private var targetObjectIsStuckTo: MagneticTarget? = null
161 
162     /**
163      * Sets the listener to receive events. This must be set, or [maybeConsumeMotionEvent]
164      * will always return false and no magnetic effects will occur.
165      */
166     lateinit var magnetListener: MagnetizedObject.MagnetListener
167 
168     /**
169      * Optional update listener to provide to the PhysicsAnimator that is used to spring the object
170      * into the target.
171      */
172     var physicsAnimatorUpdateListener: PhysicsAnimator.UpdateListener<T>? = null
173 
174     /**
175      * Optional end listener to provide to the PhysicsAnimator that is used to spring the object
176      * into the target.
177      */
178     var physicsAnimatorEndListener: PhysicsAnimator.EndListener<T>? = null
179 
180     /**
181      * Method that is called when the object should be animated stuck to the target. The default
182      * implementation uses the object's x and y properties to animate the object centered inside the
183      * target. You can override this if you need custom animation.
184      *
185      * The method is invoked with the MagneticTarget that the object is sticking to, the X and Y
186      * velocities of the gesture that brought the object into the magnetic radius, whether or not it
187      * was flung, and a callback you must call after your animation completes.
188      */
189     var animateStuckToTarget: (MagneticTarget, Float, Float, Boolean, (() -> Unit)?) -> Unit =
190             ::animateStuckToTargetInternal
191 
192     /**
193      * Sets whether forcefully flinging the object vertically towards a target causes it to be
194      * attracted to the target and then released immediately, despite never being dragged within the
195      * magnetic field.
196      */
197     var flingToTargetEnabled = true
198 
199     /**
200      * If fling to target is enabled, forcefully flinging the object towards a target will cause
201      * it to be attracted to the target and then released immediately, despite never being dragged
202      * within the magnetic field.
203      *
204      * This sets the width of the area considered 'near' enough a target to be considered a fling,
205      * in terms of percent of the target view's width. For example, setting this to 3f means that
206      * flings towards a 100px-wide target will be considered 'near' enough if they're towards the
207      * 300px-wide area around the target.
208      *
209      * Flings whose trajectory intersects the area will be attracted and released - even if the
210      * target view itself isn't intersected:
211      *
212      * |             |
213      * |           0 |
214      * |          /  |
215      * |         /   |
216      * |      X /    |
217      * |.....###.....|
218      *
219      *
220      * Flings towards the target whose trajectories do not intersect the area will be treated as
221      * normal flings and the magnet will leave the object alone:
222      *
223      * |             |
224      * |             |
225      * |   0         |
226      * |  /          |
227      * | /    X      |
228      * |.....###.....|
229      *
230      */
231     var flingToTargetWidthPercent = 3f
232 
233     /**
234      * Sets the minimum velocity (in pixels per second) required to fling an object to the target
235      * without dragging it into the magnetic field.
236      */
237     var flingToTargetMinVelocity = 4000f
238 
239     /**
240      * Sets the minimum velocity (in pixels per second) required to fling un-stuck an object stuck
241      * to the target. If this velocity is reached, the object will be freed even if it wasn't moved
242      * outside the magnetic field radius.
243      */
244     var flingUnstuckFromTargetMinVelocity = 4000f
245 
246     /**
247      * Sets the maximum X velocity above which the object will not stick to the target. Even if the
248      * object is dragged through the magnetic field, it will not stick to the target until the
249      * horizontal velocity is below this value.
250      */
251     var stickToTargetMaxXVelocity = 2000f
252 
253     /**
254      * Enable or disable haptic vibration effects when the object interacts with the magnetic field.
255      *
256      * If you're experiencing crashes when the object enters targets, ensure that you have the
257      * android.permission.VIBRATE permission!
258      */
259     var hapticsEnabled = true
260 
261     /** Default spring configuration to use for animating the object into a target. */
262     var springConfig = PhysicsAnimator.SpringConfig(
263             SpringForce.STIFFNESS_MEDIUM, SpringForce.DAMPING_RATIO_NO_BOUNCY)
264 
265     /**
266      * Spring configuration to use to spring the object into a target specifically when it's flung
267      * towards (rather than dragged near) it.
268      */
269     var flungIntoTargetSpringConfig = springConfig
270 
271     init {
272         initHapticSettingObserver(context)
273     }
274 
275     /**
276      * Adds the provided MagneticTarget to this object. The object will now be attracted to the
277      * target if it strays within its magnetic field or is flung towards it.
278      *
279      * If this target (or its magnetic field) overlaps another target added to this object, the
280      * prior target will take priority.
281      */
282     fun addTarget(target: MagneticTarget) {
283         associatedTargets.add(target)
284         target.updateLocationOnScreen()
285     }
286 
287     /**
288      * Shortcut that accepts a View and a magnetic field radius and adds it as a magnetic target.
289      *
290      * @return The MagneticTarget instance for the given View. This can be used to change the
291      * target's magnetic field radius after it's been added. It can also be added to other
292      * magnetized objects.
293      */
294     fun addTarget(target: View, magneticFieldRadiusPx: Int): MagneticTarget {
295         return MagneticTarget(target, magneticFieldRadiusPx).also { addTarget(it) }
296     }
297 
298     /**
299      * Removes the given target from this object. The target will no longer attract the object.
300      */
301     fun removeTarget(target: MagneticTarget) {
302         associatedTargets.remove(target)
303     }
304 
305     /**
306      * Removes all associated targets from this object.
307      */
308     fun clearAllTargets() {
309         associatedTargets.clear()
310     }
311 
312     /**
313      * Provide this method with all motion events that move the magnetized object. If the
314      * location of the motion events moves within the magnetic field of a target, or indicate a
315      * fling-to-target gesture, this method will return true and you should not move the object
316      * yourself until it returns false again.
317      *
318      * Note that even when this method returns true, you should continue to pass along new motion
319      * events so that we know when the events move back outside the magnetic field area.
320      *
321      * This method will always return false if you haven't set a [magnetListener].
322      */
323     fun maybeConsumeMotionEvent(ev: MotionEvent): Boolean {
324         // Short-circuit if we don't have a listener or any targets, since those are required.
325         if (associatedTargets.size == 0) {
326             return false
327         }
328 
329         // When a gesture begins, recalculate target views' positions on the screen in case they
330         // have changed. Also, clear state.
331         if (ev.action == MotionEvent.ACTION_DOWN) {
332             updateTargetViews()
333 
334             // Clear the velocity tracker and stuck target.
335             velocityTracker.clear()
336             targetObjectIsStuckTo = null
337 
338             // Set the touch down coordinates and reset movedBeyondSlop.
339             touchDown.set(ev.rawX, ev.rawY)
340             movedBeyondSlop = false
341         }
342 
343         // Always pass events to the VelocityTracker.
344         addMovement(ev)
345 
346         // If we haven't yet moved beyond the slop distance, check if we have.
347         if (!movedBeyondSlop) {
348             val dragDistance = hypot(ev.rawX - touchDown.x, ev.rawY - touchDown.y)
349             if (dragDistance > touchSlop) {
350                 // If we're beyond the slop distance, save that and continue.
351                 movedBeyondSlop = true
352             } else {
353                 // Otherwise, don't do anything yet.
354                 return false
355             }
356         }
357 
358         val targetObjectIsInMagneticFieldOf = associatedTargets.firstOrNull { target ->
359             val distanceFromTargetCenter = hypot(
360                     ev.rawX - target.centerOnScreen.x,
361                     ev.rawY - target.centerOnScreen.y)
362             distanceFromTargetCenter < target.magneticFieldRadiusPx
363         }
364 
365         // If we aren't currently stuck to a target, and we're in the magnetic field of a target,
366         // we're newly stuck.
367         val objectNewlyStuckToTarget =
368                 !objectStuckToTarget && targetObjectIsInMagneticFieldOf != null
369 
370         // If we are currently stuck to a target, we're in the magnetic field of a target, and that
371         // target isn't the one we're currently stuck to, then touch events have moved into a
372         // adjacent target's magnetic field.
373         val objectMovedIntoDifferentTarget =
374                 objectStuckToTarget &&
375                         targetObjectIsInMagneticFieldOf != null &&
376                         targetObjectIsStuckTo != targetObjectIsInMagneticFieldOf
377 
378         if (objectNewlyStuckToTarget || objectMovedIntoDifferentTarget) {
379             velocityTracker.computeCurrentVelocity(1000)
380             val velX = velocityTracker.xVelocity
381             val velY = velocityTracker.yVelocity
382 
383             // If the object is moving too quickly within the magnetic field, do not stick it. This
384             // only applies to objects newly stuck to a target. If the object is moved into a new
385             // target, it wasn't moving at all (since it was stuck to the previous one).
386             if (objectNewlyStuckToTarget && abs(velX) > stickToTargetMaxXVelocity) {
387                 return false
388             }
389 
390             // This touch event is newly within the magnetic field - let the listener know, and
391             // animate sticking to the magnet.
392             targetObjectIsStuckTo = targetObjectIsInMagneticFieldOf
393             cancelAnimations()
394             magnetListener.onStuckToTarget(targetObjectIsInMagneticFieldOf!!)
395             animateStuckToTarget(targetObjectIsInMagneticFieldOf, velX, velY, false, null)
396 
397             vibrateIfEnabled(VibrationEffect.EFFECT_HEAVY_CLICK)
398         } else if (targetObjectIsInMagneticFieldOf == null && objectStuckToTarget) {
399             velocityTracker.computeCurrentVelocity(1000)
400 
401             // This touch event is newly outside the magnetic field - let the listener know. It will
402             // move the object out of the target using its own movement logic.
403             cancelAnimations()
404             magnetListener.onUnstuckFromTarget(
405                     targetObjectIsStuckTo!!, velocityTracker.xVelocity, velocityTracker.yVelocity,
406                     wasFlungOut = false)
407             targetObjectIsStuckTo = null
408 
409             vibrateIfEnabled(VibrationEffect.EFFECT_TICK)
410         }
411 
412         // First, check for relevant gestures concluding with an ACTION_UP.
413         if (ev.action == MotionEvent.ACTION_UP) {
414 
415             velocityTracker.computeCurrentVelocity(1000 /* units */)
416             val velX = velocityTracker.xVelocity
417             val velY = velocityTracker.yVelocity
418 
419             // Cancel the magnetic animation since we might still be springing into the magnetic
420             // target, but we're about to fling away or release.
421             cancelAnimations()
422 
423             if (objectStuckToTarget) {
424                 if (-velY > flingUnstuckFromTargetMinVelocity) {
425                     // If the object is stuck, but it was forcefully flung away from the target in
426                     // the upward direction, tell the listener so the object can be animated out of
427                     // the target.
428                     magnetListener.onUnstuckFromTarget(
429                             targetObjectIsStuckTo!!, velX, velY, wasFlungOut = true)
430                 } else {
431                     // If the object is stuck and not flung away, it was released inside the target.
432                     magnetListener.onReleasedInTarget(targetObjectIsStuckTo!!)
433                     vibrateIfEnabled(VibrationEffect.EFFECT_HEAVY_CLICK)
434                 }
435 
436                 // Either way, we're no longer stuck.
437                 targetObjectIsStuckTo = null
438                 return true
439             }
440 
441             // The target we're flinging towards, or null if we're not flinging towards any target.
442             val flungToTarget = associatedTargets.firstOrNull { target ->
443                 isForcefulFlingTowardsTarget(target, ev.rawX, ev.rawY, velX, velY)
444             }
445 
446             if (flungToTarget != null) {
447                 // If this is a fling-to-target, animate the object to the magnet and then release
448                 // it.
449                 magnetListener.onStuckToTarget(flungToTarget)
450                 targetObjectIsStuckTo = flungToTarget
451 
452                 animateStuckToTarget(flungToTarget, velX, velY, true) {
453                     magnetListener.onReleasedInTarget(flungToTarget)
454                     targetObjectIsStuckTo = null
455                     vibrateIfEnabled(VibrationEffect.EFFECT_HEAVY_CLICK)
456                 }
457 
458                 return true
459             }
460 
461             // If it's not either of those things, we are not interested.
462             return false
463         }
464 
465         return objectStuckToTarget // Always consume touch events if the object is stuck.
466     }
467 
468     /** Plays the given vibration effect if haptics are enabled. */
469     @SuppressLint("MissingPermission")
470     private fun vibrateIfEnabled(effectId: Int) {
471         if (hapticsEnabled && systemHapticsEnabled) {
472             vibrator.vibrate(VibrationEffect.createPredefined(effectId))
473         }
474     }
475 
476     /** Adds the movement to the velocity tracker using raw coordinates. */
477     private fun addMovement(event: MotionEvent) {
478         // Add movement to velocity tracker using raw screen X and Y coordinates instead
479         // of window coordinates because the window frame may be moving at the same time.
480         val deltaX = event.rawX - event.x
481         val deltaY = event.rawY - event.y
482         event.offsetLocation(deltaX, deltaY)
483         velocityTracker.addMovement(event)
484         event.offsetLocation(-deltaX, -deltaY)
485     }
486 
487     /** Animates sticking the object to the provided target with the given start velocities.  */
488     private fun animateStuckToTargetInternal(
489         target: MagneticTarget,
490         velX: Float,
491         velY: Float,
492         flung: Boolean,
493         after: (() -> Unit)? = null
494     ) {
495         target.updateLocationOnScreen()
496         getLocationOnScreen(underlyingObject, objectLocationOnScreen)
497 
498         // Calculate the difference between the target's center coordinates and the object's.
499         // Animating the object's x/y properties by these values will center the object on top
500         // of the magnetic target.
501         val xDiff = target.centerOnScreen.x -
502                 getWidth(underlyingObject) / 2f - objectLocationOnScreen[0]
503         val yDiff = target.centerOnScreen.y -
504                 getHeight(underlyingObject) / 2f - objectLocationOnScreen[1]
505 
506         val springConfig = if (flung) flungIntoTargetSpringConfig else springConfig
507 
508         cancelAnimations()
509 
510         // Animate to the center of the target.
511         animator
512                 .spring(xProperty, xProperty.getValue(underlyingObject) + xDiff, velX,
513                         springConfig)
514                 .spring(yProperty, yProperty.getValue(underlyingObject) + yDiff, velY,
515                         springConfig)
516 
517         if (physicsAnimatorUpdateListener != null) {
518             animator.addUpdateListener(physicsAnimatorUpdateListener!!)
519         }
520 
521         if (physicsAnimatorEndListener != null) {
522             animator.addEndListener(physicsAnimatorEndListener!!)
523         }
524 
525         if (after != null) {
526             animator.withEndActions(after)
527         }
528 
529         animator.start()
530     }
531 
532     /**
533      * Whether or not the provided values match a 'fast fling' towards the provided target. If it
534      * does, we consider it a fling-to-target gesture.
535      */
536     private fun isForcefulFlingTowardsTarget(
537         target: MagneticTarget,
538         rawX: Float,
539         rawY: Float,
540         velX: Float,
541         velY: Float
542     ): Boolean {
543         if (!flingToTargetEnabled) {
544             return false
545         }
546 
547         // Whether velocity is sufficient, depending on whether we're flinging into a target at the
548         // top or the bottom of the screen.
549         val velocitySufficient =
550                 if (rawY < target.centerOnScreen.y) velY > flingToTargetMinVelocity
551                 else velY < flingToTargetMinVelocity
552 
553         if (!velocitySufficient) {
554             return false
555         }
556 
557         // Whether the trajectory of the fling intersects the target area.
558         var targetCenterXIntercept = rawX
559 
560         // Only do math if the X velocity is non-zero, otherwise X won't change.
561         if (velX != 0f) {
562             // Rise over run...
563             val slope = velY / velX
564             // ...y = mx + b, b = y / mx...
565             val yIntercept = rawY - slope * rawX
566 
567             // ...calculate the x value when y = the target's y-coordinate.
568             targetCenterXIntercept = (target.centerOnScreen.y - yIntercept) / slope
569         }
570 
571         // The width of the area we're looking for a fling towards.
572         val targetAreaWidth = target.targetView.width * flingToTargetWidthPercent
573 
574         // Velocity was sufficient, so return true if the intercept is within the target area.
575         return targetCenterXIntercept > target.centerOnScreen.x - targetAreaWidth / 2 &&
576                 targetCenterXIntercept < target.centerOnScreen.x + targetAreaWidth / 2
577     }
578 
579     /** Cancel animations on this object's x/y properties. */
580     internal fun cancelAnimations() {
581         animator.cancel(xProperty, yProperty)
582     }
583 
584     /** Updates the locations on screen of all of the [associatedTargets]. */
585     internal fun updateTargetViews() {
586         associatedTargets.forEach { it.updateLocationOnScreen() }
587 
588         // Update the touch slop, since the configuration may have changed.
589         if (associatedTargets.size > 0) {
590             touchSlop =
591                     ViewConfiguration.get(associatedTargets[0].targetView.context).scaledTouchSlop
592         }
593     }
594 
595     /**
596      * Represents a target view with a magnetic field radius and cached center-on-screen
597      * coordinates.
598      *
599      * Instances of MagneticTarget are passed to a MagnetizedObject's [addTarget], and can then
600      * attract the object if it's dragged near or flung towards it. MagneticTargets can be added to
601      * multiple objects.
602      */
603     class MagneticTarget(
604         val targetView: View,
605         var magneticFieldRadiusPx: Int
606     ) {
607         val centerOnScreen = PointF()
608 
609         private val tempLoc = IntArray(2)
610 
611         fun updateLocationOnScreen() {
612             targetView.post {
613                 targetView.getLocationOnScreen(tempLoc)
614 
615                 // Add half of the target size to get the center, and subtract translation since the
616                 // target could be animating in while we're doing this calculation.
617                 centerOnScreen.set(
618                         tempLoc[0] + targetView.width / 2f - targetView.translationX,
619                         tempLoc[1] + targetView.height / 2f - targetView.translationY)
620             }
621         }
622     }
623 
624     companion object {
625 
626         /**
627          * Whether the HAPTIC_FEEDBACK_ENABLED setting is true.
628          *
629          * We put it in the companion object because we need to register a settings observer and
630          * [MagnetizedObject] doesn't have an obvious lifecycle so we don't have a good time to
631          * remove that observer. Since this settings is shared among all instances we just let all
632          * instances read from this value.
633          */
634         private var systemHapticsEnabled = false
635         private var hapticSettingObserverInitialized = false
636 
637         private fun initHapticSettingObserver(context: Context) {
638             if (hapticSettingObserverInitialized) {
639                 return
640             }
641 
642             val hapticSettingObserver =
643                     object : ContentObserver(Handler.getMain()) {
644                         override fun onChange(selfChange: Boolean) {
645                             systemHapticsEnabled =
646                                     Settings.System.getIntForUser(
647                                             context.contentResolver,
648                                             Settings.System.HAPTIC_FEEDBACK_ENABLED,
649                                             0,
650                                             UserHandle.USER_CURRENT) != 0
651                         }
652                     }
653 
654             context.contentResolver.registerContentObserver(
655                     Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_ENABLED),
656                     true /* notifyForDescendants */, hapticSettingObserver)
657 
658             // Trigger the observer once to initialize systemHapticsEnabled.
659             hapticSettingObserver.onChange(false /* selfChange */)
660             hapticSettingObserverInitialized = true
661         }
662 
663         /**
664          * Magnetizes the given view. Magnetized views are attracted to one or more magnetic
665          * targets. Magnetic targets attract objects that are dragged near them, and hold them there
666          * unless they're moved away or released. Releasing objects inside a magnetic target
667          * typically performs an action on the object.
668          *
669          * Magnetized views can also be flung to targets, which will result in the view being pulled
670          * into the target and released as if it was dragged into it.
671          *
672          * To use the returned MagnetizedObject<View> instance, first set [magnetListener] to
673          * receive event callbacks. In your touch handler, pass all MotionEvents that move this view
674          * to [maybeConsumeMotionEvent]. If that method returns true, consider the event consumed by
675          * MagnetizedObject and don't move the view unless it begins returning false again.
676          *
677          * The view will be moved via translationX/Y properties, and its
678          * width/height will be determined via getWidth()/getHeight(). If you are animating
679          * something other than a view, or want to position your view using properties other than
680          * translationX/Y, implement an instance of [MagnetizedObject].
681          *
682          * Note that the magnetic library can't re-order your view automatically. If the view
683          * renders on top of the target views, it will obscure the target when it sticks to it.
684          * You'll want to bring the view to the front in [MagnetListener.onStuckToTarget].
685          */
686         @JvmStatic
687         fun <T : View> magnetizeView(view: T): MagnetizedObject<T> {
688             return object : MagnetizedObject<T>(
689                     view.context,
690                     view,
691                     DynamicAnimation.TRANSLATION_X,
692                     DynamicAnimation.TRANSLATION_Y) {
693                 override fun getWidth(underlyingObject: T): Float {
694                     return underlyingObject.width.toFloat()
695                 }
696 
697                 override fun getHeight(underlyingObject: T): Float {
698                     return underlyingObject.height.toFloat() }
699 
700                 override fun getLocationOnScreen(underlyingObject: T, loc: IntArray) {
701                     underlyingObject.getLocationOnScreen(loc)
702                 }
703             }
704         }
705     }
706 }