Class DynamicThemeHandler

java.lang.Object
android.os.Handler
com.pranavpandey.android.dynamic.support.theme.DynamicThemeHandler
All Implemented Interfaces:
DynamicListener, DynamicProductFlavor

public class DynamicThemeHandler extends Handler implements DynamicProductFlavor, DynamicListener
A Handler to handle theme updates on the main thread.
See Also:
  • Field Details

  • Constructor Details

    • DynamicThemeHandler

      public DynamicThemeHandler(@NonNull Looper looper, @NonNull List<DynamicListener> listeners)
      Constructor to initialize an object of this class.
      Parameters:
      looper - The looper to be used.
      listeners - The list of listeners to receive the callbacks.
  • Method Details

    • handleMessage

      public void handleMessage(@NonNull Message msg)
      Overrides:
      handleMessage in class Handler
    • getListeners

      @Nullable public List<DynamicListener> getListeners()
      Returns the list of listeners handled by this handler.
      Returns:
      The list of listeners handled by this handler.
    • addListener

      public void addListener(@Nullable DynamicListener listener)
      Add a dynamic listener to receive the various callbacks.
      Parameters:
      listener - The dynamic listener to be added.
      See Also:
    • removeListener

      public void removeListener(@Nullable DynamicListener listener)
      Remove a dynamic listener.
      Parameters:
      listener - The dynamic listener to be removed.
      See Also:
    • isListener

      public boolean isListener(@Nullable DynamicListener listener)
      Checks whether a dynamic listener is already registered.
      Parameters:
      listener - The dynamic listener to be checked.
      Returns:
      true if the listener is already registered.
      See Also:
    • resolveListener

      @Nullable public DynamicListener resolveListener(boolean latest)
      Returns the latest listener added to the theme.
      Parameters:
      latest - true to resolve the latest listener.
      Returns:
      The latest listener added to the theme.
    • clearListeners

      public void clearListeners()
      Remove all the dynamic listener.
      See Also:
    • getProductFlavor

      public String getProductFlavor()
      Specified by:
      getProductFlavor in interface DynamicProductFlavor
    • getContext

      @NonNull public Context getContext()
      Description copied from interface: DynamicListener
      Returns the context used by this listener.
      Specified by:
      getContext in interface DynamicListener
      Returns:
      The context used by this listener.
    • getRequiredThemeVersion

      public int getRequiredThemeVersion()
      Description copied from interface: DynamicListener
      This method will be called to get the intended theme styles version for this listener.
      Specified by:
      getRequiredThemeVersion in interface DynamicListener
      Returns:
      The intended theme styles version for this listener.
    • isNightMode

      public boolean isNightMode(boolean resolve)
      Description copied from interface: DynamicListener
      Checks whether the night mode is enabled for this listener.
      Specified by:
      isNightMode in interface DynamicListener
      Parameters:
      resolve - true to resolve based on the theme data.
      Returns:
      true if the night mode is enabled for this listener.
    • getThemeRes

      @StyleRes public int getThemeRes(@Nullable AppTheme<?> theme)
      Description copied from interface: DynamicListener
      This method will be called to return the theme style resource for this listener.

      Override this method to supply your own customised style.

      Specified by:
      getThemeRes in interface DynamicListener
      Parameters:
      theme - The optional dynamic theme to resolve the style resource.
      Returns:
      The theme style resource for this listener.
      See Also:
    • getThemeRes

      @StyleRes public int getThemeRes()
      Description copied from interface: DynamicListener
      This method will be called to return the theme style resource for this listener.

      Override this method to supply your own customised style.

      Specified by:
      getThemeRes in interface DynamicListener
      Returns:
      The theme style resource for this listener.
      See Also:
    • getDynamicTheme

      @Nullable public AppTheme<?> getDynamicTheme()
      Description copied from interface: DynamicListener
      This method will be called to return the dynamic app theme for this listener.

      Override this method to supply your own customised theme.

      Specified by:
      getDynamicTheme in interface DynamicListener
      Returns:
      The dynamic app theme for this listener.
    • isDynamicColors

      public boolean isDynamicColors()
      Description copied from interface: DynamicListener
      Returns whether the dynamic colors are enabled for this listener.
      Specified by:
      isDynamicColors in interface DynamicListener
      Returns:
      true if the dynamic colors are enabled for this listener.
    • isDynamicColor

      public boolean isDynamicColor()
      Description copied from interface: DynamicListener
      Returns whether to apply the dynamic colors for this listener.
      Specified by:
      isDynamicColor in interface DynamicListener
      Returns:
      true if the dynamic colors should be applied for this listener.
    • isSystemColor

      public boolean isSystemColor()
      Description copied from interface: DynamicListener
      Returns whether the dynamic colors should be extracted from the system for this listener.
      Specified by:
      isSystemColor in interface DynamicListener
      Returns:
      true if the dynamic colors should be extracted from the system for this listener
    • isWallpaperColor

      public boolean isWallpaperColor()
      Description copied from interface: DynamicListener
      Returns whether the dynamic colors should be extracted from the wallpaper for this listener.
      Specified by:
      isWallpaperColor in interface DynamicListener
      Returns:
      true if the dynamic colors should be extracted from the wallpaper for this listener
    • isOnSharedPreferenceChangeListener

      public boolean isOnSharedPreferenceChangeListener()
      Description copied from interface: DynamicListener
      Returns whether to register a shared preferences listener for this listener.
      Specified by:
      isOnSharedPreferenceChangeListener in interface DynamicListener
      Returns:
      true to register a SharedPreferences.OnSharedPreferenceChangeListener to receive preference change callback.
    • getDefaultColor

      @ColorInt public int getDefaultColor(int colorType)
      Description copied from interface: DynamicListener
      This method will be called to resolve the default color according to it's type.

      It is useful in resolving the color if the default theme also has auto values.

      Specified by:
      getDefaultColor in interface DynamicListener
      Parameters:
      colorType - The color type to be resolved.
      Returns:
      The resolved default color according to the color type.
      See Also:
    • onDynamicChanged

      public void onDynamicChanged(boolean context, boolean recreate)
      Description copied from interface: DynamicListener
      This method will be called when the dynamic change event occurs (like theme, locale, etc.).

      Recreate the activity or application here to adapt changes.

      Specified by:
      onDynamicChanged in interface DynamicListener
      Parameters:
      context - true if there is a context change and it must be reinitialized.
      recreate - true if listener must be recreated to adapt the changes.
    • onDynamicConfigurationChanged

      public void onDynamicConfigurationChanged(boolean locale, boolean fontScale, boolean orientation, boolean uiMode, boolean density)
      Description copied from interface: DynamicListener
      This method will be called when the dynamic configuration change event occurs (like locale, font scale, orientation, ui mode, etc.).

      It will provide more control on DynamicListener.onDynamicChanged(boolean, boolean) method call.

      Specified by:
      onDynamicConfigurationChanged in interface DynamicListener
      Parameters:
      locale - true if locale is changed.
      fontScale - true if font scale is changed.
      orientation - true if there is an orientation change.
      uiMode - true if ui mode is changed.
      density - true if configuration density is changed.
      See Also:
    • onDynamicColorsChanged

      public void onDynamicColorsChanged(@Nullable DynamicColors colors, boolean context)
      Description copied from interface: DynamicListener
      This method will be called when the dynamic color change event occurs according to the wallpaper.

      Recreate the activity or application here to adapt changes.

      Specified by:
      onDynamicColorsChanged in interface DynamicListener
      Parameters:
      colors - The new dynamic colors.
      context - true if there is a context change and it must be reinitialized.
    • onAutoThemeChanged

      public void onAutoThemeChanged(boolean context)
      Description copied from interface: DynamicListener
      This method will be called when the auto theme change event occurs according to the time.

      Recreate the activity or application here to adapt changes.

      Specified by:
      onAutoThemeChanged in interface DynamicListener
      Parameters:
      context - true if there is a context change and it must be reinitialized.
    • onPowerSaveModeChanged

      public void onPowerSaveModeChanged(boolean powerSaveMode)
      Description copied from interface: DynamicListener
      This method will be called when the power save mode has been changed.

      It will be called only on API 21 and above.

      Specified by:
      onPowerSaveModeChanged in interface DynamicListener
      Parameters:
      powerSaveMode - true if the device is in power save mode.
    • setNavigationBarTheme

      public boolean setNavigationBarTheme()
      Description copied from interface: DynamicListener
      Returns whether the navigation bar theme should be applied for the activity.

      It will be applied only on the API 21 and above.

      Specified by:
      setNavigationBarTheme in interface DynamicListener
      Returns:
      true to apply navigation bar theme for the activity.
    • onNavigationBarThemeChanged

      public void onNavigationBarThemeChanged()
      Description copied from interface: DynamicListener
      This method will be called when the navigation bar theme has been changed.
      Specified by:
      onNavigationBarThemeChanged in interface DynamicListener