-
Notifications
You must be signed in to change notification settings - Fork 500
/
SplitScreenManager.kt
72 lines (65 loc) · 2.77 KB
/
SplitScreenManager.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package org.oppia.android.app.utility
import android.content.Context
import android.content.res.Resources
import org.oppia.android.R
import org.oppia.android.app.model.EphemeralQuestion
import org.oppia.android.app.model.EphemeralState
import org.oppia.android.app.player.state.StateFragment
import org.oppia.android.app.player.state.itemviewmodel.SplitScreenInteractionIds
import org.oppia.android.app.topic.questionplayer.QuestionPlayerFragment
import javax.inject.Inject
import kotlin.math.pow
import kotlin.math.sqrt
private const val MINIMUM_DIAGONAL_WIDTH = 7.0
/**
* A helper class that is used to detect whether to split the screen in [QuestionPlayerFragment]
* and [StateFragment] or not based on multiple factors.
*/
class SplitScreenManager @Inject constructor(
private val context: Context,
@SplitScreenInteractionIds private val splitInteractionIds: Set<@JvmSuppressWildcards String>
) {
/**
* The actual function that decides whether to split or not.
*
* @param[interactionId] the id of the interaction of the state inside the [EphemeralState] or
* [EphemeralQuestion].
* @return `true` if the screen should be split, `false` otherwise.
*/
fun shouldSplitScreen(interactionId: String): Boolean {
return isDeviceLargeEnoughForSplitScreen() && isInteractionSplittable(interactionId)
}
/**
* @return The physical diagonal size of the device in inches.
*/
// https://stackoverflow.com/a/19156339/6628335
private fun computeDeviceDiagonalSizeInches(): Double {
val displayMetrics = Resources.getSystem().displayMetrics
val widthInPixel = displayMetrics.widthPixels
val heightInPixel = displayMetrics.heightPixels
val widthInInch = widthInPixel.toDouble() / displayMetrics.xdpi.toDouble()
val heightInInch = heightInPixel.toDouble() / displayMetrics.ydpi.toDouble()
val x = widthInInch.pow(2.0)
val y = heightInInch.pow(2.0)
return sqrt(x + y)
}
/**
* Checks the device efficiency for splitting based on the **density** and **diagonal physical size**.
* @return `true` if the device is splittable, `false` otherwise.
*/
private fun isDeviceLargeEnoughForSplitScreen(): Boolean {
val shouldSplit = context.resources.getBoolean(R.bool.shouldCheckForSplittingInteraction)
return shouldSplit && computeDeviceDiagonalSizeInches() >= MINIMUM_DIAGONAL_WIDTH
}
/**
* Checks whether the given interaction is suitable for splitting the screen or not.
* @param[interactionId] the id of the interaction of the state inside the [EphemeralState] or
* [EphemeralQuestion].
* @return `true` if the interaction is suitable for splitting, `false` otherwise.
*/
private fun isInteractionSplittable(interactionId: String): Boolean {
return splitInteractionIds.find {
it == interactionId
} != null
}
}