Samsung Galaxy Fold gadgets have taken the cellular trade by storm, providing customers a revolutionary solution to work together with their purposes. One in every of their key options is the rear show mode that allows customers to proceed their duties seamlessly on the quilt show whereas the principle show stays turned off. Jetpack WindowManager has launched APIs to allow this mode programmatically, and ranging from One UI 6.0, builders can now make the most of these APIs to combine rear show mode into their purposes, enhancing usability and maximizing the potential of foldable gadgets.
On this weblog put up, we dive deeper into implementing Jetpack WindowManager’s rear show mode in a digicam software. By leveraging this mode, customers can take selfies with superior picture high quality utilizing the rear digicam as a substitute of the entrance digicam. Be a part of us as we discover the thrilling prospects of foldable expertise and uncover methods to optimize your purposes for the Samsung Galaxy Fold.
You’ll be able to obtain the pattern digicam software right here.
Step 1: Add the WindowManager library into the undertaking
WindowManager, a Jetpack library launched by Google, helps rear show mode ranging from model 1.2.0-beta03. So as to add the WindowManager library, go to Gradle Scripts > construct.gradle (Module: app) and enter the next to the dependencies block:
implementation "androidx.window:window:1.3.0"
Step 2: Implement the WindowAreaSessionCallback interface in MainActivity.kt
The WindowAreaSessionCallback interface updates an Exercise about when the WindowAreaSession is began and ended. Utilizing the onSessionStarted
methodology, this interface offers the present WindowAreaSession as quickly as a brand new window session is began.
class MainActivity : AppCompatActivity() , WindowAreaSessionCallback {
…
override enjoyable onSessionEnded(t: Throwable?) {
if(t != null) {
println("One thing was damaged: ${t.message}")
}
}
override enjoyable onSessionStarted(session: WindowAreaSession) {
}
}
Step 3: Declare variables
The WindowAreaController offers details about the transferring home windows between the quilt show and the principle show of the Galaxy Fold gadget.
The WindowAreaSession interface offers an energetic window session within the onSessionStarted
methodology.
WindowAreaInfo represents the present state of a window space. It offers a token which is used later to activate rear show mode.
WindowAreaCapability.Standing represents the provision and functionality standing of the window space outlined by the WindowAreaInfo object. We make the most of this standing to alter the UI of our software. The standing of the Galaxy Fold gadget will be one of many following:
-
WINDOW_AREA_STATUS_ACTIVE: if the quilt show is presently energetic.
-
WINDOW_AREA_STATUS_AVAILABLE: if the quilt show is obtainable to be enabled.
-
WINDOW_AREA_STATUS_UNAVAILABLE: if the quilt show is presently not obtainable to be enabled.
-
WINDOW_AREA_STATUS_UNSUPPORTED: if the Galaxy Fold gadget is working on Android 13 or decrease.
personal lateinit var windowAreaController: WindowAreaController
personal var windowAreaSession: WindowAreaSession? = null
personal var windowAreaInfo: WindowAreaInfo? = null
personal var capabilityStatus: WindowAreaCapability.Standing = WindowAreaCapability.Standing.WINDOW_AREA_STATUS_UNSUPPORTED
personal val operation = WindowAreaCapability.Operation.OPERATION_TRANSFER_ACTIVITY_TO_AREA
Step 4: Create an occasion of WindowAreaController within the onCreate
methodology
windowAreaController = WindowAreaController.getOrCreate()
Step 5: Arrange a movement to get data from WindowAreaController
Within the onCreate()
methodology, add a lifecycle-aware coroutine to question the record of obtainable WindowAreaInfo objects and their standing. The coroutine executes every time the lifecycle begins.
lifecycleScope.launch(Dispatchers.Predominant) {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
windowAreaController.windowAreaInfos
.map { information -> information.firstOrNull { it.sort == WindowAreaInfo.Kind.TYPE_REAR_FACING } }
.onEach { information -> windowAreaInfo = information }
.map { it?.getCapability(operation)?.standing ?: WindowAreaCapability.Standing.WINDOW_AREA_STATUS_UNSUPPORTED }
.distinctUntilChanged()
.gather {
capabilityStatus = it
updateUI()
}
}
}
Step 6: Replace the UI in line with the gadget’s WindowAreaCapability.Standing
personal enjoyable updateUI() {
if(windowAreaSession != null) {
viewBinding.switchScreenButton.isEnabled = true
} else {
when(capabilityStatus) {
WindowAreaCapability.Standing.WINDOW_AREA_STATUS_UNSUPPORTED -> {
viewBinding.switchScreenButton.isEnabled = false
Toast.makeText(baseContext, "RearDisplay just isn't supported on this gadget", Toast.LENGTH_SHORT).present()
}
WindowAreaCapability.Standing.WINDOW_AREA_STATUS_UNAVAILABLE -> {
viewBinding.switchScreenButton.isEnabled = false
Toast.makeText(baseContext, "RearDisplay just isn't presently obtainable", Toast.LENGTH_SHORT).present()
}
WindowAreaCapability.Standing.WINDOW_AREA_STATUS_AVAILABLE -> {
viewBinding.switchScreenButton.isEnabled = true
}
WindowAreaCapability.Standing.WINDOW_AREA_STATUS_ACTIVE -> {
viewBinding.switchScreenButton.isEnabled = true
Toast.makeText(baseContext, "RearDisplay is presently energetic", Toast.LENGTH_SHORT).present()
}
else -> {
viewBinding.switchScreenButton.isEnabled = false
Toast.makeText(baseContext, "RearDisplay standing is unknown", Toast.LENGTH_SHORT).present()
}
}
}
}
Step 7: Toggle to rear show mode with WindowAreaController
Shut the session whether it is already energetic, in any other case begin a switch session to maneuver the MainActivity to the window space recognized by the token.
Whereas activating rear show mode, the system creates a dialog to request the person’s permission to permit the appliance to change screens. This dialog just isn’t customizable.
personal enjoyable toggleRearDisplayMode() {
if(capabilityStatus == WindowAreaCapability.Standing.WINDOW_AREA_STATUS_ACTIVE) {
if(windowAreaSession == null) {
windowAreaSession = windowAreaInfo?.getActiveSession(
operation
)
}
windowAreaSession?.shut()
} else {
windowAreaInfo?.token?.let { token ->
windowAreaController.transferActivityToWindowArea(
token = token,
exercise = this,
executor = displayExecutor,
windowAreaSessionCallback = this
)
}
}
}
Step 8: Begin the digicam preview
Name startCamera()
when onSessionStarted is triggered by the WindowAreaSessionCallback interface.
override enjoyable onSessionStarted(session: WindowAreaSession) {
startCamera()
}
Step 9: Add a button and set a listener to it for activating rear show mode
<Button
android:id="@+id/switch_screen_button"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_marginStart="50dp"
android:elevation="2dp"
android:textual content="@string/switch_screen"
app:layout_constraintBottom_toBottomOf="mother or father"
app:layout_constraintBottom_toTopOf="@+id/horizontal_baseline"
app:layout_constraintStart_toEndOf="@id/vertical_centerline" />
viewBinding.switchScreenButton.setOnClickListener{
updateUI()
toggleRearDisplayMode()
}
Incorporating rear show mode into your software can considerably improve person expertise by offering extra intuitive management and better flexibility. By following the outlined steps, you possibly can create a extra dynamic and user-friendly interface. As expertise continues to evolve, staying forward with options like rear show mode can set your software aside and supply customers a seamless, professional-quality expertise. To study extra about creating purposes for Galaxy Foldable gadgets, go to: developer.samsung.com/galaxy-z.