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
supportsTabletop : Boolean = supportsTabletop(),
62
72
isTabletop : Boolean = false,
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
@@ -419,20 +467,34 @@ private fun VerticalControlsLayout(
419
467
flipCameraButton : (@Composable (modifier: Modifier ) -> Unit )? ,
420
468
zoomButton : (@Composable (modifier: Modifier ) -> Unit )? ,
421
469
modifier : Modifier = Modifier ,
470
+ isCameraLeft : Boolean = false,
422
471
) {
423
472
Row (
424
473
modifier = modifier,
425
474
verticalAlignment = Alignment .CenterVertically ,
475
+ horizontalArrangement = if (isCameraLeft) Arrangement .End else Arrangement .Start ,
426
476
) {
427
- Column (horizontalAlignment = Alignment .CenterHorizontally ) {
428
- Box (Modifier .weight(1f ), contentAlignment = Alignment .Center ) {
429
- if (flipCameraButton != null ) flipCameraButton(Modifier )
477
+ if (isCameraLeft){
478
+ if (zoomButton != null ) zoomButton(Modifier )
479
+ Spacer (Modifier .width(12 .dp))
480
+ Column (horizontalAlignment = Alignment .CenterHorizontally ) {
481
+ Box (Modifier .weight(1f ), contentAlignment = Alignment .Center ) {
482
+ if (flipCameraButton != null ) flipCameraButton(Modifier )
483
+ }
484
+ captureButton(Modifier )
485
+ Spacer (modifier = Modifier .weight(1f ))
430
486
}
431
- captureButton(Modifier )
432
- Spacer (modifier = Modifier .weight(1f ))
487
+ }else {
488
+ Column (horizontalAlignment = Alignment .CenterHorizontally ) {
489
+ Box (Modifier .weight(1f ), contentAlignment = Alignment .Center ) {
490
+ if (flipCameraButton != null ) flipCameraButton(Modifier )
491
+ }
492
+ captureButton(Modifier )
493
+ Spacer (modifier = Modifier .weight(1f ))
494
+ }
495
+ Spacer (Modifier .width(12 .dp))
496
+ if (zoomButton != null ) zoomButton(Modifier )
433
497
}
434
- Spacer (Modifier .width(12 .dp))
435
- if (zoomButton != null ) zoomButton(Modifier )
436
498
}
437
499
}
438
500
0 commit comments