AmbientModeSupport
public final class AmbientModeSupport extends Fragment
Use this as a headless Fragment to add ambient support to an Activity on Wearable devices.
The application that uses this should add the WAKE_LOCK permission to its manifest.
The following describes the general use of this class:
Create a subclass of one of the FragmentActivity classes and implement the AmbientCallbackProvider interface. Override the getAmbientCallback method to provide the callbacks required for reacting to the ambient events from the Android system. If a valid AmbientCallback is not provided (either no implementation of the AmbientCallbackProvider interface, or returning null from getAmbientCallback), then ambient mode will NOT be enabled.
The primary entry point for this code is the attach method. It should be called with an FragmentActivity as an argument and that FragmentActivity will then be able to receive ambient lifecycle events through an AmbientCallback. The FragmentActivity will also receive a AmbientController object from the attachment which can be used to query the current status of the ambient mode.
An example of how to implement the AmbientCallbackProvider interface, attach AmbientModeSupport to your FragmentActivity and use the AmbientController can be found below:
public class MyActivity extends FragmentActivity implements AmbientModeSupport.AmbientCallbackProvider { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) ... AmbientModeSupport.AmbientController controller = AmbientModeSupport.attach(this); boolean isAmbient = controller.isAmbient(); } @Override AmbientModeSupport.AmbientCallback getAmbientCallback() { return new AmbientModeSupport.AmbientCallback() { public void onEnterAmbient(Bundle ambientDetails) {...} public void onExitAmbient(Bundle ambientDetails) {...} } } }
Summary
Nested types |
|---|
public abstract class AmbientModeSupport.AmbientCallbackCallback to receive ambient mode state changes. |
public interface AmbientModeSupport.AmbientCallbackProviderInterface for any |
public final inner class AmbientModeSupport.AmbientControllerA class for interacting with the ambient mode on a wearable device. |
Constants |
|
|---|---|
static final String |
EXTRA_BURN_IN_PROTECTION = "com.google.android.wearable.compat.extra.BURN_IN_PROTECTION"Property in bundle passed to |
static final String |
EXTRA_LOWBIT_AMBIENT = "com.google.android.wearable.compat.extra.LOWBIT_AMBIENT"Property in bundle passed to |
static final String |
FRAGMENT_TAG = "android.support.wearable.ambient.AmbientMode"Fragment tag used by default when adding |
Public constructors |
|---|
|
Constructor |
Public methods |
|
|---|---|
static AmbientModeSupport.AmbientController |
<T extends FragmentActivity> Attach ambient support to the given activity. |
void |
Print the Fragments's state into the given stream. |
void |
Called when a fragment is first attached to its context. |
void |
Called to do initial creation of a fragment. |
void |
Called when the fragment is no longer in use. |
void |
Called when the fragment is no longer attached to its activity. |
void |
Called when the Fragment is no longer resumed. |
void |
Called when the fragment is visible to the user and actively running. |
void |
Called when the Fragment is no longer started. |
Inherited methods |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Constants
public static final String EXTRA_BURN_IN_PROTECTION = "com.google.android.wearable.compat.extra.BURN_IN_PROTECTION"
Property in bundle passed to AmbientCallback#onEnterAmbient(Bundle) to indicate whether burn-in protection is required. When this property is set to true, views must be shifted around periodically in ambient mode. To ensure that content isn't shifted off the screen, avoid placing content within 10 pixels of the edge of the screen. Activities should also avoid solid white areas to prevent pixel burn-in. Both of these requirements only apply in ambient mode, and only when this property is set to true.
public static final String EXTRA_LOWBIT_AMBIENT = "com.google.android.wearable.compat.extra.LOWBIT_AMBIENT"
Property in bundle passed to AmbientCallback#onEnterAmbient(Bundle) to indicate whether the device has low-bit ambient mode. When this property is set to true, the screen supports fewer bits for each color in ambient mode. In this case, activities should disable anti-aliasing in ambient mode.
public static final String FRAGMENT_TAG = "android.support.wearable.ambient.AmbientMode"
Fragment tag used by default when adding AmbientModeSupport to add ambient support to a FragmentActivity.
Public constructors
Public methods
public static AmbientModeSupport.AmbientController <T extends FragmentActivity>attach(T activity)
Attach ambient support to the given activity. Calling this method with an Activity implementing the AmbientCallbackProvider interface will provide you with an opportunity to react to ambient events such as onEnterAmbient. Alternatively, you can call this method with an Activity which does not implement the AmbientCallbackProvider interface and that will only enable the auto-resume functionality. This is equivalent to providing (@code null} from the AmbientCallbackProvider.
| Parameters | |
|---|---|
T activity |
the activity to attach ambient support to. |
| Returns | |
|---|---|
AmbientModeSupport.AmbientController |
the associated |
dump
public voiddump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)
Print the Fragments's state into the given stream.
| Parameters | |
|---|---|
String prefix |
Text to print at the front of each line. |
FileDescriptor fd |
The raw file descriptor that the dump is being sent to. |
PrintWriter writer |
The PrintWriter to which you should dump your state. This will be closed for you after you return. |
String[] args |
additional arguments to the dump request. |
onAttach
@CallSuper
public voidonAttach(Context context)
Called when a fragment is first attached to its context. onCreate will be called after this.
onCreate
@CallSuper
public voidonCreate(Bundle savedInstanceState)
Called to do initial creation of a fragment. This is called after onAttach and before onCreateView.
Note that this can be called while the fragment's activity is still in the process of being created. As such, you can not rely on things like the activity's content view hierarchy being initialized at this point. If you want to do work once the activity itself is created, add a androidx.lifecycle.LifecycleObserver on the activity's Lifecycle, removing it when it receives the CREATED callback.
Any restored child fragments will be created before the base Fragment.onCreate method returns.
| Parameters | |
|---|---|
Bundle savedInstanceState |
If the fragment is being re-created from a previous saved state, this is the state. |
onDestroy
@CallSuper
public voidonDestroy()
Called when the fragment is no longer in use. This is called after onStop and before onDetach.
onDetach
@CallSuper
public voidonDetach()
Called when the fragment is no longer attached to its activity. This is called after onDestroy.
onPause
@CallSuper
public voidonPause()
Called when the Fragment is no longer resumed. This is generally tied to Activity.onPause of the containing Activity's lifecycle.
onResume
@CallSuper
public voidonResume()
Called when the fragment is visible to the user and actively running. This is generally tied to Activity.onResume of the containing Activity's lifecycle.
onStop
@CallSuper
public voidonStop()
Called when the Fragment is no longer started. This is generally tied to Activity.onStop of the containing Activity's lifecycle.