1 /*
2  * Copyright (C) 2024 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.google.android.msdl.data.repository
17 
18 import androidx.annotation.VisibleForTesting
19 import com.google.android.msdl.data.model.HapticToken
20 import com.google.android.msdl.data.model.SoundToken
21 
22 /**
23  * A repository of data for [HapticToken] and [SoundToken].
24  *
25  * The principle behind this repository is to hold the data for all tokens as a cache in memory.
26  * This is only suitable if the number of tokens and the data stored is manageable. The purpose of
27  * this design choice is to provide fast and easy access to the data when required to be played by
28  * UI interactions.
29  */
30 sealed interface MSDLRepository {
31 
32     /**
33      * Get the [MSDLHapticData] that corresponds to the given haptic reference token. This function
34      * needs to be fast since it will be called repeatedly to deliver feedback. If necessary, a
35      * caching strategy should be applied.
36      *
37      * @param[hapticToken] The [HapticToken] that points to the data.
38      * @return the data that corresponds to the token at the time this function is called.
39      */
getHapticDatanull40     fun getHapticData(hapticToken: HapticToken): MSDLHapticData?
41 
42     /**
43      * Get the [MSDLSoundData] that corresponds to the given sound reference token. This function
44      * needs to be fast since it will be called repeatedly to deliver feedback. If necessary, a
45      * caching strategy should be applied.
46      *
47      * @param[soundToken] The [SoundToken] that points to the data.
48      * @return the data that corresponds to the token at the time this function is called.
49      */
50     fun getAudioData(soundToken: SoundToken): MSDLSoundData?
51 
52     companion object {
53 
54         @VisibleForTesting fun createRepository(): MSDLRepository = MSDLRepositoryImpl()
55     }
56 }
57 
58 /** Representation of data contained in a [MSDLRepository] */
interfacenull59 fun interface MSDLHapticData {
60 
61     /** Retrieve the haptic data */
62     fun get(): Any?
63 }
64 
65 /** Representation of data contained in a [MSDLRepository] */
interfacenull66 fun interface MSDLSoundData {
67 
68     /** Retrieve the sound data */
69     fun get(): Any?
70 }
71