package de.donmanfred;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigInfo;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigValue;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import anywheresoftware.b4a.AbsObjectWrapper;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.objects.collections.Map;
@Version(1.01f)
@ShortName("RemoteConfig")
@Author(value = "DonManfred (wrapper)")
//@Permissions(values={"android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE"})
@Events(values={"onFetchComplete(success As Boolean)"})
@DependsOn(values={"com.google.firebase:firebase-config","com.google.firebase:firebase-core"})
//@DesignerProperties(values = {
// @Property(key="Checked", displayName="Checked", defaultValue="False", fieldType="Boolean")
// })
public class FirebaseRemoteConfigWrapper extends AbsObjectWrapper<FirebaseRemoteConfig> {
private BA ba;
private String eventName;
public static final boolean DEFAULT_VALUE_FOR_BOOLEAN = false;
public static final double DEFAULT_VALUE_FOR_DOUBLE = 0.0;
public static final long DEFAULT_VALUE_FOR_LONG = 0;
public static final String DEFAULT_VALUE_FOR_STRING = "";
public static final int LAST_FETCH_STATUS_FAILURE = 1;
public static final int LAST_FETCH_STATUS_NO_FETCH_YET = 0;
public static final int LAST_FETCH_STATUS_SUCCESS = -1;
public static final int LAST_FETCH_STATUS_THROTTLED = 2;
public static final int VALUE_SOURCE_DEFAULT = 1;
public static final int VALUE_SOURCE_REMOTE = 2;
public static final int VALUE_SOURCE_STATIC = 0;
public void Initialize(final BA ba, String EventName) {
this.eventName = EventName.toLowerCase(BA.cul);
this.ba = ba;
FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
final String eventName = EventName.toLowerCase(BA.cul);
setObject(mFirebaseRemoteConfig);
//getObject().fetch(cacheExpirationSeconds)
}
/**
* Fetches parameter values for your app. Configuration values may be served
* from the Default Config (local cache) or from the Remote Config Server,
* depending on how much time has elapsed since parameter values were last
* fetched from the Remote Config server. This method lets the caller
* specify the cache expiration in seconds.
* @return
*/
// public Task<Void> fetch (long cacheExpirationSeconds){
// return getObject().fetch(cacheExpirationSeconds);
// }
/**
* Fetches parameter values for your app. Parameter values may be from the
* Default Config (local cache), or from the Remote Config Server,
* depending on how much time has elapsed since parameter values were last
* fetched from the Remote Config server. This method uses the default
* cache expiration of 12 hours.
* @return
*/
// public Task<Void> fetch(){
// return getObject().fetch();
// }
public boolean activateFetched(){
return getObject().activateFetched();
}
/**
* Gets the current state of the
* FirebaseRemoteConfig singleton object.
*
* returns A FirebaseRemoteConfigInfo wrapping the current state.
*/
public FirebaseRemoteConfigInfo getInfo(){
return getObject().getInfo();
}
/**
* Sets defaults in the default namespace,
* using an XML resource.
* @param resourceId
*/
@Hide()
public void setDefaults (String resource){
BA.Log("Calling SetDefaults... "+resource);
int resourceId=BA.applicationContext.getResources().getIdentifier(resource, "values", BA.packageName);
BA.Log("resourceID="+resourceId);
//getObject().setDefaults(arg0);
//getObject().setDefaults(arg0, namespace);
//getObject().setDefaults(Object map);
//java.util.Map javaMap = map.getObject();
}
public void fetch(int cacheExpiration){
getObject().fetch(cacheExpiration)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
BA.Log("Fetch Succeeded");
if (ba.subExists(eventName + "_onfetchcomplete")) {
BA.Log("lib:Raising.. "+eventName + "_onfetchcomplete()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_onfetchcomplete", true, new Object[] {task.isSuccessful()});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_onfetchcomplete");
}
// Once the config is successfully fetched it must be activated before newly fetched
// values are returned.
//getObject().activateFetched();
} else {
BA.LogError("Fetch failed");
if (ba.subExists(eventName + "_onfetchcomplete")) {
BA.Log("lib:Raising.. "+eventName + "_onfetchcomplete()");
//app.raiseEvent(app.context, eventName+"_pagerendered", i, pageCount, filename+"-" + i + ".png");
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_onfetchcomplete", true, new Object[] {task.isSuccessful()});
}else {
BA.Log("lib: NOTFOUND '"+eventName + "_onfetchcomplete");
}
}
}
});
}
@SuppressWarnings("unchecked")
public void Defaults(Map defaults){
@SuppressWarnings("rawtypes")
java.util.Map m = defaults.getObject();
getObject().setDefaults(m);
}
public void setDefaults2(Map defaults, String namespace){
@SuppressWarnings("rawtypes")
java.util.Map m = defaults.getObject();
getObject().setDefaults(m, namespace);
}
// /**
// * Sets defaults in the given namespace,
// * using an XML resource
// * @param resourceId
// */
// @Hide()
// public void Defaults2(int resourceId, String namespace){
// getObject().setDefaults(resourceId, namespace);
// }
/**
* Changes the settings for the FirebaseRemoteConfig
* object's operations, such as turning the developer mode on.
*/
public void setConfigSettings (FirebaseRemoteConfigSettings settings){
getObject().setConfigSettings(settings);
}
/**
* Gets the FirebaseRemoteConfigValue
* corresponding to the specified key.
*/
public FirebaseRemoteConfigValue getValue(String key){
return getObject().getValue(key);
}
public FirebaseRemoteConfigValue getValue2(String key,String namespace){
return getObject().getValue(key,namespace);
}
/**
* Gets the value corresponding to the specified
* key as a byte array.
*
* Returns Value as a byte array if a value
* corresponding to the look up key was present;
* default (if set) or static default value otherwise.
*/
public byte[] getByteArray(String key){
return getObject().getByteArray(key);
}
/**
* Gets the value corresponding to the specified
* key, in the specified namespace, as a byte array.
*
* Returns Value as a byte array if a value
* corresponding to the look up key was present;
* default (if set) or static default value otherwise.
*/
public byte[] getByteArray2(String key,String namespace){
return getObject().getByteArray(key,namespace);
}
/**
* Gets the value corresponding to
* the specified key, as a string.
*
* returns alue as a string if a value corresponding
* to the look up key was present and could be converted
* to a string; default (if set) or
* static default value otherwise.
*/
public String getString(String key){
return getObject().getString(key);
}
/**
* Gets value as a string corresponding
* to the specified key in the specified namespace.
* returns alue as a string if a value corresponding
* to the look up key was present and could be converted
* to a string; default (if set) or
* static default value otherwise.
*/
public String getString2(String key,String namespace){
return getObject().getString(key,namespace);
}
/**
* Gets the value corresponding to the specified
* key, in the specified namespace, as a double.
*
* Returns Value as a double if a value corresponding
* to the look up key was present and could be
* converted to a double; default (if set) or
* static default value otherwise.
*/
public double getDouble(String key){
return getObject().getDouble(key);
}
/**
* Gets the value corresponding to the specified
* key as a double.
*
* Returns Value as a double if a value corresponding
* to the look up key was present and could be
* converted to a double; default (if set) or
* static default value otherwise.
*/
public double getDouble2(String key,String namespace){
return getObject().getDouble(key,namespace);
}
/**
* Gets the value corresponding to the specified key,
* as a boolean.
*/
public boolean getBoolean(String key){
return getObject().getBoolean(key);
}
/**
* Gets the value corresponding to the specified key,
* as a boolean, in the specified namespace.
*/
public boolean getBoolean2(String key,String namespace){
return getObject().getBoolean(key,namespace);
}
/**
* Gets the value corresponding to the
* specified key, as a long.
*
* returns Value as a long if a value corresponding
* to the look up key was present and could be
* converted to a long; default (if set) or
* static default value otherwise.
*/
public long getLong(String key){
return getObject().getLong(key);
}
/**
* Gets the value corresponding to the specified
* key, in the specified namespace, as a long.
*/
public long getLong2(String key,String namespace){
return getObject().getLong(key,namespace);
}
}