Class DynamicApplication

java.lang.Object
android.content.Context
android.content.ContextWrapper
android.app.Application
com.pranavpandey.android.dynamic.support.DynamicApplication
All Implemented Interfaces:
ComponentCallbacks, ComponentCallbacks2, SharedPreferences.OnSharedPreferenceChangeListener, Configuration.Provider, DynamicLocale, DynamicListener, DynamicProductFlavor

Base Application class which can be extended to initialize the DynamicTheme and to perform theme change operations.
  • Field Details

    • mContext

      protected Context mContext
      Dynamic context used by this application.
  • Constructor Details

    • DynamicApplication

      public DynamicApplication()
  • Method Details

    • attachBaseContext

      public void attachBaseContext(@NonNull Context base)
      Overrides:
      attachBaseContext in class ContextWrapper
    • onCreate

      public void onCreate()
      Overrides:
      onCreate in class Application
    • onConfigurationChanged

      public void onConfigurationChanged(@NonNull Configuration newConfig)
      Specified by:
      onConfigurationChanged in interface ComponentCallbacks
      Overrides:
      onConfigurationChanged in class Application
    • onLowMemory

      public void onLowMemory()
      Specified by:
      onLowMemory in interface ComponentCallbacks
      Overrides:
      onLowMemory in class Application
    • onInitialize

      protected abstract void onInitialize()
      This method will be called inside the onCreate() method before applying the theme.

      Do any initializations in this method.

    • getDynamicResolver

      @Nullable protected DynamicResolver getDynamicResolver()
      Returns the resolver for the dynamic theme to provide implementation for conditions like auto and night themes.

      Override this method to supply your own resolver implementation.

      Returns:
      The resolver for the dynamic theme.
    • onCustomiseTheme

      protected void onCustomiseTheme()
      This method will be called inside the onCreate() method after applying the theme.

      Override this method to customise the theme further.

    • onSetupDynamicWork

      protected boolean onSetupDynamicWork()
      This method will be called before setting up the dynamic work to listen changes based on time. It will be useful in updating the auto theme while the app is in background.

      Return false to skip the dynamic work initialization.

      Returns:
      true to setup the dynamic work.
    • setDynamicTheme

      protected void setDynamicTheme()
      Set the dynamic app theme and style resource for this application.
    • getWorkManagerConfiguration

      @NonNull public Configuration getWorkManagerConfiguration()
      Specified by:
      getWorkManagerConfiguration in interface Configuration.Provider
    • getSupportedLocales

      @Nullable public String[] getSupportedLocales()
      Specified by:
      getSupportedLocales in interface DynamicLocale
    • getDefaultLocale

      @NonNull public Locale getDefaultLocale(@NonNull Context context)
      Specified by:
      getDefaultLocale in interface DynamicLocale
    • setLocale

      @NonNull public Context setLocale(@NonNull Context context)
      Specified by:
      setLocale in interface DynamicLocale
    • getFontScale

      public float getFontScale()
      Specified by:
      getFontScale in interface DynamicLocale
    • 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.
    • 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
    • onSharedPreferenceChanged

      public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String key)
      Specified by:
      onSharedPreferenceChanged in interface SharedPreferences.OnSharedPreferenceChangeListener