Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class RemoteConfig

  1. Object
  2. RemoteConfig

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"));
    }
});

Fields

public static final int SOURCE_REMOTE = 2The value came from an activated fetch.
public static final int SOURCE_DEFAULT = 1The value came from setDefaults(Map).
public static final int SOURCE_STATIC = 0No value is known for the key.

Constructors

public RemoteConfig(RemoteConfigProvider provider)Creates a config backed by a provider and loads the cached values.

Methods

public void setDefaults(Map<String, ?> values)Sets the values used for keys the provider does not define.
public void setMinimumFetchInterval(long millis)Sets how long a fetch stays fresh.
public Date getLastFetchTime()
public AsyncResource<Boolean> fetch()Downloads values into the pending set, unless the last fetch is still fresh.
public boolean activate()Makes the pending values visible to the getters.
public AsyncResource<Boolean> fetchAndActivate()Fetches, then activates.
public void reset()Forgets the cached values and fetch time.
public String getString(String key)Reads a value as text.
public boolean getBoolean(String key)Reads a value as a flag.
public long getLong(String key)Reads a value as a whole number.
public double getDouble(String key)Reads a value as a decimal number.
public int getSource(String key)Tells where a value comes from.
public Set<String> getKeys()

Inherited methods

Field details

SOURCE_REMOTE

public static final int SOURCE_REMOTE = 2
The value came from an activated fetch.

SOURCE_DEFAULT

public static final int SOURCE_DEFAULT = 1
The value came from setDefaults(Map).

SOURCE_STATIC

public static final int SOURCE_STATIC = 0
No value is known for the key.

Constructor details

RemoteConfig

public RemoteConfig(RemoteConfigProvider provider)
Creates a config backed by a provider and loads the cached values.

Parameters

provider RemoteConfigProvider
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 Map<String, ?>
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 long
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 String
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 String
the key

Returns

the flag, false when unknown

getLong

public long getLong(String key)
Reads a value as a whole number.

Parameters

key String
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 String
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 String
the key

getKeys

public Set<String> getKeys()

Returns

every key with an active or default value, sorted