AmbientModeSupport

Added in 1.1.0
Deprecated in 1.3.0

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

This class is deprecated. Deprecated in Java

This interface is deprecated. Deprecated in Java

public final inner class AmbientModeSupport.AmbientController

This class is deprecated. Deprecated in Java

Constants

static final String
EXTRA_BURN_IN_PROTECTION = "com.google.android.wearable.compat.extra.BURN_IN_PROTECTION"

This field is deprecated. Deprecated in Java

static final String
EXTRA_LOWBIT_AMBIENT = "com.google.android.wearable.compat.extra.LOWBIT_AMBIENT"

This field is deprecated. Deprecated in Java

static final String
FRAGMENT_TAG = "android.support.wearable.ambient.AmbientMode"

This field is deprecated. Deprecated in Java

Public constructors

This method is deprecated. Deprecated in Java

Public methods

static AmbientModeSupport.AmbientController
<T extends FragmentActivity> attach(T activity)

This method is deprecated. Deprecated in Java

void
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void
@CallSuper
onCreate(Bundle savedInstanceState)

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

Inherited methods

From androidx.activity.result.ActivityResultCaller
abstract ActivityResultLauncher<I>
<I, O> registerForActivityResult(
    ActivityResultContract<I, O> contract,
    ActivityResultCallback<O> callback
)

This method is deprecated. Deprecated in Java

From android.content.ComponentCallbacks
abstract void

This method is deprecated. Deprecated in Java

abstract void

This method is deprecated.

From androidx.activity.contextaware.ContextAware
abstract void

This method is deprecated. Deprecated in Java

abstract Context

This method is deprecated. Deprecated in Java

abstract void

This method is deprecated. Deprecated in Java

From androidx.fragment.app.Fragment
void

This method is deprecated. Deprecated in Java

final boolean

This method is deprecated. Deprecated in Java

final @Nullable FragmentActivity

This method is deprecated. Deprecated in Java

boolean

This method is deprecated. Deprecated in Java

boolean

This method is deprecated. Deprecated in Java

final @Nullable Bundle

This method is deprecated. Deprecated in Java

final @NonNull FragmentManager

This method is deprecated. Deprecated in Java

@Nullable Context

This method is deprecated. Deprecated in Java

@NonNull CreationExtras

This method is deprecated. Deprecated in Java

@NonNull ViewModelProvider.Factory

This method is deprecated. Deprecated in Java

@Nullable Object

This method is deprecated. Deprecated in Java

@Nullable Object

This method is deprecated. Deprecated in Java

final @Nullable FragmentManager

This method is deprecated.

This has been removed in favor of getParentFragmentManager() which throws an IllegalStateException if the FragmentManager is null.

final @Nullable Object

This method is deprecated. Deprecated in Java

final int

This method is deprecated. Deprecated in Java

final @NonNull LayoutInflater

This method is deprecated. Deprecated in Java

@NonNull Lifecycle

This method is deprecated. Deprecated in Java

@NonNull LoaderManager

This method is deprecated.

Use LoaderManager.getInstance(this).

final @Nullable Fragment

This method is deprecated. Deprecated in Java

final @NonNull FragmentManager

This method is deprecated. Deprecated in Java

@Nullable Object

This method is deprecated. Deprecated in Java

final @NonNull Resources

This method is deprecated. Deprecated in Java

final boolean

This method is deprecated.

Instead of retaining the Fragment itself, use a non-retained Fragment and keep retained state in a ViewModel attached to that Fragment.

@Nullable Object

This method is deprecated. Deprecated in Java

final @NonNull SavedStateRegistry

This method is deprecated. Deprecated in Java

@Nullable Object

This method is deprecated. Deprecated in Java

@Nullable Object

This method is deprecated. Deprecated in Java

final @NonNull String
getString(@StringRes int resId)

This method is deprecated. Deprecated in Java

final @NonNull String
getString(@StringRes int resId, @Nullable Object[] formatArgs)

This method is deprecated. Deprecated in Java

final @Nullable String

This method is deprecated. Deprecated in Java

final @Nullable Fragment

This method is deprecated.

Instead of using a target fragment to pass results, use setFragmentResult to deliver results to FragmentResultListener instances registered by other fragments via setFragmentResultListener.

final int

This method is deprecated.

When using the target fragment replacement of setFragmentResultListener and setFragmentResult, consider using setArguments to pass a requestKey if you need to support dynamic request keys.

final @NonNull CharSequence
getText(@StringRes int resId)

This method is deprecated. Deprecated in Java

boolean

This method is deprecated.

Use setMaxLifecycle instead.

@Nullable View

This method is deprecated. Deprecated in Java

@NonNull LifecycleOwner

This method is deprecated. Deprecated in Java

@NonNull LiveData<LifecycleOwner>

This method is deprecated. Deprecated in Java

@NonNull ViewModelStore

This method is deprecated. Deprecated in Java

final int

This method is deprecated. Deprecated in Java

static @NonNull Fragment

This method is deprecated.

Use getFragmentFactory and instantiate

static @NonNull Fragment
instantiate(
    @NonNull Context context,
    @NonNull String fname,
    @Nullable Bundle args
)

This method is deprecated.

Use getFragmentFactory and instantiate, manually calling setArguments on the returned Fragment.

final boolean

This method is deprecated. Deprecated in Java

final boolean

This method is deprecated. Deprecated in Java

final boolean

This method is deprecated. Deprecated in Java

final boolean

This method is deprecated. Deprecated in Java

final boolean

This method is deprecated. Deprecated in Java

final boolean

This method is deprecated. Deprecated in Java

final boolean

This method is deprecated. Deprecated in Java

final boolean

This method is deprecated. Deprecated in Java

void

This method is deprecated.

use onViewCreated for code touching the view created by onCreateView and onCreate for other initialization.

void
onActivityResult(int requestCode, int resultCode, @Nullable Intent data)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

void

This method is deprecated.

See onAttach.

void

This method is deprecated.

The responsibility for listening for fragments being attached has been moved to FragmentManager.

void

This method is deprecated. Deprecated in Java

boolean

This method is deprecated. Deprecated in Java

@Nullable Animation
@MainThread
onCreateAnimation(int transit, boolean enter, int nextAnim)

This method is deprecated. Deprecated in Java

@Nullable Animator
@MainThread
onCreateAnimator(int transit, boolean enter, int nextAnim)

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

@Nullable View
@MainThread
onCreateView(
    @NonNull LayoutInflater inflater,
    @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState
)

This method is deprecated. Deprecated in Java

void

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

void

This method is deprecated. Deprecated in Java

@NonNull LayoutInflater
onGetLayoutInflater(@Nullable Bundle savedInstanceState)

This method is deprecated. Deprecated in Java

void
@MainThread
onHiddenChanged(boolean hidden)

This method is deprecated. Deprecated in Java

void
@UiThread
@CallSuper
onInflate(
    @NonNull Activity activity,
    @NonNull AttributeSet attrs,
    @Nullable Bundle savedInstanceState
)

This method is deprecated.

See onInflate.

void
@UiThread
@CallSuper
onInflate(
    @NonNull Context context,
    @NonNull AttributeSet attrs,
    @Nullable Bundle savedInstanceState
)

This method is deprecated. Deprecated in Java

void

This method is deprecated.

void
onMultiWindowModeChanged(boolean isInMultiWindowMode)

This method is deprecated. Deprecated in Java

boolean

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

void

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

void
onPictureInPictureModeChanged(boolean isInPictureInPictureMode)

This method is deprecated. Deprecated in Java

void

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

void
@MainThread
onPrimaryNavigationFragmentChanged(
    boolean isPrimaryNavigationFragment
)

This method is deprecated. Deprecated in Java

void
onRequestPermissionsResult(
    int requestCode,
    @NonNull String[] permissions,
    @NonNull int[] grantResults
)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void
@MainThread
onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

@Nullable Context

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

final void
postponeEnterTransition(long duration, @NonNull TimeUnit timeUnit)

This method is deprecated. Deprecated in Java

final @NonNull ActivityResultLauncher<I>
@MainThread
<I, O> registerForActivityResult(
    @NonNull ActivityResultContract<I, O> contract,
    @NonNull ActivityResultCallback<O> callback
)

This method is deprecated. Deprecated in Java

final @NonNull ActivityResultLauncher<I>
@MainThread
<I, O> registerForActivityResult(
    @NonNull ActivityResultContract<I, O> contract,
    @NonNull ActivityResultRegistry registry,
    @NonNull ActivityResultCallback<O> callback
)

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

final void
requestPermissions(@NonNull String[] permissions, int requestCode)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

final @NonNull FragmentActivity

This method is deprecated. Deprecated in Java

final @NonNull Bundle

This method is deprecated. Deprecated in Java

final @NonNull Context

This method is deprecated. Deprecated in Java

final @NonNull FragmentManager

This method is deprecated.

This has been renamed to getParentFragmentManager() to make it clear that you are accessing the FragmentManager that contains this Fragment and not the FragmentManager associated with child Fragments.

final @NonNull Object

This method is deprecated. Deprecated in Java

final @NonNull Fragment

This method is deprecated. Deprecated in Java

final @NonNull View

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void
setHasOptionsMenu(boolean hasMenu)

This method is deprecated.

This method is no longer needed when using a MenuProvider to provide a Menu to your activity, which replaces onCreateOptionsMenu as the recommended way to provide a consistent, optionally Lifecycle-aware, and modular way to handle menu creation and item selection.

void

This method is deprecated. Deprecated in Java

void
setMenuVisibility(boolean menuVisible)

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void
setRetainInstance(boolean retain)

This method is deprecated.

Instead of retaining the Fragment itself, use a non-retained Fragment and keep retained state in a ViewModel attached to that Fragment.

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void
setTargetFragment(@Nullable Fragment fragment, int requestCode)

This method is deprecated.

Instead of using a target fragment to pass results, the fragment requesting a result should use setFragmentResultListener to register a FragmentResultListener with a requestKey using its parent fragment manager.

void
setUserVisibleHint(boolean isVisibleToUser)

This method is deprecated.

If you are manually calling this method, use setMaxLifecycle instead.

boolean

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

void
startActivityForResult(@NonNull Intent intent, int requestCode)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

void
startActivityForResult(
    @NonNull Intent intent,
    int requestCode,
    @Nullable Bundle options
)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

void
startIntentSenderForResult(
    @NonNull IntentSender intent,
    int requestCode,
    @Nullable Intent fillInIntent,
    int flagsMask,
    int flagsValues,
    int extraFlags,
    @Nullable Bundle options
)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

void

This method is deprecated. Deprecated in Java

@NonNull String

This method is deprecated. Deprecated in Java

void

This method is deprecated. Deprecated in Java

From androidx.lifecycle.HasDefaultViewModelProviderFactory
CreationExtras

This method is deprecated. Deprecated in Java

ViewModelProvider.Factory

This method is deprecated. Deprecated in Java

From androidx.lifecycle.LifecycleOwner
abstract Lifecycle

This method is deprecated. Deprecated in Java

From androidx.savedstate.SavedStateRegistryOwner
abstract SavedStateRegistry

This method is deprecated. Deprecated in Java

From android.view.View.OnCreateContextMenuListener
abstract void

This method is deprecated. Deprecated in Java

From androidx.lifecycle.ViewModelStoreOwner
abstract ViewModelStore

This method is deprecated. Deprecated in Java

Constants

EXTRA_BURN_IN_PROTECTION

Added in 1.1.0
Deprecated in 1.3.0
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.

EXTRA_LOWBIT_AMBIENT

Added in 1.1.0
Deprecated in 1.3.0
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.

FRAGMENT_TAG

Added in 1.1.0
Deprecated in 1.3.0
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

AmbientModeSupport

Added in 1.1.0
Deprecated in 1.3.0
public AmbientModeSupport()

Constructor

Public methods

attach

Added in 1.1.0
Deprecated in 1.3.0
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 AmbientController which can be used to query the state of ambient mode.

dump

public void dump(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 void onAttach(Context context)

Called when a fragment is first attached to its context. onCreate will be called after this.

onCreate

@CallSuper
public void onCreate(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 void onDestroy()

Called when the fragment is no longer in use. This is called after onStop and before onDetach.

onDetach

@CallSuper
public void onDetach()

Called when the fragment is no longer attached to its activity. This is called after onDestroy.

onPause

@CallSuper
public void onPause()

Called when the Fragment is no longer resumed. This is generally tied to Activity.onPause of the containing Activity's lifecycle.

onResume

@CallSuper
public void onResume()

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 void onStop()

Called when the Fragment is no longer started. This is generally tied to Activity.onStop of the containing Activity's lifecycle.