[go: nahoru, domu]

[Text Selection] Add HapticFeedBack to Selection.

Bug: 142913458
Test: Manually tested on device.
Test: ./gradlew test
Test: ./gradlew buildOnServer
Test: ./gradlew :ui:ui-framework:connectedAndroidTest
Test: ./gradlew :ui:ui-platform:connectedAndroidTest
Change-Id: I8d52aa614aa6e40cec5a7adce3a2cc1587315759
diff --git a/ui/ui-framework/src/main/java/androidx/ui/core/selection/SelectionManager.kt b/ui/ui-framework/src/main/java/androidx/ui/core/selection/SelectionManager.kt
index 0c3ed1b..6e328e0 100644
--- a/ui/ui-framework/src/main/java/androidx/ui/core/selection/SelectionManager.kt
+++ b/ui/ui-framework/src/main/java/androidx/ui/core/selection/SelectionManager.kt
@@ -19,6 +19,8 @@
 import androidx.ui.core.LayoutCoordinates
 import androidx.ui.core.gesture.DragObserver
 import androidx.ui.core.gesture.LongPressDragObserver
+import androidx.ui.core.hapticfeedback.HapticFeedback
+import androidx.ui.core.hapticfeedback.HapticFeedbackType
 import androidx.ui.unit.PxPosition
 import androidx.ui.unit.px
 
@@ -39,6 +41,11 @@
     var onSelectionChange: (Selection?) -> Unit = {}
 
     /**
+     * [HapticFeedback] handle to perform haptic feedback.
+     */
+    var hapticFeedBack: HapticFeedback? = null
+
+    /**
      * Layout Coordinates of the selection container.
      */
     lateinit var containerLayoutCoordinates: LayoutCoordinates
@@ -70,6 +77,7 @@
      * @param startPosition [PxPosition] for the start of the selection
      * @param endPosition [PxPosition] for the end of the selection
      * @param longPress the selection is a result of long press
+     * @param previousSelection previous selection
      *
      * @return [Selection] object which is constructed by combining all Composables that are
      * selected.
@@ -79,11 +87,12 @@
         startPosition: PxPosition,
         endPosition: PxPosition,
         longPress: Boolean = false,
-        selection: Selection? = null,
+        previousSelection: Selection? = null,
         isStartHandle: Boolean = true
     ): Selection? {
         val handlers = selectionRegistrar.selectables
-        return handlers.fold(null) { mergedSelection: Selection?,
+
+        val newSelection = handlers.fold(null) { mergedSelection: Selection?,
                                           handler: Selectable ->
             merge(
                 mergedSelection,
@@ -92,11 +101,15 @@
                     endPosition = endPosition,
                     containerLayoutCoordinates = containerLayoutCoordinates,
                     longPress = longPress,
-                    previousSelection = selection,
+                    previousSelection = previousSelection,
                     isStartHandle = isStartHandle
                 )
             )
         }
+        if (previousSelection != newSelection) hapticFeedBack?.performHapticFeedback(
+            HapticFeedbackType.TextHandleMove
+        )
+        return newSelection
     }
 
     // This is for PressGestureDetector to cancel the selection.
@@ -105,7 +118,8 @@
         // cancel their individual selection.
         mergeSelections(
             startPosition = PxPosition((-1).px, (-1).px),
-            endPosition = PxPosition((-1).px, (-1).px)
+            endPosition = PxPosition((-1).px, (-1).px),
+            previousSelection = selection
         )
         if (selection != null) onSelectionChange(null)
     }
@@ -116,7 +130,8 @@
             val newSelection = mergeSelections(
                 startPosition = pxPosition,
                 endPosition = pxPosition,
-                longPress = true
+                longPress = true,
+                previousSelection = selection
             )
             if (newSelection != selection) onSelectionChange(newSelection)
             dragBeginPosition = pxPosition
@@ -139,7 +154,7 @@
                 startPosition = dragBeginPosition,
                 endPosition = dragBeginPosition + dragTotalDistance,
                 longPress = true,
-                selection = selection
+                previousSelection = selection
             )
 
             if (newSelection != selection) onSelectionChange(newSelection)
@@ -218,7 +233,7 @@
                 val finalSelection = mergeSelections(
                     startPosition = currentStart,
                     endPosition = currentEnd,
-                    selection = selection,
+                    previousSelection = selection,
                     isStartHandle = isStartHandle
                 )
                 onSelectionChange(finalSelection)