Utils.java


Overview

`Utils.java` is a utility class within the [com.tubitv.media.utilities](/projects/288/68383) package, designed to provide a collection of static helper methods commonly used in media-related Android applications. This class centralizes functionality related to:

These utilities simplify common tasks and ensure consistent behavior across the app's media playback and UI components.


Class: Utils

Description

A final utility class containing only static methods and fields. It is not intended to be instantiated.


Fields

Name

Type

Description

`FORMAT_HOURS`

`String`

Format string for time durations with hours (`"%d:%02d:%02d"`).

`FORMAT_MINUTES`

`String`

Format string for time durations without hours (`"%02d:%02d"`).

formatBuilder

`StringBuilder`

A reusable `StringBuilder` instance to build formatted strings.

`formatter`

`Formatter`

A `Formatter` wrapping [formatBuilder](/projects/288/68308) for locale-aware formatting.


Methods

1. public static String getProgressTime(long timeMs, boolean remaining)

Formats a time duration in milliseconds into a human-readable string suitable for display in media controls.


2. public static long progressToMilli(long playerDurationMs, SeekBar seekBar)

Converts the current progress of a `SeekBar` into the corresponding playback time in milliseconds.


3. public static void hideSystemUI(@NonNull final Activity activity, final boolean immediate)

Hides the system bars (navigation and status bars) for an immersive fullscreen experience.


4. public static void hideSystemUI(@NonNull final Activity activity, final boolean immediate, final int delayMs)

Overloaded method providing control over the delay before hiding system UI.


5. public static boolean isEmpty(@Nullable String text)

Checks if a given string is null or empty (ignoring case).


6. public static float dpFromPx(final Context context, final float px)

Converts pixel units (`px`) to density-independent pixels (`dp`), useful for consistent UI sizing across devices.


7. public static float pxFromDp(final Context context, final float dp)

Converts density-independent pixels (`dp`) to pixel units (`px`).


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[getProgressTime(timeMs, remaining)] -->|formats| B[Formatted time string]
    C[progressToMilli(playerDurationMs, seekBar)] -->|calculates| D[Playback time in ms]
    E[hideSystemUI(activity, immediate, delayMs)] -->|modifies| F[System UI visibility flags]
    G[isEmpty(text)] -->|checks| H[Boolean result]
    I[dpFromPx(context, px)] -->|converts| J[dp value]
    K[pxFromDp(context, dp)] -->|converts| L[px value]

Summary

`Utils.java` is a foundational utility class that provides essential helper methods for media playback UI, time formatting, UI scaling, and immersive mode management in an Android media application. Its static methods promote code reuse and consistency, simplifying the implementation of common tasks related to media controls and user interface behavior.