15
15
*/
16
16
package com.android.developers.androidify.camera
17
17
18
+ import android.content.Context
19
+ import android.hardware.display.DisplayManager
20
+ import android.view.Surface.ROTATION_90
18
21
import androidx.compose.foundation.background
19
22
import androidx.compose.foundation.border
23
+ import androidx.compose.foundation.layout.Arrangement
20
24
import androidx.compose.foundation.layout.Box
21
25
import androidx.compose.foundation.layout.BoxWithConstraints
22
26
import androidx.compose.foundation.layout.Column
@@ -33,12 +37,18 @@ import androidx.compose.foundation.layout.safeDrawingPadding
33
37
import androidx.compose.foundation.layout.size
34
38
import androidx.compose.foundation.layout.width
35
39
import androidx.compose.runtime.Composable
40
+ import androidx.compose.runtime.getValue
41
+ import androidx.compose.runtime.mutableStateOf
42
+ import androidx.compose.runtime.remember
43
+ import androidx.compose.runtime.setValue
36
44
import androidx.compose.ui.Alignment
37
45
import androidx.compose.ui.Modifier
38
46
import androidx.compose.ui.graphics.Color
47
+ import androidx.compose.ui.platform.LocalContext
39
48
import androidx.compose.ui.tooling.preview.Preview
40
49
import androidx.compose.ui.tooling.preview.PreviewParameter
41
50
import androidx.compose.ui.unit.dp
51
+ import androidx.lifecycle.compose.LifecycleStartEffect
42
52
import com.android.developers.androidify.theme.AndroidifyTheme
43
53
import com.android.developers.androidify.theme.TertiaryContainer
44
54
import com.android.developers.androidify.util.FoldablePreviewParameters
@@ -61,6 +71,23 @@ internal fun CameraLayout(
61
71
isTabletop : Boolean = false,
62
72
modifier : Modifier = Modifier ,
63
73
) {
74
+ val mContext = LocalContext .current
75
+ var isCameraLeft by remember { mutableStateOf(false ) }
76
+ LifecycleStartEffect (Unit ){
77
+ val displayManager = mContext.getSystemService(Context .DISPLAY_SERVICE ) as DisplayManager
78
+ val displayListener = object : DisplayManager .DisplayListener {
79
+ override fun onDisplayChanged (displayId : Int ) {
80
+ val rotation = displayManager.getDisplay(displayId).rotation
81
+ isCameraLeft = rotation == ROTATION_90
82
+ }
83
+ override fun onDisplayAdded (displayId : Int ) {}
84
+ override fun onDisplayRemoved (displayId : Int ) {}
85
+ }
86
+ displayManager.registerDisplayListener(displayListener, null )
87
+ onStopOrDispose {
88
+ displayManager.unregisterDisplayListener(displayListener)
89
+ }
90
+ }
64
91
BoxWithConstraints (
65
92
modifier
66
93
.fillMaxSize()
@@ -88,6 +115,7 @@ internal fun CameraLayout(
88
115
zoomButton,
89
116
guideText,
90
117
guide,
118
+ isCameraLeft
91
119
)
92
120
93
121
this .maxWidth > maxHeight && allowsFullContent() -> CompactHorizontalCameraLayout (
@@ -314,15 +342,25 @@ private fun MediumHorizontalCameraLayout(
314
342
zoomButton : @Composable (modifier: Modifier ) -> Unit ,
315
343
guideText : @Composable (modifier: Modifier ) -> Unit ,
316
344
guide : @Composable (modifier: Modifier ) -> Unit ,
345
+ isCameraLeft : Boolean = false,
317
346
modifier : Modifier = Modifier ,
318
347
) {
319
348
Row (modifier.fillMaxSize()) {
320
- VerticalControlsLayout (
321
- captureButton,
322
- flipCameraButton,
323
- zoomButton = null ,
324
- Modifier .weight(1f ),
325
- )
349
+ if (isCameraLeft) {
350
+ Spacer (
351
+ Modifier
352
+ .fillMaxHeight()
353
+ .weight(1f ),
354
+ )
355
+ } else {
356
+ VerticalControlsLayout (
357
+ captureButton,
358
+ flipCameraButton,
359
+ zoomButton = null ,
360
+ Modifier .weight(1f ),
361
+ isCameraLeft,
362
+ )
363
+ }
326
364
327
365
Box (
328
366
Modifier
@@ -342,11 +380,21 @@ private fun MediumHorizontalCameraLayout(
342
380
zoomButton(Modifier )
343
381
}
344
382
}
345
- Spacer (
346
- Modifier
347
- .fillMaxHeight()
348
- .weight(1f ),
349
- )
383
+ if (isCameraLeft) {
384
+ VerticalControlsLayout (
385
+ captureButton,
386
+ flipCameraButton,
387
+ zoomButton = null ,
388
+ Modifier .weight(1f ),
389
+ isCameraLeft,
390
+ )
391
+ } else {
392
+ Spacer (
393
+ Modifier
394
+ .fillMaxHeight()
395
+ .weight(1f ),
396
+ )
397
+ }
350
398
}
351
399
}
352
400
@@ -418,20 +466,34 @@ private fun VerticalControlsLayout(
418
466
flipCameraButton : (@Composable (modifier: Modifier ) -> Unit )? ,
419
467
zoomButton : (@Composable (modifier: Modifier ) -> Unit )? ,
420
468
modifier : Modifier = Modifier ,
469
+ isCameraLeft : Boolean = false,
421
470
) {
422
471
Row (
423
472
modifier = modifier,
424
473
verticalAlignment = Alignment .CenterVertically ,
474
+ horizontalArrangement = if (isCameraLeft) Arrangement .End else Arrangement .Start ,
425
475
) {
426
- Column (horizontalAlignment = Alignment .CenterHorizontally ) {
427
- Box (Modifier .weight(1f ), contentAlignment = Alignment .Center ) {
428
- if (flipCameraButton != null ) flipCameraButton(Modifier )
476
+ if (isCameraLeft){
477
+ if (zoomButton != null ) zoomButton(Modifier )
478
+ Spacer (Modifier .width(12 .dp))
479
+ Column (horizontalAlignment = Alignment .CenterHorizontally ) {
480
+ Box (Modifier .weight(1f ), contentAlignment = Alignment .Center ) {
481
+ if (flipCameraButton != null ) flipCameraButton(Modifier )
482
+ }
483
+ captureButton(Modifier )
484
+ Spacer (modifier = Modifier .weight(1f ))
429
485
}
430
- captureButton(Modifier )
431
- Spacer (modifier = Modifier .weight(1f ))
486
+ }else {
487
+ Column (horizontalAlignment = Alignment .CenterHorizontally ) {
488
+ Box (Modifier .weight(1f ), contentAlignment = Alignment .Center ) {
489
+ if (flipCameraButton != null ) flipCameraButton(Modifier )
490
+ }
491
+ captureButton(Modifier )
492
+ Spacer (modifier = Modifier .weight(1f ))
493
+ }
494
+ Spacer (Modifier .width(12 .dp))
495
+ if (zoomButton != null ) zoomButton(Modifier )
432
496
}
433
- Spacer (Modifier .width(12 .dp))
434
- if (zoomButton != null ) zoomButton(Modifier )
435
497
}
436
498
}
437
499
0 commit comments