Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 5644ade

Browse files
authored
update sample (#59)
1 parent b1b35db commit 5644ade

File tree

1 file changed

+11
-24
lines changed
  • projects/FoldableExperiments/app/src/main/java/com/example/experiments

1 file changed

+11
-24
lines changed

projects/FoldableExperiments/app/src/main/java/com/example/experiments/MainActivity.kt

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ import androidx.window.WindowManager
3434
import java.util.concurrent.Executor
3535

3636
class MainActivity : Activity() {
37-
private var guideline: Guideline? = null
38-
private var motionLayout: MotionLayout? = null
37+
private lateinit var motionLayout: MotionLayout
3938

4039
override fun onCreate(savedInstanceState: Bundle?) {
4140
super.onCreate(savedInstanceState)
@@ -54,12 +53,11 @@ class MainActivity : Activity() {
5453
}
5554
DeviceState.POSTURE_HALF_OPENED -> {
5655
// The foldable device's hinge is in an intermediate position between opened and closed state.
57-
val displayFeatures = windowManager.windowLayoutInfo.displayFeatures
58-
halfOpened(displayFeatures)
56+
var fold = foldPosition(motionLayout, windowManager.windowLayoutInfo.displayFeatures)
57+
ConstraintLayout.getSharedValues().fireNewValue(R.id.fold, fold)
5958
}
6059
DeviceState.POSTURE_OPENED -> {
6160
// The foldable device is completely open, the screen space that is presented to the user is flat.
62-
// motionLayout?.transitionToStart()
6361
ConstraintLayout.getSharedValues().fireNewValue(R.id.fold, 0);
6462
}
6563
DeviceState.POSTURE_FLIPPED -> {
@@ -75,35 +73,24 @@ class MainActivity : Activity() {
7573
callback
7674
)
7775

78-
// the idea here is to have two states in motionlayout, open and fold.
79-
// when the foldable device is open, we transition to the state open, and we do the opposite on fold.
80-
// when going to fold, we also get the position of the fold, and set the guideline we use in this example at
81-
// the position of the fold.
82-
// TODO: move this to a helper object
83-
8476
setContentView(R.layout.activity_main2)
8577
motionLayout = findViewById<MotionLayout>(R.id.root)
86-
// guideline = findViewById<Guideline>(R.id.fold)
8778
}
8879

89-
fun halfOpened(displayFeatures: List<DisplayFeature>) {
80+
/**
81+
* Returns the position of the fold relative to the view
82+
*/
83+
fun foldPosition(view: View, displayFeatures: List<DisplayFeature>) : Int {
9084
for (feature in displayFeatures) {
9185
if (feature.type != TYPE_FOLD) {
9286
continue
9387
}
94-
val splitRect = motionLayout?.let { getFeatureBoundsInWindow(feature, it) } ?: continue
95-
// var constraintSet = motionLayout?.getConstraintSet(R.id.start)
96-
//// constraintSet?.setGuidelineEnd(guideline?.id!!, 0)
97-
// var constraintSetEnd = motionLayout?.getConstraintSet(R.id.end)
98-
//// constraintSetEnd?.setGuidelineBegin(guideline?.id!!, splitRect.left)
99-
// motionLayout?.updateState(R.id.start, constraintSet)
100-
// motionLayout?.updateState(R.id.end, constraintSetEnd)
101-
// motionLayout?.transitionToEnd()
102-
var h : Int? = motionLayout?.height?.minus(splitRect.top)
103-
h?.let {
104-
ConstraintLayout.getSharedValues().fireNewValue(R.id.fold, h)
88+
val splitRect = getFeatureBoundsInWindow(feature, view)
89+
splitRect?.let {
90+
return view.height.minus(splitRect.top)
10591
}
10692
}
93+
return 0
10794
}
10895

10996
/**

0 commit comments

Comments
 (0)