1 /*
2  * Copyright 2023 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 
17 package androidx.core.haptics.signal
18 
19 import androidx.core.haptics.device.HapticDeviceProfile
20 
21 /**
22  * A [FallbackChainSignal] is a resolvable haptic signal that returns the first supported
23  * [HapticSignal] from a selection list, or null if no effect is supported by the device.
24  *
25  * A haptic fallback chain is composed of an ordered list of [ResolvableSignal], which allows
26  * composing this with generic haptic signal extensions, including other fallback chains.
27  *
28  * @sample androidx.core.haptics.samples.HapticFallbackChainOfSignals
29  * @see HapticDeviceProfile
30  */
31 public class FallbackChainSignal(
32 
33     /** The ordered list of haptic signals that defines the fallback chain. */
34     public val signals: List<ResolvableSignal>,
35 ) : ResolvableSignal {
36 
37     public companion object {
38 
39         /**
40          * Returns a [FallbackChainSignal] with given signals.
41          *
42          * @sample androidx.core.haptics.samples.HapticFallbackChainOfSignals
43          */
44         @JvmStatic
fallbackChainOfnull45         public fun fallbackChainOf(vararg signals: ResolvableSignal): FallbackChainSignal =
46             FallbackChainSignal(signals.toList())
47     }
48 
49     override fun resolve(deviceProfile: HapticDeviceProfile): HapticSignal? =
50         signals
51             .asSequence()
52             .mapNotNull { it.resolve(deviceProfile) }
<lambda>null53             .firstOrNull { deviceProfile.supports(it) }
54 }
55