Class 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"));
}
});
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe value came fromsetDefaults(Map).static final intThe value came from an activated fetch.static final intNo value is known for the key. -
Constructor Summary
ConstructorsConstructorDescriptionRemoteConfig(RemoteConfigProvider provider) Creates a config backed by a provider and loads the cached values. -
Method Summary
Modifier and TypeMethodDescriptionbooleanactivate()Makes the pending values visible to the getters.fetch()Downloads values into the pending set, unless the last fetch is still fresh.Fetches, then activates.booleangetBoolean(String key) Reads a value as a flag.doubleReads a value as a decimal number.getKeys()Returns
Returns
longReads a value as a whole number.intTells where a value comes from.Reads a value as text.voidreset()Forgets the cached values and fetch time.voidsetDefaults(Map<String, ?> values) Sets the values used for keys the provider does not define.voidsetMinimumFetchInterval(long millis) Sets how long a fetch stays fresh.
-
Field Details
-
SOURCE_REMOTE
public static final int SOURCE_REMOTEThe value came from an activated fetch.- See Also:
-
SOURCE_DEFAULT
-
SOURCE_STATIC
public static final int SOURCE_STATICNo value is known for the key.- See Also:
-
-
Constructor Details
-
RemoteConfig
Creates a config backed by a provider and loads the cached values.
Parameters
provider: the value source
-
-
Method Details
-
setDefaults
-
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
Returns
the time of the last successful fetch, or null
-
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
Fetches, then activates.
Returns
completes with true when the active values changed
-
reset
public void reset()Forgets the cached values and fetch time. -
getString
-
getBoolean
Reads a value as a flag.
true,1,yesandoncount as true, ignoring case.Parameters
key: the key
Returns
the flag, false when unknown
-
getLong
Reads a value as a whole number.
Parameters
key: the key
Returns
the number, 0 when unknown or not a number
-
getDouble
Reads a value as a decimal number.
Parameters
key: the key
Returns
the number, 0 when unknown or not a number
-
getSource
Tells where a value comes from.
Parameters
key: the key
Returns
-
getKeys
-