RotationProvider

public final class RotationProvider
extends Object

java.lang.Object
   ↳ androidx.camera.view.RotationProvider


Provider for receiving rotation updates from the SensorManager when the rotation of the device has changed.

This class monitors motion sensor and notifies the listener about physical orientation changes in the format of Surface rotation. It's useful when the Activity is in a fixed portrait or landscape orientation, while the app still wants to set the UseCase target rotation based on the device's physical rotation.


 // Create a provider.
 RotationProvider mRotationProvider = new RotationProvider(getContext());

 // Add listener to receive updates.
 mRotationProvider.addListener(rotation -> {
     mImageCapture.setTargetRotation(rotation);
 });

 // Remove when no longer needed.
 mRotationProvider.clearListener();
 

Summary

Nested classes

interface RotationProvider.Listener

Callback interface to receive rotation updates. 

Public constructors

RotationProvider(Context context)

Creates a new RotationProvider.

Public methods

boolean addListener(Executor executor, RotationProvider.Listener listener)

Sets a RotationProvider.Listener that listens for rotation changes.

void removeListener(RotationProvider.Listener listener)

Removes the given RotationProvider.Listener from this object.

Inherited methods

Public constructors

RotationProvider

public RotationProvider (Context context)

Creates a new RotationProvider.

Parameters
context Context

Public methods

addListener

public boolean addListener (Executor executor, 
                RotationProvider.Listener listener)

Sets a RotationProvider.Listener that listens for rotation changes.

Parameters
executor Executor: The executor in which the {@link {@link Listener#onRotationChanged(int)} will be run.

listener RotationProvider.Listener

Returns
boolean false if the device cannot detection rotation changes. In that case, the listener will not be set.

removeListener

public void removeListener (RotationProvider.Listener listener)

Removes the given RotationProvider.Listener from this object.

The removed listener will no longer receive rotation updates.

Parameters
listener RotationProvider.Listener