WearableButtons

public final class WearableButtons


Class containing helpers for managing wearable buttons.

Summary

Nested types

WearableButtons.ButtonInfo

Metadata for a specific button.

Public methods

static @NonNull int

Get the number of hardware buttons available.

static @Nullable Drawable
getButtonIcon(@NonNull Context context, @NonNull int keycode)

Returns an icon that can be used to represent the location of a button.

static @Nullable WearableButtons.ButtonInfo
getButtonInfo(@NonNull Context context, @NonNull int keycode)

Returns a ButtonInfo containing the metadata for a specific button.

static @Nullable CharSequence
getButtonLabel(@NonNull Context context, @NonNull int keycode)

Returns a CharSequence that describes the placement location of a button.

static @VisibleForTesting(otherwise = VisibleForTesting.NONE) @NonNull void

Testing call to allow the underlying WearableButtonsProvider to be substituted in test code.

Public methods

getButtonCount

@NonNull
public static int getButtonCount(@NonNull Context context)

Get the number of hardware buttons available. This count includes the primary stem key as well as any secondary stem keys available.

Parameters
@NonNull Context context

The context of the current activity

Returns
int

The number of buttons available, or the information is not available.

getButtonIcon

@Nullable
@NonNull
public static Drawable getButtonIcon(@NonNull Context context, @NonNull int keycode)

Returns an icon that can be used to represent the location of a button.

Parameters
@NonNull Context context

The context of the current activity

@NonNull int keycode

The keycode associated with the hardware button of interest

Returns
Drawable

A drawable representing the location of a button, or null if unavailable

getButtonInfo

@Nullable
@NonNull
public static WearableButtons.ButtonInfo getButtonInfo(@NonNull Context context, @NonNull int keycode)

Returns a ButtonInfo containing the metadata for a specific button.

The location will be populated in the following manner:

  • The provided point will be on the screen, or more typically, on the edge of the screen.
  • The point won't be off the edge of the screen.
  • The location returned is a screen coordinate. The unit of measurement is in pixels. The coordinates do not take rotation into account and assume that the device is in the standard upright position.

Common keycodes to use are KEYCODE_STEM_PRIMARY, , KEYCODE_STEM_2, and KEYCODE_STEM_3.

Parameters
@NonNull Context context

The context of the current activity

@NonNull int keycode

The keycode associated with the hardware button of interest

Returns
WearableButtons.ButtonInfo

A ButtonInfo containing the metadata for the given keycode or null if the information is not available

getButtonLabel

@Nullable
@NonNull
public static CharSequence getButtonLabel(@NonNull Context context, @NonNull int keycode)

Returns a CharSequence that describes the placement location of a button. An example might be "Top right" or "Bottom".

Parameters
@NonNull Context context

The context of the current activity

@NonNull int keycode

The keycode associated with the hardware button of interest

Returns
CharSequence

A CharSequence describing the placement location of the button, or null if no location is available for that button.

setWearableButtonsProvider

@VisibleForTesting(otherwise = VisibleForTesting.NONE)
@NonNull
public static @VisibleForTesting(otherwise = VisibleForTesting.NONE) void setWearableButtonsProvider(@NonNull WearableButtonsProvider provider)

Testing call to allow the underlying WearableButtonsProvider to be substituted in test code.

Parameters
@NonNull WearableButtonsProvider provider

The new WearableButtonsProvider to use.