Class RemoteConfig

java.lang.Object
com.codename1.cloud.RemoteConfig

public class RemoteConfig extends Object

Remote configuration and feature flags with defaults, a fetch cache and an explicit activation step.

Values come from a RemoteConfigProvider. fetch() downloads them into a pending set and activate() makes them visible to the getters, so a screen never changes under the user. Both sets are kept in Preferences and survive a restart. Keys missing remotely fall back to the defaults.

final RemoteConfig config = new RemoteConfig(
        new JsonUrlRemoteConfigProvider("https://example.com/config.json"));
Map<String, Object> defaults = new HashMap<String, Object>();
defaults.put("welcome_text", "Hello");
defaults.put("new_checkout", Boolean.FALSE);
config.setDefaults(defaults);
config.fetchAndActivate().ready(new SuccessCallback<Boolean>() {
    public void onSucess(Boolean changed) {
        Log.p("Checkout flag: " + config.getBoolean("new_checkout"));
    }
});
  • Field Details

  • Constructor Details

    • RemoteConfig

      public RemoteConfig(RemoteConfigProvider provider)

      Creates a config backed by a provider and loads the cached values.

      Parameters
      • provider: the value source
  • Method Details

    • setDefaults

      public void setDefaults(Map<String,?> values)

      Sets the values used for keys the provider does not define.

      Parameters
      • values: keys to values, converted with String.valueOf
    • setMinimumFetchInterval

      public void setMinimumFetchInterval(long millis)

      Sets how long a fetch stays fresh. A fetch() within this interval of the previous successful one completes without a request. The default is twelve hours.

      Parameters
      • millis: the interval in milliseconds, 0 to always fetch
    • getLastFetchTime

      public Date getLastFetchTime()
      Returns

      the time of the last successful fetch, or null

    • fetch

      public AsyncResource<Boolean> fetch()

      Downloads values into the pending set, unless the last fetch is still fresh.

      Returns

      completes with true when a request was made

    • activate

      public boolean activate()

      Makes the pending values visible to the getters.

      Returns

      true when the active values changed

    • fetchAndActivate

      public AsyncResource<Boolean> fetchAndActivate()

      Fetches, then activates.

      Returns

      completes with true when the active values changed

    • reset

      public void reset()
      Forgets the cached values and fetch time.
    • getString

      public String getString(String key)

      Reads a value as text.

      Parameters
      • key: the key
      Returns

      the active value, else the default, else an empty string

    • getBoolean

      public boolean getBoolean(String key)

      Reads a value as a flag. true, 1, yes and on count as true, ignoring case.

      Parameters
      • key: the key
      Returns

      the flag, false when unknown

    • getLong

      public long getLong(String key)

      Reads a value as a whole number.

      Parameters
      • key: the key
      Returns

      the number, 0 when unknown or not a number

    • getDouble

      public double getDouble(String key)

      Reads a value as a decimal number.

      Parameters
      • key: the key
      Returns

      the number, 0 when unknown or not a number

    • getSource

      public int getSource(String key)

      Tells where a value comes from.

      Parameters
      • key: the key
      Returns

      SOURCE_REMOTE, SOURCE_DEFAULT or SOURCE_STATIC

    • getKeys

      public Set<String> getKeys()
      Returns

      every key with an active or default value, sorted